zmdms-utils 0.0.75 → 0.0.76

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.
Files changed (78) hide show
  1. package/dist/es/_virtual/_commonjsHelpers.js +3 -0
  2. package/dist/es/_virtual/_tslib.js +87 -0
  3. package/dist/es/_virtual/decimal.js +7 -0
  4. package/dist/es/_virtual/parseTimeTest.js +3 -0
  5. package/dist/es/_virtual/test.js +3 -0
  6. package/dist/es/auth.d.ts +9 -0
  7. package/dist/es/auth.js +49 -0
  8. package/dist/es/baseUrl.d.ts +23 -0
  9. package/dist/es/baseUrl.js +50 -0
  10. package/dist/es/canvas.d.ts +6 -0
  11. package/dist/es/canvas.js +14 -0
  12. package/dist/es/common.d.ts +81 -0
  13. package/dist/es/common.js +304 -0
  14. package/dist/es/constants.d.ts +20 -0
  15. package/dist/es/constants.js +33 -0
  16. package/dist/es/crypto.d.ts +51 -0
  17. package/dist/es/crypto.js +97 -0
  18. package/dist/es/file.d.ts +127 -0
  19. package/dist/es/file.js +373 -0
  20. package/dist/es/htmlToPdf.d.ts +44 -0
  21. package/dist/es/htmlToPdf.js +84 -0
  22. package/dist/es/locales/i18n.d.ts +15 -0
  23. package/dist/es/locales/t-in-non-react.d.ts +17 -0
  24. package/dist/es/locales/t-options.d.ts +7 -0
  25. package/dist/es/locales/use-translation.d.ts +42 -0
  26. package/dist/es/mainApp.d.ts +36 -0
  27. package/dist/es/mainApp.js +44 -0
  28. package/dist/es/math.d.ts +29 -0
  29. package/dist/es/math.js +147 -0
  30. package/dist/es/microAppCreator.d.ts +46 -0
  31. package/dist/es/microAppCreator.js +89 -0
  32. package/dist/es/node_modules/decimal.js/decimal.js +4896 -0
  33. package/dist/es/node_modules/decimal.js/decimal2.js +4930 -0
  34. package/dist/es/node_modules/html-parse-stringify/dist/html-parse-stringify.module.js +5 -0
  35. package/dist/es/node_modules/i18next/dist/esm/i18next.js +2242 -0
  36. package/dist/es/node_modules/js-base64/base64.js +273 -0
  37. package/dist/es/node_modules/jwt-decode/build/jwt-decode.esm.js +3 -0
  38. package/dist/es/node_modules/mitt/dist/mitt.js +3 -0
  39. package/dist/es/node_modules/pinyin-pro/dist/index.js +12627 -0
  40. package/dist/es/node_modules/react-i18next/dist/es/Trans.js +47 -0
  41. package/dist/es/node_modules/react-i18next/dist/es/TransWithoutContext.js +312 -0
  42. package/dist/es/node_modules/react-i18next/dist/es/context.js +18 -0
  43. package/dist/es/node_modules/react-i18next/dist/es/defaults.js +21 -0
  44. package/dist/es/node_modules/react-i18next/dist/es/i18nInstance.js +7 -0
  45. package/dist/es/node_modules/react-i18next/dist/es/initReactI18next.js +12 -0
  46. package/dist/es/node_modules/react-i18next/dist/es/unescape.js +27 -0
  47. package/dist/es/node_modules/react-i18next/dist/es/useTranslation.js +112 -0
  48. package/dist/es/node_modules/react-i18next/dist/es/utils.js +63 -0
  49. package/dist/es/node_modules/void-elements/index.js +23 -0
  50. package/dist/es/parseTime.d.ts +29 -0
  51. package/dist/es/parseTime.js +189 -0
  52. package/dist/es/parseTimeTest.js +2 -0
  53. package/dist/es/passwordValidate.d.ts +9 -0
  54. package/dist/es/passwordValidate.js +169 -0
  55. package/dist/es/pdf.d.ts +49 -0
  56. package/dist/es/pdf.js +176 -0
  57. package/dist/es/pinyin.js +1 -0
  58. package/dist/es/print.d.ts +23 -0
  59. package/dist/es/print.js +43 -0
  60. package/dist/es/qiankunUtils.d.ts +13 -0
  61. package/dist/es/qiankunUtils.js +32 -0
  62. package/dist/es/request.d.ts +87 -0
  63. package/dist/es/request.js +308 -0
  64. package/dist/es/src/locales/i18n.js +16 -0
  65. package/dist/es/src/locales/t-in-non-react.js +54 -0
  66. package/dist/es/src/locales/t-options.js +20 -0
  67. package/dist/es/src/locales/use-translation.js +60 -0
  68. package/dist/es/src/parseTimeTest.js +288 -0
  69. package/dist/es/src/test.js +245 -0
  70. package/dist/es/test.js +2 -0
  71. package/dist/es/validate.d.ts +26 -0
  72. package/dist/es/validate.js +47 -0
  73. package/dist/es/water.d.ts +53 -0
  74. package/dist/es/water.js +88 -0
  75. package/dist/index.d.ts +26 -0
  76. package/dist/index.js +26 -0
  77. package/package.json +1 -1
  78. package/src/math.ts +1 -1
