[JavaScript]

Plain text view
Copy the code

?
001
002
003
004
005
006
007
008
009
010
011
012
013
014
015
016
017
018
019
020
021
022
023
024
025
026
027
028
029
030
031
032
033
034
035
036
037
038
039
040
041
042
043
044
045
046
047
048
049
050
051
052
053
054
055
056
057
058
059
060
061
062
063
064
065
066
067
068
069
070
071
072
073
074
075
076
077
078
079
080
081
082
083
084
085
086
087
088
089
090
091
092
093
094
095
096
097
098
099
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
/ * *

* store localStorage

* /
export const setStore = (name, content) => {

if
(! name)
return
;

if
(
typeof
content ! = =
'string'
) {

content = JSON.stringify(content);

}

window.localStorage.setItem(name, content);
};
/ * *

* get localStorage

* /
export const getStore = name => {

if
(! name)
return
;

return
window.localStorage.getItem(name);
};
/ * *

* delete localStorage

* /
export const removeStore = name => {

if
(! name)
return
;

window.localStorage.removeItem(name);
};
/ * *

* Generate random strings (length can be specified)

* @param len

* @returns {string}

* /
export const randomString = (len) => {

len = len || 8;

let $chars =
'ABCDEFGHJKMNPQRSTWXYZabcdefhijkmnprstwxyz2345678'
;

/**** removes confusing characters oOLl,9gq,Vv,Uu,I1****/ by default

let maxPos = $chars.length;

let pwd =
' '
;

for
(let i = 0; i < len; i++) {

pwd += $chars.charAt(Math.floor(Math.random() * maxPos));

}

return
pwd;
};
/ * *

* randomWord produces random alphanumeric combinations of any length

* @param randomFlag whether arbitrary length min- Arbitrary length minimum bit [fixed bit] Max - Arbitrary length maximum bit

* @param min

* @param max

* @returns {string}

* Call method:

* Generates a 3-32 bit random string

* randomWord (true, 3, 32). For example: olyOXUF5oDsuMmXl3Mi48

* Generates a random 32-bit string

* randomWord(false,32); For example: fjpnWj29Bb8boiXbLeDF0nxkR4aYcLRl

* /
export const randomWord = (randomFlag, min, max) => {

let str =
""
.

range = min,

arr = [
'0'
.
'1'
.
'2'
.
'3'
.
'4'
.
'5'
.
'6'
.
'7'
.
'8'
.
'9'
.
'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'
.
'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'
];

// randomly generated

if
(randomFlag) {

range = Math.round(Math.random() * (max - min)) + min;

}

for
(let i = 0; i < range; i++) {

let pos = Math.round(Math.random() * (arr.length - 1));

str += arr[pos];

}

return
str;
};
/ * *

* Get the post-URL parameters

* /
export const GetRequest = () => {

let url = location.search;
// Get the "?" in the url The string following the character

let theRequest =
new
Object();

if
(url.indexOf(
"?"
)! = 1) {

let str = url.substr(1);

let strs = str.split(
"&"
);

for
(let i = 0; i < strs.length; i++) {

theRequest[strs[i].split(
"="
)[0]] = (strs[i].split(
"="
) [1]);

}

}

return
theRequest;
};
/ * *

* Generate random color values

* /
export const getRandomColor = () => {

const rgb = [];

for
(let i = 0; i < 3; ++i) {

let color = Math.floor(Math.random() * 256).toString(16);

color = color.length === 1 ?
'0'
+ color : color;

rgb.push(color)

}

return
The '#'
+ rgb.join(
' '
)
};
/ * *

* Verify id number

* @param el Number input

* @returns {boolean}

* /
export const checkCardNo = (el) => {

let txtval = el.value;

let reg = /(^\d{15}$)|(^\d{18}$)|(^\d{17}(\d|X|x)$)/;

return
reg.test(txtval);
};
/ * *

* Gets the string length in bytes

* @param {String}

* @returns {Boolean}

* /
export const checkLength = (v) => {

let realLength = 0;

let len = v.length;

for
(let i = 0; i < len; i++) {

let charCode = v.charCodeAt(i);

if
(charCode >= 0 && charCode <= 128) realLength += 1;

else
realLength += 2;

}

return
realLength;
};
/ * *

* Determine the wechat browser

* @returns {Boolean}

* /
export const isWeiXin = () => {

let ua = window.navigator.userAgent.toLowerCase();

if
(ua.match(/MicroMessenger/i) ==
'micromessenger'
) {

return
true
;

}
else
{

return
false
;

}
};
/ * *

* write cookies

* /
export const setCookie = (name, value, time) => {

let strsec = getsec(time);

let exp =
new
Date();

exp.setTime(exp.getTime() + strsec * 1);

document.cookie = name +
"="
+ escape(value) +
"; expires="
+ exp.toGMTString();
};
/ * *

* read cookies

