@origen-ui/admin-cli 1.0.3 → 2.0.1

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 (237) hide show
  1. package/README.md +11 -7
  2. package/package.json +19 -3
  3. package/templates/current/clean/CHANGELOG.md +137 -0
  4. package/templates/current/clean/component-catalog.json +58 -17
  5. package/templates/current/clean/e2e/admin-shell.spec.ts +6 -0
  6. package/templates/current/clean/e2e/demo-shell.spec.ts +90 -46
  7. package/templates/current/clean/package.json +55 -41
  8. package/templates/current/clean/playwright.config.ts +1 -0
  9. package/templates/current/clean/playwright.demo.config.ts +1 -1
  10. package/templates/current/clean/scaffold.lock.json +2 -2
  11. package/templates/current/clean/scripts/clean-overlay/src/features/errors/pages/Forbidden.vue +7 -2
  12. package/templates/current/clean/scripts/clean-overlay/src/features/errors/pages/NotFound.vue +7 -2
  13. package/templates/current/clean/scripts/clean-overlay/src/features/errors/pages/ServerError.vue +7 -2
  14. package/templates/current/clean/scripts/clean-overlay/src/features/errors/pages/error-pages.spec.ts +11 -2
  15. package/templates/current/clean/scripts/clean-overlay/src/main.ts +23 -14
  16. package/templates/current/clean/scripts/clean-overlay/src/router/componentCache.ts +12 -0
  17. package/templates/current/clean/scripts/clean-overlay/src/router/componentRegistry.ts +2 -13
  18. package/templates/current/clean/scripts/clean-overlay/src/router/dynamicRoutes.spec.ts +34 -4
  19. package/templates/current/clean/scripts/clean-overlay/src/router/paths.ts +21 -0
  20. package/templates/current/clean/scripts/clean-overlay/src/router/staticRoutes.ts +6 -6
  21. package/templates/current/clean/scripts/demo-api.ts +84 -8
  22. package/templates/current/clean/scripts/generate-api.ts +87 -3
  23. package/templates/current/clean/scripts/vite-build-policy.spec.ts +72 -0
  24. package/templates/current/clean/src/App.vue +10 -3
  25. package/templates/current/clean/src/api/client.ts +90 -10
  26. package/templates/current/clean/src/api/generated/data-contracts.ts +0 -3
  27. package/templates/current/clean/src/api/generated/http-client.ts +28 -26
  28. package/templates/current/clean/src/api/generated//347/263/273/347/273/237/347/256/241/347/220/206/345/206/205/345/256/271/346/226/207/347/253/240.ts +5 -8
  29. package/templates/current/clean/src/api/generated//347/263/273/347/273/237/347/256/241/347/220/206/345/256/241/350/256/241.ts +1 -4
  30. package/templates/current/clean/src/api/generated//347/263/273/347/273/237/347/256/241/347/220/206/346/226/207/344/273/266/344/270/212/344/274/240.ts +2 -5
  31. package/templates/current/clean/src/api/generated//347/263/273/347/273/237/347/256/241/347/220/206/347/224/250/346/210/267.ts +6 -9
  32. package/templates/current/clean/src/api/generated//347/263/273/347/273/237/347/256/241/347/220/206/350/217/234/345/215/225.ts +7 -10
  33. package/templates/current/clean/src/api/generated//347/263/273/347/273/237/347/256/241/347/220/206/350/247/222/350/211/262.ts +6 -9
  34. package/templates/current/clean/src/api/generated//347/263/273/347/273/237/347/256/241/347/220/206/350/256/242/345/215/225/350/215/211/347/250/277.ts +6 -9
  35. package/templates/current/clean/src/api/generated//350/256/244/350/257/201.ts +3 -6
  36. package/templates/current/clean/src/app/adminLocale.ts +90 -0
  37. package/templates/current/clean/src/app/createAdminUi.spec.ts +65 -9
  38. package/templates/current/clean/src/app/createAdminUi.ts +14 -29
  39. package/templates/current/clean/src/app/embedded-token.spec.ts +40 -0
  40. package/templates/current/clean/src/app/embedded.spec.ts +135 -0
  41. package/templates/current/clean/src/app/embedded.ts +255 -0
  42. package/templates/current/clean/src/app/errorMessage.spec.ts +76 -0
  43. package/templates/current/clean/src/app/errorMessage.ts +53 -0
  44. package/templates/current/clean/src/app/localeFormat.ts +52 -0
  45. package/templates/current/clean/src/app/menu-host.spec.ts +77 -0
  46. package/templates/current/clean/src/app/menu.spec.ts +65 -0
  47. package/templates/current/clean/src/app/menu.ts +52 -12
  48. package/templates/current/clean/src/app/menuTypes.ts +1 -0
  49. package/templates/current/clean/src/app/origenUiLocale.spec.ts +6 -5
  50. package/templates/current/clean/src/app/origenUiLocale.ts +3 -58
  51. package/templates/current/clean/src/app/permission.spec.ts +26 -0
  52. package/templates/current/clean/src/app/runtimeConfig.ts +9 -8
  53. package/templates/current/clean/src/demo-api.spec.ts +311 -0
  54. package/templates/current/clean/src/env.d.ts +18 -0
  55. package/templates/current/clean/src/features/appearance/components/ThemePanel.vue +21 -17
  56. package/templates/current/clean/src/features/appearance/store/theme.ts +2 -1
  57. package/templates/current/clean/src/features/auth/pages/Login.vue +22 -12
  58. package/templates/current/clean/src/features/auth/store/auth.ts +6 -9
  59. package/templates/current/clean/src/features/errors/pages/Forbidden.vue +7 -2
  60. package/templates/current/clean/src/features/errors/pages/NotFound.vue +7 -2
  61. package/templates/current/clean/src/features/errors/pages/ServerError.vue +7 -2
  62. package/templates/current/clean/src/features/errors/pages/error-pages.spec.ts +11 -2
  63. package/templates/current/clean/src/features/security/components/DevtoolsProtectionOverlay.vue +5 -2
  64. package/templates/current/clean/src/features/security/devtoolsProtection.spec.ts +7 -10
  65. package/templates/current/clean/src/features/security/devtoolsProtection.ts +2 -1
  66. package/templates/current/clean/src/features/system/access/api/admin-repository.spec.ts +14 -0
  67. package/templates/current/clean/src/features/system/access/api/admin-repository.ts +34 -15
  68. package/templates/current/clean/src/features/system/access/api/rbac.ts +52 -32
  69. package/templates/current/clean/src/features/system/access/composables/useAuthorizationPolling.spec.ts +134 -0
  70. package/templates/current/clean/src/features/system/access/composables/useAuthorizationPolling.ts +96 -0
  71. package/templates/current/clean/src/features/workspace/pages/Empty.vue +4 -1
  72. package/templates/current/clean/src/features/workspace/pages/Profile.vue +14 -12
  73. package/templates/current/clean/src/layouts/AdminLayout.vue +6 -1
  74. package/templates/current/clean/src/layouts/TopBar.vue +60 -11
  75. package/templates/current/clean/src/layouts/topBarMenuSearch.spec.ts +24 -1
  76. package/templates/current/clean/src/layouts/topBarMenuSearch.ts +10 -0
  77. package/templates/current/clean/src/locales/en-US.json +517 -0
  78. package/templates/current/clean/src/locales/en-US.runtime.ts +22 -0
  79. package/templates/current/clean/src/locales/index.ts +144 -47
  80. package/templates/current/clean/src/locales/messages-types.ts +62 -0
  81. package/templates/current/clean/src/locales/messages.spec.ts +113 -6
  82. package/templates/current/clean/src/locales/vue-i18n.d.ts +5 -0
  83. package/templates/current/clean/src/locales/zh-CN.json +517 -0
  84. package/templates/current/clean/src/locales/zh-CN.runtime.ts +22 -0
  85. package/templates/current/clean/src/locales/zh-TW.json +517 -0
  86. package/templates/current/clean/src/locales/zh-TW.runtime.ts +22 -0
  87. package/templates/current/clean/src/main.ts +23 -14
  88. package/templates/current/clean/src/router/bootstrap.ts +20 -11
  89. package/templates/current/clean/src/router/componentCache.ts +12 -0
  90. package/templates/current/clean/src/router/componentRegistry.ts +2 -13
  91. package/templates/current/clean/src/router/dynamicMenuRefresh.spec.ts +101 -0
  92. package/templates/current/clean/src/router/dynamicMenuRefresh.ts +24 -0
  93. package/templates/current/clean/src/router/dynamicRoutes.spec.ts +34 -4
  94. package/templates/current/clean/src/router/dynamicRoutes.ts +19 -4
  95. package/templates/current/clean/src/router/paths.spec.ts +50 -0
  96. package/templates/current/clean/src/router/paths.ts +21 -0
  97. package/templates/current/clean/src/router/queryEncryption.spec.ts +30 -4
  98. package/templates/current/clean/src/router/queryEncryption.ts +48 -44
  99. package/templates/current/clean/src/router/staticRoutes.ts +6 -6
  100. package/templates/current/clean/src/router/useRouteQueryDefaults.spec.ts +2 -1
  101. package/templates/current/clean/src/router/useRouteQueryDefaults.types.ts +2 -1
  102. package/templates/current/clean/src/templates/og-table.ts +79 -37
  103. package/templates/current/clean/src/templates/templates.spec.ts +19 -0
  104. package/templates/current/clean/tsconfig.json +0 -1
  105. package/templates/current/clean/vite.build-policy.ts +161 -289
  106. package/templates/current/clean/vite.config.ts +34 -5
  107. package/templates/current/examples/CHANGELOG.md +137 -0
  108. package/templates/current/examples/component-catalog.json +58 -17
  109. package/templates/current/examples/e2e/admin-shell.spec.ts +6 -0
  110. package/templates/current/examples/e2e/demo-shell.spec.ts +90 -46
  111. package/templates/current/examples/package.json +55 -41
  112. package/templates/current/examples/playwright.config.ts +1 -0
  113. package/templates/current/examples/playwright.demo.config.ts +1 -1
  114. package/templates/current/examples/scaffold.lock.json +2 -2
  115. package/templates/current/examples/scripts/clean-overlay/src/features/errors/pages/Forbidden.vue +7 -2
  116. package/templates/current/examples/scripts/clean-overlay/src/features/errors/pages/NotFound.vue +7 -2
  117. package/templates/current/examples/scripts/clean-overlay/src/features/errors/pages/ServerError.vue +7 -2
  118. package/templates/current/examples/scripts/clean-overlay/src/features/errors/pages/error-pages.spec.ts +11 -2
  119. package/templates/current/examples/scripts/clean-overlay/src/main.ts +23 -14
  120. package/templates/current/examples/scripts/clean-overlay/src/router/componentCache.ts +12 -0
  121. package/templates/current/examples/scripts/clean-overlay/src/router/componentRegistry.ts +2 -13
  122. package/templates/current/examples/scripts/clean-overlay/src/router/dynamicRoutes.spec.ts +34 -4
  123. package/templates/current/examples/scripts/clean-overlay/src/router/paths.ts +21 -0
  124. package/templates/current/examples/scripts/clean-overlay/src/router/staticRoutes.ts +6 -6
  125. package/templates/current/examples/scripts/demo-api.ts +84 -8
  126. package/templates/current/examples/scripts/generate-api.ts +87 -3
  127. package/templates/current/examples/scripts/vite-build-policy.spec.ts +72 -0
  128. package/templates/current/examples/src/App.vue +10 -3
  129. package/templates/current/examples/src/api/client.ts +90 -10
  130. package/templates/current/examples/src/api/generated/data-contracts.ts +0 -3
  131. package/templates/current/examples/src/api/generated/http-client.ts +28 -26
  132. package/templates/current/examples/src/api/generated//347/263/273/347/273/237/347/256/241/347/220/206/345/206/205/345/256/271/346/226/207/347/253/240.ts +5 -8
  133. package/templates/current/examples/src/api/generated//347/263/273/347/273/237/347/256/241/347/220/206/345/256/241/350/256/241.ts +1 -4
  134. package/templates/current/examples/src/api/generated//347/263/273/347/273/237/347/256/241/347/220/206/346/226/207/344/273/266/344/270/212/344/274/240.ts +2 -5
  135. package/templates/current/examples/src/api/generated//347/263/273/347/273/237/347/256/241/347/220/206/347/224/250/346/210/267.ts +6 -9
  136. package/templates/current/examples/src/api/generated//347/263/273/347/273/237/347/256/241/347/220/206/350/217/234/345/215/225.ts +7 -10
  137. package/templates/current/examples/src/api/generated//347/263/273/347/273/237/347/256/241/347/220/206/350/247/222/350/211/262.ts +6 -9
  138. package/templates/current/examples/src/api/generated//347/263/273/347/273/237/347/256/241/347/220/206/350/256/242/345/215/225/350/215/211/347/250/277.ts +6 -9
  139. package/templates/current/examples/src/api/generated//350/256/244/350/257/201.ts +3 -6
  140. package/templates/current/examples/src/app/adminLocale.ts +90 -0
  141. package/templates/current/examples/src/app/createAdminUi.spec.ts +65 -9
  142. package/templates/current/examples/src/app/createAdminUi.ts +14 -29
  143. package/templates/current/examples/src/app/embedded-token.spec.ts +40 -0
  144. package/templates/current/examples/src/app/embedded.spec.ts +135 -0
  145. package/templates/current/examples/src/app/embedded.ts +255 -0
  146. package/templates/current/examples/src/app/errorMessage.spec.ts +76 -0
  147. package/templates/current/examples/src/app/errorMessage.ts +53 -0
  148. package/templates/current/examples/src/app/localeFormat.ts +52 -0
  149. package/templates/current/examples/src/app/menu-host.spec.ts +77 -0
  150. package/templates/current/examples/src/app/menu.spec.ts +65 -0
  151. package/templates/current/examples/src/app/menu.ts +52 -12
  152. package/templates/current/examples/src/app/menuTypes.ts +1 -0
  153. package/templates/current/examples/src/app/origenUiLocale.spec.ts +6 -5
  154. package/templates/current/examples/src/app/origenUiLocale.ts +3 -58
  155. package/templates/current/examples/src/app/permission.spec.ts +26 -0
  156. package/templates/current/examples/src/app/runtimeConfig.ts +9 -8
  157. package/templates/current/examples/src/demo-api.spec.ts +311 -0
  158. package/templates/current/examples/src/env.d.ts +18 -0
  159. package/templates/current/examples/src/features/appearance/components/ThemePanel.vue +21 -17
  160. package/templates/current/examples/src/features/appearance/store/theme.ts +2 -1
  161. package/templates/current/examples/src/features/auth/pages/Login.vue +22 -12
  162. package/templates/current/examples/src/features/auth/store/auth.ts +6 -9
  163. package/templates/current/examples/src/features/errors/pages/Forbidden.vue +7 -2
  164. package/templates/current/examples/src/features/errors/pages/NotFound.vue +7 -2
  165. package/templates/current/examples/src/features/errors/pages/ServerError.vue +7 -2
  166. package/templates/current/examples/src/features/errors/pages/error-pages.spec.ts +9 -1
  167. package/templates/current/examples/src/features/feature-composables.spec.ts +47 -10
  168. package/templates/current/examples/src/features/security/components/DevtoolsProtectionOverlay.vue +5 -2
  169. package/templates/current/examples/src/features/security/devtoolsProtection.spec.ts +7 -10
  170. package/templates/current/examples/src/features/security/devtoolsProtection.ts +2 -1
  171. package/templates/current/examples/src/features/system/access/api/admin-repository.spec.ts +14 -0
  172. package/templates/current/examples/src/features/system/access/api/admin-repository.ts +34 -15
  173. package/templates/current/examples/src/features/system/access/api/rbac.ts +52 -32
  174. package/templates/current/examples/src/features/system/access/composables/useAuthorizationPolling.spec.ts +134 -0
  175. package/templates/current/examples/src/features/system/access/composables/useAuthorizationPolling.ts +96 -0
  176. package/templates/current/examples/src/features/system/audit/composables/useAuditManagement.ts +43 -38
  177. package/templates/current/examples/src/features/system/menus/components/MenuEditorForm.vue +137 -47
  178. package/templates/current/examples/src/features/system/menus/composables/useMenuManagement.ts +215 -107
  179. package/templates/current/examples/src/features/system/menus/menuDraft.spec.ts +204 -0
  180. package/templates/current/examples/src/features/system/menus/menuDraft.ts +116 -0
  181. package/templates/current/examples/src/features/system/menus/menuTreePrecheck.spec.ts +268 -0
  182. package/templates/current/examples/src/features/system/menus/menuTreePrecheck.ts +317 -0
  183. package/templates/current/examples/src/features/system/menus/pages/MenuList.vue +3 -1
  184. package/templates/current/examples/src/features/system/roles/components/RoleEditorForm.vue +19 -15
  185. package/templates/current/examples/src/features/system/roles/composables/useRoleManagement.ts +49 -53
  186. package/templates/current/examples/src/features/system/roles/pages/RoleList.vue +3 -1
  187. package/templates/current/examples/src/features/system/users/components/UserEditorForm.vue +22 -19
  188. package/templates/current/examples/src/features/system/users/composables/useUserManagement.ts +59 -64
  189. package/templates/current/examples/src/features/system/users/pages/UserList.vue +3 -1
  190. package/templates/current/examples/src/features/workspace/pages/Dashboard.spec.ts +9 -0
  191. package/templates/current/examples/src/features/workspace/pages/Dashboard.vue +54 -26
  192. package/templates/current/examples/src/features/workspace/pages/Empty.vue +4 -1
  193. package/templates/current/examples/src/features/workspace/pages/Profile.vue +14 -12
  194. package/templates/current/examples/src/layouts/AdminLayout.vue +6 -1
  195. package/templates/current/examples/src/layouts/TopBar.vue +60 -11
  196. package/templates/current/examples/src/layouts/topBarMenuSearch.spec.ts +24 -1
  197. package/templates/current/examples/src/layouts/topBarMenuSearch.ts +10 -0
  198. package/templates/current/examples/src/locales/en-US.json +517 -0
  199. package/templates/current/examples/src/locales/en-US.runtime.ts +22 -0
  200. package/templates/current/examples/src/locales/index.ts +144 -47
  201. package/templates/current/examples/src/locales/messages-types.ts +62 -0
  202. package/templates/current/examples/src/locales/messages.spec.ts +113 -6
  203. package/templates/current/examples/src/locales/vue-i18n.d.ts +5 -0
  204. package/templates/current/examples/src/locales/zh-CN.json +517 -0
  205. package/templates/current/examples/src/locales/zh-CN.runtime.ts +22 -0
  206. package/templates/current/examples/src/locales/zh-TW.json +517 -0
  207. package/templates/current/examples/src/locales/zh-TW.runtime.ts +22 -0
  208. package/templates/current/examples/src/main.ts +43 -15
  209. package/templates/current/examples/src/router/bootstrap.ts +20 -11
  210. package/templates/current/examples/src/router/componentCache.ts +18 -0
  211. package/templates/current/examples/src/router/componentRegistry.ts +1 -18
  212. package/templates/current/examples/src/router/dynamicMenuRefresh.spec.ts +101 -0
  213. package/templates/current/examples/src/router/dynamicMenuRefresh.ts +24 -0
  214. package/templates/current/examples/src/router/dynamicRoutes.spec.ts +37 -2
  215. package/templates/current/examples/src/router/dynamicRoutes.ts +19 -4
  216. package/templates/current/examples/src/router/paths.spec.ts +50 -0
  217. package/templates/current/examples/src/router/paths.ts +21 -0
  218. package/templates/current/examples/src/router/queryEncryption.spec.ts +30 -4
  219. package/templates/current/examples/src/router/queryEncryption.ts +48 -44
  220. package/templates/current/examples/src/router/staticRoutes.ts +4 -4
  221. package/templates/current/examples/src/router/useRouteQueryDefaults.spec.ts +2 -1
  222. package/templates/current/examples/src/router/useRouteQueryDefaults.types.ts +2 -1
  223. package/templates/current/examples/src/templates/og-table.ts +79 -37
  224. package/templates/current/examples/src/templates/templates.spec.ts +19 -0
  225. package/templates/current/examples/tsconfig.json +0 -1
  226. package/templates/current/examples/vite.build-policy.ts +161 -289
  227. package/templates/current/examples/vite.config.ts +34 -5
  228. package/templates/current/clean/scripts/check-admin-bundle.mjs +0 -214
  229. package/templates/current/clean/scripts/performance/baselines/bundle-gzip-v2-pre-entry-unification.json +0 -38
  230. package/templates/current/clean/src/locales/en-US.ts +0 -55
  231. package/templates/current/clean/src/locales/zh-CN.ts +0 -55
  232. package/templates/current/clean/src/locales/zh-TW.ts +0 -44
  233. package/templates/current/examples/scripts/check-admin-bundle.mjs +0 -214
  234. package/templates/current/examples/scripts/performance/baselines/bundle-gzip-v2-pre-entry-unification.json +0 -38
  235. package/templates/current/examples/src/locales/en-US.ts +0 -55
  236. package/templates/current/examples/src/locales/zh-CN.ts +0 -55
  237. package/templates/current/examples/src/locales/zh-TW.ts +0 -44
