中国算力平台算力登记系统2.0
yanzhaofeige
3 days ago 9a224d216652ef2d6ff2e0bd01fba69c86a3f277
commit | author | age
43dc29 1
Y 2
3 /**
4  * 通用js方法封装处理
5  * Copyright (c) 2019 ruoyi
6  */
7
8 // 日期格式化
9 export function parseTime(time, pattern) {
10   if (arguments.length === 0 || !time) {
11     return null
12   }
13   const format = pattern || '{y}-{m}-{d} {h}:{i}:{s}'
14   let date
15   if (typeof time === 'object') {
16     date = time
17   } else {
18     if ((typeof time === 'string') && (/^[0-9]+$/.test(time))) {
19       time = parseInt(time)
20     } else if (typeof time === 'string') {
21       time = time.replace(new RegExp(/-/gm), '/').replace('T', ' ').replace(new RegExp(/\.[\d]{3}/gm), '');
22     }
23     if ((typeof time === 'number') && (time.toString().length === 10)) {
24       time = time * 1000
25     }
26     date = new Date(time)
27   }
28   const formatObj = {
29     y: date.getFullYear(),
30     m: date.getMonth() + 1,
31     d: date.getDate(),
32     h: date.getHours(),
33     i: date.getMinutes(),
34     s: date.getSeconds(),
35     a: date.getDay()
36   }
37   const time_str = format.replace(/{(y|m|d|h|i|s|a)+}/g, (result, key) => {
38     let value = formatObj[key]
39     // Note: getDay() returns 0 on Sunday
40     if (key === 'a') { return ['日', '一', '二', '三', '四', '五', '六'][value] }
41     if (result.length > 0 && value < 10) {
42       value = '0' + value
43     }
44     return value || 0
45   })
46   return time_str
47 }
48
49 // 表单重置
50 export function resetForm(refName) {
51   if (this.$refs[refName]) {
52     this.$refs[refName].resetFields();
53   }
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 // 回显数据字典
72 export function selectDictLabel(datas, value) {
73   if (value === undefined) {
74     return "";
75   }
76   var actions = [];
77   Object.keys(datas).some((key) => {
78     if (datas[key].value == ('' + value)) {
79       actions.push(datas[key].label);
80       return true;
81     }
82   })
83   if (actions.length === 0) {
84     actions.push(value);
85   }
86   return actions.join('');
87 }
88
89 // 回显数据字典(字符串、数组)
90 export function selectDictLabels(datas, value, separator) {
91   if (value === undefined || value.length ===0) {
92     return "";
93   }
94   if (Array.isArray(value)) {
95     value = value.join(",");
96   }
97   var actions = [];
98   var currentSeparator = undefined === separator ? "," : separator;
99   var temp = value.split(currentSeparator);
100   Object.keys(value.split(currentSeparator)).some((val) => {
101     var match = false;
102     Object.keys(datas).some((key) => {
103       if (datas[key].value == ('' + temp[val])) {
104         actions.push(datas[key].label + currentSeparator);
105         match = true;
106       }
107     })
108     if (!match) {
109       actions.push(temp[val] + currentSeparator);
110     }
111   })
112   return actions.join('').substring(0, actions.join('').length - 1);
113 }
114
115 // 字符串格式化(%s )
116 export function sprintf(str) {
117   var args = arguments, flag = true, i = 1;
118   str = str.replace(/%s/g, function () {
119     var arg = args[i++];
120     if (typeof arg === 'undefined') {
121       flag = false;
122       return '';
123     }
124     return arg;
125   });
126   return flag ? str : '';
127 }
128
129 // 转换字符串,undefined,null等转化为""
130 export function parseStrEmpty(str) {
131   if (!str || str == "undefined" || str == "null") {
132     return "";
133   }
134   return str;
135 }
136
137 // 数据合并
138 export function mergeRecursive(source, target) {
139   for (var p in target) {
140     try {
141       if (target[p].constructor == Object) {
142         source[p] = mergeRecursive(source[p], target[p]);
143       } else {
144         source[p] = target[p];
145       }
146     } catch (e) {
147       source[p] = target[p];
148     }
149   }
150   return source;
151 };
152
153 /**
154  * 构造树型结构数据
155  * @param {*} data 数据源
156  * @param {*} id id字段 默认 'id'
157  * @param {*} parentId 父节点字段 默认 'parentId'
158  * @param {*} children 孩子节点字段 默认 'children'
159  */
160 export function handleTree(data, id, parentId, children) {
161   let config = {
162     id: id || 'id',
163     parentId: parentId || 'parentId',
164     childrenList: children || 'children'
165   };
166
167   var childrenListMap = {};
168   var nodeIds = {};
169   var tree = [];
170
171   for (let d of data) {
172     let parentId = d[config.parentId];
173     if (childrenListMap[parentId] == null) {
174       childrenListMap[parentId] = [];
175     }
176     nodeIds[d[config.id]] = d;
177     childrenListMap[parentId].push(d);
178   }
179
180   for (let d of data) {
181     let parentId = d[config.parentId];
182     if (nodeIds[parentId] == null) {
183       tree.push(d);
184     }
185   }
186
187   for (let t of tree) {
188     adaptToChildrenList(t);
189   }
190
191   function adaptToChildrenList(o) {
192     if (childrenListMap[o[config.id]] !== null) {
193       o[config.childrenList] = childrenListMap[o[config.id]];
194     }
195     if (o[config.childrenList]) {
196       for (let c of o[config.childrenList]) {
197         adaptToChildrenList(c);
198       }
199     }
200   }
201   return tree;
202 }
203
204 /**
205 * 参数处理
206 * @param {*} params  参数
207 */
208 export function tansParams(params) {
209   let result = ''
210   for (const propName of Object.keys(params)) {
211     const value = params[propName];
212     var part = encodeURIComponent(propName) + "=";
213     if (value !== null && value !== "" && typeof (value) !== "undefined") {
214       if (typeof value === 'object') {
215         for (const key of Object.keys(value)) {
216           if (value[key] !== null && value[key] !== "" && typeof (value[key]) !== 'undefined') {
217             let params = propName + '[' + key + ']';
218             var subPart = encodeURIComponent(params) + "=";
219             result += subPart + encodeURIComponent(value[key]) + "&";
220           }
221         }
222       } else {
223         result += part + encodeURIComponent(value) + "&";
224       }
225     }
226   }
227   return result
228 }
229
230 // 验证是否为blob格式
231 export function blobValidate(data) {
232   return data.type !== 'application/json'
233 }