ruoyi.js 9.1 KB

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