@@ -1,17 +1,16 @@
1
1
  import type { RouterAdapter } from '@origen-ui/router'
2
2
  import type { BackendMenuNode } from '../app/menuTypes'
3
- import {
4
- createAccessPolicy,
5
- createRouterAdapter,
6
- createWebStorageAdapter,
7
- } from '@origen-ui/router'
3
+ import { createAccessPolicy, createRouterAdapter, createWebStorageAdapter } from '@origen-ui/router'
8
4
  import { createWebHistory } from 'vue-router'
9
- import { fetchMenus } from '../app/menu'
5
+ import { embeddedMode, reportMenusToHost } from '../app/embedded'
6
+ import { loadMenusFromHost } from '../app/menu'
10
7
  import { appRuntimeConfig } from '../app/runtimeConfig'
11
8
  import { useAuthStore } from '../features/auth/store/auth'
12
9
  import { pinia } from '../store'
13
10
  import { hasPermission } from '../utils/permissions'
14
- import { componentRegistry, routeTransformer } from './dynamicRoutes'
11
+ import { componentRegistry } from './componentRegistry'
12
+ import { routeTransformer } from './dynamicRoutes'
13
+ import { NOT_FOUND_ROUTE_NAME } from './paths'
15
14
  import { staticRoutes } from './staticRoutes'
