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
// 判空
export function isEmpty(val) {
if (typeof val === 'undefined' || val == null || val === '') {
return true;
} else {
return false;
};
};
// 将base64转换为文件
export function dataURLtoFile(dataurl, filename) {
var arr = dataurl.split(',');
var mime = arr[0].match(/:(.*?);/)[1];
var bstr = atob(arr[1]);
var n = bstr.length;
var u8arr = new Uint8Array(n);
while (n--) {
u8arr[n] = bstr.charCodeAt(n);
};
return new File([u8arr], filename, {type: mime});
};
// 证件号校验
export function validateCard(type, val) {
if (type === 1) {
let reg = /^[1-9]\d{5}(18|19|([23]\d))\d{2}((0[1-9])|(10|11|12))(([0-2][1-9])|10|20|30|31)\d{3}[0-9Xx]$/g;
if (reg.test(val)) {
return false;
} else {
return true;
};
} else if (type === 2) {
let reg = /^([a-zA-z]|[0-9]){5,17}$/g;
if (reg.test(val)) {
return false;
} else {
return true;
};
};
};
// 手机号校验
export function validatePhone(val) {
let reg = /^1(?:3[0-9]|4[5-9]|5[0-9]|6[12456]|7[0-8]|8[0-9]|9[0-9])[0-9]{8}$/g;
if (reg.test(val)) {
return false;
} else {
return true;
};
};
// 判断ios还是安卓
export function isIOSWebKit() {
const aa = window.navigator.userAgent;
if (!!aa.match(/\(i[^;]+;( U;)? CPU.+Mac OS X/)) {// ios端
return true;
} else if (aa.indexOf('Android') !== -1 || aa.indexOf('Adr') !== -1) {// 安卓端
return false;
}
}
//当前方法传参数两种 字符串或者map
export function callMobile(handlerInterface, parameters) {
let classStr = Object.prototype.toString.call(parameters);
if (classStr === '[object String]' || classStr === '[object Object]') {
let param = parameters;
if (classStr === "[object Object]") { //判断传参是str ,还是object
//handlerInterface由iOS addScriptMessageHandler与andorid addJavascriptInterface 代码注入而来。
param = JSON.stringify(parameters);//参数必须转化成json格式
}
try {
if (isIOSWebKit()) {//ios
if (window.webkit !== undefined) {
if (param == '{}') {
window.webkit.messageHandlers[handlerInterface].postMessage(null);
} else {
window.webkit.messageHandlers[handlerInterface].postMessage(param);
}
}
} else if (isIOSWebKit() === false) {
//安卓传输不了js json对象,只能传输string
if (param == '{}') {
window.H5page[handlerInterface]();
} else {
window.H5page[handlerInterface](param);
}
}
} catch (e) {
}
}
}
export function getDateStr(date) {
let mouth = date.getMonth() + 1;
let day = date.getDate();
if (mouth < 10) {
mouth = "0" + mouth;
}
if (day < 10) {
day = "0" + day;
}
let resultDateStr = date.getFullYear() + "-" + mouth + "-" + day;//将日期转化为字符串格式
return resultDateStr;
}