zmdms-utils 0.0.26 → 0.0.28

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.
@@ -21,5 +21,9 @@ interface IConfig {
21
21
  * { key1: "20xx-xx-xx", key1: "20xx-xx-xx" } => { mergeKey: [Dayjs, Dayjs] }
22
22
  */
23
23
  declare function parseTime(config: IConfig): any;
24
+ /**
25
+ * 计算年龄
26
+ */
27
+ declare function calculateAge(birthday: any): number;
24
28
 
25
- export { parseTime as default };
29
+ export { calculateAge, parseTime };
@@ -167,5 +167,23 @@ function parseByResult(option) {
167
167
  }
168
168
  };
169
169
  }
170
+ /**
171
+ * 计算年龄
172
+ */
173
+ function calculateAge(birthday) {
174
+ if (!birthday) {
175
+ console.error("请传入一个生日信息");
176
+ return -1;
177
+ }
178
+ var birthDate = dayjs(birthday);
179
+ var today = dayjs();
180
+ var age = today.year() - birthDate.year();
181
+ var monthDifference = today.month() - birthDate.month();
182
+ if (monthDifference < 0 ||
183
+ (monthDifference === 0 && today.date() < birthDate.date())) {
184
+ age -= 1;
185
+ }
186
+ return age;
187
+ }
170
188
 
171
- export { parseTime as default };
189
+ export { calculateAge, parseTime };
@@ -1,5 +1,4 @@
1
- import * as axios from 'axios';
2
- import { AxiosRequestConfig } from 'axios';
1
+ import { AxiosResponse, AxiosRequestConfig } from 'axios';
3
2
  import { Crypto } from './crypto.js';
4
3
 
