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