ruoyi.js 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313
  1. /**
  2. * 通用js方法封装处理
  3. * Copyright (c) 2019 ruoyi
  4. */
  5. let Base64 = require('js-base64').Base64;
  6. // 判断当前用户网络 外网/内网 返回接口地址
  7. export function judgmentNetworkReturnAddress() {
  8. /*判断是否是内网IP*/
  9. // 获取当前页面url
  10. var curPageUrl = window.location.href;
  11. // console.log('curPageUrl-0 '+curPageUrl);
  12. var reg1 = /(http|ftp|https|www):\/\//g;//去掉前缀
  13. curPageUrl =curPageUrl.replace(reg1,'');
  14. // console.log('curPageUrl-1 '+curPageUrl);
  15. var reg2 = /\:+/g;//替换冒号为一点
  16. curPageUrl =curPageUrl.replace(reg2,'.');
  17. // console.log('curPageUrl-2 '+curPageUrl);
  18. curPageUrl = curPageUrl.split('.');//通过一点来划分数组
  19. // console.log(curPageUrl);
  20. var ipAddress = curPageUrl[0]+'.'+curPageUrl[1]+'.'+curPageUrl[2]+'.'+curPageUrl[3];
  21. var isInnerIp = false;//默认给定IP不是内网IP
  22. var ipNum = getIpNum(ipAddress);
  23. /**
  24. * 私有IP:A类 10.0.0.0 -10.255.255.255
  25. * B类 172.16.0.0 -172.31.255.255
  26. * C类 192.168.0.0 -192.168.255.255
  27. * D类 127.0.0.0 -127.255.255.255(环回地址)
  28. **/
  29. var aBegin = getIpNum("10.0.0.0");
  30. var aEnd = getIpNum("10.255.255.255");
  31. var bBegin = getIpNum("172.16.0.0");
  32. var bEnd = getIpNum("172.31.255.255");
  33. var cBegin = getIpNum("192.168.0.0");
  34. var cEnd = getIpNum("192.168.255.255");
  35. var dBegin = getIpNum("127.0.0.0");
  36. var dEnd = getIpNum("127.255.255.255");
  37. isInnerIp = isInner(ipNum,aBegin,aEnd) || isInner(ipNum,bBegin,bEnd) || isInner(ipNum,cBegin,cEnd) || isInner(ipNum,dBegin,dEnd);
  38. // console.log('是否是内网:'+isInnerIp);
  39. return isInnerIp?process.env.VUE_APP_BASE_LOCAL_API:process.env.VUE_APP_BASE_API;
  40. /*获取IP数*/
  41. function getIpNum(ipAddress) {
  42. var ip = ipAddress.split(".");
  43. var a = parseInt(ip[0]);
  44. var b = parseInt(ip[1]);
  45. var c = parseInt(ip[2]);
  46. var d = parseInt(ip[3]);
  47. var ipNum = a * 256 * 256 * 256 + b * 256 * 256 + c * 256 + d;
  48. return ipNum;
  49. }
  50. function isInner(userIp,begin,end){
  51. return (userIp>=begin) && (userIp<=end);
  52. }
  53. }
  54. const baseURL = window.location.href.split('://')[0]+'://' + judgmentNetworkReturnAddress()
  55. // 日期格式化
  56. export function parseTime(time, pattern) {
  57. if (arguments.length === 0 || !time) {
  58. return null
  59. }
  60. const format = pattern || '{y}-{m}-{d} {h}:{i}:{s}'
  61. let date
  62. if (typeof time === 'object') {
  63. date = time
  64. } else {
  65. if ((typeof time === 'string') && (/^[0-9]+$/.test(time))) {
  66. time = parseInt(time)
  67. } else if (typeof time === 'string') {
  68. time = time.replace(new RegExp(/-/gm), '/');
  69. }
  70. if ((typeof time === 'number') && (time.toString().length === 10)) {
  71. time = time * 1000
  72. }
  73. date = new Date(time)
  74. }
  75. const formatObj = {
  76. y: date.getFullYear(),
  77. m: date.getMonth() + 1,
  78. d: date.getDate(),
  79. h: date.getHours(),
  80. i: date.getMinutes(),
  81. s: date.getSeconds(),
  82. a: date.getDay()
  83. }
  84. const time_str = format.replace(/{(y|m|d|h|i|s|a)+}/g, (result, key) => {
  85. let value = formatObj[key]
  86. // Note: getDay() returns 0 on Sunday
  87. if (key === 'a') { return ['日', '一', '二', '三', '四', '五', '六'][value] }
  88. if (result.length > 0 && value < 10) {
  89. value = '0' + value
  90. }
  91. return value || 0
  92. })
  93. return time_str
  94. }
  95. // 表单重置
  96. export function resetForm(refName) {
  97. if (this.$refs[refName]) {
  98. this.$refs[refName].resetFields();
  99. }
  100. }
  101. // 添加日期范围
  102. export function addDateRange(params, dateRange, propName) {
  103. var search = params;
  104. search.params = {};
  105. if (null != dateRange && '' != dateRange) {
  106. if (typeof (propName) === "undefined") {
  107. search.params["beginTime"] = dateRange[0];
  108. search.params["endTime"] = dateRange[1];
  109. } else {
  110. search.params["begin" + propName] = dateRange[0];
  111. search.params["end" + propName] = dateRange[1];
  112. }
  113. }
  114. return search;
  115. }
  116. // 回显数据字典
  117. export function selectDictLabel(datas, value) {
  118. var actions = [];
  119. Object.keys(datas).some((key) => {
  120. if (datas[key].dictValue == ('' + value)) {
  121. actions.push(datas[key].dictLabel);
  122. return true;
  123. }
  124. })
  125. return actions.join('');
  126. }
  127. // 回显数据字典(字符串数组)
  128. export function selectDictLabels(datas, value, separator) {
  129. var actions = [];
  130. var currentSeparator = undefined === separator ? "," : separator;
  131. var temp = value.split(currentSeparator);
  132. Object.keys(value.split(currentSeparator)).some((val) => {
  133. Object.keys(datas).some((key) => {
  134. if (datas[key].dictValue == ('' + temp[val])) {
  135. actions.push(datas[key].dictLabel + currentSeparator);
  136. }
  137. })
  138. })
  139. return actions.join('').substring(0, actions.join('').length - 1);
  140. }
  141. // 通用下载方法
  142. export function download(fileName) {
  143. window.location.href = baseURL + "/common/download?fileName=" + encodeURI(fileName) + "&delete=" + true;
  144. }
  145. // 通用上传地址
  146. export function uploadUrl() {
  147. return baseURL + "/base/file/upload";
  148. }
  149. // 字符串格式化(%s )
  150. export function sprintf(str) {
  151. var args = arguments, flag = true, i = 1;
  152. str = str.replace(/%s/g, function () {
  153. var arg = args[i++];
  154. if (typeof arg === 'undefined') {
  155. flag = false;
  156. return '';
  157. }
  158. return arg;
  159. });
  160. return flag ? str : '';
  161. }
  162. // 转换字符串,undefined,null等转化为""
  163. export function praseStrEmpty(str) {
  164. if (!str || str == "undefined" || str == "null") {
  165. return "";
  166. }
  167. return str;
  168. }
  169. /**
  170. * 构造树型结构数据
  171. * @param {*} data 数据源
  172. * @param {*} id id字段 默认 'id'
  173. * @param {*} parentId 父节点字段 默认 'parentId'
  174. * @param {*} children 孩子节点字段 默认 'children'
  175. */
  176. export function handleTree(data, id, parentId, children) {
  177. let config = {
  178. id: id || 'id',
  179. parentId: parentId || 'parentId',
  180. childrenList: children || 'children'
  181. };
  182. var childrenListMap = {};
  183. var nodeIds = {};
  184. var tree = [];
  185. for (let d of data) {
  186. let parentId = d[config.parentId];
  187. if (childrenListMap[parentId] == null) {
  188. childrenListMap[parentId] = [];
  189. }
  190. nodeIds[d[config.id]] = d;
  191. childrenListMap[parentId].push(d);
  192. }
  193. for (let d of data) {
  194. let parentId = d[config.parentId];
  195. if (nodeIds[parentId] == null) {
  196. tree.push(d);
  197. }
  198. }
  199. for (let t of tree) {
  200. adaptToChildrenList(t);
  201. }
  202. function adaptToChildrenList(o) {
  203. if (childrenListMap[o[config.id]] !== null) {
  204. o[config.childrenList] = childrenListMap[o[config.id]];
  205. }
  206. if (o[config.childrenList]) {
  207. for (let c of o[config.childrenList]) {
  208. adaptToChildrenList(c);
  209. }
  210. }
  211. }
  212. return tree;
  213. }
  214. /**
  215. * 参数处理
  216. * @param {*} params 参数
  217. */
  218. export function tansParams(params) {
  219. let result = ''
  220. for (const propName of Object.keys(params)) {
  221. const value = params[propName];
  222. var part = encodeURIComponent(propName) + "=";
  223. if (value !== null && typeof (value) !== "undefined") {
  224. if (typeof value === 'object') {
  225. for (const key of Object.keys(value)) {
  226. if (value[key] !== null && typeof (value[key]) !== 'undefined') {
  227. let params = propName + '[' + key + ']';
  228. var subPart = encodeURIComponent(params) + "=";
  229. result += subPart + encodeURIComponent(value[key]) + "&";
  230. }
  231. }
  232. } else {
  233. result += part + encodeURIComponent(value) + "&";
  234. }
  235. }
  236. }
  237. return result
  238. }
  239. /**
  240. * input 空格判断
  241. */
  242. export function spaceJudgment(rule, value, callback) {
  243. let reg = /^\s+$/;
  244. if(reg.test(value)){
  245. return callback(new Error('请不要只输入空格'));
  246. }else{
  247. callback()
  248. }
  249. }
  250. /**
  251. * input 是否是数字判断
  252. */
  253. export function isNum(rule, value, callback) {
  254. console.log('进来啦');
  255. const num= /^[0-9]*$/;
  256. if (!num.test(value)) {
  257. return callback(new Error('只能输入数字'))
  258. }else{
  259. callback()
  260. }
  261. }
  262. /**
  263. * 富文本 空格判断
  264. */
  265. export function spaceJudgmentHTML(rule, value, callback) {
  266. let obj = {value:value}
  267. let text = JSON.parse(JSON.stringify(obj));
  268. let newText = text.value.replace(/<[^>]+>/g, '');
  269. let reg = /^\s+$/;
  270. if(reg.test(newText)){
  271. return callback(new Error('请不要只输入空格'));
  272. }else if(newText.length<1){
  273. return callback(new Error('请输入内容'));
  274. }else{
  275. callback()
  276. }
  277. }
  278. /**
  279. * 预览地址判断
  280. */
  281. export function urlJudge(url) {
  282. let src = window.location.href.split('://')[0]+'://' + judgmentNetworkReturnAddress()+'/statics'+ url.split('statics')[1]
  283. return localStorage.getItem('filePreviewUrl')+'/onlinePreview?url='+encodeURIComponent(btoa(unescape(encodeURIComponent(src))));
  284. }