utils.js
6.81 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
/**
* 获取当前链接参数
* @param {*} name
*/
export const getLinkParam = name => {
return decodeURIComponent((new RegExp('[?|&]' + name + '=' + '([^&;]+?)(&|#|;|$)').exec(location.href) || [, ""])[1].replace(/\+/g, '%20')) || null;
};
/**
* 去掉字符串两端空格 trim
* @param {string} str
*/
export const trim = str => {
return str.replace(/(^\s*)|(\s*$)/g, '');
}
/**
* 验证邮箱
* @param {string} str
*/
export const checkEmail = str => {
let re = /^(\w-*\.*)+@(\w-?)+(\.\w{2,})+$/;
if (re.test(str)) {
return true;
} else {
return false;
}
}
/**
* 验证手机 1开头+10位数
* @param {string} str
*/
export const checkMobile = str => {
let re = /^1\d{10}$/;
// let re = /^(13[0-9]|14[57]|15[0-9]|17[0-9]|18[0-9])\d{8}$/; //严格模式
if (re.test(str)) {
return true;
} else {
return false;
}
}
/**
* 获取Uuid
*/
export const uuid = () => {
var s = [];
var hexDigits = "0123456789abcdef";
for (var i = 0; i < 36; i++) {
s[i] = hexDigits.substr(Math.floor(Math.random() * 0x10), 1);
}
s[14] = "4"; // bits 12-15 of the time_hi_and_version field to 0010
s[19] = hexDigits.substr((s[19] & 0x3) | 0x8, 1); // bits 6-7 of the clock_seq_hi_and_reserved to 01
s[8] = s[13] = s[18] = s[23] = "-";
var uuid = s.join("");
return uuid;
}
/**
* 设置cookies
* @param {*} name
* @param {*} value
* @param {*} Days
*/
export const setCookie = (name, value, Days = 0) => {
if (Days <= 0) Days = 30;
var exp = new Date();
exp.setTime(exp.getTime() + Days * 24 * 60 * 60 * 1000);
document.cookie = name + "=" + encodeURI(value) + ";expires=" + exp.toUTCString();
}
/**
* 获取cookies
* @param {*} name
* @param {*} value
* @param {*} Days
*/
export const getCookie = (name) => {
var arr, reg = new RegExp("(^| )" + name + "=([^;]*)(;|$)");
if (arr = document.cookie.match(reg)) {
return decodeURI(arr[2]);
} else {
return "";
}
}
/**
* 删除cookies
* @param {*} name
*/
export const deleteCookie = (name) => {
var exp = new Date();
exp.setTime(exp.getTime() - 1);
var cval = getCookie(name);
if (cval != null)
document.cookie = name + "=" + cval + ";expires=" + exp.toUTCString();
}
/**
* 判断是否微信客户端
* @param {*} name
*/
export const isWeiXin = () => {
var ua = window.navigator.userAgent.toLowerCase();
if (ua.match(/MicroMessenger/i) == 'micromessenger') {
return true;
} else {
return false;
}
}
/**
* 时间戳格式化(yyyy-MM-dd hh:mm:ss)
* @param {*} timestamp
* @param {*} format
*/
export const timestampFormat = (timestamp, format) => {
Date.prototype.Format = function (fmt) {
var o = {
"M+": this.getMonth() + 1, //月份
"d+": this.getDate(), //日
"h+": this.getHours(), //小时
"m+": this.getMinutes(), //分
"s+": this.getSeconds(), //秒
"q+": Math.floor((this.getMonth() + 3) / 3), //季度
"S": this.getMilliseconds() //毫秒
};
if (/(y+)/.test(fmt)) fmt = fmt.replace(RegExp.$1, (this.getFullYear() + "").substr(4 - RegExp.$1.length));
for (var 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;
}
if (timestamp && timestamp.length == 10) timestamp += "000";
var date = new Date();
if (timestamp) date.setTime(timestamp);
if (format == null || format == "") {
format = "yyyy-MM-dd hh:mm:ss";
}
return date.Format(format);
};
/**
* 日期转时间戳
* @param {*} stringTime
*/
export const dateParse = stringTime => {
if (stringTime) {
var date = new Date(stringTime);
} else {
var date = new Date();
}
return Date.parse(date);
};
/**
* 生成指定范围内的随机数
* @param {*} min
* @param {*} max
*/
export const getRandom = (min, max) => {
var c = max - min + 1;
return Math.floor(Math.random() * c + min);
};
/**
* 日期格式化
*/
export const formatDate = {
SIGN_REGEXP: /([yMdhsm])(\1*)/g,
DEFAULT_PATTERN: 'yyyy-MM-dd',
// 格林威治时间转时间格式 兼容ios
GMTToStr: function (time) {
var date = time.substr(0, 10); //年月日
var hours = time.substring(11, 13);
var minutes = time.substring(14, 16);
var seconds = time.substring(17, 19);
var timeFlag = date + ' ' + hours + ':' + minutes + ':' + seconds;
timeFlag = timeFlag.replace(/-/g, "/");
timeFlag = new Date(timeFlag);
timeFlag = new Date(timeFlag.getTime() + 8 * 3600 * 1000);
timeFlag = timeFlag.getFullYear() + '-' + ((timeFlag.getMonth() + 1) < 10 ? "0" + (timeFlag.getMonth() + 1) : (timeFlag.getMonth() + 1)) + '-' + (timeFlag.getDate() < 10 ? "0" + timeFlag.getDate() : timeFlag.getDate()) + ' ' + timeFlag.getHours() + ':' + timeFlag.getMinutes() + ':' + (timeFlag.getSeconds() < 10 ? "0" + timeFlag.getSeconds() : timeFlag.getSeconds());
return timeFlag.replace(/-/g, "/");
},
padding: function (s, len) {
var len = len - (s + '').length;
for (var i = 0; i < len; i++) {
s = '0' + s;
}
return s;
},
// 格林威治时间转时间格式 兼容ios
formatGMT: function (time, pattern) {
return formatDate.format(new Date(formatDate.GMTToStr(time)), pattern);
},
format: function (date, pattern) {
pattern = pattern || formatDate.DEFAULT_PATTERN;
return pattern.replace(formatDate.SIGN_REGEXP, function ($0) {
switch ($0.charAt(0)) {
case 'y':
return formatDate.padding(date.getFullYear(), $0.length);
case 'M':
return formatDate.padding(date.getMonth() + 1, $0.length);
case 'd':
return formatDate.padding(date.getDate(), $0.length);
case 'w':
return date.getDay() + 1;
case 'h':
return formatDate.padding(date.getHours(), $0.length);
case 'm':
return formatDate.padding(date.getMinutes(), $0.length);
case 's':
return formatDate.padding(date.getSeconds(), $0.length);
}
});
},
parse: function (dateString, pattern) {
var matchs1 = pattern.match(formatDate.SIGN_REGEXP);
var matchs2 = dateString.match(/(\d)+/g);
if (matchs1.length == matchs2.length) {
var _date = new Date(1970, 0, 1);
for (var i = 0; i < matchs1.length; i++) {
var _int = parseInt(matchs2[i]);
var sign = matchs1[i];
switch (sign.charAt(0)) {
case 'y':
_date.setFullYear(_int);
break;
case 'M':
_date.setMonth(_int - 1);
break;
case 'd':
_date.setDate(_int);
break;
case 'h':
_date.setHours(_int);
break;
case 'm':
_date.setMinutes(_int);
break;
case 's':
_date.setSeconds(_int);
break;
}
}
return _date;
}
return null;
}
}