common.js
11.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
import axios from 'axios'
import {
MessageBox
} from 'element-ui'
export default {
/**
* 数组元素查询——针对数组元素中object某一项值
* @param val
* @param store
* @param findKey
*/
arrayFind: function(val, store, findKey) {
let res = ''
if(!store) {
return val
}
if(!findKey) {
findKey = 'value'
}
try {
store.forEach(function(item, index) {
if(item[findKey] == val) {
let curItem = JSON.parse(JSON.stringify(item))
curItem.findIndex = index
res = curItem
throw new Error('end forEach')
}
})
} catch(e) {
if(e.message = 'end forEach') {
console.log(e.message)
}
}
return res
},
/**
* 枚举渲染
* @param ename
* @param store
* @returns {*}
*/
globalRender: function(ename, store, checkFlag) {
let val = ''
if (!checkFlag) {
val = ename
}
if (!store) {
return val
}
if (store.length != 0) {
for (let i = 0; i < store.length; i++) {
if (ename == store[i].key) {
return store[i].value || store[i].key
} else if (i == store.length - 1) {
return val;
}
}
} else {
return val;
}
},
/**
* 时间戳转字符串
* @param num
* @returns {*}
*/
detailTime: function(num) {
if (num == null) {
return '';
} else {
num = Number(num);
let d = new Date(num);
let year = d.getFullYear();
let month = d.getMonth() + 1;
let date = d.getDate();
let hour = d.getHours();
let minute = d.getMinutes();
let second = d.getSeconds();
month < 10 ? month = '0' + month : month;
date < 10 ? date = '0' + date : date;
hour < 10 ? hour = '0' + hour : hour;
minute < 10 ? minute = '0' + minute : minute;
second < 10 ? second = '0' + second : second;
return year + "-" + month + "-" + date + " " + hour + ":" + minute + ":" + second;
}
},
/**
* 文件后缀时间戳
* @param {*} num
* @returns
*/
fileTimeStamp: function(num) {
if (num == null) {
return '';
} else {
num = Number(num);
let d = new Date(num);
let year = d.getFullYear();
let month = d.getMonth() + 1;
let date = d.getDate();
let hour = d.getHours();
let minute = d.getMinutes();
let second = d.getSeconds();
month < 10 ? month = '0' + month : month;
date < 10 ? date = '0' + date : date;
hour < 10 ? hour = '0' + hour : hour;
minute < 10 ? minute = '0' + minute : minute;
second < 10 ? second = '0' + second : second;
return year + "-" + month + "-" + date + "-" + hour + "" + minute + "" + second;
}
},
/**
* 浮点数加法,用于解决精度丢失问题
* @param {*} num1
* @param {*} num2
* @returns
*/
addStr: function(num1, num2) {
var r1, r2, m;
try {
r1 = num1.toString().split('.')[1].length;
} catch (e) {
r1 = 0;
}
try {
r2 = num2.toString().split(".")[1].length;
} catch (e) {
r2 = 0;
}
m = Math.pow(10, Math.max(r1, r2));
return Math.round(num1 * m + num2 * m) / m;
},
/**
* 浮点数减法,用于解决精度丢失问题
* @param {*} arg1
* @param {*} arg2
* @returns
*/
subStr: function(arg1, arg2) {
var r1, r2, m, n;
try {
r1 = arg1.toString().split(".")[1].length
} catch (e) {
r1 = 0
}
try {
r2 = arg2.toString().split(".")[1].length
} catch (e) {
r2 = 0
}
m = Math.pow(10, Math.max(r1, r2));
n = (r1 >= r2) ? r1 : r2;
return ((arg1 * m - arg2 * m) / m).toFixed(n);
},
/**
* 浮点数乘法,用于解决精度丢失问题
* @param {*} arg1
* @param {*} arg2
* @param {*} n 保留位数
* @returns
*/
mulStr: function(arg1, arg2, n) {
var m = 0,
s1 = arg1.toString(),
s2 = arg2.toString();
try {
m += s1.split('.')[1].length
} catch (e) {
//TODO handle the exception
}
try {
m += s2.split('.')[1].length
} catch (e) {}
var val = Number(s1.replace('.', '')) * Number(s2.replace('.', '')) / Math.pow(10, m)
if(n) {
val = val.toFixed(n)
}
return val
},
/**
* 浮点数除法,用于解决精度丢失问题
* @param {*} arg1
* @param {*} arg2
* @param {*} n 保留位数
* @returns
*/
divStr: function(arg1, arg2, n) {
var t1 = 0,
t2 = 0,
r1, r2;
try {
t1 = arg1.toString().split('.')[1].length
} catch (e) {
//TODO handle the exception
}
try {
t2 = arg2.toString().split('.')[1].length
} catch (e) {
//TODO handle the exception
}
r1 = Number(arg1.toString().replace('.', ''));
r2 = Number(arg2.toString().replace('.', ''));
var val = (r1 / r2) * Math.pow(10, t2 - t1)
if(n) {
val = val.toFixed(n)
}
return val
},
/**
* 隐藏身份证号
* @param {*} idCard
* @returns
*/
hideIdNumber: function(idCard) {
if (idCard.length === 18) {
return idCard.substr(0, 6) + '********' + idCard.substr(-4, 4)
} else if (idCard.length === 15) {
return idCard.substr(0, 6) + '******' + idCard.substr(-3, 3)
} else {
return idCard
}
},
/**
* 隐藏手机号
* @param {*} phone
* @returns
*/
hidePhone: function(phone) {
if(phone.length == 11) {
return phone.substr(0,3) + '*****' + phone.substr(-3,3)
} else {
return phone
}
},
/**
* 导出excel
* @param content
* @param fileName
* @param fileParam
* @param blobType
* @param fileType
* */
exportExcel: function({
content,
fileName,
fileParam,
blobType,
fileType
}) {
let __this = this
if (!content) return
if (!blobType) blobType = 'application/vnd.ms-excel'
if (!fileName) fileName = '导出文件'
if (!fileType) fileType = 'xls'
if (!fileParam) fileParam = ''
let fileNames = fileName + fileParam
__this.createBlob(content, blobType, fileNames, fileType)
},
/**
* 下载本地文件
* @param fileJson
* */
downloadLocalFile: function(fileSrc, blobType, fileName, fileType) {
let __this = this
let getUrl = fileSrc
if (getUrl.indexOf('?') < 0) getUrl = fileSrc + '?' + new Date().getTime()
console.log(getUrl)
axios.create().get(getUrl, {
responseType: 'blob'
}).then(response => {
if (window.navigator.msSaveBlob) { //ie浏览器不支持通过a标签下载文件
try {
window.navigator.msSaveBlob(response.data, (fileName + '-' + __this.fileTimeStamp(new Date().getTime()) +
'.' + fileType))
} catch (e) {
MessageBox.confirm('该浏览器内核不支持下载此文件,推荐使用谷歌浏览器访问本平台', '提示', {
confirmButtonText: "确定",
showClose: false,
showCancelButton: false,
callback: function(action) {
// window.location.reload()
}
})
}
} else {
__this.createBlob(response.data, blobType, fileName, fileType)
}
})
},
/**
* 创建blob,用于下载文件
* @param {*} content
* @param {*} blobType
* @param {*} fileName
* @param {*} fileType
*/
createBlob: function(content, blobType, fileName, fileType) {
let __this = this
let blob = new Blob([content], {
type: blobType
})
let filename = fileName + '-' + __this.fileTimeStamp(new Date().getTime()) + '.' + fileType
if ('download' in document.createElement('a')) {
let eleLink = document.createElement('a')
eleLink.download = filename
eleLink.style.display = 'none'
eleLink.href = URL.createObjectURL(blob)
document.body.appendChild(eleLink)
eleLink.click()
URL.revokeObjectURL(eleLink.href)
document.body.removeChild(eleLink)
} else {
navigator.msSaveBlob(blob, filename)
}
},
/**
* 通过url下载文件
* @param {*} url
*/
downloadFile(url) {
let urlParam = '';
if (url.indexOf('?') >= 0) {
urlParam = url.split('?')[1]
url = url.split('?')[0]
}
let splitArr = url.split('/')
let fileName = splitArr[splitArr.length - 1].slice(0, -4)
let fileType = splitArr[splitArr.length - 1].slice(-4)
if (fileType.indexOf('.') < 0) {
fileName = fileName.slice(0, -1)
} else {
fileType = fileType.replace(/./, '')
}
let lastType = fileType
fileType = fileType.toLowerCase()
console.log(fileName, fileType)
let blobType = ''
switch (fileType) {
case 'xls':
case 'xlsx':
blobType = 'application/vnd.ms-excel'
break;
// case 'xlsx':
// blobType = 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet.'
// break;
case 'doc':
case 'docx':
blobType = 'application/msword'
break;
case 'ppt':
case 'pptx':
blobType = 'application/vnd.ms-powerpoint'
break;
case 'txt':
blobType = 'text/plain'
break;
case 'jpg':
case 'jpeg':
blobType = 'image/jpeg'
break;
case 'png':
blobType = 'image/png'
break;
case 'pdf':
blobType = 'application/pdf'
break;
case 'zip':
blobType = 'application/zip'
break;
}
let imgType = '.jpg,.jpeg,.png'
if (imgType.indexOf(fileType) >= 0) {
let image = new Image()
image.setAttribute("crossOrigin", "anonymous");
image.onload = function() {
let canvas = document.createElement("canvas");
canvas.width = image.width;
canvas.height = image.height;
let context = canvas.getContext("2d");
context.drawImage(image, 0, 0, image.width, image.height);
let url = canvas.toDataURL(blobType, 1.0); //得到图片的base64编码数据
let a = document.createElement("a"); // 生成一个a元素
let event = new MouseEvent("click"); // 创建一个单击事件
// let filename = fileName + '-' + fileTimeStamp(new Date().getTime()) + '.' + fileType
let filename = fileName + '.' + lastType
a.download = filename; // 设置图片名称
a.href = url; // 将生成的URL设置为a.href属性
a.dispatchEvent(event); // 触发a的单击事件
};
image.src = url;
} else {
let fileSrc = splitArr.slice(0, -1).join('/') + '/' + encodeURIComponent(fileName) + '.' + fileType
if (fileType == 'txt') {
console.log(fileSrc, url)
downloadLocalFile(fileSrc, blobType, fileName, fileType)
} else {
let form = document.createElement('form')
form.method = 'get'
form.action = urlParam ? (fileSrc + '?' + urlParam) : fileSrc
document.body.append(form)
form.submit()
form.remove()
}
}
}
}