工作多年,肯定会遇到接手别人项目的时候,有时候我们遇到大佬写的代码很优雅,封装的方法很高大上,就会很欣喜。但有的时候就会遇到一些渣的不可维护代码,头疼的鸭皮。。。
以下这些是项目中用到的,封装的方法,如果有问题还望指出,虚心求教。
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
265
266
267
/**
* 存储 sessionStorage
*/
export const __setItem = (name, content) => {
if (!name) return;
if (typeof content !== 'string') {
content = JSON.stringify(content);
}
window.sessionStorage.setItem(name, content);
};
/**
* 获取 sessionStorage
*/
export const __getItem = name => {
if (!name) return;
return window.sessionStorage.getItem(name);
};
/**
* 删除 sessionStorage
*/
export const __removeItem = name => {
if (!name) return;
window.sessionStorage.removeItem(name);
};
/**
* 检测手机
* @param {Number} mobile
* @returns {Boolean}
*/
export const _isMobile = mobile => {
var REG = /^(10|11|12|13|14|15|16|17|18|19)[0-9]{9}$/;
if (REG.test(mobile)) return true;
else return false;
};
/**
* 替换电话号码中间四位
*/
export const replacePhone = num => {
let mphone = num;
if (_isMobile(num)) {
mphone = num.substr(0, 3) + '****' + num.substr(7);
}
return mphone;
};
/**
* 去除内容中的空格
*/
export const deblank = str => {
str = str.replace(/\s*/g, '');
return str;
};
/**
* 电话号码格式344替换
*/
export const phoneSeparated = num => {
let mphone = num;
if (_isMobile(num)) {
mphone =
num.substring(0, 3) +
' ' +
num.substring(3, 7) +
' ' +
num.substring(7, 11);
}
return mphone;
};
/**
* 银行卡格式4444替换
*/
export const cardSeparated = num => {
let index = num ? num.length / 4 : 0;
let result = '';
for (let i = 0; i < index; i++) {
result += num.substring(i * 4, (i + 1) * 4) + ' ';
}
return result;
};
/**
* 身份证格式生日替换
*/
export const identityCardSeparated = num => {
if (num.length === 18) {
var str = num.substr(0, 6) + '********' + num.substr(14);
return str;
} else {
return num;
}
};
/**
* 护照号替换
*/
export const passportSeparated = num => {
if (num.length > 4) {
var str = num.substr(0, num.length - 4) + '****';
return str;
} else {
return num;
}
};
/**
* 卡号每个四位加短线
*/
export const cardNoFormat = cardNo => {
// cardNo = cardNo.replace(/\D/g, '').replace(/....(?!$)/g, '$&-');
return cardNo;
};
/**
* 每个四位加空格
*/
export const fourSpace = e => {
e.target.value = e.target.value
.replace(/\D/g, '')
.replace(/....(?!$)/g, '$& ');
console.log(e.target.value);
return e.target.value;
};
// 返回数字
export const removeFormatMoney = s => {
return parseFloat(s.replace(/[^\d.-]/g, ''));
};
/**
* 身份证校验
*/
export const IdentityCodeValid = code => {
let city = {
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: '国外'
};
let tip = '';
let pass = true;
if (!code || !/(^\d{15}$)|(^\d{18}$)|(^\d{17}(\d|X|x)$)/.test(code)) {
tip = '身份证号格式错误';
pass = false;
} else if (!city[code.substr(0, 2)]) {
tip = '地址编码错误';
pass = false;
}
if (!pass) message.error(tip);
return pass;
};
/**
* HmacSHA256加密
*/
export const encryptHmacSHA256 = value => {
const userInfo = getUserInfo();
let key = '';
if (userInfo.data) {
key = userInfo.data.publicKey;
}
let ciphertext = CryptoJS.HmacSHA256(value, key);
let hashInBase64 = CryptoJS.enc.Base64.stringify(ciphertext);
return hashInBase64;
};
/**
* 对象中的null转为空字符串
*/
export const _replaceNull = obj => {
if (typeof obj === 'object') {
Object.keys(obj).forEach(element => {
let value = obj[element];
if (value === null || value === undefined) {
// obj[element] = '';
delete obj[element];
} else if (typeof value === 'object') {
_replaceNull(value);
}
});
}
return obj;
};
/**
* 文件导出
*/
export const _checkoutFile = (fileName, response) => {
console.log('response');
console.log(response);
let blob = new Blob([response], { type: 'application/vnd.ms-excel' });
let objectUrl = URL.createObjectURL(blob);
const a = document.createElement('a');
a.setAttribute('href', objectUrl);
a.setAttribute('download', fileName);
a.click();
URL.revokeObjectURL(objectUrl);
};
/**
* url文件导出
*/
export const _urlExportFile = url => {
const a = document.createElement('a');
a.setAttribute('href', url);
a.click();
};
/**
* url校验
* disabled Boolean
*/
export const validateURL = (url, list) => {
let i = 0;
if (list) {
list.forEach(el => {
if (url === el) {
i = 1;
}
});
}
if (i !== 0) return false;
if (i === 0) return true;
};
/**
* 页面可视高度
*/
export const clientHeight = () => {
let clientHeight = document.getElementById('root').clientHeight;
let offsetHeight = document.getElementById('root').offsetHeight;
return clientHeight || offsetHeight;
};
/**
* 页面可视宽度
*/
export const clientWidth = () => {
let clientWidth = document.getElementById('root').clientWidth;
let offsetWidth = document.getElementById('root').offsetWidth;
return clientWidth || offsetWidth;
};