16
15
 
17
16
  type QueryCodecs = Pick<
@@ -40,23 +39,33 @@ export function initializeRouting(
40
39
  if (routing) return routing
41
40
  const workspace = appRuntimeConfig.router.workspace
42
41
  routing = createRouterAdapter<BackendMenuNode>({
43
- history: createWebHistory(),
42
+ history: createWebHistory(import.meta.env.BASE_URL),
44
43
  staticRoutes,
45
44
  ...queryCodecs,
46
45
  ...titleOptions,
46
+ // 嵌入模式:document.title 归主壳所有,子应用路由不再写入
47
+ syncDocumentTitle: !embeddedMode,
47
48
  access: createAccessPolicy({
48
49
  getSession: () => useAuthStore(pinia).session,
49
- canAccess: (to, session) => hasPermission(session, to.meta.adapter?.permissions ?? []),
50
+ canAccess: (_to, session, meta) => hasPermission(session, meta.permissions ?? []),
50
51
  loginRoute: { name: 'login' },
51
52
  forbiddenRoute: { name: '403' },
52
53
  }),
53
54
  dynamic: {
54
- load: fetchMenus,
55
+ // 嵌入模式:菜单装载完成后向主壳上报(sub-app-menu);独立模式不触发。
56
+ // 上报在 embedded.ts 内 try/catch 兜底,装载主流程不受影响。
57
+ load: async () => {
58
+ // 嵌入模式:菜单装载完成后向主壳上报(sub-app-menu);独立模式不触发。
59
+ // loadMenusFromHost 内部处理主壳未下发时的 fetchMenus 回落。
60
+ const menus = await loadMenusFromHost()
61
+ if (embeddedMode) reportMenusToHost(menus)
62
+ return menus
63
+ },
55
64
  transform: routeTransformer,
56
65
  resolveComponent: (key) => componentRegistry.get(key),
57
66
  },
58
67
  fallbacks: {
59
- notFound: { name: '404' },
68
+ notFound: { name: NOT_FOUND_ROUTE_NAME },
60
69
  error: { name: '500' },
61
70
  },
62
71
  workspace: {
@@ -0,0 +1,18 @@
1
+ /**
2
+ * componentKey → 页面组件 name 映射,用于生成 KeepAlive 缓存名单。
3
+ *
4
+ * KeepAlive 的 `:include` 匹配的是页面组件渲染后的 name(SFC `defineOptions({ name })`),
5
+ * 因此这里的值必须与各页面组件的 defineOptions name 完全一致,否则缓存失效。
6
+ * 修改组件 name 时必须同步更新此表,并保持 dynamicRoutes.spec.ts 断言一致。
7
+ */
8
+ export const componentCacheNames: Readonly<Record<string, string>> = {
9
+ 'dashboard': 'DashboardPage',
10
+ 'profile': 'ProfilePage',
11
+ 'user-list': 'UserListPage',
12
+ 'role-list': 'RoleListPage',
13
+ 'menu-list': 'MenuListPage',
14
+ 'audit-list': 'AuditListPage',
15
+ 'empty': 'EmptyPage',
16
+ }
17
+
18
+ export const fallbackComponentKey = 'empty'
@@ -30,24 +30,7 @@ function withComponentStyles(key: string, loadComponent: ComponentLoader): Compo
30
30
  }
31
31
  }
32
32
 
33
- /**
34
- * componentKey → 页面组件 name 映射,用于生成 KeepAlive 缓存名单。
35
- *
36
- * KeepAlive 的 `:include` 匹配的是页面组件渲染后的 name(SFC `defineOptions({ name })`),
37
- * 因此这里的值必须与各页面组件的 defineOptions name 完全一致,否则缓存失效。
38
- * 修改组件 name 时必须同步更新此表,并保持 dynamicRoutes.spec.ts 断言一致。
39
- */
40
- export const componentCacheNames: Readonly<Record<string, string>> = {
41
- 'dashboard': 'DashboardPage',
42
- 'profile': 'ProfilePage',
43
- 'user-list': 'UserListPage',
44
- 'role-list': 'RoleListPage',
45
- 'menu-list': 'MenuListPage',
46
- 'audit-list': 'AuditListPage',
47
- 'empty': 'EmptyPage',
48
- }
49
-
50
- export const fallbackComponentKey = 'empty'
33
+ export { componentCacheNames, fallbackComponentKey } from './componentCache'
51
34
 
52
35
  export const componentRegistry: Map<string, ComponentLoader> = new Map([
53
36
  [
@@ -0,0 +1,101 @@
1
+ import type { BackendMenuNode } from '../app/menuTypes'
2
+ import { createRouterAdapter } from '@origen-ui/router'
3
+ import { describe, expect, it } from 'vitest'
4
+ import { defineComponent } from 'vue'
5
+ import { createMemoryHistory } from 'vue-router'
6
+ import { replaceDynamicMenusWithAccessibleFallback } from './dynamicMenuRefresh'
7
+ import { routeTransformer } from './dynamicRoutes'
8
+
9
+ const View = defineComponent({ template: '<div />' })
10
+
11
+ const staticRoutes = [
12
+ { name: 'login', path: '/login', component: View, meta: { adapter: { public: true, menu: { hidden: true } } } },
13
+ { name: '404', path: '/404', component: View, meta: { adapter: { public: true, menu: { hidden: true } } } },
14
+ { name: '500', path: '/500', component: View, meta: { adapter: { public: true, menu: { hidden: true } } } },
15
+ ]
16
+
17
+ function createAdaptersWithMenus(menus: readonly BackendMenuNode[]) {
18
+ let current = menus
19
+ const adapter = createRouterAdapter<BackendMenuNode>({
20
+ history: createMemoryHistory(),
21
+ staticRoutes,
22
+ dynamic: {
23
+ load: async () => current,
24
+ transform: routeTransformer,
25
+ resolveComponent: () => () => Promise.resolve(View),
26
+ },
27
+ fallbacks: { notFound: { name: '404' }, error: { name: '500' } },
28
+ })
29
+ return {
30
+ adapter,
31
+ setMenus(next: readonly BackendMenuNode[]) {
32
+ current = [...next]
33
+ },
34
+ }
35
+ }
36
+
37
+ describe('replaceDynamicMenusWithAccessibleFallback', () => {
38
+ it('falls back to the first reachable menu entry when home is revoked', async () => {
39
+ const { adapter, setMenus } = createAdaptersWithMenus([
40
+ { code: 'dashboard', path: '/dashboard', title: 'Dashboard' },
41
+ { code: 'reports', path: '/reports', title: 'Reports' },
42
+ ])
43
+ await adapter.router.push('/reports')
44
+ await adapter.router.isReady()
45
+ expect(adapter.router.currentRoute.value.path).toBe('/reports')
46
+
47
+ // 主页与当前页同时被裁撤:回落到刷新后菜单里首个真实可达入口。
48
+ setMenus([{ code: 'audit', path: '/audit', title: 'Audit' }])
49
+ await replaceDynamicMenusWithAccessibleFallback(adapter)
50
+
51
+ expect(adapter.router.currentRoute.value.path).toBe('/audit')
52
+ })
53
+
54
+ it('lands on home when the current route disappears but home remains', async () => {
55
+ const { adapter, setMenus } = createAdaptersWithMenus([
56
+ { code: 'dashboard', path: '/dashboard', title: 'Dashboard' },
57
+ { code: 'reports', path: '/reports', title: 'Reports' },
58
+ ])
59
+ await adapter.router.push('/reports')
60
+ await adapter.router.isReady()
61
+
62
+ setMenus([{ code: 'dashboard', path: '/dashboard', title: 'Dashboard' }])
63
+ await replaceDynamicMenusWithAccessibleFallback(adapter)
64
+
65
+ expect(adapter.router.currentRoute.value.path).toBe('/dashboard')
66
+ })
67
+
68
+ it('skips a directory whose redirect target was revoked and lands on a real entry', async () => {
69
+ const { adapter, setMenus } = createAdaptersWithMenus([
70
+ { code: 'system', path: '/system', title: 'System', redirect: '/dashboard' },
71
+ { code: 'reports', path: '/reports', title: 'Reports' },
72
+ ])
73
+ await adapter.router.push('/reports')
74
+ await adapter.router.isReady()
75
+
76
+ // 主页被裁撤后,首个菜单项(目录)的 redirect 仍指向 /dashboard——
77
+ // 可达性校验必须跳过它,否则会再次落回 404。
78
+ setMenus([
79
+ { code: 'system', path: '/system', title: 'System', redirect: '/dashboard' },
80
+ { code: 'audit', path: '/audit', title: 'Audit' },
81
+ ])
82
+ await replaceDynamicMenusWithAccessibleFallback(adapter)
83
+
84
+ expect(adapter.router.currentRoute.value.path).toBe('/audit')
85
+ })
86
+
87
+ it('does not redirect when the session is already on the not-found page', async () => {
88
+ const { adapter, setMenus } = createAdaptersWithMenus([
89
+ { code: 'dashboard', path: '/dashboard', title: 'Dashboard' },
90
+ { code: 'reports', path: '/reports', title: 'Reports' },
91
+ ])
92
+ await adapter.router.push('/reports')
93
+ await adapter.router.isReady()
94
+ await adapter.router.push('/404')
95
+
96
+ setMenus([{ code: 'audit', path: '/audit', title: 'Audit' }])
97
+ await replaceDynamicMenusWithAccessibleFallback(adapter)
98
+
99
+ expect(adapter.router.currentRoute.value.name).toBe('404')
100
+ })
101
+ })
@@ -0,0 +1,24 @@
1
+ import type { RouterAdapter } from '@origen-ui/router'
2
+ import type { RouteLocationRaw } from 'vue-router'
3
+ import type { BackendMenuNode } from '../app/menuTypes'
4
+ import { isUnmatchedRoute } from '@origen-ui/router'
5
+ import { firstMenuTarget, HOME_ROUTE, NOT_FOUND_ROUTE_NAME } from './paths'
6
+
7
+ function isReachableTarget(routing: RouterAdapter<BackendMenuNode>, to: RouteLocationRaw): boolean {
8
+ const resolved = routing.router.resolve(to)
9
+ return resolved.matched.length > 0 && !isUnmatchedRoute(resolved)
10
+ }
11
+
12
+ /**
13
+ * 替换动态菜单并在当前路由失效时回落:优先主页;主页已被裁撤时取
14
+ * 刷新后菜单树的首个真实可达入口,而不是把会话滞留在 404 页。
15
+ */
16
+ export async function replaceDynamicMenusWithAccessibleFallback(
17
+ routing: RouterAdapter<BackendMenuNode>,
18
+ ): Promise<void> {
19
+ const wasOnNotFound = routing.router.currentRoute.value.name === NOT_FOUND_ROUTE_NAME
20
+ await routing.replaceDynamicRoutes(undefined, { fallback: HOME_ROUTE })
21
+ if (wasOnNotFound || routing.router.currentRoute.value.name !== NOT_FOUND_ROUTE_NAME) return
22
+ const fallback = firstMenuTarget(routing.menu.value, (to) => isReachableTarget(routing, to))
23
+ if (fallback) await routing.router.replace(fallback)
24
+ }
@@ -1,6 +1,7 @@
1
1
  import { describe, expect, it } from 'vitest'
2
- import { componentCacheNames } from './componentRegistry'
3
- import { componentRegistry, routeTransformer } from './dynamicRoutes'
2
+ import { componentCacheNames } from './componentCache'
3
+ import { componentRegistry } from './componentRegistry'
4
+ import { routeTransformer } from './dynamicRoutes'
4
5
  import { staticRoutes } from './staticRoutes'
5
6
 
6
7
  describe('dynamic component registry', () => {
@@ -18,6 +19,23 @@ describe('dynamic component registry', () => {
18
19
  expect(componentRegistry.has('empty')).toBe(true)
19
20
  })
20
21
 
22
+ it('copies titleKey to route and breadcrumb metadata', () => {
23
+ const [route] = routeTransformer([
24
+ {
25
+ code: 'dashboard',
26
+ path: '/dashboard',
27
+ title: 'Dashboard',
28
+ titleKey: 'menu.dashboard',
29
+ },
30
+ ])
31
+
32
+ expect(route?.meta).toMatchObject({
33
+ title: 'Dashboard',
34
+ titleKey: 'menu.dashboard',
35
+ breadcrumb: { label: 'Dashboard', labelKey: 'menu.dashboard' },
36
+ })
37
+ })
38
+
21
39
  it('keeps duplicate relative path segments under their nested parents', () => {
22
40
  const routes = routeTransformer([
23
41
  {
@@ -66,6 +84,23 @@ describe('dynamic component registry', () => {
66
84
  children: [{ path: 'users', children: [{ path: 'roles' }] }],
67
85
  })
68
86
  })
87
+
88
+ it('rejects flat parentPath entries whose parent route does not exist', () => {
89
+ expect(() =>
90
+ routeTransformer([
91
+ { code: 'orphan', path: 'orphan', parentPath: '/missing', title: 'Orphan' },
92
+ ]),
93
+ ).toThrow('Unresolved dynamic route parent path: /missing')
94
+ })
95
+
96
+ it('rejects duplicate route codes with an explicit error', () => {
97
+ expect(() =>
98
+ routeTransformer([
99
+ { code: 'dup', path: '/first', title: 'First' },
100
+ { code: 'dup', path: '/second', title: 'Second' },
101
+ ]),
102
+ ).toThrow('Duplicate dynamic route code: dup')
103
+ })
69
104
  })
70
105
 
71
106
  describe('keep-alive cache names', () => {
@@ -1,8 +1,6 @@
1
1
  import type { RouteSourceNode } from '@origen-ui/router'
2
2
  import type { BackendMenuNode } from '../app/menuTypes'
3
- import { componentCacheNames, fallbackComponentKey } from './componentRegistry'
4
-
5
- export { componentRegistry } from './componentRegistry'
3
+ import { componentCacheNames, fallbackComponentKey } from './componentCache'
6
4
 
7
5
  function routeName(source: BackendMenuNode, used: Set<string>): string {
8
6
  const base = `dyn-${source.code.replace(/[^\w-]+/g, '-').replace(/^-|-$/g, '') || 'route'}`
@@ -29,10 +27,14 @@ function routeMeta(source: BackendMenuNode, hasChildren: boolean): RouteSourceNo
29
27
  : undefined
30
28
  return {
31
29
  title: source.title,
30
+ ...(source.titleKey ? { titleKey: source.titleKey } : {}),
32
31
  icon: source.icon,
33
32
  permissions: source.permissions,
34
33
  menu: { hidden: source.hidden === true || source.menu === false, order: source.order },
35
- breadcrumb: { label: source.title },
34
+ breadcrumb: {
35
+ label: source.title,
36
+ ...(source.titleKey ? { labelKey: source.titleKey } : {}),
37
+ },
36
38
  tab: { hidden: source.tabHidden === true || hasChildren },
37
39
  cache,
38
40
  }
@@ -124,6 +126,8 @@ function buildTree(nodes: readonly BackendMenuNode[]): readonly BackendMenuNode[
124
126
  ])
125
127
  candidates.delete(item)
126
128
  if (candidates.size > 1) throw new Error(`Ambiguous dynamic route parent path: ${parentPath}`)
129
+ if (candidates.size === 0)
130
+ throw new Error(`Unresolved dynamic route parent path: ${parentPath}`)
127
131
  }
128
132
 
129
133
  for (const item of entries) {
@@ -140,7 +144,18 @@ function buildTree(nodes: readonly BackendMenuNode[]): readonly BackendMenuNode[
140
144
  return roots.map(materialize)
141
145
  }
142
146
 
147
+ function assertUniqueCodes(nodes: readonly BackendMenuNode[], seen = new Set<string>()): void {
148
+ for (const node of nodes) {
149
+ if (seen.has(node.code)) throw new Error(`Duplicate dynamic route code: ${node.code}`)
150
+ seen.add(node.code)
151
+ assertUniqueCodes(node.children ?? [], seen)
152
+ }
153
+ }
154
+
143
155
  export function routeTransformer(sources: readonly BackendMenuNode[]): readonly RouteSourceNode[] {
156
+ // buildTree 按 code 去重会静默丢弃后到的重复节点,连同其子树一起消失;
157
+ // 在此显式失败,给出可定位的数据错误。
158
+ assertUniqueCodes(sources)
144
159
  const usedNames = new Set<string>()
145
160
  const tree = buildTree(sources)
146
161
 
@@ -0,0 +1,50 @@
1
+ import type { RouteMenuItem } from '@origen-ui/router'
2
+ import { describe, expect, it } from 'vitest'
3
+ import { firstMenuTarget, HOME_ROUTE, NOT_FOUND_ROUTE_NAME } from './paths'
4
+ import { staticRoutes } from './staticRoutes'
5
+
6
+ describe('router paths', () => {
7
+ it('keeps the root static redirect aligned with HOME_ROUTE', () => {
8
+ const root = staticRoutes.find((route) => route.name === 'root')
9
+ expect(root?.redirect).toBe(HOME_ROUTE)
10
+ })
11
+
12
+ it('pins the not-found fallback to the static 404 route', () => {
13
+ expect(staticRoutes.some((route) => route.name === NOT_FOUND_ROUTE_NAME)).toBe(true)
14
+ })
15
+
16
+ it('resolves the first navigable menu target depth-first', () => {
17
+ const items = [
18
+ { id: 'locked', label: 'Locked', children: [] },
19
+ {
20
+ id: 'section',
21
+ label: 'Section',
22
+ to: { name: 'section' },
23
+ children: [{ id: 'inner', label: 'Inner', to: { name: 'inner' }, children: [] }],
24
+ },
25
+ ] as readonly RouteMenuItem[]
26
+
27
+ expect(firstMenuTarget(items)).toEqual({ name: 'section' })
28
+ })
29
+
30
+ it('returns undefined when no menu item is navigable', () => {
31
+ const items = [{ id: 'empty', label: 'Empty', children: [] }] as readonly RouteMenuItem[]
32
+
33
+ expect(firstMenuTarget(items)).toBeUndefined()
34
+ })
35
+
36
+ it('skips entries rejected by the reachability predicate', () => {
37
+ const items = [
38
+ { id: 'gone', label: 'Gone', to: { path: '/gone' }, children: [] },
39
+ { id: 'live', label: 'Live', to: { name: 'live' }, children: [] },
40
+ ] as readonly RouteMenuItem[]
41
+
42
+ expect(
43
+ firstMenuTarget(items, (to) => {
44
+ const path = typeof to === 'string' ? to : to.path
45
+ return path !== '/gone'
46
+ }),
47
+ ).toEqual({ name: 'live' })
48
+ expect(firstMenuTarget(items, () => false)).toBeUndefined()
49
+ })
50
+ })
@@ -0,0 +1,21 @@
1
+ import type { RouteMenuItem } from '@origen-ui/router'
2
+ import type { RouteLocationRaw } from 'vue-router'
3
+
4
+ /** 授权后的默认首页路由;与 staticRoutes 根路由 redirect 由 paths.spec 钉住一致。 */
5
+ export const HOME_ROUTE = '/dashboard'
6
+
7
+ /** staticRoutes 中 404 兜底页的路由名。 */
8
+ export const NOT_FOUND_ROUTE_NAME = '404'
9
+
10
+ /** 深度优先取菜单树中首个可导航入口;isReachable 用于剔除重定向到已裁撤路由的目录项。 */
11
+ export function firstMenuTarget(
12
+ items: readonly RouteMenuItem[],
13
+ isReachable: (to: RouteLocationRaw) => boolean = () => true,
14
+ ): RouteLocationRaw | undefined {
15
+ for (const item of items) {
16
+ if (item.to && isReachable(item.to)) return item.to
17
+ const child = firstMenuTarget(item.children, isReachable)
18
+ if (child) return child
19
+ }
20
+ return undefined
21
+ }
@@ -1,7 +1,9 @@
1
+ import type { RouteRecordRaw } from 'vue-router'
2
+ import { gcm } from '@noble/ciphers/aes.js'
1
3
  import { createRouterAdapter } from '@origen-ui/router'
2
4
  import { describe, expect, it } from 'vitest'
3
5
  import { defineComponent } from 'vue'
4
- import { createMemoryHistory, type RouteRecordRaw } from 'vue-router'
6
+ import { createMemoryHistory } from 'vue-router'
5
7
  import { createRouteQueryEncryption } from './queryEncryption'
6
8
 
7
9
  const key = '0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef'
@@ -11,6 +13,19 @@ const staticRoutes: RouteRecordRaw[] = [
11
13
  { name: '500', path: '/500', component: View, meta: { adapter: { public: true } } },
12
14
  ]
13
15
 
16
+ function toUrlSafeBase64(value: Uint8Array): string {
17
+ let binary = ''
18
+ for (const byte of value) binary += String.fromCharCode(byte)
19
+ return btoa(binary).replaceAll('+', '-').replaceAll('/', '_').replace(/=+$/u, '')
20
+ }
21
+
22
+ function encodeV2(plaintext: string): string {
23
+ const nonce = new Uint8Array(12)
24
+ const encryptionKey = Uint8Array.from(key.match(/../gu)!.map((pair) => Number.parseInt(pair, 16)))
25
+ const ciphertextAndTag = gcm(encryptionKey, nonce).encrypt(new TextEncoder().encode(plaintext))
26
+ return `_q=v2.${toUrlSafeBase64(nonce)}.${toUrlSafeBase64(ciphertextAndTag)}`
27
+ }
28
+
14
29
  describe('route query encryption', () => {
15
30
  it('is disabled without an explicit enablement flag', () => {
16
31
  expect(createRouteQueryEncryption({ key })).toBeUndefined()
@@ -23,13 +38,13 @@ describe('route query encryption', () => {
23
38
  )
24
39
  })
25
40
 
26
- it('round-trips every query value through one encrypted parameter', () => {
41
+ it('uses a random AES-256-GCM nonce and round-trips Unicode, arrays, and null', () => {
27
42
  const codec = createRouteQueryEncryption({ enabled: true, key })!
28
43
  const query = { count: 2, filter: 'active', ids: ['a', 'b'], nullable: null, unicode: '绠$悊' }
29
44
  const first = codec.stringifyQuery(query)
30
45
  const second = codec.stringifyQuery(query)
31
46
 
32
- expect(first).toMatch(/^_q=v1\./)
47
+ expect(first).toMatch(/^_q=v2\.[\w-]{16}\.[\w-]+$/)
33
48
  expect(first).not.toContain('active')
34
49
  expect(first).not.toBe(second)
35
50
  expect(codec.parseQuery(first)).toEqual({
@@ -41,7 +56,7 @@ describe('route query encryption', () => {
41
56
  })
42
57
  })
43
58
 
44
- it('returns an empty query for tampered ciphertext and legacy plaintext parameters', () => {
59
+ it('only accepts one well-formed v2 AES-GCM query payload', () => {
45
60
  const codec = createRouteQueryEncryption({ enabled: true, key })!
46
61
  const encoded = codec.stringifyQuery({ redirect: '/dashboard' })
47
62
  const tampered = `${encoded.slice(0, -1)}${encoded.endsWith('a') ? 'b' : 'a'}`
@@ -49,6 +64,17 @@ describe('route query encryption', () => {
49
64
  expect(codec.parseQuery(tampered)).toEqual({})
50
65
  expect(codec.parseQuery('redirect=%2Fdashboard')).toEqual({})
51
66
  expect(codec.parseQuery('_q=invalid')).toEqual({})
67
+ expect(codec.parseQuery('_q=v1.nonce.ciphertext.mac')).toEqual({})
68
+ expect(codec.parseQuery('_q=v2.invalid.ciphertext')).toEqual({})
69
+ expect(codec.parseQuery('_q=v2.aaaaaaaaaaaaaaaa.aaaaaaaaaaaaaaaa')).toEqual({})
70
+ expect(codec.parseQuery(`${encoded}&_q=${encodeURIComponent('v2.invalid.ciphertext')}`)).toEqual({})
71
+ })
72
+
73
+ it('rejects authenticated JSON values that are not a route query shape', () => {
74
+ const codec = createRouteQueryEncryption({ enabled: true, key })!
75
+
76
+ expect(codec.parseQuery(encodeV2('["not-an-object"]'))).toEqual({})
77
+ expect(codec.parseQuery(encodeV2('{"nested":{"value":"not-a-query-value"}}'))).toEqual({})
52
78
  })
53
79
 
54
80
  it('preserves encrypted query semantics while a dynamic route loads', async () => {
@@ -1,13 +1,15 @@
1
1
  import type { LocationQuery, LocationQueryRaw } from 'vue-router'
2
- import AES from 'crypto-js/aes'
3
- import CryptoJS from 'crypto-js/core'
4
- import Base64 from 'crypto-js/enc-base64'
5
- import HmacSHA256 from 'crypto-js/hmac-sha256'
2
+ import { gcm } from '@noble/ciphers/aes.js'
3
+ import { randomBytes } from '@noble/ciphers/utils.js'
6
4
 
7
5
  const QUERY_PARAMETER = '_q'
8
- const VERSION = 'v1'
6
+ const VERSION = 'v2'
9
7
  const HEX_KEY = /^[\dA-F]{64}$/i
10
8
  const URL_SAFE_BASE64 = /^[\w-]+$/
9
+ const NONCE_LENGTH = 12
10
+ const TAG_LENGTH = 16
11
+ const encoder = new TextEncoder()
12
+ const decoder = new TextDecoder('utf-8', { fatal: true })
11
13
 
12
14
  type QueryValue = string | null | string[] | (string | null)[]
13
15
  type EncodedQuery = Record<string, QueryValue>
@@ -22,22 +24,24 @@ export interface RouteQueryEncryption {
22
24
  stringifyQuery: (query: LocationQueryRaw | undefined) => string
23
25
  }
24
26
 
25
- function toUrlSafeBase64(value: CryptoJS.lib.WordArray): string {
26
- return Base64.stringify(value).replaceAll('+', '-').replaceAll('/', '_').replace(/=+$/u, '')
27
+ function toUrlSafeBase64(value: Uint8Array): string {
28
+ let binary = ''
29
+ for (const byte of value) binary += String.fromCharCode(byte)
30
+ return btoa(binary).replaceAll('+', '-').replaceAll('/', '_').replace(/=+$/u, '')
27
31
  }
28
32
 
29
- function fromUrlSafeBase64(value: string): CryptoJS.lib.WordArray | undefined {
30
- if (!URL_SAFE_BASE64.test(value)) return undefined
31
- const padded = `${value.replaceAll('-', '+').replaceAll('_', '/')}${'='.repeat((4 - (value.length % 4)) % 4)}`
32
- return Base64.parse(padded)
33
- }
34
-
35
- function equalsMac(left: string, right: string): boolean {
36
- if (left.length !== right.length) return false
37
- let difference = 0
38
- for (let index = 0; index < left.length; index += 1)
39
- difference |= left.charCodeAt(index) ^ right.charCodeAt(index)
40
- return difference === 0
33
+ function fromUrlSafeBase64(value: string): Uint8Array | undefined {
34
+ if (!URL_SAFE_BASE64.test(value) || value.length % 4 === 1) return undefined
35
+ try {
36
+ const padded = `${value.replaceAll('-', '+').replaceAll('_', '/')}${'='.repeat(
37
+ (4 - (value.length % 4)) % 4,
38
+ )}`
39
+ const binary = atob(padded)
40
+ const bytes = Uint8Array.from(binary, (character) => character.charCodeAt(0))
41
+ return toUrlSafeBase64(bytes) === value ? bytes : undefined
42
+ } catch {
43
+ return undefined
44
+ }
41
45
  }
42
46
 
43
47
  function normalizeQuery(query: LocationQueryRaw | undefined): EncodedQuery {
@@ -72,6 +76,10 @@ function readCiphertext(search: string): string | undefined {
72
76
  return values.length === 1 ? values[0] : undefined
73
77
  }
74
78
 
79
+ function keyFromHex(value: string): Uint8Array {
80
+ return Uint8Array.from(value.match(/../gu)!.map((pair) => Number.parseInt(pair, 16)))
81
+ }
82
+
75
83
  export function createRouteQueryEncryption(
76
84
  options: RouteQueryEncryptionOptions = {},
77
85
  ): RouteQueryEncryption | undefined {
@@ -82,47 +90,43 @@ export function createRouteQueryEncryption(
82
90
  )
83
91
  }
84
92
 
85
- const encryptionKey = CryptoJS.enc.Hex.parse(options.key)
86
- const authenticationKey = HmacSHA256('route-query-authentication', encryptionKey)
87
-
88
- function mac(value: string): string {
89
- return HmacSHA256(value, authenticationKey).toString(CryptoJS.enc.Hex)
90
- }
93
+ const encryptionKey = keyFromHex(options.key)
91
94
 
92
95
  return {
93
96
  stringifyQuery(query) {
94
97
  const normalized = normalizeQuery(query)
95
98
  if (Object.keys(normalized).length === 0) return ''
96
- const iv = CryptoJS.lib.WordArray.random(16)
97
- const ciphertext = AES.encrypt(JSON.stringify(normalized), encryptionKey, { iv }).ciphertext
98
- const payload = `${VERSION}.${toUrlSafeBase64(iv)}.${toUrlSafeBase64(ciphertext)}`
99
- return `${QUERY_PARAMETER}=${encodeURIComponent(`${payload}.${mac(payload)}`)}`
99
+ const nonce = randomBytes(NONCE_LENGTH)
100
+ const ciphertextAndTag = gcm(encryptionKey, nonce).encrypt(
101
+ encoder.encode(JSON.stringify(normalized)),
102
+ )
103
+ const payload = `${VERSION}.${toUrlSafeBase64(nonce)}.${toUrlSafeBase64(ciphertextAndTag)}`
104
+ return `${QUERY_PARAMETER}=${encodeURIComponent(payload)}`
100
105
  },
101
106
  parseQuery(search) {
102
107
  const value = readCiphertext(search)
103
108
  if (!value) return {}
104
109
  try {
105
- const [version, ivValue, ciphertextValue, suppliedMac, ...extra] = value.split('.')
110
+ const [version, nonceValue, ciphertextValue, ...extra] = value.split('.')
106
111
  if (
107
112
  version !== VERSION ||
108
- !ivValue ||
113
+ !nonceValue ||
109
114
  !ciphertextValue ||
110
- !suppliedMac ||
111
- extra.length > 0 ||
112
- !/^[\dA-F]{64}$/i.test(suppliedMac)
115
+ extra.length > 0
116
+ ) {
117
+ return {}
118
+ }
119
+ const nonce = fromUrlSafeBase64(nonceValue)
120
+ const ciphertextAndTag = fromUrlSafeBase64(ciphertextValue)
121
+ if (
122
+ !nonce ||
123
+ nonce.length !== NONCE_LENGTH ||
124
+ !ciphertextAndTag ||
125
+ ciphertextAndTag.length <= TAG_LENGTH
113
126
  ) {
114
127
  return {}
115
128
  }
116
- const payload = `${version}.${ivValue}.${ciphertextValue}`
117
- if (!equalsMac(suppliedMac, mac(payload))) return {}
118
- const iv = fromUrlSafeBase64(ivValue)
119
- const ciphertext = fromUrlSafeBase64(ciphertextValue)
120
- if (!iv || !ciphertext) return {}
121
- const plaintext = AES.decrypt(
122
- CryptoJS.lib.CipherParams.create({ ciphertext }),
123
- encryptionKey,
124
- { iv },
125
- ).toString(CryptoJS.enc.Utf8)
129
+ const plaintext = decoder.decode(gcm(encryptionKey, nonce).decrypt(ciphertextAndTag))
126
130
  const query: unknown = JSON.parse(plaintext)
127
131
  return isEncodedQuery(query) ? (query as LocationQuery) : {}
128
132
  } catch {
@@ -37,7 +37,7 @@ export const staticRoutes: RouteRecordRaw[] = [
37
37
  titleKey: 'menu.profile',
38
38
  icon: 'profile',
39
39
  menu: { order: 20 },
40
- breadcrumb: { label: '个人资料' },
40
+ breadcrumb: { labelKey: 'menu.profile' },
41
41
  cache: { enabled: true, name: 'ProfilePage' },
42
42
  },
43
43
  },
@@ -46,18 +46,18 @@ export const staticRoutes: RouteRecordRaw[] = [
46
46
  path: '/403',
47
47
  name: '403',
48
48
  component: Forbidden,
49
- meta: { adapter: { titleKey: 'errors.forbidden', ...hiddenPublic } },
49
+ meta: { adapter: { titleKey: 'errors.forbidden.title', ...hiddenPublic } },
50
50
  },
51
51
  {
52
52
  path: '/404',
53
53
  name: '404',
54
54
  component: NotFound,
55
- meta: { adapter: { titleKey: 'errors.notFound', ...hiddenPublic } },
55
+ meta: { adapter: { titleKey: 'errors.notFound.title', ...hiddenPublic } },
56
56
  },
57
57
  {
58
58
  path: '/500',
59
59
  name: '500',
60
60
  component: ServerError,
61
- meta: { adapter: { titleKey: 'errors.server', ...hiddenPublic } },
61
+ meta: { adapter: { titleKey: 'errors.serverError.title', ...hiddenPublic } },
62
62
  },
63
63
  ]