util.js
4.64 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
import {
Toast,
Notify,
Dialog
} from 'vant';
const Util = {
/**
* 简单alert
* title 标题
* message 内容
* className 自定义class
* showCancelButton 是否展示取消按钮
* confirmButtonText 确认按钮文案
* cancelButtonText 取消按钮文案
* confirmButtonColor 确认按钮颜色
* cancelButtonColor 取消按钮颜色
*/
msg: function(options) {
Dialog({
title: options.title || '',
message: options.message || '',
className: options.className || '',
showCancelButton: options.showCancelButton || false,
confirmButtonText: options.confirmButtonText || '确认',
cancelButtonText: options.cancelButtonText || '取消',
confirmButtonColor: options.confirmButtonColor || '#1989fa',
cancelButtonColor: options.cancelButtonColor || '#000',
beforeClose: function(e, done) {
if (e == 'confirm') {
options.beforeClose ? (options.beforeClose)(done) : done();
} else {
done();
}
}
});
},
/**
* 提示框
*/
Info: function(options) {
Notify({
message: options.message || '提示',
duration: options.time || 2000,
background: options.background || '#f44'
});
},
/**
* 轻提示
*/
Tips: function(options) {
Toast({
message: options.message || '提示',
duration: options.duration || 3000,
type: options.type || 'text',
icon: options.icon || ''
})
},
/**
* loading
*/
showLoading: function(message) {
Toast.loading({
mask: true,
message: message || '加载中...'
});
},
/**
* 关闭loading
*/
hideLoading: function() {
Toast.clear();
},
/**
* 校验手机号码格式
*/
checkPhone: function(mobile) {
var myreg = /^(13[0-9]|15[0-9]|16[0-9]|17[0-9]|18[0-9]|19[0-9]|14[0-9])[0-9]{8}$/;
return myreg.test(mobile);
},
/**
* 是否特殊字符
*/
isSpecialLetter: function(str) {
var reg = new RegExp("[`~!@#$^&*=|{}':;',\\[\\]<>?~!@#¥……&*——|{}【】‘;:”“'。,、? ]");
return reg.test(str);
},
/**
* 身份证号码为15位或者18位,15位时全为数字,18位前17位为数字,最后一位是校验位,可能为数字或字符X
*/
isCardId: function(sId) {
let iSum = 0;
let sBirthday = '';
let aCity = {
11: '北京',
12: '天津',
13: '河北',
14: '山西',
15: '内蒙古',
21: '辽宁',
22: '吉林',
23: '黑龙江',
31: '上海',
32: '江苏',
33: '浙江',
34: '安徽',
35: '福建',
36: '江西',
37: '山东',
41: '河南',
42: '湖北',
43: '湖南',
44: '广东',
45: '广西',
46: '海南',
50: '重庆',
51: '四川',
52: '贵州',
53: '云南',
54: '西藏',
61: '陕西',
62: '甘肃',
63: '青海',
64: '宁夏',
65: '新疆',
71: '台湾',
81: '香港',
82: '澳门',
91: '国外'
};
if (!/^\d{17}(\d|x)$/i.test(sId)) {
//return "你输入的身份证长度或格式错误";
return false;
}
sId = sId.replace(/x$/i, 'a');
if (aCity[parseInt(sId.substr(0, 2))] == null) {
//return "你的身份证地区非法";
return false;
}
sBirthday = sId.substr(6, 4) + '-' + Number(sId.substr(10, 2)) + '-' + Number(sId.substr(12, 2));
let d = new Date(sBirthday.replace(/-/g, '/'));
if (sBirthday != (d.getFullYear() + '-' + (d.getMonth() + 1) + '-' + d.getDate())) {
//return "身份证上的出生日期非法";
return false;
}
for (let i = 17; i >= 0; i--) {
iSum += (Math.pow(2, i) % 11) * parseInt(sId.charAt(17 - i), 11);
}
if (iSum % 11 != 1) {
//return "你输入的身份证号非法";
return false;
//aCity[parseInt(sId.substr(0,2))]+","+sBirthday+","+(sId.substr(16,1)%2?"男":"女");
//此次还可以判断出输入的身份证号的人性别
}
return true;
},
/**
* 获取浏览器参数
*/
getUrlParams: function(name) {
var vars = [],
hash;
var hashes = window.location.href.slice(window.location.href.indexOf('?') + 1).split('&');
for (var i = 0; i < hashes.length; i++) {
hash = hashes[i].split('=');
vars.push(hash[0]);
vars[hash[0]] = hash[1];
}
return vars[name];
},
};
export const getUrlParams = Util.getUrlParams;
export const checkPhone = Util.checkPhone;
export const showToast = Util.Info;
export const showModal = Util.msg;
export const showLoading = Util.showLoading;
export const hideLoading = Util.hideLoading;