常用工具集(utils.js)
export const isMobile = (s) => {
return /^(0|86|17951)?(13[0-9]|15[012356789]|17[678]|18[0-9]|14[57])[0-9]{8}$/.test(s)
}
export const isTelephone = (s) => {
return /^1[0-9]{10}$/.test(s)
}
export const isEmail = (s) => {
return /\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*/.test(s);
}
export const formatBytes = (bytes, decimals = 2) => {
if (bytes === 0) return '0 B'; //Bytes
const k = 1024;
const dm = decimals < 0 ? 0 : decimals;
const sizes = ['B', 'KB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB'];
const i = Math.floor(Math.log(bytes) / Math.log(k));
return parseFloat((bytes / Math.pow(k, i)).toFixed(dm)) + ' ' + sizes[i];
}
export const getCookie = (name) => {
let reg = new RegExp("(^| )" + name + "=([^;]*)(;|$)");
let arr = document.cookie.match(reg)
if (arr) {
return unescape(arr[2]);
} else {
return '';
}
}
export const dateTime = () => {
var date = new Date();
var seperator1 = "-";
var seperator2 = ":";
var month = date.getMonth() + 1;
var strDate = date.getDate();
if (month >= 1 && month <= 9) {
month = "0" + month;
}
if (strDate >= 0 && strDate <= 9) {
strDate = "0" + strDate;
}
var currentdate = date.getFullYear() + seperator1 + month + seperator1 + strDate
+ " " + date.getHours() + seperator2 + date.getMinutes()
+ seperator2 + date.getSeconds();
return currentdate;
}
export const dataTimeFormat = function (datetime, fmt) {
let o = {
"M+": datetime.getMonth() + 1, //月份
"d+": datetime.getDate(), //日
"h+": datetime.getHours(), //小时
"m+": datetime.getMinutes(), //分
"s+": datetime.getSeconds(), //秒
"q+": Math.floor((datetime.getMonth() + 3) / 3), //季度
"S": datetime.getMilliseconds() //毫秒
};
if (/(y+)/.test(fmt)) {
fmt = fmt.replace(RegExp.$1, (datetime.getFullYear() + "").substr(4 - RegExp.$1.length));
}
for (let k in o) {
if (new RegExp("(" + k + ")").test(fmt)) {
fmt = fmt.replace(RegExp.$1, (RegExp.$1.length == 1) ? (o[k]) : (("00" + o[k]).substr(("" + o[k]).length)));
}
}
return fmt;
}
// 判断是否是纯粹对象
const isPlainObject = obj => {
return Object.prototype.toString.call(obj) === '[object Object]'
}
export const formatBytes = (bytes, decimals = 2) => {
if (bytes === 0) return '0 B'; //Bytes
const k = 1024;
const dm = decimals < 0 ? 0 : decimals;
const sizes = ['B', 'KB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB'];
const i = Math.floor(Math.log(bytes) / Math.log(k));
return parseFloat((bytes / Math.pow(k, i)).toFixed(dm)) + ' ' + sizes[i];
}
export const getCookie = (name) => {
let reg = new RegExp("(^| )" + name + "=([^;]*)(;|$)");
let arr = document.cookie.match(reg)
if (arr) {
return unescape(arr[2]);
} else {
return '';
}
}
export const assignDeep = function () {
const args = Array.from(arguments);
if (args.length < 2) return args[0];
let result = args[0];
args.shift();
args.forEach(item => {
if (isPlainObject(item)) {
if (!isPlainObject(result)) result = {}
for (let key in item) {
if (result[key] && isPlainObject(item[key])) {
result[key] = assignDeep(result[key], item[key])
} else {
result[key] = item[key]
}
}
}
else if (item instanceof Array) {
if (!(result instanceof Array)) result = []
item.forEach((arrItem, arrIndex) => {
if (isPlainObject(arrItem)) {
result[arrIndex] = assignDeep(result[arrIndex])
} else {
result[arrIndex] = arrItem
}
})
}
})
return result;
}
export const exportToFile = (filename, data) => {
// let urlObject = window.URL || window.webkitURL || window;
// let export_blob = new Blob([data]);
// let save_link = document.createElementNS("http://www.w3.org/1999/xhtml", "a")
// save_link.href = urlObject.createObjectURL(export_blob);
// save_link.download = name;
// let ev = document.createEvent("MouseEvents");
// ev.initMouseEvent("click", true, false, window, 0, 0, 0, 0, 0, false, false, false, false, 0, null);
// save_link.dispatchEvent(ev);
if (window.navigator.msSaveOrOpenBlob) {
// 兼容IE10
navigator.msSaveBlob(new Blob([data]), filename ? filename : `xxx`);
} else {
let url = window.URL.createObjectURL(
new Blob([data], { type: "application/octet-stream" })
);
let link = document.createElement("a");
link.style.display = "none";
link.href = url;
link.setAttribute("download", filename ? filename : `xxx`);
document.body.appendChild(link);
link.click();
link.remove();
}
}
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
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
session.js
export const getItem = (key) => {
let data = window.sessionStorage.getItem(key)
if (data) {
return JSON.parse(data)
} else {
return false
}
};
export const setItem = (key, data) => {
if (data) {
window.sessionStorage.setItem(key, JSON.stringify(data))
return true
} else {
return true
}
};
export const removeItem = (key) => {
window.sessionStorage.removeItem(key)
return true
};
export const updateItem = (key, obj) => {
let data = window.sessionStorage.getItem(key)
if (data) {
data = JSON.parse(data);
window.sessionStorage.setItem(key, JSON.stringify({ ...data, ...obj }))
return true
} else {
return false
}
};
export const clear = () => {
window.sessionStorage.clear()
return true
};
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
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
mitt.js
import mitt from 'mitt';
export default mitt();
1
2
2
上次更新: 2024/01/30, 00:35:17
- 02
- Node与GLIBC_2.27不兼容解决方案08-19
- 03
- Git清空本地文件跟踪缓存08-13