zmdms-utils 0.0.4 → 0.0.6
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/es/auth.d.ts +2 -1
- package/dist/es/auth.js +9 -1
- package/dist/es/baseUrl.d.ts +6 -0
- package/dist/es/baseUrl.js +23 -1
- package/dist/es/common.d.ts +39 -0
- package/dist/es/common.js +125 -0
- package/dist/es/crypto.d.ts +48 -0
- package/dist/es/crypto.js +93 -0
- package/dist/es/math.d.ts +29 -0
- package/dist/es/math.js +87 -0
- package/dist/es/node_modules/mitt/dist/mitt.js +3 -0
- package/dist/es/passwordValidate.d.ts +9 -0
- package/dist/es/passwordValidate.js +169 -0
- package/dist/es/request.d.ts +22 -2
- package/dist/es/request.js +123 -28
- package/dist/es/validate.d.ts +22 -0
- package/dist/es/validate.js +43 -0
- package/dist/index.d.ts +7 -1
- package/dist/index.js +7 -1
- package/package.json +7 -2
- package/src/auth.ts +8 -0
- package/src/baseUrl.ts +28 -1
- package/src/common.ts +129 -0
- package/src/crypto.ts +109 -0
- package/src/index.ts +20 -1
- package/src/math.ts +90 -0
- package/src/passwordValidate.ts +196 -0
- package/src/request.ts +177 -27
- package/src/validate.ts +45 -0
package/src/math.ts
ADDED
|
@@ -0,0 +1,90 @@
|
|
|
1
|
+
type numType = "number" | "string";
|
|
2
|
+
/**
|
|
3
|
+
* 加法
|
|
4
|
+
*/
|
|
5
|
+
export function plus(num1: numType, num2: numType) {
|
|
6
|
+
try {
|
|
7
|
+
const _num1 = Number(num1);
|
|
8
|
+
const _num2 = Number(num2);
|
|
9
|
+
return (isNaN(_num1) ? 0 : _num1) + (isNaN(_num2) ? 0 : _num2);
|
|
10
|
+
} catch (err) {
|
|
11
|
+
console.error("计算加法出错", err);
|
|
12
|
+
return 0;
|
|
13
|
+
}
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
/**
|
|
17
|
+
* 减法
|
|
18
|
+
*/
|
|
19
|
+
export function minus(num1: numType, num2: numType) {
|
|
20
|
+
try {
|
|
21
|
+
const _num1 = Number(num1);
|
|
22
|
+
const _num2 = Number(num2);
|
|
23
|
+
return (isNaN(_num1) ? 0 : _num1) - (isNaN(_num2) ? 0 : _num2);
|
|
24
|
+
} catch (err) {
|
|
25
|
+
console.error("计算减法出错", err);
|
|
26
|
+
return 0;
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
/**
|
|
31
|
+
* 乘法
|
|
32
|
+
*/
|
|
33
|
+
export function times(num1: numType, num2: numType) {
|
|
34
|
+
try {
|
|
35
|
+
const _num1 = Number(num1);
|
|
36
|
+
const _num2 = Number(num2);
|
|
37
|
+
return (isNaN(_num1) ? 0 : _num1) * (isNaN(_num2) ? 0 : _num2);
|
|
38
|
+
} catch (err) {
|
|
39
|
+
console.error("计算乘法出错", err);
|
|
40
|
+
return 0;
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
/**
|
|
45
|
+
* 除法
|
|
46
|
+
*/
|
|
47
|
+
export function divide(num1: numType, num2: numType) {
|
|
48
|
+
try {
|
|
49
|
+
const _num1 = Number(num1);
|
|
50
|
+
const _num2 = Number(num2);
|
|
51
|
+
return (isNaN(_num1) ? 0 : _num1) / (isNaN(_num2) ? 0 : _num2);
|
|
52
|
+
} catch (err) {
|
|
53
|
+
console.error("计算除法出错", err);
|
|
54
|
+
return 0;
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
/**
|
|
59
|
+
* 四舍五入保留小数
|
|
60
|
+
*/
|
|
61
|
+
export function exactRound(num: numType, ratio: number) {
|
|
62
|
+
try {
|
|
63
|
+
return Number(num).toFixed(ratio);
|
|
64
|
+
} catch (err) {
|
|
65
|
+
return num;
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
/**
|
|
70
|
+
* 将字节数加上合适的单位
|
|
71
|
+
* @param numBytes 字节数
|
|
72
|
+
* @returns
|
|
73
|
+
*/
|
|
74
|
+
export function formatUnit(numBytes: number) {
|
|
75
|
+
let _numBytes = Number(numBytes);
|
|
76
|
+
|
|
77
|
+
if (isNaN(_numBytes)) {
|
|
78
|
+
return numBytes;
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
const units = ["B", "KB", "MB", "GB", "TB"];
|
|
82
|
+
let unitIndex = 0;
|
|
83
|
+
|
|
84
|
+
while (_numBytes >= 1024 && unitIndex < units.length - 1) {
|
|
85
|
+
_numBytes /= 1024;
|
|
86
|
+
unitIndex++;
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
return _numBytes.toFixed(2) + units[unitIndex];
|
|
90
|
+
}
|
|
@@ -0,0 +1,196 @@
|
|
|
1
|
+
const alphabet1 = [
|
|
2
|
+
"abcdefghijklmnopqrstuvwxyz",
|
|
3
|
+
"ABCDEFGHIJKLMNOPQRSTUVWXYZ",
|
|
4
|
+
"0123456789",
|
|
5
|
+
];
|
|
6
|
+
const alphabet2 = ["qwertyuiop", "asdfghjkl", "zxcvbnm"];
|
|
7
|
+
const alphabet3 = ["qaz", "wsx", "edc", "rfv", "tgb", "yhn", "ujm"];
|
|
8
|
+
|
|
9
|
+
function isConsecutiveChars(
|
|
10
|
+
char1: string,
|
|
11
|
+
char2: string,
|
|
12
|
+
alphabetArr: string[]
|
|
13
|
+
) {
|
|
14
|
+
// const lowerCaseAlphabet = 'abcdefghijklmnopqrstuvwxyz'; // 小写字母
|
|
15
|
+
// const upperCaseAlphabet = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'; // 大写字母
|
|
16
|
+
// const numeric = '0123456789'; // 数字
|
|
17
|
+
|
|
18
|
+
// 找到包含两个字符的字符集
|
|
19
|
+
const alphabet = alphabetArr.find(
|
|
20
|
+
(alph: string) => alph.includes(char1) && alph.includes(char2)
|
|
21
|
+
);
|
|
22
|
+
|
|
23
|
+
if (!alphabet) {
|
|
24
|
+
return false; // 如果两个字符不在同一字符集中,那么它们不可能连续
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
const index1 = alphabet.indexOf(char1);
|
|
28
|
+
const index2 = alphabet.indexOf(char2);
|
|
29
|
+
|
|
30
|
+
// return Math.abs(index1 - index2) === 1; // 如果两个字符在字符集中的位置相邻,那么它们是连续的
|
|
31
|
+
return index1 - index2;
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
export function validatePassword(password: string, username: string) {
|
|
35
|
+
// 用正则表达式检查密码基本规则
|
|
36
|
+
const regex = /^(?=.*[a-z])(?=.*[A-Z])(?=.*\d)(?=.*[^\da-zA-Z]).{8,50}$/;
|
|
37
|
+
if (!regex.test(password)) {
|
|
38
|
+
return {
|
|
39
|
+
result: false,
|
|
40
|
+
message:
|
|
41
|
+
"密码中必须包含大小写字母、数字、特殊字符,且长度必须大于8个字符!",
|
|
42
|
+
};
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
// 检查密码是否与用户名相同
|
|
46
|
+
if (password === username) {
|
|
47
|
+
return {
|
|
48
|
+
result: false,
|
|
49
|
+
message: "密码与用户名相同!",
|
|
50
|
+
};
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
// 检查密码中是否有相同且连续的字符 超过三次
|
|
54
|
+
let count1 = 0;
|
|
55
|
+
for (let i = 0; i < password.length - 1; i++) {
|
|
56
|
+
if (password[i] === password[i + 1]) {
|
|
57
|
+
count1++;
|
|
58
|
+
if (count1 >= 2) {
|
|
59
|
+
return {
|
|
60
|
+
result: false,
|
|
61
|
+
message: "密码中有相同且连续超过三次的字符!",
|
|
62
|
+
};
|
|
63
|
+
}
|
|
64
|
+
} else {
|
|
65
|
+
count1 = 0;
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
// 检查密码中是否有连续的字母或数字 连续超过三次
|
|
70
|
+
let count = 0;
|
|
71
|
+
let startChart = 0;
|
|
72
|
+
let currentConsecutiveCount = 0; // 记录上次是递增还是递减
|
|
73
|
+
|
|
74
|
+
// 第一次循环 找出 连续字母 数字
|
|
75
|
+
for (let i = 0; i < password.length - 1; i++) {
|
|
76
|
+
const consecutiveCount = isConsecutiveChars(
|
|
77
|
+
password[i],
|
|
78
|
+
password[i + 1],
|
|
79
|
+
alphabet1
|
|
80
|
+
);
|
|
81
|
+
// 是递增 还是 递减
|
|
82
|
+
if (consecutiveCount === 1 || consecutiveCount === -1) {
|
|
83
|
+
if (currentConsecutiveCount === 0) {
|
|
84
|
+
currentConsecutiveCount = consecutiveCount;
|
|
85
|
+
}
|
|
86
|
+
if (count === 0) {
|
|
87
|
+
startChart = i;
|
|
88
|
+
}
|
|
89
|
+
// 如果记录到上次跟本次的连续规则相同
|
|
90
|
+
if (currentConsecutiveCount === consecutiveCount) {
|
|
91
|
+
count++;
|
|
92
|
+
if (count >= 2) {
|
|
93
|
+
// 密码中不应含有连续的字母或数字
|
|
94
|
+
return {
|
|
95
|
+
result: false,
|
|
96
|
+
message: `从第${startChart + 1}个字符开始,有连续${
|
|
97
|
+
currentConsecutiveCount === 1 ? "递减" : "递增"
|
|
98
|
+
}超过三位的字母或数字!`,
|
|
99
|
+
};
|
|
100
|
+
}
|
|
101
|
+
} else {
|
|
102
|
+
// 需要重置规则
|
|
103
|
+
count = 0;
|
|
104
|
+
startChart = 0;
|
|
105
|
+
}
|
|
106
|
+
currentConsecutiveCount = consecutiveCount;
|
|
107
|
+
} else {
|
|
108
|
+
count = 0;
|
|
109
|
+
startChart = 0;
|
|
110
|
+
currentConsecutiveCount = 0;
|
|
111
|
+
}
|
|
112
|
+
}
|
|
113
|
+
// 第二次循环 找出键盘连续横排字母
|
|
114
|
+
for (let i = 0; i < password.length - 1; i++) {
|
|
115
|
+
const consecutiveCount = isConsecutiveChars(
|
|
116
|
+
password[i],
|
|
117
|
+
password[i + 1],
|
|
118
|
+
alphabet2
|
|
119
|
+
);
|
|
120
|
+
// 是递增 还是 递减
|
|
121
|
+
if (consecutiveCount === 1 || consecutiveCount === -1) {
|
|
122
|
+
if (currentConsecutiveCount === 0) {
|
|
123
|
+
currentConsecutiveCount = consecutiveCount;
|
|
124
|
+
}
|
|
125
|
+
if (count === 0) {
|
|
126
|
+
startChart = i;
|
|
127
|
+
}
|
|
128
|
+
// 如果记录到上次跟本次的连续规则相同
|
|
129
|
+
if (currentConsecutiveCount === consecutiveCount) {
|
|
130
|
+
count++;
|
|
131
|
+
if (count >= 2) {
|
|
132
|
+
// 密码中不应含有连续的字母或数字
|
|
133
|
+
return {
|
|
134
|
+
result: false,
|
|
135
|
+
message: `从第${startChart + 1}个字符开始,有按键盘横向连续${
|
|
136
|
+
currentConsecutiveCount === 1 ? "递减" : "递增"
|
|
137
|
+
}超过三位的字母!`,
|
|
138
|
+
};
|
|
139
|
+
}
|
|
140
|
+
} else {
|
|
141
|
+
// 需要重置规则
|
|
142
|
+
count = 0;
|
|
143
|
+
startChart = 0;
|
|
144
|
+
}
|
|
145
|
+
currentConsecutiveCount = consecutiveCount;
|
|
146
|
+
} else {
|
|
147
|
+
count = 0;
|
|
148
|
+
startChart = 0;
|
|
149
|
+
currentConsecutiveCount = 0;
|
|
150
|
+
}
|
|
151
|
+
}
|
|
152
|
+
// 第三次循环 找出键盘连续竖排字母
|
|
153
|
+
for (let i = 0; i < password.length - 1; i++) {
|
|
154
|
+
const consecutiveCount = isConsecutiveChars(
|
|
155
|
+
password[i],
|
|
156
|
+
password[i + 1],
|
|
157
|
+
alphabet3
|
|
158
|
+
);
|
|
159
|
+
// 是递增 还是 递减
|
|
160
|
+
if (consecutiveCount === 1 || consecutiveCount === -1) {
|
|
161
|
+
if (currentConsecutiveCount === 0) {
|
|
162
|
+
currentConsecutiveCount = consecutiveCount;
|
|
163
|
+
}
|
|
164
|
+
if (count === 0) {
|
|
165
|
+
startChart = i;
|
|
166
|
+
}
|
|
167
|
+
// 如果记录到上次跟本次的连续规则相同
|
|
168
|
+
if (currentConsecutiveCount === consecutiveCount) {
|
|
169
|
+
count++;
|
|
170
|
+
if (count >= 2) {
|
|
171
|
+
// 密码中不应含有连续的字母或数字
|
|
172
|
+
return {
|
|
173
|
+
result: false,
|
|
174
|
+
message: `从第${startChart + 1}个字符开始,有按键盘竖向连续${
|
|
175
|
+
currentConsecutiveCount === 1 ? "递减" : "递增"
|
|
176
|
+
}超过三位的字母或数字!`,
|
|
177
|
+
};
|
|
178
|
+
}
|
|
179
|
+
} else {
|
|
180
|
+
// 需要重置规则
|
|
181
|
+
count = 0;
|
|
182
|
+
startChart = 0;
|
|
183
|
+
}
|
|
184
|
+
currentConsecutiveCount = consecutiveCount;
|
|
185
|
+
} else {
|
|
186
|
+
count = 0;
|
|
187
|
+
startChart = 0;
|
|
188
|
+
currentConsecutiveCount = 0;
|
|
189
|
+
}
|
|
190
|
+
}
|
|
191
|
+
|
|
192
|
+
// 如果通过了所有的检查,则返回true
|
|
193
|
+
return {
|
|
194
|
+
result: true,
|
|
195
|
+
};
|
|
196
|
+
}
|
package/src/request.ts
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
// 请求方法
|
|
2
2
|
import axios, { AxiosRequestConfig } from "axios";
|
|
3
|
-
import { getToken } from "./
|
|
3
|
+
import { getToken, removeToken } from "./auth";
|
|
4
|
+
import { Crypto } from "./crypto";
|
|
4
5
|
|
|
5
6
|
const defaultOptions: IOtherOptions = {
|
|
6
7
|
TenantId: "000000",
|
|
@@ -10,12 +11,44 @@ const defaultOptions: IOtherOptions = {
|
|
|
10
11
|
|
|
11
12
|
export class AxiosRequest {
|
|
12
13
|
private otherOptions: IOtherOptions;
|
|
14
|
+
private crypto: IOtherOptions["crypto"]; // 加密方法处理
|
|
15
|
+
private jumpCallback: IOtherOptions["jumpCallback"]; // 需要做的跳转处理
|
|
16
|
+
// 登录是否过期 这个变量是为了让弹出提示只展示一次
|
|
17
|
+
private isExpired: boolean = false;
|
|
18
|
+
// 被挤下线 定义变量的目的同上
|
|
19
|
+
private isOffline: boolean = false;
|
|
20
|
+
// 404
|
|
21
|
+
private isNotFound: boolean = false;
|
|
22
|
+
// timeout
|
|
23
|
+
private isTimeout: boolean = false;
|
|
24
|
+
// 是否需要超时 弹框提示
|
|
25
|
+
private isTimeoutConfirm?: IOtherOptions["isTimeoutConfirm"];
|
|
26
|
+
private isTimeoutConfirmStr?: IOtherOptions["isTimeoutConfirmStr"];
|
|
27
|
+
// messageWarning 方法
|
|
28
|
+
private messageWarning?: IOtherOptions["messageWarning"];
|
|
29
|
+
// modalConfirm 方法
|
|
30
|
+
private modalConfirm?: IOtherOptions["modalConfirm"];
|
|
13
31
|
|
|
14
32
|
constructor(otherOptions: IOtherOptions) {
|
|
15
|
-
|
|
33
|
+
const {
|
|
34
|
+
crypto,
|
|
35
|
+
jumpCallback,
|
|
36
|
+
isTimeoutConfirm,
|
|
37
|
+
isTimeoutConfirmStr,
|
|
38
|
+
messageWarning,
|
|
39
|
+
modalConfirm,
|
|
40
|
+
...resetOtherOptions
|
|
41
|
+
} = otherOptions;
|
|
42
|
+
this.otherOptions = { ...defaultOptions, ...resetOtherOptions };
|
|
43
|
+
this.crypto = crypto;
|
|
44
|
+
this.isTimeoutConfirm = isTimeoutConfirm;
|
|
45
|
+
this.isTimeoutConfirmStr = isTimeoutConfirmStr;
|
|
46
|
+
this.jumpCallback = jumpCallback;
|
|
47
|
+
this.messageWarning = messageWarning;
|
|
48
|
+
this.modalConfirm = modalConfirm;
|
|
16
49
|
this.defaultSetting();
|
|
17
50
|
this.interceptorsRequest();
|
|
18
|
-
this.
|
|
51
|
+
this.interceptorsResponse();
|
|
19
52
|
}
|
|
20
53
|
|
|
21
54
|
// 设置axios方法默认值
|
|
@@ -43,27 +76,117 @@ export class AxiosRequest {
|
|
|
43
76
|
}
|
|
44
77
|
|
|
45
78
|
// 拦截响应
|
|
46
|
-
private
|
|
79
|
+
private interceptorsResponse() {
|
|
47
80
|
axios.interceptors.response.use(
|
|
48
81
|
(response) => {
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
}
|
|
53
|
-
} else if (response.status === 201) {
|
|
54
|
-
return Promise.resolve(response);
|
|
82
|
+
console.log(response);
|
|
83
|
+
// 对返回数据进行解密后 返回给客户端
|
|
84
|
+
if ((response.config as any)?.isCrypto) {
|
|
55
85
|
}
|
|
56
86
|
return response;
|
|
57
87
|
},
|
|
58
88
|
(error) => {
|
|
89
|
+
console.log(error);
|
|
90
|
+
// 统一处理响应异常
|
|
91
|
+
this.responseError(error);
|
|
59
92
|
return Promise.reject(error);
|
|
60
93
|
}
|
|
61
94
|
);
|
|
62
95
|
}
|
|
63
96
|
|
|
97
|
+
// 响应错误处理
|
|
98
|
+
private responseError(error: any = {}) {
|
|
99
|
+
const { response } = error;
|
|
100
|
+
// 处理token失效
|
|
101
|
+
// TODO: 刷新token逻辑
|
|
102
|
+
if (response?.status === 401) {
|
|
103
|
+
removeToken();
|
|
104
|
+
if (!this.isExpired) {
|
|
105
|
+
this.isExpired = true;
|
|
106
|
+
this.messageWarning?.("登录过期", 3.5, () => {
|
|
107
|
+
this.isExpired = false;
|
|
108
|
+
});
|
|
109
|
+
this.logoutHandle(response.status);
|
|
110
|
+
}
|
|
111
|
+
return;
|
|
112
|
+
}
|
|
113
|
+
// 处理被挤下线逻辑
|
|
114
|
+
if (response?.status === 501) {
|
|
115
|
+
removeToken();
|
|
116
|
+
if (!this.isOffline) {
|
|
117
|
+
this.isOffline = true;
|
|
118
|
+
this.messageWarning?.("你的账号已在其他地方登录!", 3.5, () => {
|
|
119
|
+
this.isOffline = false;
|
|
120
|
+
});
|
|
121
|
+
this.logoutHandle(response.status);
|
|
122
|
+
}
|
|
123
|
+
return;
|
|
124
|
+
}
|
|
125
|
+
// 处理404逻辑
|
|
126
|
+
if (response?.status === 404) {
|
|
127
|
+
if (!this.isNotFound) {
|
|
128
|
+
this.isNotFound = true;
|
|
129
|
+
this.messageWarning?.(
|
|
130
|
+
`接口:${response.config.url} 未找到!`,
|
|
131
|
+
3.5,
|
|
132
|
+
() => {
|
|
133
|
+
this.isNotFound = false;
|
|
134
|
+
}
|
|
135
|
+
);
|
|
136
|
+
}
|
|
137
|
+
return;
|
|
138
|
+
}
|
|
139
|
+
// 处理接口超时逻辑
|
|
140
|
+
if (
|
|
141
|
+
this.isTimeoutConfirm &&
|
|
142
|
+
error?.config?.method?.toUpperCase?.() === "POST" &&
|
|
143
|
+
!response &&
|
|
144
|
+
error.code === "ECONNABORTED" &&
|
|
145
|
+
error.message?.indexOf?.(`${error.config.timeout}ms`) !== -1
|
|
146
|
+
) {
|
|
147
|
+
if (!this.isTimeout) {
|
|
148
|
+
this.isTimeout = true;
|
|
149
|
+
this.modalConfirm?.(
|
|
150
|
+
`${
|
|
151
|
+
this.isTimeoutConfirmStr
|
|
152
|
+
? this.isTimeoutConfirmStr
|
|
153
|
+
: "请求超时,请稍后再试,或联系相关人员确认后处理!"
|
|
154
|
+
}
|
|
155
|
+
接口地址: ${error?.config?.url}
|
|
156
|
+
请求方式: ${error?.config?.method}
|
|
157
|
+
请求超时时间: ${error?.config?.timeout / 1000}秒
|
|
158
|
+
`,
|
|
159
|
+
() => {
|
|
160
|
+
this.isTimeout = false;
|
|
161
|
+
}
|
|
162
|
+
);
|
|
163
|
+
}
|
|
164
|
+
return;
|
|
165
|
+
}
|
|
166
|
+
// 提示业务错误
|
|
167
|
+
if (error?.config?.isErrorMessage) {
|
|
168
|
+
// 详细信息 再开发模式开启 之后上到别的环境会去掉
|
|
169
|
+
this.modalConfirm?.(`${error?.response?.data?.msg || "服务器异常"}
|
|
170
|
+
接口地址: ${error?.config?.url}
|
|
171
|
+
响应状态码: ${error?.response?.status}
|
|
172
|
+
请求方式: ${error?.config?.method}
|
|
173
|
+
`);
|
|
174
|
+
return;
|
|
175
|
+
}
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
// 跳转逻辑处理
|
|
179
|
+
private logoutHandle(status: number) {
|
|
180
|
+
// 自定义跳转
|
|
181
|
+
if (this.jumpCallback) {
|
|
182
|
+
this.jumpCallback(status);
|
|
183
|
+
} else {
|
|
184
|
+
window.location.href = "/login";
|
|
185
|
+
}
|
|
186
|
+
}
|
|
187
|
+
|
|
64
188
|
// 实例方法
|
|
65
189
|
public request(options: IOptions) {
|
|
66
|
-
const token = getToken();
|
|
67
190
|
const { isFormData, isAuth = true, ...resetOptions } = options;
|
|
68
191
|
|
|
69
192
|
const newOptions = resetOptions;
|
|
@@ -73,20 +196,10 @@ export class AxiosRequest {
|
|
|
73
196
|
newOptions.headers = {};
|
|
74
197
|
}
|
|
75
198
|
if (isAuth) {
|
|
199
|
+
const token = getToken();
|
|
76
200
|
newOptions.headers["Zmdms-Auth"] = `bearer ${token}`;
|
|
77
201
|
}
|
|
78
202
|
|
|
79
|
-
// 处理url
|
|
80
|
-
if (newOptions.baseURL?.endsWith("/")) {
|
|
81
|
-
newOptions.url = newOptions.url?.startsWith("/")
|
|
82
|
-
? newOptions.url?.slice(1)
|
|
83
|
-
: newOptions.url;
|
|
84
|
-
} else {
|
|
85
|
-
newOptions.url = newOptions.url?.startsWith("/")
|
|
86
|
-
? newOptions.url
|
|
87
|
-
: `/${newOptions.url}`;
|
|
88
|
-
}
|
|
89
|
-
|
|
90
203
|
// 遍历params,将undefined值 转为null值
|
|
91
204
|
if (newOptions.data) {
|
|
92
205
|
newOptions.data = transformData(newOptions.data);
|
|
@@ -120,6 +233,32 @@ export class AxiosRequest {
|
|
|
120
233
|
}
|
|
121
234
|
|
|
122
235
|
// 数据加密处理 TODO:
|
|
236
|
+
// 将参数进行aes加密
|
|
237
|
+
if (newOptions.isCrypto === true || newOptions.isCrypto === "aes") {
|
|
238
|
+
if (newOptions.data) {
|
|
239
|
+
newOptions.data = this.crypto?.encrypt?.(
|
|
240
|
+
JSON.stringify(newOptions.data)
|
|
241
|
+
);
|
|
242
|
+
}
|
|
243
|
+
if (newOptions.params) {
|
|
244
|
+
newOptions.params = this.crypto?.encrypt?.(
|
|
245
|
+
JSON.stringify(newOptions.params)
|
|
246
|
+
);
|
|
247
|
+
}
|
|
248
|
+
}
|
|
249
|
+
// 将参数进行des加密
|
|
250
|
+
if (newOptions.isCrypto === "des") {
|
|
251
|
+
if (newOptions.data) {
|
|
252
|
+
newOptions.data = this.crypto?.encrypt_des?.(
|
|
253
|
+
JSON.stringify(newOptions.data)
|
|
254
|
+
);
|
|
255
|
+
}
|
|
256
|
+
if (newOptions.params) {
|
|
257
|
+
newOptions.params = this.crypto?.encrypt_des?.(
|
|
258
|
+
JSON.stringify(newOptions.params)
|
|
259
|
+
);
|
|
260
|
+
}
|
|
261
|
+
}
|
|
123
262
|
|
|
124
263
|
return axios(newOptions);
|
|
125
264
|
}
|
|
@@ -149,13 +288,24 @@ function transformData(data?: any): any {
|
|
|
149
288
|
|
|
150
289
|
export interface IOptions extends AxiosRequestConfig {
|
|
151
290
|
isFormData?: boolean;
|
|
152
|
-
isAuth?: boolean; // 是否需要携带请求头
|
|
291
|
+
isAuth?: boolean; // 是否需要携带请求头 默认携带
|
|
292
|
+
isCrypto?: boolean | "aes" | "des"; // 是否对传输数据开启加密模式
|
|
293
|
+
isErrorMessage?: boolean; // 是否交给实例处理异常
|
|
153
294
|
}
|
|
154
295
|
|
|
155
296
|
export interface IOtherOptions {
|
|
156
|
-
jumpCallback?: () => void; // token过期等 需要跳转到登录页
|
|
157
|
-
|
|
297
|
+
jumpCallback?: (status: number) => void; // token过期等 需要跳转到登录页
|
|
298
|
+
messageWarning?: (
|
|
299
|
+
msg: string,
|
|
300
|
+
duration: number,
|
|
301
|
+
callback?: () => void
|
|
302
|
+
) => void; // 提示方法 外部传入
|
|
303
|
+
modalConfirm?: (msg: string, callback?: () => void) => void;
|
|
304
|
+
TenantId?: string; // token相关值
|
|
158
305
|
Authorization?: string; // auth值
|
|
159
|
-
timeout?: number;
|
|
160
|
-
baseURL?: string;
|
|
306
|
+
timeout?: number; // 超时时间
|
|
307
|
+
baseURL?: string; // 基础url
|
|
308
|
+
crypto?: Crypto; // 加密实例方法
|
|
309
|
+
isTimeoutConfirm?: boolean; // 超时是否需要弹框
|
|
310
|
+
isTimeoutConfirmStr?: string; // 超时弹框提示内容
|
|
161
311
|
}
|
package/src/validate.ts
ADDED
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
export const strLenValidate = (type: "s" | "m" | "l" | "ml" = "s") => {
|
|
2
|
+
if (type === "m") {
|
|
3
|
+
return {
|
|
4
|
+
regex: /^\s*([\s\S]{0,400})\s*$/,
|
|
5
|
+
message: `超过最大字符长度400!`,
|
|
6
|
+
};
|
|
7
|
+
}
|
|
8
|
+
if (type === "l") {
|
|
9
|
+
return {
|
|
10
|
+
regex: /^\s*([\s\S]{0,600})\s*$/,
|
|
11
|
+
message: `超过最大字符长度600!`,
|
|
12
|
+
};
|
|
13
|
+
}
|
|
14
|
+
if (type === "ml") {
|
|
15
|
+
return {
|
|
16
|
+
regex: /^\s*([\s\S]{0,5000})\s*$/,
|
|
17
|
+
message: `超过最大字符长度5000!`,
|
|
18
|
+
};
|
|
19
|
+
}
|
|
20
|
+
return {
|
|
21
|
+
regex: /^\s*([\s\S]{0,200})\s*$/,
|
|
22
|
+
message: `超过最大字符长度200!`,
|
|
23
|
+
};
|
|
24
|
+
};
|
|
25
|
+
|
|
26
|
+
export const phoneValidate = {
|
|
27
|
+
regex: /^(?:\+?86-?)?(?:(?:0\d{2,3}-?)?[1-9]\d{6,7}|1[3-9]\d{9})$/,
|
|
28
|
+
message: "请输入正确的手机号!",
|
|
29
|
+
};
|
|
30
|
+
export const morePhoneValidate = {
|
|
31
|
+
regex:
|
|
32
|
+
/^(?:(?:\+?86-?)?(?:(?:0\d{2,3}-?)?[1-9]\d{6,7}|1[3-9]\d{9})(?:,\s?)?)+$/,
|
|
33
|
+
message: "请检查输入的电话号码中是否有不符合规则的号码!",
|
|
34
|
+
};
|
|
35
|
+
|
|
36
|
+
export const idCardValidate = {
|
|
37
|
+
regex:
|
|
38
|
+
/^[1-9]\d{5}(?:19|20)\d{2}(?:0[1-9]|1[0-2])(?:0[1-9]|[1-2]\d|3[0-1])\d{3}[\dX]$/i,
|
|
39
|
+
message: "请输入正确的身份证号!",
|
|
40
|
+
};
|
|
41
|
+
|
|
42
|
+
export const emailValidate = {
|
|
43
|
+
regex: /^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$/,
|
|
44
|
+
message: "请输入正确的邮箱!",
|
|
45
|
+
};
|