zmdms-utils 0.0.5 → 0.0.7

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/src/request.ts CHANGED
@@ -1,6 +1,7 @@
1
1
  // 请求方法
2
2
  import axios, { AxiosRequestConfig } from "axios";
3
- import { getToken } from "./index";
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
- this.otherOptions = { ...defaultOptions, ...otherOptions };
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.interceptorsReponse();
51
+ this.interceptorsResponse();
19
52
  }
20
53
 
21
54
  // 设置axios方法默认值
@@ -43,20 +76,117 @@ export class AxiosRequest {
43
76
  }
44
77
 
45
78
  // 拦截响应
46
- private interceptorsReponse() {
79
+ private interceptorsResponse() {
47
80
  axios.interceptors.response.use(
48
81
  (response) => {
82
+ console.log(response);
83
+ // 对返回数据进行解密后 返回给客户端
84
+ if ((response.config as any)?.isCrypto) {
85
+ }
49
86
  return response;
50
87
  },
51
88
  (error) => {
89
+ console.log(error);
90
+ // 统一处理响应异常
91
+ this.responseError(error);
52
92
  return Promise.reject(error);
53
93
  }
54
94
  );
55
95
  }
56
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
+
57
188
  // 实例方法
58
189
  public request(options: IOptions) {
59
- const token = getToken();
60
190
  const { isFormData, isAuth = true, ...resetOptions } = options;
61
191
 
62
192
  const newOptions = resetOptions;
@@ -66,20 +196,10 @@ export class AxiosRequest {
66
196
  newOptions.headers = {};
67
197
  }
68
198
  if (isAuth) {
199
+ const token = getToken();
69
200
  newOptions.headers["Zmdms-Auth"] = `bearer ${token}`;
70
201
  }
71
202
 
72
- // 处理url
73
- if (newOptions.baseURL?.endsWith("/")) {
74
- newOptions.url = newOptions.url?.startsWith("/")
75
- ? newOptions.url?.slice(1)
76
- : newOptions.url;
77
- } else {
78
- newOptions.url = newOptions.url?.startsWith("/")
79
- ? newOptions.url
80
- : `/${newOptions.url}`;
81
- }
82
-
83
203
  // 遍历params,将undefined值 转为null值
84
204
  if (newOptions.data) {
85
205
  newOptions.data = transformData(newOptions.data);
@@ -113,6 +233,32 @@ export class AxiosRequest {
113
233
  }
114
234
 
115
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
+ }
116
262
 
117
263
  return axios(newOptions);
118
264
  }
@@ -142,13 +288,42 @@ function transformData(data?: any): any {
142
288
 
143
289
  export interface IOptions extends AxiosRequestConfig {
144
290
  isFormData?: boolean;
145
- isAuth?: boolean; // 是否需要携带请求头
291
+ /**
292
+ * 是否需要携带请求头默认携带
293
+ */
294
+ isAuth?: boolean;
295
+ /**
296
+ * 是否对传输数据开启加密模式
297
+ */
298
+ isCrypto?: boolean | "aes" | "des";
299
+ /**
300
+ * 是否交给实例处理异常
301
+ */
302
+ isErrorMessage?: boolean;
146
303
  }
147
304
 
148
305
  export interface IOtherOptions {
149
- jumpCallback?: () => void; // token过期等 需要跳转到登录页
306
+ // token过期等 需要跳转到登录页
307
+ jumpCallback?: (status: number) => void;
308
+ // 提示方法 外部传入
309
+ messageWarning?: (
310
+ msg: string,
311
+ duration: number,
312
+ callback?: () => void
313
+ ) => void;
314
+ modalConfirm?: (msg: string, callback?: () => void) => void;
315
+ // token相关值
150
316
  TenantId?: string;
151
- Authorization?: string; // auth值
317
+ // auth值
318
+ Authorization?: string;
319
+ // 超时时间
152
320
  timeout?: number;
321
+ // 基础url
153
322
  baseURL?: string;
323
+ // 加密实例方法
324
+ crypto?: Crypto;
325
+ // 超时是否需要弹框
326
+ isTimeoutConfirm?: boolean;
327
+ // 超时弹框提示内容
328
+ isTimeoutConfirmStr?: string;
154
329
  }
@@ -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
+ };