zmdms-utils 0.0.72 → 0.0.74

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 (40) hide show
  1. package/dist/es/crypto.d.ts +3 -0
  2. package/dist/es/crypto.js +4 -0
  3. package/dist/es/locales/i18n.d.ts +15 -0
  4. package/dist/es/locales/t-in-non-react.d.ts +17 -0
  5. package/dist/es/locales/t-options.d.ts +7 -0
  6. package/dist/es/locales/use-translation.d.ts +42 -0
  7. package/dist/es/microAppCreator.d.ts +25 -2
  8. package/dist/es/microAppCreator.js +28 -6
  9. package/dist/es/node_modules/html-parse-stringify/dist/html-parse-stringify.module.js +5 -0
  10. package/dist/es/node_modules/i18next/dist/esm/i18next.js +2242 -0
  11. package/dist/es/node_modules/js-base64/base64.js +273 -0
  12. package/dist/es/node_modules/react-i18next/dist/es/Trans.js +47 -0
  13. package/dist/es/node_modules/react-i18next/dist/es/TransWithoutContext.js +312 -0
  14. package/dist/es/node_modules/react-i18next/dist/es/context.js +18 -0
  15. package/dist/es/node_modules/react-i18next/dist/es/defaults.js +21 -0
  16. package/dist/es/node_modules/react-i18next/dist/es/i18nInstance.js +7 -0
  17. package/dist/es/node_modules/react-i18next/dist/es/initReactI18next.js +12 -0
  18. package/dist/es/node_modules/react-i18next/dist/es/unescape.js +27 -0
  19. package/dist/es/node_modules/react-i18next/dist/es/useTranslation.js +112 -0
  20. package/dist/es/node_modules/react-i18next/dist/es/utils.js +63 -0
  21. package/dist/es/node_modules/void-elements/index.js +23 -0
  22. package/dist/es/request.d.ts +1 -1
  23. package/dist/es/request.js +4 -3
  24. package/dist/es/src/locales/i18n.js +16 -0
  25. package/dist/es/src/locales/t-in-non-react.js +54 -0
  26. package/dist/es/src/locales/t-options.js +20 -0
  27. package/dist/es/src/locales/use-translation.js +60 -0
  28. package/dist/index.d.ts +6 -0
  29. package/dist/index.js +6 -0
  30. package/package.json +4 -1
  31. package/src/crypto.ts +6 -0
  32. package/src/index.ts +4 -1
  33. package/src/locales/i18n.ts +18 -0
  34. package/src/locales/index.ts +8 -0
  35. package/src/locales/t-in-non-react.ts +65 -0
  36. package/src/locales/t-options.ts +24 -0
  37. package/src/locales/use-translation.ts +76 -0
  38. package/src/locales//345/244/232/350/257/255/350/250/200.md +1 -0
  39. package/src/microAppCreator.ts +47 -8
  40. package/src/request.ts +5 -4
