tools.js
8.42 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
import XLSX2 from "xlsx";
import XLSX from "xlsx-style";
/**
* 获取数据类型
* @param {All} [o] 需要检测的数据
* @returns {String}
*/
export function getType (o) {
return Object.prototype.toString.call(o).slice(8, -1);
}
/**
* 判断是否是指定数据类型
* @param {All} [o] 需要检测的数据
* @param {String} [type] 数据类型
* @returns {Boolean}
*/
export function isKeyType (o, type) {
return getType(o).toLowerCase() === type.toLowerCase();
}
export function deepCopy (sth) {
let copy;
if (null == sth || "object" != typeof sth) return sth;
if (isKeyType(sth, 'date')) {
copy = new Date();
copy.setTime(sth.getTime());
return copy;
}
if (isKeyType(sth, 'array')) {
copy = [];
for (let i = 0, len = sth.length; i < len; i++) {
copy[i] = deepCopy(sth[i]);
}
return copy;
}
if (isKeyType(sth, 'object')) {
copy = {};
for (let attr in sth) {
if (sth.hasOwnProperty(attr)) copy[attr] = deepCopy(sth[attr]);
}
return copy;
}
return null;
}
/**
* 关键信息隐藏
* @param str 字符串
* @param frontLen 字符串前面保留位数
* @param endLen 字符串后面保留位数
* @returns {string}
*/
export function hideCode (str, frontLen, endLen = 0) {
var len = str.length - frontLen - endLen;
var xing = '';
for (var i = 0; i < len; i++) {
xing += '*';
}
return str.substring(0, frontLen) + xing + str.substring(str.length - endLen);
};
// 数组去重
export function unique (arr) {
var obj = {};
return arr.filter(function (item, index, arr) {
return obj.hasOwnProperty(typeof item + item) ? false : (obj[typeof item + item] = true)
})
}
// 创造id
export function getUuid (len, radix) {
var chars = "0123456789abcdefghijklmnopqrstuvwxyz".split(
""
);
var uuid = [],
i;
radix = radix || chars.length;
if (len) {
for (i = 0; i < len; i++) uuid[i] = chars[0 | (Math.random() * radix)];
} else {
var r;
uuid[8] = uuid[13] = uuid[18] = uuid[23] = "-";
uuid[14] = "4";
for (i = 0; i < 36; i++) {
if (!uuid[i]) {
r = 0 | (Math.random() * 16);
uuid[i] = chars[i == 19 ? (r & 0x3) | 0x8 : r];
}
}
}
return uuid.join("");
}
//js计算两个时间戳之间的时间差 (月)
export function intervalTime (startTime, endTime) {
// var timestamp=new Date().getTime(); //计算当前时间戳
var timestamp = (Date.parse(new Date())) / 1000;//计算当前时间戳 (毫秒级)
var date1 = ""; //开始时间
if (timestamp < startTime) {
date1 = startTime;
} else {
date1 = timestamp; //开始时间
}
var date2 = endTime; //结束时间
// var date3 = date2.getTime() - date1.getTime(); //时间差的毫秒数
var date3 = (date2 - date1) * 1000; //时间差的毫秒数
//计算出相差天数
var mon = Math.floor(date3 / (30 * 24 * 3600 * 1000 * 1000));
// return days + "天 " + hours + "小时 " + minutes + " 分钟" + seconds + " 秒"
return mon
}
// 日期转时间戳
export function js_strto_time (str_time) {
var str = str_time.replace(/-/g, '/') // 将-替换成/,因为下面这个构造函数只支持/分隔的日期字符串
var date = new Date(str) // 构造一个日期型数据,值为传入的字符串
return date.getTime()
}
// 时间戳转日期
export function timestampToTime (timestamp) {
var date = new Date(timestamp)//时间戳为10位需*1000,时间戳为13位的话不需乘1000
var Y = date.getFullYear() + '-'
var M = (date.getMonth() + 1 < 10 ? '0' + (date.getMonth() + 1) : date.getMonth() + 1) + '-'
var D = date.getDate() > 10 ? date.getDate() : '0' + date.getDate()
return Y + M + D
}
function formatDate (value) {
var date = new Date(value);
var y = date.getFullYear(),
m = date.getMonth() + 1,
d = date.getDate()
if (m < 10) { m = '0' + m; }
if (d < 10) { d = '0' + d; }
var t = y + '-' + m + '-' + d
return t;
}
export function getCurrentDate (date = 'firstDay') {
var now = new Date() // 当前日期
var nowYear = now.getFullYear() //当前年
var nowMonth = now.getMonth() //当前月 (值为0~11)
var d = now.getDate() //当天
if (date == 'firstDay') {
return formatDate(new Date(nowYear, nowMonth, 1)) // 本月开始时间
} else {
return formatDate(new Date(nowYear, nowMonth + 1, 0)) // 本月结束时间
}
}
export function setExport2Excel (exportName) {
/* generate workbook object from table */
var wb = XLSX2.utils.table_to_sheet(document.querySelector("#mytable"));//mytable为表格的id名
if (!wb['!merges']) {
this.$message.warning('无法导出:报表无数据');
return
}
for (var i = 0; i < 11; i++) {
wb["!cols"][i] = { wpx: 150 }
}
// 样式的文档地址
// https://www.npmjs.com/package/xlsx-style
for (const key in wb) {
if (key == 'A1') {
wb[key].s = {
font: {//字体设置
sz: 24,
bold: true,
},
alignment: {//文字居中
horizontal: 'center',
vertical: 'center',
wrapText: 1,
indent: 0,
wrap_text: true
}
}
} else if (key.indexOf('!') === -1 && wb[key].v) {
wb[key].s = {
font: {//字体设置
sz: 13,
bold: false,
color: {
rgb: '000000'//十六进制,不带#
}
},
alignment: {//文字居中
horizontal: 'center',
vertical: 'center',
wrapText: 1,
indent: 0,
wrap_text: true
},
border: { // 设置边框
top: { style: 'thin' },
bottom: { style: 'thin' },
left: { style: 'thin' },
right: { style: 'thin' }
}
}
}
}
var data = addRangeBorder(wb['!merges'], wb) //合并项添加边框
var filedata = sheet2blob(data)
openDownloadDialog(filedata, exportName + ".xlsx")
}
//为合并项添加边框
function addRangeBorder (range, ws) {
let arr = ["A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z"];
range.forEach(item => {
let startColNumber = Number(item.s.r), endColNumber = Number(item.e.r);
let startRowNumber = Number(item.s.c), endRowNumber = Number(item.e.c);
const test = ws[arr[startRowNumber] + (startColNumber + 1)];
for (let col = startColNumber; col <= endColNumber; col++) {
for (let row = startRowNumber; row <= endRowNumber; row++) {
ws[arr[row] + (col + 1)] = test;
}
}
})
return ws;
}
//将一个sheet转成最终的excel文件的blob对象,然后利用URL.createObjectURL下载
function sheet2blob (sheet, sheetName) {
sheetName = sheetName || 'sheet1';
var workbook = {
SheetNames: [sheetName],
Sheets: {}
};
workbook.Sheets[sheetName] = sheet; // 生成excel的配置项
var wopts = {
bookType: 'xlsx', // 要生成的文件类型
bookSST: false, // 是否生成Shared String Table,官方解释是,如果开启生成速度会下降,但在低版本IOS设备上有更好的兼容性
type: 'binary'
};
var wbout = XLSX.write(workbook, wopts);
var blob = new Blob([s2ab(wbout)], {
type: "application/octet-stream"
}); // 字符串转ArrayBuffer
function s2ab (s) {
var buf = new ArrayBuffer(s.length);
var view = new Uint8Array(buf);
for (var i = 0; i != s.length; ++i) view[i] = s.charCodeAt(i) & 0xFF;
return buf;
}
return blob;
}
function openDownloadDialog (url, saveName) {
if (typeof url == 'object' && url instanceof Blob) {
url = URL.createObjectURL(url); // 创建blob地址
}
var aLink = document.createElement('a');
aLink.href = url;
aLink.download = saveName || ''; // HTML5新增的属性,指定保存文件名,可以不要后缀,注意,file:///模式下不会生效
var event;
if (window.MouseEvent) event = new MouseEvent('click');
else {
event = document.createEvent('MouseEvents');
event.initMouseEvent('click', true, false, window, 0, 0, 0, 0, 0, false, false, false, false, 0, null);
}
aLink.dispatchEvent(event);
}
function judgeObjectComplete (ObjectValue) {
let flag = new Boolean()
flag = true
for (let key in ObjectValue) {
if (ObjectValue[key]) { } else {
flag = false
}
}
if (!flag) {
return false
} else {
return true
}
}
export function judgeListComplete (list) {
let isNotComplete = list.findIndex(item => {
return judgeObjectComplete(item) === false
})
if (isNotComplete > -1) {
return false
} else {
return true
}
}