5
4
  declare class AxiosRequest {
@@ -18,9 +17,10 @@ declare class AxiosRequest {
18
17
  private defaultSetting;
19
18
  private interceptorsRequest;
20
19
  private interceptorsResponse;
20
+ private responseSuccess;
21
21
  private responseError;
22
22
  private logoutHandle;
23
- request(options: IOptions): Promise<axios.AxiosResponse<any, any>>;
23
+ request(options: IOptions): Promise<AxiosResponse<any, any>>;
24
24
  }
25
25
  interface IOptions extends AxiosRequestConfig {
26
26
  isFormData?: boolean;
@@ -36,6 +36,10 @@ interface IOptions extends AxiosRequestConfig {
36
36
  * 是否交给实例处理异常
37
37
  */
38
38
  isErrorMessage?: boolean;
39
+ /**
40
+ * 实例返回信息 简化处理
41
+ */
42
+ isEasyMessage?: boolean;
39
43
  }
40
44
  interface IOtherOptions {
41
45
  /** token过期等 需要跳转到登录页 */
@@ -59,11 +59,10 @@ var AxiosRequest = /** @class */ (function () {
59
59
  return;
60
60
  }
61
61
  axios.interceptors.response.use(function (response) {
62
- var _a;
63
62
  // console.log(response);
64
- // 对返回数据进行解密后 返回给客户端
65
- if ((_a = response.config) === null || _a === void 0 ? void 0 : _a.isCrypto) ;
66
- return response;
63
+ // 统一处理响应正常
64
+ var result = _this.responseSuccess(response);
65
+ return result;
67
66
  }, function (error) {
68
67
  console.log(error);
69
68
  // 统一处理响应异常
@@ -71,10 +70,49 @@ var AxiosRequest = /** @class */ (function () {
71
70
  return Promise.reject(error);
72
71
  });
73
72
  };
73
+ // 响应成功处理
74
+ AxiosRequest.prototype.responseSuccess = function (response) {
75
+ var _a;
76
+ var config = response.config, status = response.status, data = response.data;
77
+ // 对返回数据进行解密后 返回给客户端
78
+ if ((_a = response.config) === null || _a === void 0 ? void 0 : _a.isCrypto) ;
79
+ if (!config.isEasyMessage) {
80
+ return response;
81
+ }
82
+ // 简化处理信息
83
+ if (status === 200) {
84
+ // 字符串的情况也基本不会出现
85
+ if (typeof data === "string") {
86
+ return {
87
+ success: true,
88
+ msg: "操作成功",
89
+ data: data,
90
+ };
91
+ }
92
+ // 这里的data 不存在 可能说明这个接口没有返回信息
93
+ // 大多数情况下 不会这样 如果这种情况 也认为接口返回正常
94
+ if (!data) {
95
+ return {
96
+ success: true,
97
+ msg: "操作成功",
98
+ data: data,
99
+ };
100
+ }
101
+ // 大多数情况下 都是这种逻辑
102
+ if ((data === null || data === void 0 ? void 0 : data.code) === 200) {
103
+ return data;
104
+ }
105
+ }
106
+ // 201也可能出现,比如附件下载
107
+ if (status === 201) {
108
+ return response;
109
+ }
110
+ return Promise.reject(response.data);
111
+ };
74
112
  // 响应错误处理
75
113
  AxiosRequest.prototype.responseError = function (error) {
76
114
  var _this = this;
77
- var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k, _l, _m, _o, _p, _q, _r, _s, _t, _u;
115
+ var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k, _l, _m, _o, _p, _q, _r;
78
116
  if (error === void 0) { error = {}; }
79
117
  var response = error.response;
80
118
  // 处理token失效
@@ -131,7 +169,10 @@ var AxiosRequest = /** @class */ (function () {
131
169
  // 提示业务错误
132
170
  if ((_o = error === null || error === void 0 ? void 0 : error.config) === null || _o === void 0 ? void 0 : _o.isErrorMessage) {
133
171
  // 详细信息 再开发模式开启 之后上到别的环境会去掉
134
- (_p = this.modalConfirm) === null || _p === void 0 ? void 0 : _p.call(this, "".concat(((_r = (_q = error === null || error === void 0 ? void 0 : error.response) === null || _q === void 0 ? void 0 : _q.data) === null || _r === void 0 ? void 0 : _r.msg) || "服务器异常", "\n \u63A5\u53E3\u5730\u5740: ").concat((_s = error === null || error === void 0 ? void 0 : error.config) === null || _s === void 0 ? void 0 : _s.url, "\n \u54CD\u5E94\u72B6\u6001\u7801: ").concat((_t = error === null || error === void 0 ? void 0 : error.response) === null || _t === void 0 ? void 0 : _t.status, "\n \u8BF7\u6C42\u65B9\u5F0F: ").concat((_u = error === null || error === void 0 ? void 0 : error.config) === null || _u === void 0 ? void 0 : _u.method, "\n "));
172
+ (_p = this.modalConfirm) === null || _p === void 0 ? void 0 : _p.call(this, "".concat(((_r = (_q = error === null || error === void 0 ? void 0 : error.response) === null || _q === void 0 ? void 0 : _q.data) === null || _r === void 0 ? void 0 : _r.msg) || (error === null || error === void 0 ? void 0 : error.msg) || "服务器异常"));
173
+ // 接口地址: ${error?.config?.url}
174
+ // 响应状态码: ${error?.response?.status}
175
+ // 请求方式: ${error?.config?.method}
135
176
  return;
136
177
  }
137
178
  };
@@ -252,4 +252,37 @@ console.log(
252
252
  toKeys: [["time1", "time2"], "timea"],
253
253
  format: ["YYYY-MM-DD", "YYYY-MM-DD HH:mm:ss"],
254
254
  })
255
- );
255
+ );
256
+
257
+ function calculateAge(birthday) {
258
+ if (!birthday) {
259
+ console.error("请传入一个生日信息");
260
+ return -1;
261
+ }
262
+ const birthDate = dayjs(birthday);
263
+ const today = dayjs();
264
+
265
+ let age = today.year() - birthDate.year();
266
+ const monthDifference = today.month() - birthDate.month();
267
+
268
+ if (
269
+ monthDifference < 0 ||
270
+ (monthDifference === 0 && today.date() < birthDate.date())
271
+ ) {
272
+ age -= 1;
273
+ }
274
+
275
+ return age;
276
+ }
277
+
278
+ console.log(calculateAge()); // -1
279
+ console.log(calculateAge("1994-04-02")); // 29
280
+ console.log(calculateAge("1994-10-12")); // 28
281
+ console.log(calculateAge("2023-10-11")); // 0
282
+ console.log(calculateAge("2023-10-12")); // -1
283
+ console.log(calculateAge("2023-10-10")); // 0
284
+ console.log(calculateAge("2022-10-11")); // 1
285
+ console.log(calculateAge("2022-10-10")); // 1
286
+ console.log(calculateAge("2022-10-12")); // 0
287
+ console.log(calculateAge(dayjs()));
288
+ console.log(calculateAge(dayjs("1994-04-02")));
package/dist/index.d.ts CHANGED
@@ -10,5 +10,5 @@ export { divide, exactRound, formatUnit, minus, plus, times } from './es/math.js
10
10
  export { validatePassword } from './es/passwordValidate.js';
11
11
  export { emailValidate, idCardValidate, morePhoneValidate, phoneValidate, strLenValidate } from './es/validate.js';
12
12
  export { batchDownloadFiles, createDeleteFileLink, createDownloadLink, createOriginalLink, createThumbnailLink, createUploadFileLink, downloadFile, exportBolb, previewFile } from './es/file.js';
13
- export { default as parseTime } from './es/parseTime.js';
13
+ export { calculateAge, parseTime } from './es/parseTime.js';
14
14
  export { EMITTER_CCP_KEY, EMITTER_PCC_KEY, EMITTER_TYPE } from './es/constants.js';
package/dist/index.js CHANGED
@@ -10,5 +10,5 @@ export { divide, exactRound, formatUnit, minus, plus, times } from './es/math.js
10
10
  export { validatePassword } from './es/passwordValidate.js';
11
11
  export { emailValidate, idCardValidate, morePhoneValidate, phoneValidate, strLenValidate } from './es/validate.js';
12
12
  export { batchDownloadFiles, createDeleteFileLink, createDownloadLink, createOriginalLink, createThumbnailLink, createUploadFileLink, downloadFile, exportBolb, previewFile } from './es/file.js';
13
- export { default as parseTime } from './es/parseTime.js';
13
+ export { calculateAge, parseTime } from './es/parseTime.js';
14
14
  export { EMITTER_CCP_KEY, EMITTER_PCC_KEY, EMITTER_TYPE } from './es/constants.js';
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "zmdms-utils",
3
- "version": "0.0.26",
3
+ "version": "0.0.28",
4
4
  "description": "",
5
5
  "main": "dist/index.js",
6
6
  "module": "dist/index.js",
package/src/index.ts CHANGED
@@ -43,7 +43,7 @@ export {
43
43
  /**
44
44
  * 时间处理函数
45
45
  */
46
- export { default as parseTime } from "./parseTime";
46
+ export { parseTime, calculateAge } from "./parseTime";
47
47
 
48
48
  /**
49
49
  * 常量相关
package/src/parseTime.ts CHANGED
@@ -31,7 +31,7 @@ interface IConfig {
31
31
  * { key: "20xx-xx-xx" } => { key: Dayjs }
32
32
  * { key1: "20xx-xx-xx", key1: "20xx-xx-xx" } => { mergeKey: [Dayjs, Dayjs] }
33
33
  */
34
- export default function parseTime(config: IConfig) {
34
+ export function parseTime(config: IConfig) {
35
35
  const {
36
36
  params,
37
37
  format = defaultFormat,
@@ -207,3 +207,27 @@ function parseByResult(option: IProps) {
207
207
  }
208
208
  };
209
209
  }
210
+
211
+ /**
212
+ * 计算年龄
213
+ */
214
+ export function calculateAge(birthday: any) {
215
+ if (!birthday) {
216
+ console.error("请传入一个生日信息");
217
+ return -1;
218
+ }
219
+ const birthDate = dayjs(birthday);
220
+ const today = dayjs();
221
+
222
+ let age = today.year() - birthDate.year();
223
+ const monthDifference = today.month() - birthDate.month();
224
+
225
+ if (
226
+ monthDifference < 0 ||
227
+ (monthDifference === 0 && today.date() < birthDate.date())
228
+ ) {
229
+ age -= 1;
230
+ }
231
+
232
+ return age;
233
+ }
@@ -251,3 +251,36 @@ console.log(
251
251
  format: ["YYYY-MM-DD", "YYYY-MM-DD HH:mm:ss"],
252
252
  })
253
253
  );
254
+
255
+ function calculateAge(birthday) {
256
+ if (!birthday) {
257
+ console.error("请传入一个生日信息");
258
+ return -1;
259
+ }
260
+ const birthDate = dayjs(birthday);
261
+ const today = dayjs();
262
+
263
+ let age = today.year() - birthDate.year();
264
+ const monthDifference = today.month() - birthDate.month();
265
+
266
+ if (
267
+ monthDifference < 0 ||
268
+ (monthDifference === 0 && today.date() < birthDate.date())
269
+ ) {
270
+ age -= 1;
271
+ }
272
+
273
+ return age;
274
+ }
275
+
276
+ console.log(calculateAge()); // -1
277
+ console.log(calculateAge("1994-04-02")); // 29
278
+ console.log(calculateAge("1994-10-12")); // 28
279
+ console.log(calculateAge("2023-10-11")); // 0
280
+ console.log(calculateAge("2023-10-12")); // -1
281
+ console.log(calculateAge("2023-10-10")); // 0
282
+ console.log(calculateAge("2022-10-11")); // 1
283
+ console.log(calculateAge("2022-10-10")); // 1
284
+ console.log(calculateAge("2022-10-12")); // 0
285
+ console.log(calculateAge(dayjs()));
286
+ console.log(calculateAge(dayjs("1994-04-02")));
package/src/request.ts CHANGED
@@ -1,5 +1,5 @@
1
1
  // 请求方法
2
- import axios, { AxiosRequestConfig } from "axios";
2
+ import axios, { AxiosRequestConfig, AxiosResponse } from "axios";
3
3
  import { getToken, removeToken } from "./auth";
4
4
  import { Crypto } from "./crypto";
5
5
 
@@ -88,10 +88,9 @@ export class AxiosRequest {
88
88
  axios.interceptors.response.use(
89
89
  (response) => {
90
90
  // console.log(response);
91
- // 对返回数据进行解密后 返回给客户端
92
- if ((response.config as any)?.isCrypto) {
93
- }
94
- return response;
91
+ // 统一处理响应正常
92
+ const result = this.responseSuccess(response);
93
+ return result;
95
94
  },
96
95
  (error) => {
97
96
  console.log(error);
@@ -102,6 +101,46 @@ export class AxiosRequest {
102
101
  );
103
102
  }
104
103
 
104
+ // 响应成功处理
105
+ private responseSuccess(response: AxiosResponse) {
106
+ const { config, status, data } = response;
107
+ // 对返回数据进行解密后 返回给客户端
108
+ if ((response.config as any)?.isCrypto) {
109
+ }
110
+ if (!(config as any).isEasyMessage) {
111
+ return response;
112
+ }
113
+ // 简化处理信息
114
+ if (status === 200) {
115
+ // 字符串的情况也基本不会出现
116
+ if (typeof data === "string") {
117
+ return {
118
+ success: true,
119
+ msg: "操作成功",
120
+ data,
121
+ };
122
+ }
123
+ // 这里的data 不存在 可能说明这个接口没有返回信息
124
+ // 大多数情况下 不会这样 如果这种情况 也认为接口返回正常
125
+ if (!data) {
126
+ return {
127
+ success: true,
128
+ msg: "操作成功",
129
+ data,
130
+ };
131
+ }
132
+ // 大多数情况下 都是这种逻辑
133
+ if (data?.code === 200) {
134
+ return data;
135
+ }
136
+ }
137
+ // 201也可能出现,比如附件下载
138
+ if (status === 201) {
139
+ return response;
140
+ }
141
+ return Promise.reject(response.data);
142
+ }
143
+
105
144
  // 响应错误处理
106
145
  private responseError(error: any = {}) {
107
146
  const { response } = error;
@@ -174,11 +213,12 @@ export class AxiosRequest {
174
213
  // 提示业务错误
175
214
  if (error?.config?.isErrorMessage) {
176
215
  // 详细信息 再开发模式开启 之后上到别的环境会去掉
177
- this.modalConfirm?.(`${error?.response?.data?.msg || "服务器异常"}
178
- 接口地址: ${error?.config?.url}
179
- 响应状态码: ${error?.response?.status}
180
- 请求方式: ${error?.config?.method}
181
- `);
216
+ this.modalConfirm?.(
217
+ `${error?.response?.data?.msg || error?.msg || "服务器异常"}`
218
+ );
219
+ // 接口地址: ${error?.config?.url}
220
+ // 响应状态码: ${error?.response?.status}
221
+ // 请求方式: ${error?.config?.method}
182
222
  return;
183
223
  }
184
224
  }
@@ -308,6 +348,10 @@ export interface IOptions extends AxiosRequestConfig {
308
348
  * 是否交给实例处理异常
309
349
  */
310
350
  isErrorMessage?: boolean;
351
+ /**
352
+ * 实例返回信息 简化处理
353
+ */
354
+ isEasyMessage?: boolean;
311
355
  }
312
356
 
313
357
  export interface IOtherOptions {