* /
export const getCookie = (name) => {

let arr, reg =
new
RegExp(
"(^| )"
+ name +
"= (/ ^; (*). | $)"
);

if
(arr = document.cookie.match(reg))
return
(arr[2]);

else
return
null
;
};
/ * *

* delete cookies

* /
export const delCookie = (name) => {

let exp =
new
Date();

exp.setTime(exp.getTime() - 1);

let cval = getCookie(name);

if
(cval ! =
null
) document.cookie = name +
"="
+ cval +
"; expires="
+ exp.toGMTString();
};
/ * *

* Browser judgment

* Usage examples -- multiple sets of pages to determine whether to display mobile:

* let ua = parseUA();

* if (! ua.mobile) {

* location.href = './pc.html';

*}

* /
export const parseUA = () => {

let u = navigator.userAgent;

let u2 = navigator.userAgent.toLowerCase();

return
{
// Mobile terminal browser version information

trident: u.indexOf(
'Trident'
) > 1,

/ / IE kernel

presto: u.indexOf(
'Presto'
) > 1,

/ / opera kernel

webKit: u.indexOf(
'AppleWebKit'
) > 1,

// Apple, Google kernel

gecko: u.indexOf(
'Gecko'
) > -1 && u.indexOf(
'KHTML'
) == -1,

// The Firefox kernel

mobile: !! u.match(/AppleWebKit.*Mobile.*/),

// Whether it is a mobile terminal

ios: !! u.match(/\(i[^;] +; ( U;) ? CPU.+Mac OS X/),

/ / ios terminal

android: u.indexOf(
'Android'
) > -1 || u.indexOf(
'Linux'
) > 1,

// An Android terminal or uc browser

iPhone: u.indexOf(
'iPhone'
) > 1,

// Whether the browser is iPhone or QQHD

iPad: u.indexOf(
'iPad'
) > 1,

/ / whether the device

webApp: u.indexOf(
'Safari'
) == -1,

// Whether the web should be a program, without a header and a bottom

iosv: u.substr(u.indexOf(
'iPhone OS'
) + 9, 3),

weixin: u2.match(/MicroMessenger/i) ==
"micromessenger"
.

ali: u.indexOf(
'AliApp'
) > 1,

};
};
/ * *

* generate UUID

* @returns {string}

* /
export const generateUUID = () => {

let d =
new
Date().getTime();

let uuid =
'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'
.replace(/[xy]/g,
function
(c) {

let r = (d + Math.random() * 16) % 16 | 0;

d = Math.floor(d / 16);

return
(c ===
'x'
? r : (r & 0x7 | 0x8)).toString(16);

});

return
uuid;
};
/ * *

* Delete the left and right Spaces

* @param str

* @returns {string | * | void}

* /
function
trim(str) {

return
str.replace(/(^\s*)|(\s*$)/g,
""
);
}
/ * *

* Remove the left space

* @param str

* @returns {string | * | void}

* /
function
ltrim(str) {

return
str.replace(/(^\s*)/g,
""
);
}
/ * *

* Delete the space on the right

* @param str

* @returns {string | * | void}

* /
function
rtrim(str) {

return
str.replace(/(\s*$)/g,
""
);
}
/ * *

* Object array to two-dimensional array

* @param objArr

* /
function
obj2Arr(objArr) {

objArr.length > 0 && objArr.map(item => {

return
Object.values(item);

})
}
/ * *

* Find the maximum value of an attribute in the object array

* @param array

* @param item

* @returns val

* /
function
maxItemInObjArr(array, item) {

let max = Math.max.apply(Math, array.map(
function
(obj) {

return
obj[item];

}));

return
max;
}
/ * *

* Determine the current network environment

* /
export const isWifi = () => {

try
{

let wifi =
true
;

let ua = window.navigator.userAgent;

let con = window.navigator.connection;

// If it is wechat

if
(/MicroMessenger/.test(ua)) {

if
(ua.indexOf(
'WIFI'
) > = 0) {

return
true

}
else
{

wifi =
false

}

// If navigator. Connection is supported

}
else
if
(con) {

let network = con.type;

if
(network ! = =
'wifi'
&& network ! = =
'2'
&& network ! = =
'unknown'
) {

wifi =
false

}

}

return
wifi

}
catch
(e) {

return
false

}
};
/ * *

* Capitalize the first letter

* @param str

* @returns {string}

* /
export const fistLetterUpper = (str) => {

return
str.charAt(0).toUpperCase() + str.slice(1);
};
/ * *

* Filter illegal strings

* /
export const illegalFilter = (str) => {

let regEn = /[`~!@
# $% ^ & * () _ + < >? : "{},. / /; '[\]]/im;

Let regCn = /[·!
#¥(--) :; "" ',, |"." ? , 【 】 [\]] / im;

if
(regEn.test(str) || regCn.test(str))
return
false
;

return
true
;
};