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.
@@ -1,24 +1,44 @@
1
1
  import * as axios from 'axios';
2
2
  import { AxiosRequestConfig } from 'axios';
3
+ import { Crypto } from './crypto.js';
3
4
 
4
5
  declare class AxiosRequest {
5
6
  private otherOptions;
7
+ private crypto;
8
+ private jumpCallback;
9
+ private isExpired;
10
+ private isOffline;
11
+ private isNotFound;
12
+ private isTimeout;
13
+ private isTimeoutConfirm?;
14
+ private isTimeoutConfirmStr?;
15
+ private messageWarning?;
16
+ private modalConfirm?;
6
17
  constructor(otherOptions: IOtherOptions);
7
18
  private defaultSetting;
8
19
  private interceptorsRequest;
9
- private interceptorsReponse;
20
+ private interceptorsResponse;
21
+ private responseError;
22
+ private logoutHandle;
10
23
  request(options: IOptions): Promise<axios.AxiosResponse<any, any>>;
11
24
  }
12
25
  interface IOptions extends AxiosRequestConfig {
13
26
  isFormData?: boolean;
14
27
  isAuth?: boolean;
28
+ isCrypto?: boolean | "aes" | "des";
29
+ isErrorMessage?: boolean;
15
30
  }
16
31
  interface IOtherOptions {
17
- jumpCallback?: () => void;
32
+ jumpCallback?: (status: number) => void;
33
+ messageWarning?: (msg: string, duration: number, callback?: () => void) => void;
34
+ modalConfirm?: (msg: string, callback?: () => void) => void;
18
35
  TenantId?: string;
19
36
  Authorization?: string;
20
37
  timeout?: number;
21
38
  baseURL?: string;
39
+ crypto?: Crypto;
40
+ isTimeoutConfirm?: boolean;
41
+ isTimeoutConfirmStr?: string;
22
42
  }
23
43
 
24
44
  export { AxiosRequest, IOptions, IOtherOptions };
@@ -1,7 +1,6 @@
1
1
  import { __rest, __assign } from './_virtual/_tslib.js';
2
2
  import axios from 'axios';
3
- import 'qiankun';
4
- import { getToken } from './auth.js';
3
+ import { removeToken, getToken } from './auth.js';
5
4
 