@@ -0,0 +1,76 @@
1
+ import { useTranslation, Trans } from "react-i18next";
2
+ import type { TOpionsEx } from "./t-options";
3
+ import { getTFuncArgs } from "./t-options";
4
+
5
+ type Prams = Parameters<typeof useTranslation>;
6
+ type NS = Prams[0];
7
+ type Options = Prams[1];
8
+
9
+ type EnhanceTFunc = (
10
+ key: string,
11
+ defaultValueOrOptions: string | TOpionsEx
12
+ ) => string;
13
+
14
+ /**
15
+ * 这里只是简单代理一下useTranslation,方便后续扩展或添加其他逻辑
16
+ * @param ns 命名空间 大多数情况下都不需要传入这个参数,除非你用到了命名空间。
17
+ * @param options 其他参数 大多数情况下,都不需要关注这个参数。
18
+ * options.keyPrefix 为所有翻译键添加前缀
19
+ * // JSON 文件:{ "greeting": { "welcome": "Welcome" } }
20
+ * const { t } = useTranslation('common', { keyPrefix: 'greeting' });
21
+ * // 使用时省略前缀
22
+ * t('welcome'); // 等价于 t('greeting:welcome')
23
+ * @returns { t, i18n, ready }
24
+ * t: 翻译方法
25
+ * i18n: i18next实例 访问 i18next 的完整 API(如切换语言、加载命名空间)
26
+ * i18n.changeLanguage('zh-CN'); 使用它切换语言,可以触发组件渲染。但是当前项目中是使用全局刷新的模式,来更新语言的。
27
+ * ready: 是否加载完成
28
+ */
29
+ function useZtTranslation(ns?: NS, options?: Options) {
30
+ const { t: _tOld, i18n, ready } = useTranslation(ns, options);
31
+
32
+ // 扩展t方法,用于处理嵌套的翻译键
33
+ const tEnhance: EnhanceTFunc = (key, defaultValueOrOptions) => {
34
+ const { defaultValue, options } = getTFuncArgs(defaultValueOrOptions);
35
+
36
+ if (!key) {
37
+ // 情况1:键名为空,返回默认值或键名
38
+ console.error(
39
+ `tEnhance: 请不要传入空 key 值进行 t 函数执行,这将会导致直接返回空字符串!`
40
+ );
41
+ return defaultValue || key;
42
+ }
43
+ if (!i18n.exists(key)) {
44
+ // 情况2:键名不存在,返回默认值或键名
45
+ console.error(
46
+ `tEnhance: i18n-key '${key}' 不存在,请检查对应的 @/locales/resources/locales/xx.json 中是否存在对应值`
47
+ );
48
+ return defaultValue || key;
49
+ }
50
+ try {
51
+ return _tOld(key, options);
52
+ } catch (error) {
53
+ console.warn(`tEnhance: t 函数执行异常,请检查! key=${key}, err=`, error);
54
+ return defaultValue || key || "unknown";
55
+ }
56
+ };
57
+
58
+ return {
59
+ /**
60
+ * t函数的使用方式:基本上使用1、2两种方式就可以了。
61
+ * 1、t('key') // 基础用法
62
+ * 2、t('greeting', { name: 'John' }); // 对应 JSON: "greeting": "Hello {{name}}"
63
+ * 3、t('auth:login'); // 显式指定命名空间
64
+ *
65
+ * 关于t函数的第二个参数的说明。
66
+ * 为了防止一些异常情况,翻译失败。可以传入一个默认值。这个默认值就是原本的中文就行。
67
+ * 1、t('key', '默认值');
68
+ * 2、t('key', { defaultValue: '默认值', ...otherOptions });
69
+ */
70
+ t: tEnhance,
71
+ i18n,
72
+ ready,
73
+ };
74
+ }
75
+
76
+ export { useZtTranslation as useTranslation, Trans };
@@ -0,0 +1 @@
1
+ # React 项目多语言
@@ -16,11 +16,32 @@ export function update(props: any) {
16
16
  * @param microAppOptions 微应用的一些配置
17
17
  */
18
18
  export interface IMicroAppOptions {
19
+ /**
20
+ * @deprecated 该属性已废弃,无意义
21
+ */
19
22
  microAppContainerId: string; // 微应用加载的dom容器 id
20
23
  microAppKey: string; // 微应用的唯一key值 用途:1. qiankun下的路由前缀;2. 主、子通信时的标识
24
+ /**
25
+ * @deprecated 该属性已废弃,无意义
26
+ */
21
27
  microAppRoot: any; // 微应用根节点实例
22
- microAppMountHandle?: (props: any) => void; // 微应用每次进入都会调用的方法
23
- microAppUnMountHandle?: (props: any) => void; // 微应用每次切出/卸载 都会调用的方法
28
+ /**
29
+ * 只会在微应用初始化的时候调用一次,下次微应用重新进入时会直接调用 mount 钩子,不会再重复触发 bootstrap
30
+ * 通常我们可以在这里做一些全局变量的初始化,比如不会在 unmount 阶段被销毁的应用级别的缓存等。
31
+ **/
32
+ microAppBootstrapHandle?: () => void;
33
+ /**
34
+ * 应用每次进入都会调用 mount 方法,通常我们在这里触发应用的渲染方法
35
+ */
36
+ microAppMountHandle: (props: any) => void;
37
+ /**
38
+ * 应用每次 切出/卸载 会调用的方法,通常在这里我们会卸载微应用的应用实例
39
+ */
40
+ microAppUnMountHandle: (props: any) => void;
41
+ /**
42
+ * 可选生命周期钩子,仅使用 loadMicroApp 方式加载微应用时生效
43
+ */
44
+ microAppUpdateHandle?: (props: any) => void;
24
45
  }
25
46
  export function initMicroApp(render: any, microAppOptions: IMicroAppOptions) {
26
47
  // 初始加载应用
@@ -32,9 +53,10 @@ export function initMicroApp(render: any, microAppOptions: IMicroAppOptions) {
32
53
  // 2. 进入微前端环境的运行逻辑
33
54
  const {
34
55
  microAppKey,
35
- microAppRoot,
56
+ microAppBootstrapHandle,
36
57
  microAppMountHandle,
37
58
  microAppUnMountHandle,
59
+ microAppUpdateHandle,
38
60
  } = microAppOptions;
39
61
 
40
62
  // 唯一key没传 给出开发提示
@@ -47,15 +69,20 @@ export function initMicroApp(render: any, microAppOptions: IMicroAppOptions) {
47
69
  * 通常我们可以在这里做一些全局变量的初始化,比如不会在 unmount 阶段被销毁的应用级别的缓存等。
48
70
  */
49
71
  async function bootstrap() {
50
- console.log("微应用 bootstraped");
72
+ console.log(`微应用==${microAppKey}==bootstraped`);
73
+ if (microAppBootstrapHandle) {
74
+ microAppBootstrapHandle();
75
+ }
51
76
  }
52
77
 
53
78
  /**
54
79
  * 应用每次进入都会调用 mount 方法,通常我们在这里触发应用的渲染方法
55
80
  */
56
81
  async function mount(props: any) {
57
- console.log("微应用 mount");
58
- microAppMountHandle && microAppMountHandle(props);
82
+ console.log(`微应用==${microAppKey}==mount`);
83
+ if (microAppMountHandle) {
84
+ microAppMountHandle(props);
85
+ }
59
86
  render(props);
60
87
  }
61
88
 
@@ -63,13 +90,25 @@ export function initMicroApp(render: any, microAppOptions: IMicroAppOptions) {
63
90
  * 应用每次 切出/卸载 会调用的方法,通常在这里我们会卸载微应用的应用实例
64
91
  */
65
92
  async function unmount(props: any) {
66
- console.log("微应用 unmount", props);
67
- microAppUnMountHandle && microAppUnMountHandle(props);
93
+ console.log(`微应用==${microAppKey}==unmount`);
94
+ if (microAppUnMountHandle) {
95
+ microAppUnMountHandle(props);
96
+ }
97
+ }
98
+ /**
99
+ * 应用每次 切出/卸载 会调用的方法,通常在这里我们会卸载微应用的应用实例
100
+ */
101
+ async function update(props: any) {
102
+ console.log(`微应用==${microAppKey}==update`);
103
+ if (microAppUpdateHandle) {
104
+ microAppUpdateHandle(props);
105
+ }
68
106
  }
69
107
 
70
108
  return {
71
109
  bootstrap,
72
110
  mount,
73
111
  unmount,
112
+ update,
74
113
  };
75
114
  }
package/src/request.ts CHANGED
@@ -10,7 +10,7 @@ import { Crypto } from "./crypto";
10
10
  import { TOKEN_KEY } from "./constants";
11
11
 
12
12
  const defaultOptions: IOtherOptions = {
13
- TenantId: "000000",
13
+ // TenantId: "000000", // 租户ID不需要了
14
14
  Authorization: "Basic em1kbXM6em1kbXNfc2VjcmV0",
15
15
  timeout: 60000,
16
16
  };
@@ -66,8 +66,9 @@ export class AxiosRequest {
66
66
  // 设置axios方法默认值
67
67
  private defaultSetting() {
68
68
  // 设置默认请求头
69
- this.instanceAxios.defaults.headers.common["Tenant-Id"] =
70
- this.otherOptions.TenantId;
69
+ // 租户ID只有登录的时候才需要传递
70
+ // this.instanceAxios.defaults.headers.common["Tenant-Id"] =
71
+ // this.otherOptions.TenantId;
71
72
  this.instanceAxios.defaults.headers.common["Authorization"] =
72
73
  this.otherOptions.Authorization;
73
74
 
@@ -406,7 +407,7 @@ export interface IOtherOptions {
406
407
  callback?: () => void
407
408
  ) => void;
408
409
  modalConfirm?: (msg: string, callback?: () => void) => void;
409
- /** token相关值 */
410
+ /** @deprecated 租户ID,只有登录接口 需要 传递。 */
410
411
  TenantId?: string;
411
412
  /** auth值 */
412
413
  Authorization?: string;