@@ -0,0 +1,32 @@
1
+ /**
2
+ * @description 关于qiankun的一些方法
3
+ */
4
+ /**
5
+ * @description 环境判断
6
+ */
7
+ function isQiankun() {
8
+ return window.__POWERED_BY_QIANKUN__;
9
+ }
10
+ /**
11
+ * @description 取微应用地址
12
+ */
13
+ function getMicroUrl(appKey, apps) {
14
+ var _a;
15
+ if (apps === void 0) { apps = []; }
16
+ var result = (_a = apps === null || apps === void 0 ? void 0 : apps.find) === null || _a === void 0 ? void 0 : _a.call(apps, function (app) { return (app === null || app === void 0 ? void 0 : app.appCode) === appKey; });
17
+ if (result && result.appUrl) {
18
+ try {
19
+ return result.appUrl.endsWith("/")
20
+ ? result.appUrl.slice(result.appUrl.length - 1)
21
+ : result.appUrl;
22
+ }
23
+ catch (err) {
24
+ console.log("未取到对应的微应用地址!");
25
+ return "";
26
+ }
27
+ }
28
+ console.log("未取到对应的微应用地址!");
29
+ return "";
30
+ }
31
+
32
+ export { getMicroUrl, isQiankun };
@@ -0,0 +1,87 @@
1
+ import { AxiosResponse, AxiosRequestConfig, InternalAxiosRequestConfig } from 'axios';
2
+ import { Crypto } from './crypto.js';
3
+
4
+ declare class AxiosRequest {
5
+ private otherOptions;
6
+ private crypto;
7
+ private jumpCallback;
8
+ private isExpired;
9
+ private isOffline;
10
+ private isNotFound;
11
+ private isTimeout;
12
+ private isTimeoutConfirm?;
13
+ private isTimeoutConfirmStr?;
14
+ private messageWarning?;
15
+ private modalConfirm?;
16
+ private instanceAxios;
17
+ private tokenKey;
18
+ constructor(otherOptions: IOtherOptions);
19
+ private defaultSetting;
20
+ private interceptorsRequest;
21
+ private interceptorsResponse;
22
+ private responseSuccess;
23
+ private responseError;
24
+ private logoutHandle;
25
+ request(options: IOptions): Promise<AxiosResponse<any, any>>;
26
+ }
27
+ interface IOptions extends AxiosRequestConfig {
28
+ isFormData?: boolean;
29
+ /**
30
+ * 是否需要携带请求头默认携带
31
+ */
32
+ isAuth?: boolean;
33
+ /**
34
+ * 是否对传输数据开启加密模式
35
+ */
36
+ isCrypto?: boolean | "aes" | "des";
37
+ /**
38
+ * 是否交给实例处理异常
39
+ */
40
+ isErrorMessage?: boolean;
41
+ /**
42
+ * 实例返回信息 简化处理
43
+ */
44
+ isEasyMessage?: boolean;
45
+ /**
46
+ * 登录过期是否交给自己处理
47
+ */
48
+ isHandleLoginExpiration?: boolean;
49
+ /**
50
+ * 登录过期跳转逻辑交给实例自己处理
51
+ */
52
+ expirationHandle?: (options: any) => void;
53
+ }
54
+ interface IOtherOptions {
55
+ /** token过期等 需要跳转到登录页 */
56
+ jumpCallback?: (status: number) => void;
57
+ /** 提示方法 外部传入 */
58
+ messageWarning?: (msg: string, duration: number, callback?: () => void) => void;
59
+ modalConfirm?: (msg: string, callback?: () => void) => void;
60
+ /** @deprecated 租户ID,只有登录接口 需要 传递。 */
61
+ TenantId?: string;
62
+ /** auth值 */
63
+ Authorization?: string;
64
+ /** 公用请求头 */
65
+ commonHeaders?: {
66
+ [prop: string]: any;
67
+ };
68
+ /** 请求认证TOKEN KEY */
69
+ tokenKey?: string;
70
+ /** 超时时间 */
71
+ timeout?: number;
72
+ /** 基础url */
73
+ baseURL?: string;
74
+ /** 加密实例方法 */
75
+ crypto?: Crypto;
76
+ /** 超时是否需要弹框 */
77
+ isTimeoutConfirm?: boolean;
78
+ /** 超时弹框提示内容 */
79
+ isTimeoutConfirmStr?: string;
80
+ /** 拦截器自定义 */
81
+ interceptorsRequestHandle?: () => void;
82
+ interceptorsRequestConfig?: (config: InternalAxiosRequestConfig<any>) => InternalAxiosRequestConfig<any>;
83
+ /** 响应器自定义 */
84
+ interceptorsResponseHandle?: () => void;
85
+ }
86
+
87
+ export { AxiosRequest, IOptions, IOtherOptions };
@@ -0,0 +1,308 @@
1
+ import { __rest, __assign } from './_virtual/_tslib.js';
2
+ import axios from 'axios';
3
+ import { removeToken, getToken } from './auth.js';
4
+ import { TOKEN_KEY } from './constants.js';
5
+
6
+ var defaultOptions = {
7
+ // TenantId: "000000", // 租户ID不需要了
8
+ Authorization: "Basic em1kbXM6em1kbXNfc2VjcmV0",
9
+ timeout: 60000,
10
+ };
11
+ var AxiosRequest = /** @class */ (function () {
12
+ function AxiosRequest(otherOptions) {
13
+ // 登录是否过期 这个变量是为了让弹出提示只展示一次
14
+ this.isExpired = false;
15
+ // 被挤下线 定义变量的目的同上
16
+ this.isOffline = false;
17
+ // 404
18
+ this.isNotFound = false;
19
+ // timeout
20
+ this.isTimeout = false;
21
+ // axios单例
22
+ this.instanceAxios = axios.create();
23
+ // tokenKey
24
+ this.tokenKey = TOKEN_KEY;
25
+ var crypto = otherOptions.crypto, jumpCallback = otherOptions.jumpCallback, isTimeoutConfirm = otherOptions.isTimeoutConfirm, isTimeoutConfirmStr = otherOptions.isTimeoutConfirmStr, messageWarning = otherOptions.messageWarning, modalConfirm = otherOptions.modalConfirm, tokenKey = otherOptions.tokenKey, resetOtherOptions = __rest(otherOptions, ["crypto", "jumpCallback", "isTimeoutConfirm", "isTimeoutConfirmStr", "messageWarning", "modalConfirm", "tokenKey"]);
26
+ this.otherOptions = __assign(__assign({}, defaultOptions), resetOtherOptions);
27
+ this.crypto = crypto;
28
+ this.tokenKey = tokenKey || TOKEN_KEY;
29
+ this.isTimeoutConfirm = isTimeoutConfirm;
30
+ this.isTimeoutConfirmStr = isTimeoutConfirmStr;
31
+ this.jumpCallback = jumpCallback;
32
+ this.messageWarning = messageWarning;
33
+ this.modalConfirm = modalConfirm;
34
+ this.defaultSetting();
35
+ this.interceptorsRequest();
36
+ this.interceptorsResponse();
37
+ }
38
+ // 设置axios方法默认值
39
+ AxiosRequest.prototype.defaultSetting = function () {
40
+ var _this = this;
41
+ // 设置默认请求头
42
+ // 租户ID只有登录的时候才需要传递
43
+ // this.instanceAxios.defaults.headers.common["Tenant-Id"] =
44
+ // this.otherOptions.TenantId;
45
+ this.instanceAxios.defaults.headers.common["Authorization"] =
46
+ this.otherOptions.Authorization;
47
+ if (this.otherOptions.commonHeaders) {
48
+ Object.keys(this.otherOptions.commonHeaders).forEach(function (key) {
49
+ var _a;
50
+ _this.instanceAxios.defaults.headers.common[key] =
51
+ (_a = _this.otherOptions.commonHeaders) === null || _a === void 0 ? void 0 : _a[key];
52
+ });
53
+ }
54
+ // 其他默认参数
55
+ this.instanceAxios.defaults.timeout = this.otherOptions.timeout;
56
+ this.instanceAxios.defaults.baseURL = this.otherOptions.baseURL;
57
+ };
58
+ // 拦截请求
59
+ AxiosRequest.prototype.interceptorsRequest = function () {
60
+ var _this = this;
61
+ if (this.otherOptions.interceptorsRequestHandle) {
62
+ this.otherOptions.interceptorsRequestHandle();
63
+ return;
64
+ }
65
+ this.instanceAxios.interceptors.request.use(function (config) {
66
+ if (_this.otherOptions.interceptorsRequestConfig) {
67
+ return _this.otherOptions.interceptorsRequestConfig(config);
68
+ }
69
+ return config;
70
+ }, function (error) {
71
+ return Promise.reject(error);
72
+ });
73
+ };
74
+ // 拦截响应
75
+ AxiosRequest.prototype.interceptorsResponse = function () {
76
+ var _this = this;
77
+ if (this.otherOptions.interceptorsResponseHandle) {
78
+ this.otherOptions.interceptorsResponseHandle();
79
+ return;
80
+ }
81
+ this.instanceAxios.interceptors.response.use(function (response) {
82
+ // console.log(response);
83
+ // 统一处理响应正常
84
+ var result = _this.responseSuccess(response);
85
+ return result;
86
+ }, function (error) {
87
+ console.log(error);
88
+ // 统一处理响应异常
89
+ _this.responseError(error);
90
+ return Promise.reject(error);
91
+ });
92
+ };
93
+ // 响应成功处理
94
+ AxiosRequest.prototype.responseSuccess = function (response) {
95
+ var _a;
96
+ var config = response.config, status = response.status, data = response.data;
97
+ // 对返回数据进行解密后 返回给客户端
98
+ if ((_a = response.config) === null || _a === void 0 ? void 0 : _a.isCrypto) ;
99
+ if (!config.isEasyMessage) {
100
+ return response;
101
+ }
102
+ // 简化处理信息
103
+ if (status === 200) {
104
+ // 字符串的情况也基本不会出现
105
+ if (typeof data === "string") {
106
+ return {
107
+ success: true,
108
+ msg: "操作成功",
109
+ data: data,
110
+ };
111
+ }
112
+ // 这里的data 不存在 可能说明这个接口没有返回信息
113
+ // 大多数情况下 不会这样 如果这种情况 也认为接口返回正常
114
+ if (!data) {
115
+ return {
116
+ success: true,
117
+ msg: "操作成功",
118
+ data: data,
119
+ };
120
+ }
121
+ // 大多数情况下 都是这种逻辑
122
+ if ((data === null || data === void 0 ? void 0 : data.code) === 200) {
123
+ return data;
124
+ }
125
+ }
126
+ // 201也可能出现,比如附件下载
127
+ if (status === 201) {
128
+ return response;
129
+ }
130
+ return Promise.reject(response.data);
131
+ };
132
+ // 响应错误处理
133
+ AxiosRequest.prototype.responseError = function (error) {
134
+ var _this = this;
135
+ var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k, _l, _m, _o, _p, _q, _r, _s, _t;
136
+ if (error === void 0) { error = {}; }
137
+ var response = error.response;
138
+ // 登录过期是否自己处理
139
+ var isHandleLoginExpiration = (_a = error === null || error === void 0 ? void 0 : error.config) === null || _a === void 0 ? void 0 : _a.isHandleLoginExpiration;
140
+ var expirationHandle = (_b = error === null || error === void 0 ? void 0 : error.config) === null || _b === void 0 ? void 0 : _b.expirationHandle;
141
+ // 处理token失效
142
+ // TODO: 刷新token逻辑
143
+ if ((response === null || response === void 0 ? void 0 : response.status) === 401 && !isHandleLoginExpiration) {
144
+ removeToken();
145
+ if (!this.isExpired) {
146
+ this.isExpired = true;
147
+ (_c = this.messageWarning) === null || _c === void 0 ? void 0 : _c.call(this, "登录过期", 3.5, function () {
148
+ _this.isExpired = false;
149
+ });
150
+ if (expirationHandle) {
151
+ expirationHandle(response);
152
+ }
153
+ else {
154
+ this.logoutHandle(response.status);
155
+ }
156
+ }
157
+ return;
158
+ }
159
+ // 处理被挤下线逻辑
160
+ if ((response === null || response === void 0 ? void 0 : response.status) === 501 && !isHandleLoginExpiration) {
161
+ removeToken();
162
+ if (!this.isOffline) {
163
+ this.isOffline = true;
164
+ (_d = this.messageWarning) === null || _d === void 0 ? void 0 : _d.call(this, "你的账号已在其他地方登录!", 3.5, function () {
165
+ _this.isOffline = false;
166
+ });
167
+ if (expirationHandle) {
168
+ expirationHandle(response);
169
+ }
170
+ else {
171
+ this.logoutHandle(response.status);
172
+ }
173
+ }
174
+ return;
175
+ }
176
+ // 处理404逻辑
177
+ if ((response === null || response === void 0 ? void 0 : response.status) === 404) {
178
+ if (!this.isNotFound) {
179
+ this.isNotFound = true;
180
+ (_e = this.messageWarning) === null || _e === void 0 ? void 0 : _e.call(this, "\u63A5\u53E3\uFF1A".concat(response.config.url, " \u672A\u627E\u5230\uFF01"), 3.5, function () {
181
+ _this.isNotFound = false;
182
+ });
183
+ }
184
+ return;
185
+ }
186
+ // 处理接口超时逻辑
187
+ if (this.isTimeoutConfirm &&
188
+ ((_h = (_g = (_f = error === null || error === void 0 ? void 0 : error.config) === null || _f === void 0 ? void 0 : _f.method) === null || _g === void 0 ? void 0 : _g.toUpperCase) === null || _h === void 0 ? void 0 : _h.call(_g)) === "POST" &&
189
+ !response &&
190
+ error.code === "ECONNABORTED" &&
191
+ ((_k = (_j = error.message) === null || _j === void 0 ? void 0 : _j.indexOf) === null || _k === void 0 ? void 0 : _k.call(_j, "".concat(error.config.timeout, "ms"))) !== -1) {
192
+ if (!this.isTimeout) {
193
+ this.isTimeout = true;
194
+ (_l = this.modalConfirm) === null || _l === void 0 ? void 0 : _l.call(this, "".concat(this.isTimeoutConfirmStr
195
+ ? this.isTimeoutConfirmStr
196
+ : "请求超时,请稍后再试,或联系相关人员确认后处理!", "\n \u63A5\u53E3\u5730\u5740: ").concat((_m = error === null || error === void 0 ? void 0 : error.config) === null || _m === void 0 ? void 0 : _m.url, "\n \u8BF7\u6C42\u65B9\u5F0F: ").concat((_o = error === null || error === void 0 ? void 0 : error.config) === null || _o === void 0 ? void 0 : _o.method, "\n \u8BF7\u6C42\u8D85\u65F6\u65F6\u95F4: ").concat(((_p = error === null || error === void 0 ? void 0 : error.config) === null || _p === void 0 ? void 0 : _p.timeout) / 1000, "\u79D2\n "), function () {
197
+ _this.isTimeout = false;
198
+ });
199
+ }
200
+ return;
201
+ }
202
+ // 提示业务错误
203
+ if ((_q = error === null || error === void 0 ? void 0 : error.config) === null || _q === void 0 ? void 0 : _q.isErrorMessage) {
204
+ // 详细信息 再开发模式开启 之后上到别的环境会去掉
205
+ (_r = this.modalConfirm) === null || _r === void 0 ? void 0 : _r.call(this, "".concat(((_t = (_s = error === null || error === void 0 ? void 0 : error.response) === null || _s === void 0 ? void 0 : _s.data) === null || _t === void 0 ? void 0 : _t.msg) || (error === null || error === void 0 ? void 0 : error.msg) || "服务器异常"));
206
+ // 接口地址: ${error?.config?.url}
207
+ // 响应状态码: ${error?.response?.status}
208
+ // 请求方式: ${error?.config?.method}
209
+ return;
210
+ }
211
+ };
212
+ // 跳转逻辑处理
213
+ AxiosRequest.prototype.logoutHandle = function (status) {
214
+ // 自定义跳转
215
+ if (this.jumpCallback) {
216
+ this.jumpCallback(status);
217
+ }
218
+ else {
219
+ window.location.href = "/login";
220
+ }
221
+ };
222
+ // 实例方法
223
+ AxiosRequest.prototype.request = function (options) {
224
+ var _a, _b, _c, _d, _e, _f, _g, _h, _j;
225
+ var isFormData = options.isFormData, _k = options.isAuth, isAuth = _k === void 0 ? true : _k, resetOptions = __rest(options, ["isFormData", "isAuth"]);
226
+ var newOptions = resetOptions;
227
+ // 自定义请求头
228
+ if (!newOptions.headers) {
229
+ newOptions.headers = {};
230
+ }
231
+ if (isAuth) {
232
+ var token = getToken();
233
+ newOptions.headers[this.tokenKey] = "bearer ".concat(token);
234
+ }
235
+ // 遍历params,将undefined值 转为null值
236
+ if (newOptions.data) {
237
+ newOptions.data = transformData(newOptions.data);
238
+ }
239
+ if (newOptions.params) {
240
+ newOptions.params = transformData(newOptions.params);
241
+ }
242
+ // 处理特殊请求
243
+ var specialMethod = ["POST", "PUT", "PATCH"];
244
+ var method = ((_a = newOptions.method) === null || _a === void 0 ? void 0 : _a.toUpperCase()) || "";
245
+ if (specialMethod.includes(method)) {
246
+ if (isFormData) {
247
+ var formData = new FormData();
248
+ var dataObj = newOptions.data || {};
249
+ var dataKey = Object.keys(dataObj);
250
+ var dataVal = Object.values(dataObj);
251
+ for (var i = 0; i < dataKey.length; i++) {
252
+ if (dataVal[i]) {
253
+ formData.append(dataKey[i], dataVal[i]);
254
+ }
255
+ }
256
+ newOptions.headers["Content-Type"] = "multipart/form-data";
257
+ newOptions.data = formData;
258
+ }
259
+ else {
260
+ newOptions.headers = __assign({ "Content-Type": "application/json" }, newOptions.headers);
261
+ }
262
+ }
263
+ // 数据加密处理 TODO:
264
+ // 将参数进行aes加密
265
+ if (newOptions.isCrypto === true || newOptions.isCrypto === "aes") {
266
+ if (newOptions.data) {
267
+ 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));
268
+ }
269
+ if (newOptions.params) {
270
+ 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));
271
+ }
272
+ }
273
+ // 将参数进行des加密
274
+ if (newOptions.isCrypto === "des") {
275
+ if (newOptions.data) {
276
+ 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));
277
+ }
278
+ if (newOptions.params) {
279
+ 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));
280
+ }
281
+ }
282
+ return this.instanceAxios(newOptions);
283
+ };
284
+ return AxiosRequest;
285
+ }());
286
+ function transformData(data) {
287
+ if (data == null) {
288
+ return data;
289
+ }
290
+ if (typeof data !== "object") {
291
+ return data;
292
+ }
293
+ if (Array.isArray(data)) {
294
+ return data.map(function (item) { return transformData(item); });
295
+ }
296
+ if (Object.prototype.toString.call(data) === "[object Object]") {
297
+ var newData = {};
298
+ for (var key in data) {
299
+ if (data.hasOwnProperty(key)) {
300
+ newData[key] = transformData(data[key]);
301
+ }
302
+ }
303
+ return newData;
304
+ }
305
+ return data;
306
+ }
307
+
308
+ export { AxiosRequest };
@@ -0,0 +1,16 @@
1
+ import instance from '../../node_modules/i18next/dist/esm/i18next.js';
2
+ import { initReactI18next } from '../../node_modules/react-i18next/dist/es/initReactI18next.js';
3
+
4
+ /**
5
+ * 初始化逻辑
6
+ * - 本函数主要用于初始化 i18next 实例,同时使用 react-i18next 插件进行 react 组件的支持
7
+ * - 本函数的调用时机,一般是在项目的入口文件中进行调用,如 src/main.tsx 中
8
+ * @param options 参数 同i18next.init 第一个参数
9
+ * @param callback 参数 同i18next.init 第二个参数
10
+ * @returns
11
+ */
12
+ function initLocale(options, callback) {
13
+ return instance.use(initReactI18next).init(options, callback);
14
+ }
15
+
16
+ export { instance as i18n, initLocale };
@@ -0,0 +1,54 @@
1
+ import instance from '../../node_modules/i18next/dist/esm/i18next.js';
2
+ import { getTFuncArgs } from './t-options.js';
3
+
4
+ /**
5
+ * t 函数封装:
6
+ * - 本文件的几个 t 函数,主要用于弥补一些在 React 组件之外使用的场景
7
+ * - 常规情况下,在 react 组件中,更加推荐的是使用 react-i18next 包中的 t 函数,即 const { t } = useTranslate(); 形式
8
+ */
9
+ /** 包裹一层 try-catch 的 t 函数 */
10
+ var tryT = function (key, defaultValueOrOptions) {
11
+ var _a = getTFuncArgs(defaultValueOrOptions), defaultValue = _a.defaultValue, options = _a.options;
12
+ if (!key) {
13
+ // 情况1:键名为空,返回默认值或键名
14
+ console.error("tryT: \u8BF7\u4E0D\u8981\u4F20\u5165\u7A7A key \u503C\u8FDB\u884C i18n.t \u51FD\u6570\u6267\u884C\uFF0C\u8FD9\u5C06\u4F1A\u5BFC\u81F4\u76F4\u63A5\u8FD4\u56DE\u7A7A\u5B57\u7B26\u4E32\uFF01");
15
+ return defaultValue || key;
16
+ }
17
+ if (!instance.exists(key)) {
18
+ // 情况2:键名不存在,返回默认值或键名
19
+ console.error("tryT: i18n-key '".concat(key, "' \u4E0D\u5B58\u5728\uFF0C\u8BF7\u68C0\u67E5\u5BF9\u5E94\u7684 @/locales/resources/locales/xx.json \u4E2D\u662F\u5426\u5B58\u5728\u5BF9\u5E94\u503C"));
20
+ return defaultValue || key;
21
+ }
22
+ try {
23
+ // 情况3:尝试执行 i18n.t 函数
24
+ return instance.t(key, options);
25
+ }
26
+ catch (err) {
27
+ // 情况4:执行 i18n.t 函数异常,返回默认值或键名
28
+ console.error("tryT: i18n.t key=".concat(key, " \u51FD\u6570\u6267\u884C\u5F02\u5E38\uFF1Aerr="), err);
29
+ return defaultValue || key;
30
+ }
31
+ };
32
+ /**
33
+ * 同步版本的 i18n.t 函数(应对在 react 组件外使用的翻译函数)
34
+ * - 如果 i18n 未初始化,则返回默认值或键名
35
+ * - 如果 i18n 初始化后,键名不存在,则返回默认值或键名
36
+ * - 其他正常情况(已经初始化+键名存在),则返回翻译值
37
+ * @description 本函数为在非 react 组件中使用的翻译函数;(在react中请求使用 useTranslate() 返回的 t 函数)
38
+ * @param {string} key
39
+ * @param {TOpionsEx|string} defaultValue 必填值
40
+ * @returns {string}
41
+ * @example
42
+ * const txt = t('someLocaleKey', '兜底默认值');
43
+ */
44
+ function safeT(key, defaultValueOrOptions) {
45
+ if (instance.isInitialized) {
46
+ return tryT(key, defaultValueOrOptions);
47
+ }
48
+ // 异常情况:没有初始化完成时,返回默认值
49
+ var defaultValue = getTFuncArgs(defaultValueOrOptions).defaultValue;
50
+ console.warn("safeT: i18n is not initialized. Returning defaultValue(".concat(defaultValue, ") or origin key (").concat(key, ")."));
51
+ return defaultValue || key;
52
+ }
53
+
54
+ export { safeT };
@@ -0,0 +1,20 @@
1
+ /**
2
+ * 获取 t 函数的参数
3
+ * @param {string | TOpionsEx} defaultValueOrOptions 默认值或者选项
4
+ * @returns { {defaultValue: string, options: TOpionsEx} } 默认值和选项
5
+ */
6
+ function getTFuncArgs(defaultValueOrOptions) {
7
+ var options = null;
8
+ var defaultValue = "";
9
+ if (typeof defaultValueOrOptions === "string") {
10
+ defaultValue = defaultValueOrOptions;
11
+ options = { defaultValue: defaultValue };
12
+ }
13
+ else {
14
+ options = defaultValueOrOptions;
15
+ defaultValue = options.defaultValue;
16
+ }
17
+ return { defaultValue: defaultValue, options: options };
18
+ }
19
+
20
+ export { getTFuncArgs };
@@ -0,0 +1,60 @@
1
+ import { getTFuncArgs } from './t-options.js';
2
+ import { useTranslation } from '../../node_modules/react-i18next/dist/es/useTranslation.js';
3
+
4
+ /**
5
+ * 这里只是简单代理一下useTranslation,方便后续扩展或添加其他逻辑
6
+ * @param ns 命名空间 大多数情况下都不需要传入这个参数,除非你用到了命名空间。
7
+ * @param options 其他参数 大多数情况下,都不需要关注这个参数。
8
+ * options.keyPrefix 为所有翻译键添加前缀
9
+ * // JSON 文件:{ "greeting": { "welcome": "Welcome" } }
10
+ * const { t } = useTranslation('common', { keyPrefix: 'greeting' });
11
+ * // 使用时省略前缀
12
+ * t('welcome'); // 等价于 t('greeting:welcome')
13
+ * @returns { t, i18n, ready }
14
+ * t: 翻译方法
15
+ * i18n: i18next实例 访问 i18next 的完整 API(如切换语言、加载命名空间)
16
+ * i18n.changeLanguage('zh-CN'); 使用它切换语言,可以触发组件渲染。但是当前项目中是使用全局刷新的模式,来更新语言的。
17
+ * ready: 是否加载完成
18
+ */
19
+ function useZtTranslation(ns, options) {
20
+ var _a = useTranslation(ns, options), _tOld = _a.t, i18n = _a.i18n, ready = _a.ready;
21
+ // 扩展t方法,用于处理嵌套的翻译键
22
+ var tEnhance = function (key, defaultValueOrOptions) {
23
+ var _a = getTFuncArgs(defaultValueOrOptions), defaultValue = _a.defaultValue, options = _a.options;
24
+ if (!key) {
25
+ // 情况1:键名为空,返回默认值或键名
26
+ console.error("tEnhance: \u8BF7\u4E0D\u8981\u4F20\u5165\u7A7A key \u503C\u8FDB\u884C t \u51FD\u6570\u6267\u884C\uFF0C\u8FD9\u5C06\u4F1A\u5BFC\u81F4\u76F4\u63A5\u8FD4\u56DE\u7A7A\u5B57\u7B26\u4E32\uFF01");
27
+ return defaultValue || key;
28
+ }
29
+ if (!i18n.exists(key)) {
30
+ // 情况2:键名不存在,返回默认值或键名
31
+ console.error("tEnhance: i18n-key '".concat(key, "' \u4E0D\u5B58\u5728\uFF0C\u8BF7\u68C0\u67E5\u5BF9\u5E94\u7684 @/locales/resources/locales/xx.json \u4E2D\u662F\u5426\u5B58\u5728\u5BF9\u5E94\u503C"));
32
+ return defaultValue || key;
33
+ }
34
+ try {
35
+ return _tOld(key, options);
36
+ }
37
+ catch (error) {
38
+ console.warn("tEnhance: t \u51FD\u6570\u6267\u884C\u5F02\u5E38\uFF0C\u8BF7\u68C0\u67E5! key=".concat(key, ", err="), error);
39
+ return defaultValue || key || "unknown";
40
+ }
41
+ };
42
+ return {
43
+ /**
44
+ * t函数的使用方式:基本上使用1、2两种方式就可以了。
45
+ * 1、t('key') // 基础用法
46
+ * 2、t('greeting', { name: 'John' }); // 对应 JSON: "greeting": "Hello {{name}}"
47
+ * 3、t('auth:login'); // 显式指定命名空间
48
+ *
49
+ * 关于t函数的第二个参数的说明。
50
+ * 为了防止一些异常情况,翻译失败。可以传入一个默认值。这个默认值就是原本的中文就行。
51
+ * 1、t('key', '默认值');
52
+ * 2、t('key', { defaultValue: '默认值', ...otherOptions });
53
+ */
54
+ t: tEnhance,
55
+ i18n: i18n,
56
+ ready: ready,
57
+ };
58
+ }
59
+
60
+ export { useZtTranslation as useTranslation };