6
5
  var defaultOptions = {
7
6
  TenantId: "000000",
@@ -10,10 +9,25 @@ var defaultOptions = {
10
9
  };
11
10
  var AxiosRequest = /** @class */ (function () {
12
11
  function AxiosRequest(otherOptions) {
13
- this.otherOptions = __assign(__assign({}, defaultOptions), otherOptions);
12
+ // 登录是否过期 这个变量是为了让弹出提示只展示一次
13
+ this.isExpired = false;
14
+ // 被挤下线 定义变量的目的同上
15
+ this.isOffline = false;
16
+ // 404
17
+ this.isNotFound = false;
18
+ // timeout
19
+ this.isTimeout = false;
20
+ var crypto = otherOptions.crypto, jumpCallback = otherOptions.jumpCallback, isTimeoutConfirm = otherOptions.isTimeoutConfirm, isTimeoutConfirmStr = otherOptions.isTimeoutConfirmStr, messageWarning = otherOptions.messageWarning, modalConfirm = otherOptions.modalConfirm, resetOtherOptions = __rest(otherOptions, ["crypto", "jumpCallback", "isTimeoutConfirm", "isTimeoutConfirmStr", "messageWarning", "modalConfirm"]);
21
+ this.otherOptions = __assign(__assign({}, defaultOptions), resetOtherOptions);
22
+ this.crypto = crypto;
23
+ this.isTimeoutConfirm = isTimeoutConfirm;
24
+ this.isTimeoutConfirmStr = isTimeoutConfirmStr;
25
+ this.jumpCallback = jumpCallback;
26
+ this.messageWarning = messageWarning;
27
+ this.modalConfirm = modalConfirm;
14
28
  this.defaultSetting();
15
29
  this.interceptorsRequest();
16
- this.interceptorsReponse();
30
+ this.interceptorsResponse();
17
31
  }
18
32
  // 设置axios方法默认值
19
33
  AxiosRequest.prototype.defaultSetting = function () {
@@ -34,45 +48,108 @@ var AxiosRequest = /** @class */ (function () {
34
48
  });
35
49
  };
36
50
  // 拦截响应
37
- AxiosRequest.prototype.interceptorsReponse = function () {
51
+ AxiosRequest.prototype.interceptorsResponse = function () {
52
+ var _this = this;
38
53
  axios.interceptors.response.use(function (response) {
39
- if (response.status === 200) {
40
- if (response.data.code === 200) {
41
- return Promise.resolve(response.data);
42
- }
43
- }
44
- else if (response.status === 201) {
45
- return Promise.resolve(response);
46
- }
54
+ var _a;
55
+ console.log(response);
56
+ // 对返回数据进行解密后 返回给客户端
57
+ if ((_a = response.config) === null || _a === void 0 ? void 0 : _a.isCrypto) ;
47
58
  return response;
48
59
  }, function (error) {
60
+ console.log(error);
61
+ // 统一处理响应异常
62
+ _this.responseError(error);
49
63
  return Promise.reject(error);
50
64
  });
51
65
  };
66
+ // 响应错误处理
67
+ AxiosRequest.prototype.responseError = function (error) {
68
+ var _this = this;
69
+ var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k, _l, _m, _o, _p, _q, _r, _s, _t, _u;
70
+ if (error === void 0) { error = {}; }
71
+ var response = error.response;
72
+ // 处理token失效
73
+ // TODO: 刷新token逻辑
74
+ if ((response === null || response === void 0 ? void 0 : response.status) === 401) {
75
+ removeToken();
76
+ if (!this.isExpired) {
77
+ this.isExpired = true;
78
+ (_a = this.messageWarning) === null || _a === void 0 ? void 0 : _a.call(this, "登录过期", 3.5, function () {
79
+ _this.isExpired = false;
80
+ });
81
+ this.logoutHandle(response.status);
82
+ }
83
+ return;
84
+ }
85
+ // 处理被挤下线逻辑
86
+ if ((response === null || response === void 0 ? void 0 : response.status) === 501) {
87
+ removeToken();
88
+ if (!this.isOffline) {
89
+ this.isOffline = true;
90
+ (_b = this.messageWarning) === null || _b === void 0 ? void 0 : _b.call(this, "你的账号已在其他地方登录!", 3.5, function () {
91
+ _this.isOffline = false;
92
+ });
93
+ this.logoutHandle(response.status);
94
+ }
95
+ return;
96
+ }
97
+ // 处理404逻辑
98
+ if ((response === null || response === void 0 ? void 0 : response.status) === 404) {
99
+ if (!this.isNotFound) {
100
+ this.isNotFound = true;
101
+ (_c = this.messageWarning) === null || _c === void 0 ? void 0 : _c.call(this, "\u63A5\u53E3\uFF1A".concat(response.config.url, " \u672A\u627E\u5230\uFF01"), 3.5, function () {
102
+ _this.isNotFound = false;
103
+ });
104
+ }
105
+ return;
106
+ }
107
+ // 处理接口超时逻辑
108
+ if (this.isTimeoutConfirm &&
109
+ ((_f = (_e = (_d = error === null || error === void 0 ? void 0 : error.config) === null || _d === void 0 ? void 0 : _d.method) === null || _e === void 0 ? void 0 : _e.toUpperCase) === null || _f === void 0 ? void 0 : _f.call(_e)) === "POST" &&
110
+ !response &&
111
+ error.code === "ECONNABORTED" &&
112
+ ((_h = (_g = error.message) === null || _g === void 0 ? void 0 : _g.indexOf) === null || _h === void 0 ? void 0 : _h.call(_g, "".concat(error.config.timeout, "ms"))) !== -1) {
113
+ if (!this.isTimeout) {
114
+ this.isTimeout = true;
115
+ (_j = this.modalConfirm) === null || _j === void 0 ? void 0 : _j.call(this, "".concat(this.isTimeoutConfirmStr
116
+ ? this.isTimeoutConfirmStr
117
+ : "请求超时,请稍后再试,或联系相关人员确认后处理!", "\n \u63A5\u53E3\u5730\u5740: ").concat((_k = error === null || error === void 0 ? void 0 : error.config) === null || _k === void 0 ? void 0 : _k.url, "\n \u8BF7\u6C42\u65B9\u5F0F: ").concat((_l = error === null || error === void 0 ? void 0 : error.config) === null || _l === void 0 ? void 0 : _l.method, "\n \u8BF7\u6C42\u8D85\u65F6\u65F6\u95F4: ").concat(((_m = error === null || error === void 0 ? void 0 : error.config) === null || _m === void 0 ? void 0 : _m.timeout) / 1000, "\u79D2\n "), function () {
118
+ _this.isTimeout = false;
119
+ });
120
+ }
121
+ return;
122
+ }
123
+ // 提示业务错误
124
+ if ((_o = error === null || error === void 0 ? void 0 : error.config) === null || _o === void 0 ? void 0 : _o.isErrorMessage) {
125
+ // 详细信息 再开发模式开启 之后上到别的环境会去掉
126
+ (_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 "));
127
+ return;
128
+ }
129
+ };
130
+ // 跳转逻辑处理
131
+ AxiosRequest.prototype.logoutHandle = function (status) {
132
+ // 自定义跳转
133
+ if (this.jumpCallback) {
134
+ this.jumpCallback(status);
135
+ }
136
+ else {
137
+ window.location.href = "/login";
138
+ }
139
+ };
52
140
  // 实例方法
53
141
  AxiosRequest.prototype.request = function (options) {
54
- var _a, _b, _c, _d, _e;
55
- var token = getToken();
56
- var isFormData = options.isFormData, _f = options.isAuth, isAuth = _f === void 0 ? true : _f, resetOptions = __rest(options, ["isFormData", "isAuth"]);
142
+ var _a, _b, _c, _d, _e, _f, _g, _h, _j;
143
+ var isFormData = options.isFormData, _k = options.isAuth, isAuth = _k === void 0 ? true : _k, resetOptions = __rest(options, ["isFormData", "isAuth"]);
57
144
  var newOptions = resetOptions;
58
145
  // 自定义请求头
59
146
  if (!newOptions.headers) {
60
147
  newOptions.headers = {};
61
148
  }
62
149
  if (isAuth) {
150
+ var token = getToken();
63
151
  newOptions.headers["Zmdms-Auth"] = "bearer ".concat(token);
64
152
  }
65
- // 处理url
66
- if ((_a = newOptions.baseURL) === null || _a === void 0 ? void 0 : _a.endsWith("/")) {
67
- newOptions.url = ((_b = newOptions.url) === null || _b === void 0 ? void 0 : _b.startsWith("/"))
68
- ? (_c = newOptions.url) === null || _c === void 0 ? void 0 : _c.slice(1)
69
- : newOptions.url;
70
- }
71
- else {
72
- newOptions.url = ((_d = newOptions.url) === null || _d === void 0 ? void 0 : _d.startsWith("/"))
73
- ? newOptions.url
74
- : "/".concat(newOptions.url);
75
- }
76
153
  // 遍历params,将undefined值 转为null值
77
154
  if (newOptions.data) {
78
155
  newOptions.data = transformData(newOptions.data);
@@ -82,7 +159,7 @@ var AxiosRequest = /** @class */ (function () {
82
159
  }
83
160
  // 处理特殊请求
84
161
  var specialMethod = ["POST", "PUT", "PATCH"];
85
- var method = ((_e = newOptions.method) === null || _e === void 0 ? void 0 : _e.toUpperCase()) || "";
162
+ var method = ((_a = newOptions.method) === null || _a === void 0 ? void 0 : _a.toUpperCase()) || "";
86
163
  if (specialMethod.includes(method)) {
87
164
  if (isFormData) {
88
165
  var formData = new FormData();
@@ -102,6 +179,24 @@ var AxiosRequest = /** @class */ (function () {
102
179
  }
103
180
  }
104
181
  // 数据加密处理 TODO:
182
+ // 将参数进行aes加密
183
+ if (newOptions.isCrypto === true || newOptions.isCrypto === "aes") {
184
+ if (newOptions.data) {
185
+ newOptions.data = (_c = (_b = this.crypto) === null || _b === void 0 ? void 0 : _b.encrypt) === null || _c === void 0 ? void 0 : _c.call(_b, JSON.stringify(newOptions.data));
186
+ }
187
+ if (newOptions.params) {
188
+ newOptions.params = (_e = (_d = this.crypto) === null || _d === void 0 ? void 0 : _d.encrypt) === null || _e === void 0 ? void 0 : _e.call(_d, JSON.stringify(newOptions.params));
189
+ }
190
+ }
191
+ // 将参数进行des加密
192
+ if (newOptions.isCrypto === "des") {
193
+ if (newOptions.data) {
194
+ newOptions.data = (_g = (_f = this.crypto) === null || _f === void 0 ? void 0 : _f.encrypt_des) === null || _g === void 0 ? void 0 : _g.call(_f, JSON.stringify(newOptions.data));
195
+ }
196
+ if (newOptions.params) {
197
+ newOptions.params = (_j = (_h = this.crypto) === null || _h === void 0 ? void 0 : _h.encrypt_des) === null || _j === void 0 ? void 0 : _j.call(_h, JSON.stringify(newOptions.params));
198
+ }
199
+ }
105
200
  return axios(newOptions);
106
201
  };
107
202
  return AxiosRequest;
@@ -0,0 +1,22 @@
1
+ declare const strLenValidate: (type?: "s" | "m" | "l" | "ml") => {
2
+ regex: RegExp;
3
+ message: string;
4
+ };
5
+ declare const phoneValidate: {
6
+ regex: RegExp;
7
+ message: string;
8
+ };
9
+ declare const morePhoneValidate: {
10
+ regex: RegExp;
11
+ message: string;
12
+ };
13
+ declare const idCardValidate: {
14
+ regex: RegExp;
15
+ message: string;
16
+ };
17
+ declare const emailValidate: {
18
+ regex: RegExp;
19
+ message: string;
20
+ };
21
+
22
+ export { emailValidate, idCardValidate, morePhoneValidate, phoneValidate, strLenValidate };
@@ -0,0 +1,43 @@
1
+ var strLenValidate = function (type) {
2
+ if (type === void 0) { type = "s"; }
3
+ if (type === "m") {
4
+ return {
5
+ regex: /^\s*([\s\S]{0,400})\s*$/,
6
+ message: "\u8D85\u8FC7\u6700\u5927\u5B57\u7B26\u957F\u5EA6400!",
7
+ };
8
+ }
9
+ if (type === "l") {
10
+ return {
11
+ regex: /^\s*([\s\S]{0,600})\s*$/,
12
+ message: "\u8D85\u8FC7\u6700\u5927\u5B57\u7B26\u957F\u5EA6600!",
13
+ };
14
+ }
15
+ if (type === "ml") {
16
+ return {
17
+ regex: /^\s*([\s\S]{0,5000})\s*$/,
18
+ message: "\u8D85\u8FC7\u6700\u5927\u5B57\u7B26\u957F\u5EA65000!",
19
+ };
20
+ }
21
+ return {
22
+ regex: /^\s*([\s\S]{0,200})\s*$/,
23
+ message: "\u8D85\u8FC7\u6700\u5927\u5B57\u7B26\u957F\u5EA6200!",
24
+ };
25
+ };
26
+ var phoneValidate = {
27
+ regex: /^(?:\+?86-?)?(?:(?:0\d{2,3}-?)?[1-9]\d{6,7}|1[3-9]\d{9})$/,
28
+ message: "请输入正确的手机号!",
29
+ };
30
+ var morePhoneValidate = {
31
+ regex: /^(?:(?:\+?86-?)?(?:(?:0\d{2,3}-?)?[1-9]\d{6,7}|1[3-9]\d{9})(?:,\s?)?)+$/,
32
+ message: "请检查输入的电话号码中是否有不符合规则的号码!",
33
+ };
34
+ var idCardValidate = {
35
+ regex: /^[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,
36
+ message: "请输入正确的身份证号!",
37
+ };
38
+ var emailValidate = {
39
+ regex: /^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$/,
40
+ message: "请输入正确的邮箱!",
41
+ };
42
+
43
+ export { emailValidate, idCardValidate, morePhoneValidate, phoneValidate, strLenValidate };
package/dist/index.d.ts CHANGED
@@ -1,5 +1,11 @@
1
1
  export { initMicroApp } from './es/microAppCreator.js';
2
2
  export { isQiankun } from './es/qiankunUtils.js';
3
3
  export { initGlobalError, initMainApp } from './es/mainApp.js';
4
- export { getToken, setToken } from './es/auth.js';
4
+ export { getToken, removeToken, setToken } from './es/auth.js';
5
5
  export { AxiosRequest, IOptions, IOtherOptions } from './es/request.js';
6
+ export { getBaseUrl } from './es/baseUrl.js';
7
+ export { Crypto, md5 } from './es/crypto.js';
8
+ export { addThousedSeparator, copyToClipboard, delay, emitter, parseQueryParams, unescapeString } from './es/common.js';
9
+ export { divide, exactRound, formatUnit, minus, plus, times } from './es/math.js';
10
+ export { validatePassword } from './es/passwordValidate.js';
11
+ export { emailValidate, idCardValidate, morePhoneValidate, phoneValidate, strLenValidate } from './es/validate.js';
package/dist/index.js CHANGED
@@ -1,5 +1,11 @@
1
1
  export { initMicroApp } from './es/microAppCreator.js';
2
2
  export { isQiankun } from './es/qiankunUtils.js';
3
3
  export { initGlobalError, initMainApp } from './es/mainApp.js';
4
- export { getToken, setToken } from './es/auth.js';
4
+ export { getToken, removeToken, setToken } from './es/auth.js';
5
5
  export { AxiosRequest } from './es/request.js';
6
+ export { getBaseUrl } from './es/baseUrl.js';
7
+ export { Crypto, md5 } from './es/crypto.js';
8
+ export { addThousedSeparator, copyToClipboard, delay, emitter, parseQueryParams, unescapeString } from './es/common.js';
9
+ export { divide, exactRound, formatUnit, minus, plus, times } from './es/math.js';
10
+ export { validatePassword } from './es/passwordValidate.js';
11
+ export { emailValidate, idCardValidate, morePhoneValidate, phoneValidate, strLenValidate } from './es/validate.js';
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "zmdms-utils",
3
- "version": "0.0.4",
3
+ "version": "0.0.6",
4
4
  "description": "",
5
5
  "main": "dist/index.js",
6
6
  "module": "dist/index.js",
@@ -14,14 +14,19 @@
14
14
  "license": "ISC",
15
15
  "peerDependencies": {
16
16
  "axios": "^1.4.0",
17
+ "crypto-js": "^4.1.1",
17
18
  "qiankun": "^2.10.8"
18
19
  },
19
- "dependencies": {},
20
+ "dependencies": {
21
+ "mitt": "^3.0.0"
22
+ },
20
23
  "devDependencies": {
21
24
  "@rollup/plugin-commonjs": "^24.0.1",
22
25
  "@rollup/plugin-json": "^6.0.0",
23
26
  "@rollup/plugin-node-resolve": "^15.0.1",
27
+ "@types/crypto-js": "^4.1.1",
24
28
  "axios": "^1.4.0",
29
+ "crypto-js": "^4.1.1",
25
30
  "qiankun": "^2.10.8",
26
31
  "rimraf": "^4.4.1",
27
32
  "rollup": "^3.20.2",
package/src/auth.ts CHANGED
@@ -17,3 +17,11 @@ export function setToken(token: string) {
17
17
  console.error("设置token失败", err);
18
18
  }
19
19
  }
20
+
21
+ export function removeToken() {
22
+ try {
23
+ window.localStorage.removeItem("system-token");
24
+ } catch (err) {
25
+ console.error("设置token失败", err);
26
+ }
27
+ }
package/src/baseUrl.ts CHANGED
@@ -1 +1,28 @@
1
- export function getBaseUrl() {}
1
+ // 根据当前环境变量获取基础的URL
2
+ export function getBaseUrl(processObj: any) {
3
+ const ENV = processObj.REACT_APP_ENV; // 获取当前环境变量
4
+
5
+ // api各个环境地址
6
+ const apiBaseUrlObj: any = {
7
+ dev: processObj.REACT_APP_API_DEV || "http://192.168.0.83:8000",
8
+ test: processObj.REACT_APP_API_TEST || window.location.origin,
9
+ stage: processObj.REACT_APP_API_STAGE || window.location.origin,
10
+ product: processObj.REACT_APP_API_PRODUCT || window.location.origin,
11
+ };
12
+
13
+ // file 服务器
14
+ const fileBaseUrlObj: any = {
15
+ dev: processObj.REACT_APP_FILE_DEV || "http://192.168.0.83:88",
16
+ test: processObj.REACT_APP_FILE_TEST || "http://172.55.5.101:33013",
17
+ stage: processObj.REACT_APP_FILE_STAGE || "https://dzfile-prod.zmd.com.cn",
18
+ product:
19
+ processObj.REACT_APP_FILE_PRODUCT || "https://dzfile.zmd.com.cn:8012",
20
+ };
21
+
22
+ const currentKey = ENV.toLowerCase() || "dev";
23
+
24
+ return {
25
+ api: apiBaseUrlObj[currentKey],
26
+ file: fileBaseUrlObj[currentKey],
27
+ };
28
+ }
package/src/common.ts ADDED
@@ -0,0 +1,129 @@
1
+ // 基础函数
2
+ import mitt from "mitt";
3
+ /**
4
+ * 同步阻断
5
+ * @param time 秒
6
+ */
7
+ export function delay(time: number) {
8
+ const now = Date.now();
9
+ while (Date.now() - now < time * 1000) {}
10
+ }
11
+
12
+ /**
13
+ * 发布订阅实例
14
+ */
15
+ export const emitter = mitt();
16
+
17
+ /**
18
+ * 添加位数分割符
19
+ * @param n 需要分割的字符
20
+ * @param digit 多少位分割
21
+ * @param symbol 添加的符号
22
+ * @returns
23
+ */
24
+ export function addThousedSeparator(
25
+ n: any,
26
+ digit: number = 3,
27
+ symbol: string = ","
28
+ ) {
29
+ if (n != null) {
30
+ let _n = n.toString();
31
+ let pointBeforeNum = "";
32
+ let pointAfterNum = "";
33
+ if (_n.startsWith("-")) {
34
+ pointBeforeNum = "-";
35
+ _n = _n.slice(1);
36
+ }
37
+ const pointIndex = _n.indexOf(".");
38
+ if (pointIndex !== -1) {
39
+ n = _n.slice(0, pointIndex);
40
+ pointAfterNum = _n.slice(pointIndex);
41
+ } else {
42
+ n = _n;
43
+ }
44
+
45
+ let res = "";
46
+ const s = n.toString();
47
+ const length = s.length;
48
+
49
+ for (let i = length - 1; i >= 0; i--) {
50
+ const j = length - i;
51
+ if (j % digit === 0) {
52
+ if (i === 0) {
53
+ res = s[i] + res;
54
+ } else {
55
+ res = symbol + s[i] + res;
56
+ }
57
+ } else {
58
+ res = s[i] + res;
59
+ }
60
+ }
61
+
62
+ return pointBeforeNum + res + pointAfterNum;
63
+ } else {
64
+ return n;
65
+ }
66
+ }
67
+
68
+ /**
69
+ * 获取地址栏参数
70
+ * @returns 地址栏参数对象
71
+ */
72
+ export function parseQueryParams(url: string): { [key: string]: string } {
73
+ if (!url) {
74
+ return {};
75
+ }
76
+ const queryString = url.split("?")[1];
77
+ const params = new URLSearchParams(queryString);
78
+ const result: { [key: string]: string } = {};
79
+ params.forEach((value, key) => {
80
+ let val: any = decodeURIComponent(value);
81
+ if (val.startsWith("{")) {
82
+ try {
83
+ val = JSON.parse(val);
84
+ } catch (err) {
85
+ console.log("解析失败", err);
86
+ }
87
+ } else if (val === "true" || val === "false") {
88
+ val = val === "true";
89
+ }
90
+ result[key] = val;
91
+ });
92
+ return result;
93
+ }
94
+
95
+ /**
96
+ * 将一些转义字符转换成原先的字符
97
+ * @param input 需要转换的内容
98
+ * @returns 转换过后的结果
99
+ */
100
+ export function unescapeString(input: string): string {
101
+ if (!input) {
102
+ return "";
103
+ }
104
+
105
+ const escapedChars: { [key: string]: string } = {
106
+ "&amp;": "&",
107
+ "&lt;": "<",
108
+ "&gt;": ">",
109
+ "&quot;": '"',
110
+ "&#039;": "'",
111
+ };
112
+
113
+ return input.replace(/&(amp|lt|gt|quot|#039);/g, (match) => {
114
+ return escapedChars[match] || match;
115
+ });
116
+ }
117
+
118
+ /**
119
+ * 将内容复制到粘贴板中
120
+ * @param text 需要复制的内容
121
+ */
122
+ export function copyToClipboard(text: string) {
123
+ const textarea = document.createElement("textarea");
124
+ textarea.value = text;
125
+ document.body.appendChild(textarea);
126
+ textarea.select();
127
+ document?.execCommand?.("copy");
128
+ document.body.removeChild(textarea);
129
+ }
package/src/crypto.ts ADDED
@@ -0,0 +1,109 @@
1
+ import CryptoJS from "crypto-js";
2
+
3
+ export class Crypto {
4
+ // 使用AesUtil.genAesKey()生成,需和后端配置保持一致
5
+ private aesKey: string;
6
+
7
+ // 使用DesUtil.genDesKey()生成,需和后端配置保持一致
8
+ private desKey: string;
9
+
10
+ constructor(aesKey: string, desKey: string) {
11
+ this.aesKey = aesKey;
12
+ this.desKey = desKey;
13
+ }
14
+
15
+ /**
16
+ * aes 加密方法
17
+ * @param data
18
+ * @returns {*}
19
+ */
20
+ encrypt(data: string) {
21
+ return this.encryptAES(data, this.aesKey);
22
+ }
23
+
24
+ /**
25
+ * aes 解密方法
26
+ * @param data
27
+ * @returns {*}
28
+ */
29
+ decrypt(data: string) {
30
+ return this.decryptAES(data, this.aesKey);
31
+ }
32
+
33
+ /**
34
+ * des 加密方法
35
+ * @param data
36
+ * @returns {*}
37
+ */
38
+ encrypt_des(data: string) {
39
+ return this.encryptDES(data, this.desKey);
40
+ }
41
+
42
+ /**
43
+ * des 解密方法
44
+ * @param data
45
+ * @returns {*}
46
+ */
47
+ decrypt_des(data: string) {
48
+ return this.decryptDES(data, this.desKey);
49
+ }
50
+
51
+ /**
52
+ * aes 加密方法,同java:AesUtil.encryptToBase64(text, aesKey);
53
+ */
54
+ private encryptAES(data: string, key: string) {
55
+ const dataBytes = CryptoJS.enc.Utf8.parse(data);
56
+ const keyBytes = CryptoJS.enc.Utf8.parse(key);
57
+ const encrypted = CryptoJS.AES.encrypt(dataBytes, keyBytes, {
58
+ iv: keyBytes,
59
+ mode: CryptoJS.mode.CBC,
60
+ padding: CryptoJS.pad.Pkcs7,
61
+ });
62
+ return CryptoJS.enc.Base64.stringify(encrypted.ciphertext);
63
+ }
64
+
65
+ /**
66
+ * aes 解密方法,同java:AesUtil.decryptFormBase64ToString(encrypt, aesKey);
67
+ */
68
+ private decryptAES(data: string, key: string) {
69
+ const keyBytes = CryptoJS.enc.Utf8.parse(key);
70
+ const decrypted = CryptoJS.AES.decrypt(data, keyBytes, {
71
+ iv: keyBytes,
72
+ mode: CryptoJS.mode.CBC,
73
+ padding: CryptoJS.pad.Pkcs7,
74
+ });
75
+ return CryptoJS.enc.Utf8.stringify(decrypted);
76
+ }
77
+
78
+ /**
79
+ * des 加密方法,同java:DesUtil.encryptToBase64(text, desKey)
80
+ */
81
+ private encryptDES(data: string, key: string) {
82
+ const keyHex = CryptoJS.enc.Utf8.parse(key);
83
+ const encrypted = CryptoJS.DES.encrypt(data, keyHex, {
84
+ mode: CryptoJS.mode.ECB,
85
+ padding: CryptoJS.pad.Pkcs7,
86
+ });
87
+ return encrypted.toString();
88
+ }
89
+
90
+ /**
91
+ * des 解密方法,同java:DesUtil.decryptFormBase64(encryptBase64, desKey);
92
+ */
93
+ private decryptDES(data: string, key: string) {
94
+ const keyHex = CryptoJS.enc.Utf8.parse(key);
95
+ const decrypted = CryptoJS.DES.decrypt(
96
+ {
97
+ ciphertext: CryptoJS.enc.Base64.parse(data),
98
+ } as any,
99
+ keyHex,
100
+ {
101
+ mode: CryptoJS.mode.ECB,
102
+ padding: CryptoJS.pad.Pkcs7,
103
+ }
104
+ );
105
+ return decrypted.toString(CryptoJS.enc.Utf8);
106
+ }
107
+ }
108
+
109
+ export const md5 = (str: string) => CryptoJS.MD5(str).toString().toLowerCase();
package/src/index.ts CHANGED
@@ -1,5 +1,24 @@
1
1
  export { initMicroApp } from "./microAppCreator";
2
2
  export { isQiankun } from "./qiankunUtils";
3
3
  export { initMainApp, initGlobalError } from "./mainApp";
4
- export { getToken, setToken } from "./auth";
4
+ export { getToken, setToken, removeToken } from "./auth";
5
5
  export { AxiosRequest, type IOtherOptions, type IOptions } from "./request";
6
+ export { getBaseUrl } from "./baseUrl";
7
+ export { Crypto, md5 } from "./crypto";
8
+ export {
9
+ delay,
10
+ emitter,
11
+ addThousedSeparator,
12
+ parseQueryParams,
13
+ unescapeString,
14
+ copyToClipboard,
15
+ } from "./common";
16
+ export { plus, minus, times, divide, exactRound, formatUnit } from "./math";
17
+ export { validatePassword } from "./passwordValidate";
18
+ export {
19
+ strLenValidate,
20
+ phoneValidate,
21
+ morePhoneValidate,
22
+ idCardValidate,
23
+ emailValidate,
24
+ } from "./validate";