ruoyi.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391
  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. if(time.indexOf('T')!== -1){
  61. let newTime = time.split('T')
  62. time = newTime[0] + ' ' + newTime[1]
  63. }
  64. const format = pattern || '{y}-{m}-{d} {h}:{i}:{s}'
  65. let date
  66. if (typeof time === 'object') {
  67. date = time
  68. } else {
  69. if ((typeof time === 'string') && (/^[0-9]+$/.test(time))) {
  70. time = parseInt(time)
  71. } else if (typeof time === 'string') {
  72. time = time.replace(new RegExp(/-/gm), '/');
  73. }
  74. if ((typeof time === 'number') && (time.toString().length === 10)) {
  75. time = time * 1000
  76. }
  77. date = new Date(time)
  78. }
  79. const formatObj = {
  80. y: date.getFullYear(),
  81. m: date.getMonth() + 1,
  82. d: date.getDate(),
  83. h: date.getHours(),
  84. i: date.getMinutes(),
  85. s: date.getSeconds(),
  86. a: date.getDay()
  87. }
  88. const time_str = format.replace(/{(y|m|d|h|i|s|a)+}/g, (result, key) => {
  89. let value = formatObj[key]
  90. // Note: getDay() returns 0 on Sunday
  91. if (key === 'a') { return ['日', '一', '二', '三', '四', '五', '六'][value] }
  92. if (result.length > 0 && value < 10) {
  93. value = '0' + value
  94. }
  95. return value || 0
  96. })
  97. return time_str
  98. }
  99. // 表单重置
  100. export function resetForm(refName) {
  101. if (this.$refs[refName]) {
  102. this.$refs[refName].resetFields();
  103. }
  104. }
  105. // 添加日期范围
  106. export function addDateRange(params, dateRange, propName) {
  107. var search = params;
  108. search.params = {};
  109. if (null != dateRange && '' != dateRange) {
  110. if (typeof (propName) === "undefined") {
  111. search.params["beginTime"] = dateRange[0];
  112. search.params["endTime"] = dateRange[1];
  113. } else {
  114. search.params["begin" + propName] = dateRange[0];
  115. search.params["end" + propName] = dateRange[1];
  116. }
  117. }
  118. return search;
  119. }
  120. // 回显数据字典
  121. export function selectDictLabel(datas, value) {
  122. var actions = [];
  123. Object.keys(datas).some((key) => {
  124. if (datas[key].dictValue == ('' + value)) {
  125. actions.push(datas[key].dictLabel);
  126. return true;
  127. }
  128. })
  129. return actions.join('');
  130. }
  131. // 回显数据字典(字符串数组)
  132. export function selectDictLabels(datas, value, separator) {
  133. var actions = [];
  134. var currentSeparator = undefined === separator ? "," : separator;
  135. var temp = value.split(currentSeparator);
  136. Object.keys(value.split(currentSeparator)).some((val) => {
  137. Object.keys(datas).some((key) => {
  138. if (datas[key].dictValue == ('' + temp[val])) {
  139. actions.push(datas[key].dictLabel + currentSeparator);
  140. }
  141. })
  142. })
  143. return actions.join('').substring(0, actions.join('').length - 1);
  144. }
  145. // 通用下载方法
  146. export function download(fileName) {
  147. window.location.href = baseURL + "/common/download?fileName=" + encodeURI(fileName) + "&delete=" + true;
  148. }
  149. // 通用上传地址
  150. export function uploadUrl() {
  151. return baseURL + "/base/file/upload";
  152. }
  153. // 字符串格式化(%s )
  154. export function sprintf(str) {
  155. var args = arguments, flag = true, i = 1;
  156. str = str.replace(/%s/g, function () {
  157. var arg = args[i++];
  158. if (typeof arg === 'undefined') {
  159. flag = false;
  160. return '';
  161. }
  162. return arg;
  163. });
  164. return flag ? str : '';
  165. }
  166. // 转换字符串,undefined,null等转化为""
  167. export function praseStrEmpty(str) {
  168. if (!str || str == "undefined" || str == "null") {
  169. return "";
  170. }
  171. return str;
  172. }
  173. /**
  174. * 构造树型结构数据
  175. * @param {*} data 数据源
  176. * @param {*} id id字段 默认 'id'
  177. * @param {*} parentId 父节点字段 默认 'parentId'
  178. * @param {*} children 孩子节点字段 默认 'children'
  179. */
  180. export function handleTree(data, id, parentId, children) {
  181. let config = {
  182. id: id || 'id',
  183. parentId: parentId || 'parentId',
  184. childrenList: children || 'children'
  185. };
  186. var childrenListMap = {};
  187. var nodeIds = {};
  188. var tree = [];
  189. for (let d of data) {
  190. let parentId = d[config.parentId];
  191. if (childrenListMap[parentId] == null) {
  192. childrenListMap[parentId] = [];
  193. }
  194. nodeIds[d[config.id]] = d;
  195. childrenListMap[parentId].push(d);
  196. }
  197. for (let d of data) {
  198. let parentId = d[config.parentId];
  199. if (nodeIds[parentId] == null) {
  200. tree.push(d);
  201. }
  202. }
  203. for (let t of tree) {
  204. adaptToChildrenList(t);
  205. }
  206. function adaptToChildrenList(o) {
  207. if (childrenListMap[o[config.id]] !== null) {
  208. o[config.childrenList] = childrenListMap[o[config.id]];
  209. }
  210. if (o[config.childrenList]) {
  211. for (let c of o[config.childrenList]) {
  212. adaptToChildrenList(c);
  213. }
  214. }
  215. }
  216. return tree;
  217. }
  218. /**
  219. * 参数处理
  220. * @param {*} params 参数
  221. */
  222. export function tansParams(params) {
  223. let result = ''
  224. for (const propName of Object.keys(params)) {
  225. const value = params[propName];
  226. var part = encodeURIComponent(propName) + "=";
  227. if (value !== null && typeof (value) !== "undefined") {
  228. if (typeof value === 'object') {
  229. for (const key of Object.keys(value)) {
  230. if (value[key] !== null && typeof (value[key]) !== 'undefined') {
  231. let params = propName + '[' + key + ']';
  232. var subPart = encodeURIComponent(params) + "=";
  233. result += subPart + encodeURIComponent(value[key]) + "&";
  234. }
  235. }
  236. } else {
  237. result += part + encodeURIComponent(value) + "&";
  238. }
  239. }
  240. }
  241. return result
  242. }
  243. /**
  244. * input 空格判断
  245. */
  246. export function spaceJudgment(rule, value, callback) {
  247. let reg = /^\s+$/;
  248. if(reg.test(value)){
  249. return callback(new Error('请不要只输入空格'));
  250. }else{
  251. callback()
  252. }
  253. }
  254. /**
  255. * input 是否是数字判断
  256. */
  257. export function isNum(rule, value, callback) {
  258. console.log('进来啦');
  259. const num= /^[0-9]*$/;
  260. if (!num.test(value)) {
  261. return callback(new Error('只能输入数字'))
  262. }else{
  263. callback()
  264. }
  265. }
  266. /**
  267. * 富文本 空格判断
  268. */
  269. export function spaceJudgmentHTML(rule, value, callback) {
  270. let obj = {value:value}
  271. let text = JSON.parse(JSON.stringify(obj));
  272. let newText = text.value.replace(/<[^>]+>/g, '');
  273. let reg = /^\s+$/;
  274. if(reg.test(newText)){
  275. return callback(new Error('请不要只输入空格'));
  276. }else if(newText.length<1){
  277. return callback(new Error('请输入内容'));
  278. }else{
  279. callback()
  280. }
  281. }
  282. /**
  283. * 预览地址判断
  284. */
  285. export function urlJudge(url) {
  286. let src = window.location.href.split('://')[0]+'://' + judgmentNetworkReturnAddress()+'/statics'+ url.split('statics')[1]
  287. return localStorage.getItem('filePreviewUrl')+'/onlinePreview?url='+encodeURIComponent(btoa(unescape(encodeURIComponent(src))));
  288. }
  289. /**
  290. * 返回版本字符字段
  291. */
  292. export function versionField() {
  293. return process.env.VUE_APP_VERSION_DIFFERENCE_FIELD
  294. }
  295. /**
  296. * 点击复制文本
  297. */
  298. export function clickCopy(context,name) {
  299. // 创建输入框元素
  300. let oInput = document.createElement('input');
  301. // 将想要复制的值
  302. oInput.value = context;
  303. // 页面底部追加输入框
  304. document.body.appendChild(oInput);
  305. // 选中输入框
  306. oInput.select();
  307. // 执行浏览器复制命令
  308. document.execCommand('Copy');
  309. // 弹出复制成功信息
  310. this.msgSuccess(name?name+'复制成功':'复制成功');
  311. // 复制后移除输入框
  312. oInput.remove();
  313. }
  314. //检查打印服务
  315. export function needCLodop() {
  316. try {
  317. var ua = navigator.userAgent
  318. if (ua.match(/Windows\sPhone/i) != null) return true
  319. if (ua.match(/iPhone|iPod/i) != null) return true
  320. if (ua.match(/Android/i) != null) return true
  321. if (ua.match(/Edge\D?\d+/i) != null) return true
  322. var verTrident = ua.match(/Trident\D?\d+/i)
  323. var verIE = ua.match(/MSIE\D?\d+/i)
  324. var verOPR = ua.match(/OPR\D?\d+/i)
  325. var verFF = ua.match(/Firefox\D?\d+/i)
  326. var x64 = ua.match(/x64/i)
  327. if (verTrident == null && verIE == null && x64 !== null) return true
  328. else if (verFF !== null) {
  329. verFF = verFF[0].match(/\d+/)
  330. if (verFF[0] >= 42 || x64 !== null) return true
  331. } else if (verOPR !== null) {
  332. verOPR = verOPR[0].match(/\d+/)
  333. if (verOPR[0] >= 32) return true
  334. } else if (verTrident == null && verIE == null) {
  335. var verChrome = ua.match(/Chrome\D?\d+/i)
  336. if (verChrome !== null) {
  337. verChrome = verChrome[0].match(/\d+/)
  338. if (verChrome[0] >= 42) return true
  339. }
  340. }
  341. return false
  342. } catch (err) {
  343. return true
  344. }
  345. }
  346. export function CreateOneFormPage(code) {
  347. LODOP = getLodop()
  348. LODOP.PRINT_INIT('') //打印初始化
  349. LODOP.SET_PRINT_STYLE('Bold', 1) //设置对象风格
  350. LODOP.ADD_PRINT_TEXT(90,35,300,200,code,) //(上边距,左边距,打印宽度,打印高度,打印文本内容)
  351. LODOP.SET_PRINT_STYLEA(0,'FontSize', 8) //设置对象风格
  352. LODOP.SET_PRINT_PAGESIZE(1, 500, 300, '') //设定纸张大小
  353. LODOP.SET_PRINT_MODE('PRINT_PAGE_PERCENT', '100%')//设置缩放
  354. LODOP.SET_PREVIEW_WINDOW(2, 2, 0, 0, 0, '')//设置窗口
  355. //LODOP.ADD_PRINT_HTM(2,80,160,160, document.getElementById("form1").innerHTML);//增加超文本项
  356. LODOP.ADD_PRINT_BARCODE(8,53,100, 100,'QRCode',code);
  357. LODOP.PREVIEW() //打印预览
  358. // LODOP.PRINT() //直接打印
  359. }