common.js 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. /**
  2. * 通用js方法封装处理
  3. * Copyright (c) 2019 ruoyi
  4. */
  5. const baseURL = process.env.VUE_APP_BASE_API
  6. // 日期格式化
  7. export function parseTime(time, pattern) {
  8. if (arguments.length === 0 || !time) {
  9. return null
  10. }
  11. const format = pattern || '{y}-{m}-{d} {h}:{i}:{s}'
  12. let date
  13. if (typeof time === 'object') {
  14. date = time
  15. } else {
  16. if ((typeof time === 'string') && (/^[0-9]+$/.test(time))) {
  17. time = parseInt(time)
  18. } else if (typeof time === 'string') {
  19. time = time.replace(new RegExp(/-/gm), '/').replace('T', ' ').replace(new RegExp(/\.[\d]{3}/gm),'');
  20. }
  21. if ((typeof time === 'number') && (time.toString().length === 10)) {
  22. time = time * 1000
  23. }
  24. date = new Date(time)
  25. }
  26. const formatObj = {
  27. y: date.getFullYear(),
  28. m: date.getMonth() + 1,
  29. d: date.getDate(),
  30. h: date.getHours(),
  31. i: date.getMinutes(),
  32. s: date.getSeconds(),
  33. a: date.getDay()
  34. }
  35. const time_str = format.replace(/{(y|m|d|h|i|s|a)+}/g, (result, key) => {
  36. let value = formatObj[key]
  37. // Note: getDay() returns 0 on Sunday
  38. if (key === 'a') { return ['日', '一', '二', '三', '四', '五', '六'][value] }
  39. if (result.length > 0 && value < 10) {
  40. value = '0' + value
  41. }
  42. return value || 0
  43. })
  44. return time_str
  45. }
  46. export function cloneObject(obj) {
  47. if (typeof obj != "object") return;
  48. return JSON.parse(JSON.stringify(obj));
  49. }
  50. // 表单重置
  51. export function resetForm(refName) {
  52. if (this.$refs[refName]) {
  53. this.$refs[refName].resetFields();
  54. }
  55. }
  56. // 添加日期范围
  57. export function addDateRange(params, dateRange, propName) {
  58. let search = params;
  59. search.params = typeof (search.params) === 'object' && search.params !== null && !Array.isArray(search.params) ? search.params : {};
  60. dateRange = Array.isArray(dateRange) ? dateRange : [];
  61. if (typeof (propName) === 'undefined') {
  62. search.params['beginTime'] = dateRange[0];
  63. search.params['endTime'] = dateRange[1];
  64. } else {
  65. search.params['begin' + propName] = dateRange[0];
  66. search.params['end' + propName] = dateRange[1];
  67. }
  68. return search;
  69. }
  70. // 回显数据字典
  71. export function selectDictLabel(datas, value) {
  72. var actions = [];
  73. Object.keys(datas).some((key) => {
  74. if (datas[key].dictValue == ('' + value)) {
  75. actions.push(datas[key].dictLabel);
  76. return true;
  77. }
  78. })
  79. return actions.join('');
  80. }
  81. // 回显数据字典(字符串数组)
  82. export function selectDictLabels(datas, value, separator) {
  83. var actions = [];
  84. var currentSeparator = undefined === separator ? "," : separator;
  85. var temp = value.split(currentSeparator);
  86. Object.keys(value.split(currentSeparator)).some((val) => {
  87. Object.keys(datas).some((key) => {
  88. if (datas[key].dictValue == ('' + temp[val])) {
  89. actions.push(datas[key].dictLabel + currentSeparator);
  90. }
  91. })
  92. })
  93. return actions.join('').substring(0, actions.join('').length - 1);
  94. }
  95. // 通用下载方法
  96. export function download(fileName) {
  97. window.location.href = baseURL + "/common/download?fileName=" + encodeURI(fileName) + "&delete=" + false;
  98. }
  99. // 字符串格式化(%s )
  100. export function sprintf(str) {
  101. var args = arguments, flag = true, i = 1;
  102. str = str.replace(/%s/g, function () {
  103. var arg = args[i++];
  104. if (typeof arg === 'undefined') {
  105. flag = false;
  106. return '';
  107. }
  108. return arg;
  109. });
  110. return flag ? str : '';
  111. }
  112. // 转换字符串,undefined,null等转化为""
  113. export function praseStrEmpty(str) {
  114. if (!str || str == "undefined" || str == "null") {
  115. return "";
  116. }
  117. return str;
  118. }
  119. /**
  120. * 构造树型结构数据
  121. * @param {*} data 数据源
  122. * @param {*} id id字段 默认 'id'
  123. * @param {*} parentId 父节点字段 默认 'parentId'
  124. * @param {*} children 孩子节点字段 默认 'children'
  125. * @param {*} rootId 根Id 默认 0
  126. */
  127. export function handleTree(data, id, parentId, children, rootId) {
  128. id = id || 'id'
  129. parentId = parentId || 'parentId'
  130. children = children || 'children'
  131. rootId = rootId || Math.min.apply(Math, data.map(item => { return item[parentId] })) || 0
  132. //对源数据深度克隆
  133. const cloneData = JSON.parse(JSON.stringify(data))
  134. //循环所有项
  135. const treeData = cloneData.filter(father => {
  136. let branchArr = cloneData.filter(child => {
  137. //返回每一项的子级数组
  138. return father[id] === child[parentId]
  139. });
  140. branchArr.length > 0 ? father.children = branchArr : '';
  141. //返回第一层
  142. return father[parentId] === rootId;
  143. });
  144. return treeData != '' ? treeData : data;
  145. }
  146. // /**
  147. // * 构造树型结构数据
  148. // * @param {*} data 数据源
  149. // * @param {*} id id字段 默认 'id'
  150. // * @param {*} parentId 父节点字段 默认 'parentId'
  151. // * @param {*} children 孩子节点字段 默认 'children'
  152. // */
  153. // export function handleTree(data, id, parentId, children) {
  154. // let config = {
  155. // id: id || 'id',
  156. // parentId: parentId || 'parentId',
  157. // childrenList: children || 'children'
  158. // };
  159. // var childrenListMap = {};
  160. // var nodeIds = {};
  161. // var tree = [];
  162. // for (let d of data) {
  163. // let parentId = d[config.parentId];
  164. // if (childrenListMap[parentId] == null) {
  165. // childrenListMap[parentId] = [];
  166. // }
  167. // nodeIds[d[config.id]] = d;
  168. // childrenListMap[parentId].push(d);
  169. // }
  170. // for (let d of data) {
  171. // let parentId = d[config.parentId];
  172. // if (nodeIds[parentId] == null) {
  173. // tree.push(d);
  174. // }
  175. // }
  176. // for (let t of tree) {
  177. // adaptToChildrenList(t);
  178. // }
  179. // function adaptToChildrenList(o) {
  180. // if (childrenListMap[o[config.id]] !== null) {
  181. // o[config.childrenList] = childrenListMap[o[config.id]];
  182. // }
  183. // if (o[config.childrenList]) {
  184. // for (let c of o[config.childrenList]) {
  185. // adaptToChildrenList(c);
  186. // }
  187. // }
  188. // }
  189. // return tree;
  190. // }