@origen-ui/admin-cli 1.1.0 → 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 (159) hide show
  1. package/README.md +11 -7
  2. package/package.json +18 -2
  3. package/templates/current/clean/CHANGELOG.md +103 -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 +35 -21
  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 +1 -0
  24. package/templates/current/clean/src/api/generated/data-contracts.ts +2 -5
  25. package/templates/current/clean/src/api/generated/http-client.ts +28 -26
  26. 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
  27. 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
  28. 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
  29. 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 +8 -10
  30. 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 +11 -12
  31. 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 +8 -10
  32. 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 +8 -10
  33. package/templates/current/clean/src/api/generated//350/256/244/350/257/201.ts +3 -6
  34. package/templates/current/clean/src/app/errorMessage.spec.ts +76 -0
  35. package/templates/current/clean/src/app/errorMessage.ts +53 -0
  36. package/templates/current/clean/src/app/menu-host.spec.ts +77 -0
  37. package/templates/current/clean/src/app/menu.spec.ts +39 -0
  38. package/templates/current/clean/src/app/menu.ts +48 -12
  39. package/templates/current/clean/src/app/runtimeConfig.ts +1 -1
  40. package/templates/current/clean/src/demo-api.spec.ts +311 -0
  41. package/templates/current/clean/src/features/auth/pages/Login.vue +8 -2
  42. package/templates/current/clean/src/features/auth/store/auth.ts +6 -19
  43. package/templates/current/clean/src/features/errors/pages/Forbidden.vue +7 -2
  44. package/templates/current/clean/src/features/errors/pages/NotFound.vue +7 -2
  45. package/templates/current/clean/src/features/errors/pages/ServerError.vue +7 -2
  46. package/templates/current/clean/src/features/errors/pages/error-pages.spec.ts +11 -2
  47. package/templates/current/clean/src/features/system/access/api/admin-repository.ts +2 -6
  48. package/templates/current/clean/src/features/system/access/api/rbac.ts +19 -9
  49. package/templates/current/clean/src/features/system/access/composables/useAuthorizationPolling.spec.ts +134 -0
  50. package/templates/current/clean/src/features/system/access/composables/useAuthorizationPolling.ts +96 -0
  51. package/templates/current/clean/src/features/workspace/pages/Profile.vue +1 -1
  52. package/templates/current/clean/src/layouts/AdminLayout.vue +3 -0
  53. package/templates/current/clean/src/layouts/TopBar.vue +37 -6
  54. package/templates/current/clean/src/layouts/topBarMenuSearch.spec.ts +24 -1
  55. package/templates/current/clean/src/layouts/topBarMenuSearch.ts +10 -0
  56. package/templates/current/clean/src/locales/en-US.json +30 -3
  57. package/templates/current/clean/src/locales/en-US.runtime.ts +2 -0
  58. package/templates/current/clean/src/locales/index.ts +39 -26
  59. package/templates/current/clean/src/locales/zh-CN.json +30 -3
  60. package/templates/current/clean/src/locales/zh-CN.runtime.ts +2 -0
  61. package/templates/current/clean/src/locales/zh-TW.json +30 -3
  62. package/templates/current/clean/src/locales/zh-TW.runtime.ts +2 -0
  63. package/templates/current/clean/src/main.ts +23 -14
  64. package/templates/current/clean/src/router/bootstrap.ts +10 -16
  65. package/templates/current/clean/src/router/componentCache.ts +12 -0
  66. package/templates/current/clean/src/router/componentRegistry.ts +2 -13
  67. package/templates/current/clean/src/router/dynamicMenuRefresh.spec.ts +101 -0
  68. package/templates/current/clean/src/router/dynamicMenuRefresh.ts +24 -0
  69. package/templates/current/clean/src/router/dynamicRoutes.spec.ts +34 -4
  70. package/templates/current/clean/src/router/dynamicRoutes.ts +14 -3
  71. package/templates/current/clean/src/router/paths.spec.ts +50 -0
  72. package/templates/current/clean/src/router/paths.ts +21 -0
  73. package/templates/current/clean/src/router/staticRoutes.ts +6 -6
  74. package/templates/current/clean/src/templates/og-table.ts +2 -4
  75. package/templates/current/clean/tsconfig.json +0 -1
  76. package/templates/current/clean/vite.build-policy.ts +55 -8
  77. package/templates/current/clean/vite.config.ts +30 -0
  78. package/templates/current/examples/CHANGELOG.md +103 -0
  79. package/templates/current/examples/component-catalog.json +58 -17
  80. package/templates/current/examples/e2e/admin-shell.spec.ts +6 -0
  81. package/templates/current/examples/e2e/demo-shell.spec.ts +90 -46
  82. package/templates/current/examples/package.json +35 -21
  83. package/templates/current/examples/playwright.config.ts +1 -0
  84. package/templates/current/examples/playwright.demo.config.ts +1 -1
  85. package/templates/current/examples/scaffold.lock.json +2 -2
  86. package/templates/current/examples/scripts/clean-overlay/src/features/errors/pages/Forbidden.vue +7 -2
  87. package/templates/current/examples/scripts/clean-overlay/src/features/errors/pages/NotFound.vue +7 -2
  88. package/templates/current/examples/scripts/clean-overlay/src/features/errors/pages/ServerError.vue +7 -2
  89. package/templates/current/examples/scripts/clean-overlay/src/features/errors/pages/error-pages.spec.ts +11 -2
  90. package/templates/current/examples/scripts/clean-overlay/src/main.ts +23 -14
  91. package/templates/current/examples/scripts/clean-overlay/src/router/componentCache.ts +12 -0
  92. package/templates/current/examples/scripts/clean-overlay/src/router/componentRegistry.ts +2 -13
  93. package/templates/current/examples/scripts/clean-overlay/src/router/dynamicRoutes.spec.ts +34 -4
  94. package/templates/current/examples/scripts/clean-overlay/src/router/paths.ts +21 -0
  95. package/templates/current/examples/scripts/clean-overlay/src/router/staticRoutes.ts +6 -6
  96. package/templates/current/examples/scripts/demo-api.ts +84 -8
  97. package/templates/current/examples/scripts/generate-api.ts +87 -3
  98. package/templates/current/examples/scripts/vite-build-policy.spec.ts +1 -0
  99. package/templates/current/examples/src/api/generated/data-contracts.ts +2 -5
  100. package/templates/current/examples/src/api/generated/http-client.ts +28 -26
  101. 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
  102. 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
  103. 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
  104. 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 +8 -10
  105. 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 +11 -12
  106. 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 +8 -10
  107. 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 +8 -10
  108. package/templates/current/examples/src/api/generated//350/256/244/350/257/201.ts +3 -6
  109. package/templates/current/examples/src/app/errorMessage.spec.ts +76 -0
  110. package/templates/current/examples/src/app/errorMessage.ts +53 -0
  111. package/templates/current/examples/src/app/menu-host.spec.ts +77 -0
  112. package/templates/current/examples/src/app/menu.spec.ts +39 -0
  113. package/templates/current/examples/src/app/menu.ts +48 -12
  114. package/templates/current/examples/src/app/runtimeConfig.ts +1 -1
  115. package/templates/current/examples/src/demo-api.spec.ts +311 -0
  116. package/templates/current/examples/src/features/auth/pages/Login.vue +8 -2
  117. package/templates/current/examples/src/features/auth/store/auth.ts +6 -19
  118. package/templates/current/examples/src/features/errors/pages/Forbidden.vue +2 -1
  119. package/templates/current/examples/src/features/errors/pages/NotFound.vue +2 -1
  120. package/templates/current/examples/src/features/errors/pages/ServerError.vue +2 -1
  121. package/templates/current/examples/src/features/feature-composables.spec.ts +11 -1
  122. package/templates/current/examples/src/features/system/access/api/admin-repository.ts +2 -6
  123. package/templates/current/examples/src/features/system/access/api/rbac.ts +19 -9
  124. package/templates/current/examples/src/features/system/access/composables/useAuthorizationPolling.spec.ts +134 -0
  125. package/templates/current/examples/src/features/system/access/composables/useAuthorizationPolling.ts +96 -0
  126. package/templates/current/examples/src/features/system/menus/components/MenuEditorForm.vue +115 -28
  127. package/templates/current/examples/src/features/system/menus/composables/useMenuManagement.ts +162 -66
  128. package/templates/current/examples/src/features/system/menus/menuDraft.spec.ts +204 -0
  129. package/templates/current/examples/src/features/system/menus/menuDraft.ts +116 -0
  130. package/templates/current/examples/src/features/system/menus/menuTreePrecheck.spec.ts +268 -0
  131. package/templates/current/examples/src/features/system/menus/menuTreePrecheck.ts +317 -0
  132. package/templates/current/examples/src/features/system/roles/components/RoleEditorForm.vue +1 -0
  133. package/templates/current/examples/src/features/workspace/pages/Profile.vue +1 -1
  134. package/templates/current/examples/src/layouts/AdminLayout.vue +3 -0
  135. package/templates/current/examples/src/layouts/TopBar.vue +37 -6
  136. package/templates/current/examples/src/layouts/topBarMenuSearch.spec.ts +24 -1
  137. package/templates/current/examples/src/layouts/topBarMenuSearch.ts +10 -0
  138. package/templates/current/examples/src/locales/en-US.json +30 -3
  139. package/templates/current/examples/src/locales/en-US.runtime.ts +2 -0
  140. package/templates/current/examples/src/locales/index.ts +39 -26
  141. package/templates/current/examples/src/locales/zh-CN.json +30 -3
  142. package/templates/current/examples/src/locales/zh-CN.runtime.ts +2 -0
  143. package/templates/current/examples/src/locales/zh-TW.json +30 -3
  144. package/templates/current/examples/src/locales/zh-TW.runtime.ts +2 -0
  145. package/templates/current/examples/src/main.ts +24 -22
  146. package/templates/current/examples/src/router/bootstrap.ts +10 -16
  147. package/templates/current/examples/src/router/componentCache.ts +18 -0
  148. package/templates/current/examples/src/router/componentRegistry.ts +1 -18
  149. package/templates/current/examples/src/router/dynamicMenuRefresh.spec.ts +101 -0
  150. package/templates/current/examples/src/router/dynamicMenuRefresh.ts +24 -0
  151. package/templates/current/examples/src/router/dynamicRoutes.spec.ts +20 -2
  152. package/templates/current/examples/src/router/dynamicRoutes.ts +14 -3
  153. package/templates/current/examples/src/router/paths.spec.ts +50 -0
  154. package/templates/current/examples/src/router/paths.ts +21 -0
  155. package/templates/current/examples/src/router/staticRoutes.ts +3 -3
  156. package/templates/current/examples/src/templates/og-table.ts +2 -4
  157. package/templates/current/examples/tsconfig.json +0 -1
  158. package/templates/current/examples/vite.build-policy.ts +55 -8
  159. package/templates/current/examples/vite.config.ts +30 -0
@@ -5,21 +5,21 @@ import type {
5
5
  OgTableRequestContext,
6
6
  TableColumn,
7
7
  } from '@origen-ui/table'
8
- import type { AdminMessageKey } from '../../../../locales'
9
8
  import type { MenuKind, RbacMenu, RbacMenuDraft, RbacStatus } from '../../access/api/rbac'
10
9
  import type { AuthorizationRefresh } from '../../access/composables/useAuthorizationRefresh'
11
- import type { MenuEditorDraft } from '../components/MenuEditorForm.vue'
10
+ import type { MenuEditorDraft } from '../menuDraft'
11
+ import type { MenuTreeConflict, MenuTreeConflictKind } from '../menuTreePrecheck'
12
12
  import { useDialog } from '@origen-ui/dialog'
13
13
  import { ElMessage } from 'element-plus'
14
14
  import { computed } from 'vue'
15
15
  import { useI18n } from 'vue-i18n'
16
16
  import { formatAdminShortDateTime } from '../../../../app/localeFormat'
17
- import { getAdminI18n } from '../../../../locales'
18
17
  import { useAuthStore } from '../../../auth/store/auth'
19
18
  import {
20
19
  createMenu,
21
20
  getPageCapability,
22
21
  listMenus,
22
+ MENU_VERSION_CONFLICT_CODE,
23
23
  menuPageParentOptions,
24
24
  menuParentOptions,
25
25
  pageCatalogOptions,
@@ -29,6 +29,13 @@ import {
29
29
  updateMenu,
30
30
  } from '../../access/api/rbac'
31
31
  import { useAuthorizationRefresh } from '../../access/composables/useAuthorizationRefresh'
32
+ import { parseMenuDraft } from '../menuDraft'
33
+ import {
34
+ applyMenuDraftToRows,
35
+ collectMenuTreeConflicts,
36
+ fetchCompleteMenuPages,
37
+ filterIntroducedConflicts,
38
+ } from '../menuTreePrecheck'
32
39
 
33
40
  export interface MenuQuery {
34
41
  kind?: MenuKind
@@ -40,14 +47,6 @@ interface UseMenuManagementOptions {
40
47
  refreshAuthorization?: AuthorizationRefresh
41
48
  }
42
49
 
43
- function translate(key: string): string {
44
- const global = getAdminI18n()?.global as unknown as
45
- | { te?: (key: string) => boolean, t: (key: string) => string } |
46
- undefined
47
- if (!global) return key
48
- return global.te?.(key) ? global.t(key as AdminMessageKey) : key
49
- }
50
-
51
50
  function formatDate(value: unknown): string {
52
51
  return formatAdminShortDateTime(value)
53
52
  }
@@ -61,6 +60,7 @@ function createDraft(row?: RbacMenu): MenuEditorDraft {
61
60
  parentId: row?.parentId ?? '',
62
61
  kind: row?.kind ?? 'page',
63
62
  title: row?.title ?? '',
63
+ titleKey: row?.titleKey ?? '',
64
64
  code: row?.code ?? '',
65
65
  path: row?.path ?? '',
66
66
  componentKey: row?.componentKey ?? '',
@@ -71,53 +71,12 @@ function createDraft(row?: RbacMenu): MenuEditorDraft {
71
71
  visible: row?.visible ?? true,
72
72
  cache: row?.cache ?? true,
73
73
  tabHidden: row?.tabHidden ?? false,
74
+ redirect: row?.redirect ?? '',
74
75
  }
75
76
  }
76
77
 
77
- function parseDraft(value: Record<string, unknown> | undefined): RbacMenuDraft {
78
- const draft = value as Partial<MenuEditorDraft> | undefined
79
- const kind = draft?.kind
80
- const title = draft?.title?.trim()
81
- const code = draft?.code?.trim()
82
- const path = draft?.path?.trim()
83
- const parentId = draft?.parentId?.trim() || undefined
84
- const componentKey = draft?.componentKey?.trim() || undefined
85
- const permission = draft?.permission?.trim() || undefined
86
-
87
- if ((kind !== 'page' && kind !== 'directory' && kind !== 'button') || !title || !code)
88
- throw new Error(translate('system.menus.errors.formRequired'))
89
- if (!/^[a-z][a-z0-9-]{2,47}$/.test(code)) throw new Error(translate('system.menus.errors.codeInvalid'))
90
- if (kind === 'button' && (!parentId || !permission))
91
- throw new Error(translate('system.menus.errors.buttonRequiresPage'))
92
- if (kind !== 'button') {
93
- if (!path) throw new Error(translate('system.menus.errors.pathRequired'))
94
- if (parentId ? path.startsWith('/') : !path.startsWith('/')) {
95
- throw new Error(
96
- translate(parentId ? 'system.menus.errors.submenuRelativePath' : 'system.menus.errors.rootPathSlash'),
97
- )
98
- }
99
- }
100
- if (kind === 'page' && !getPageCapability(componentKey))
101
- throw new Error(translate('system.menus.errors.componentRequired'))
102
-
103
- return {
104
- parentId,
105
- kind,
106
- title,
107
- code,
108
- path: kind === 'button' ? undefined : path,
109
- componentKey: kind === 'page' ? componentKey : undefined,
110
- permission: kind === 'button' ? permission : undefined,
111
- icon: kind === 'button' ? undefined : draft?.icon?.trim() || undefined,
112
- order: Math.max(1, Math.floor(Number(draft?.order) || 1)),
113
- status: draft?.status === 'suspended' ? 'suspended' : 'active',
114
- visible: kind === 'button' ? false : draft?.visible !== false,
115
- cache: kind === 'button' ? false : draft?.cache !== false,
116
- tabHidden: kind === 'button' ? true : draft?.tabHidden === true,
117
- redirect: kind === 'directory' ? '/dashboard' : undefined,
118
- }
119
- }
120
-
78
+ // 表单 schema 已承载全部校验(必填/裁剪/跨字段斜杠规则/组件能力约束,
79
+ // 规则实现见 ../menuDraft.ts),这里只透传提交前的归一化结果。
121
80
  export function useMenuManagement(options: UseMenuManagementOptions) {
122
81
  const auth = useAuthStore()
123
82
  const dialog = useDialog()
@@ -148,14 +107,123 @@ export function useMenuManagement(options: UseMenuManagementOptions) {
148
107
  return pageCatalogOptions(bound)
149
108
  }
150
109
 
110
+ const treeConflictMessages: Record<MenuTreeConflictKind, string> = {
111
+ 'cycle': 'system.menus.errors.treeCycle',
112
+ 'orphan-parent': 'system.menus.errors.treeOrphanParent',
113
+ 'duplicate-id': 'system.menus.errors.treeDuplicateId',
114
+ 'duplicate-code': 'system.menus.errors.treeDuplicateCode',
115
+ 'duplicate-path': 'system.menus.errors.treeDuplicatePath',
116
+ 'ambiguous-parent': 'system.menus.errors.treeAmbiguousParent',
117
+ 'unresolved-parent': 'system.menus.errors.treeUnresolvedParent',
118
+ 'path-missing': 'system.menus.errors.pathRequired',
119
+ 'title-key-missing': 'system.menus.errors.treeTitleKeyMissing',
120
+ 'unknown': 'system.menus.errors.treePrecheckFailed',
121
+ }
122
+
123
+ async function fetchPrecheckRows(
124
+ signal: AbortSignal,
125
+ ): Promise<readonly RbacMenu[] | undefined> {
126
+ const firstPage = await listMenus(
127
+ {
128
+ page: 1,
129
+ pageSize: 1000,
130
+ keyword: '',
131
+ query: {},
132
+ sort: { prop: 'order', order: 'ascending' },
133
+ },
134
+ signal,
135
+ )
136
+ const rows = await fetchCompleteMenuPages(
137
+ firstPage,
138
+ (page, pageSize) =>
139
+ listMenus(
140
+ {
141
+ page,
142
+ pageSize,
143
+ keyword: '',
144
+ query: {},
145
+ sort: { prop: 'order', order: 'ascending' },
146
+ },
147
+ signal,
148
+ ),
149
+ )
150
+ if (!rows) notifyConflict({ kind: 'unknown' })
151
+ return rows
152
+ }
153
+
154
+ function notifyConflict(conflict: MenuTreeConflict): void {
155
+ if (conflict.diagnostic) console.error('[admin] menu tree precheck diagnostic', conflict.diagnostic)
156
+ const detail = conflict.detail
157
+ ? ` ${t('system.menus.errors.treeDetail', { detail: conflict.detail })}`
158
+ : ''
159
+ ElMessage.error(`${t(treeConflictMessages[conflict.kind])}${detail}`)
160
+ }
161
+
162
+ // 试算「保存后」的整树并只阻断本次保存引入的冲突——存量冲突由运行时
163
+ // 装载兜底,不应把整个菜单管理页卡死在历史数据上。
164
+ async function rejectTreeConflicts(
165
+ row: RbacMenu | undefined,
166
+ draft: RbacMenuDraft,
167
+ signal: AbortSignal,
168
+ ): Promise<boolean> {
169
+ const rows = await fetchPrecheckRows(signal)
170
+ if (!rows) return true
171
+ const introduced = filterIntroducedConflicts(
172
+ collectMenuTreeConflicts(rows),
173
+ collectMenuTreeConflicts(applyMenuDraftToRows(rows, draft, row?.id)),
174
+ )
175
+ if (!introduced.length) return false
176
+ notifyConflict(introduced[0]!)
177
+ return true
178
+ }
179
+
180
+ // 发布/下线同样改变运行时可见的树:激活会把剪枝子树重新注入运行时,
181
+ // 需与保存同门槛试算;下线的剪枝只会让冲突消失,天然不会被阻断。
182
+ async function rejectStatusConflicts(
183
+ ids: readonly string[],
184
+ status: RbacStatus,
185
+ signal: AbortSignal,
186
+ ): Promise<boolean> {
187
+ const rows = await fetchPrecheckRows(signal)
188
+ if (!rows) return true
189
+ const idSet = new Set(ids)
190
+ const introduced = filterIntroducedConflicts(
191
+ collectMenuTreeConflicts(rows),
192
+ collectMenuTreeConflicts(
193
+ rows.map((row) => (idSet.has(row.id) ? { ...row, status } : row)),
194
+ ),
195
+ )
196
+ if (!introduced.length) return false
197
+ notifyConflict(introduced[0]!)
198
+ return true
199
+ }
200
+
201
+ function isMenuVersionConflict(error: unknown): boolean {
202
+ return (
203
+ typeof error === 'object' &&
204
+ error !== null &&
205
+ (error as { kind?: unknown }).kind === 'business' &&
206
+ (error as { code?: unknown }).code === MENU_VERSION_CONFLICT_CODE
207
+ )
208
+ }
209
+
151
210
  async function save(
152
211
  row: RbacMenu | undefined,
153
212
  model: Record<string, unknown> | undefined,
154
213
  signal: AbortSignal,
155
214
  ): Promise<boolean> {
156
- const draft = parseDraft(model)
157
- if (row) await updateMenu(row.id, draft, signal)
158
- else await createMenu(draft, signal)
215
+ const draft = parseMenuDraft(model)
216
+ if (await rejectTreeConflicts(row, draft, signal)) return false
217
+ try {
218
+ if (row) await updateMenu(row.id, draft, row.version, signal)
219
+ else await createMenu(draft, signal)
220
+ } catch (error) {
221
+ if (isMenuVersionConflict(error)) {
222
+ ElMessage.error(t('system.menus.errors.versionConflict'))
223
+ return false
224
+ }
225
+ throw error
226
+ }
159
227
  ElMessage.success(row ? t('system.menus.menuUpdated') : t('system.menus.menuCreated'))
160
228
  await reconcile()
161
229
  return true
@@ -199,14 +267,20 @@ export function useMenuManagement(options: UseMenuManagementOptions) {
199
267
  status: RbacStatus,
200
268
  signal?: AbortSignal,
201
269
  ): Promise<void> {
202
- await setMenusStatus(ids, status, signal ?? new AbortController().signal)
203
- ElMessage.success(status === 'active' ? t('system.menus.menuPublished') : t('system.menus.menuUnpublished'))
270
+ const abort = signal ?? new AbortController().signal
271
+ if (await rejectStatusConflicts(ids, status, abort)) return
272
+ await setMenusStatus(ids, status, abort)
273
+ ElMessage.success(
274
+ status === 'active' ? t('system.menus.menuPublished') : t('system.menus.menuUnpublished'),
275
+ )
204
276
  await reconcile()
205
277
  }
206
278
 
207
279
  async function remove(ids: readonly string[], signal?: AbortSignal): Promise<void> {
208
280
  await removeMenus(ids, signal ?? new AbortController().signal)
209
- ElMessage.success(ids.length > 1 ? t('system.menus.selectedDeleted') : t('system.menus.menuDeleted'))
281
+ ElMessage.success(
282
+ ids.length > 1 ? t('system.menus.selectedDeleted') : t('system.menus.menuDeleted'),
283
+ )
210
284
  await reconcile()
211
285
  }
212
286
 
@@ -252,9 +326,16 @@ export function useMenuManagement(options: UseMenuManagementOptions) {
252
326
  prop: 'status',
253
327
  label: t('system.menus.status'),
254
328
  width: 90,
255
- format: (value) => (value === 'active' ? t('system.menus.published') : t('system.menus.unpublished')),
329
+ format: (value) =>
330
+ value === 'active' ? t('system.menus.published') : t('system.menus.unpublished'),
331
+ },
332
+ {
333
+ prop: 'updatedAt',
334
+ label: t('system.menus.updatedAt'),
335
+ minWidth: 134,
336
+ sortable: 'custom',
337
+ format: formatDate,
256
338
  },
257
- { prop: 'updatedAt', label: t('system.menus.updatedAt'), minWidth: 134, sortable: 'custom', format: formatDate },
258
339
  {
259
340
  type: 'action',
260
341
  key: 'actions',
@@ -262,7 +343,12 @@ export function useMenuManagement(options: UseMenuManagementOptions) {
262
343
  width: 218,
263
344
  fixed: 'right',
264
345
  actions: [
265
- { key: 'edit', label: t('common.actions.edit'), permission: 'system:menu:write', onClick: openEditor },
346
+ {
347
+ key: 'edit',
348
+ label: t('common.actions.edit'),
349
+ permission: 'system:menu:write',
350
+ onClick: openEditor,
351
+ },
266
352
  {
267
353
  key: 'toggle-status',
268
354
  label: t('common.actions.toggleStatus'),
@@ -291,8 +377,18 @@ export function useMenuManagement(options: UseMenuManagementOptions) {
291
377
  quickSearch: { placeholder: t('system.menus.searchPlaceholder') },
292
378
  columns: 4,
293
379
  fields: [
294
- { prop: 'kind', label: t('system.menus.nodeType'), type: 'select', options: kindOptions.value },
295
- { prop: 'status', label: t('system.menus.publishStatus'), type: 'select', options: statusOptions.value },
380
+ {
381
+ prop: 'kind',
382
+ label: t('system.menus.nodeType'),
383
+ type: 'select',
384
+ options: kindOptions.value,
385
+ },
386
+ {
387
+ prop: 'status',
388
+ label: t('system.menus.publishStatus'),
389
+ type: 'select',
390
+ options: statusOptions.value,
391
+ },
296
392
  ],
297
393
  },
298
394
  pagination: false,
@@ -0,0 +1,204 @@
1
+ import { describe, expect, it } from 'vitest'
2
+ import {
3
+ parseMenuDraft,
4
+ validateMenuComponentKeyRule,
5
+ validateMenuParentRule,
6
+ validateMenuPathRule,
7
+ validateMenuTitleKeyRule,
8
+ validateMenuTitleRule,
9
+ } from './menuDraft'
10
+
11
+ const model = (kind: 'page' | 'directory' | 'button', parentId?: string) => ({ kind, parentId })
12
+
13
+ describe('validateMenuPathRule', () => {
14
+ it('requires root paths to start with a slash', () => {
15
+ expect(validateMenuPathRule('users', model('page'))).toBe('system.menus.errors.rootPathSlash')
16
+ expect(validateMenuPathRule('/users', model('page'))).toBeUndefined()
17
+ })
18
+
19
+ it('requires child paths to stay relative', () => {
20
+ expect(validateMenuPathRule('/users', model('page', 'p1'))).toBe(
21
+ 'system.menus.errors.submenuRelativePath',
22
+ )
23
+ expect(validateMenuPathRule('users', model('page', 'p1'))).toBeUndefined()
24
+ })
25
+
26
+ it('skips buttons and rejects blank paths for page kinds', () => {
27
+ expect(validateMenuPathRule('anything', model('button'))).toBeUndefined()
28
+ expect(validateMenuPathRule('', model('page'))).toBe('system.menus.errors.pathRequired')
29
+ expect(validateMenuPathRule(' ', model('directory', 'p1'))).toBe(
30
+ 'system.menus.errors.pathRequired',
31
+ )
32
+ })
33
+ })
34
+
35
+ describe('validateMenuTitleRule', () => {
36
+ it('rejects blank titles and accepts trimmed content', () => {
37
+ expect(validateMenuTitleRule(' ')).toBe('system.menus.errors.formRequired')
38
+ expect(validateMenuTitleRule('用户管理')).toBeUndefined()
39
+ })
40
+ })
41
+
42
+ describe('validateMenuTitleKeyRule', () => {
43
+ it('requires translation keys for system page and directory nodes', () => {
44
+ expect(validateMenuTitleKeyRule('', model('page'))).toBe('system.menus.errors.titleKeyRequired')
45
+ expect(validateMenuTitleKeyRule('menus.users.title', model('directory'))).toBeUndefined()
46
+ expect(validateMenuTitleKeyRule('', model('button'))).toBeUndefined()
47
+ })
48
+ })
49
+
50
+ describe('validateMenuParentRule', () => {
51
+ it('requires a parent page for buttons only', () => {
52
+ expect(validateMenuParentRule('', model('button'))).toBe(
53
+ 'system.menus.errors.buttonRequiresPage',
54
+ )
55
+ expect(validateMenuParentRule(' ', model('button'))).toBe(
56
+ 'system.menus.errors.buttonRequiresPage',
57
+ )
58
+ expect(validateMenuParentRule('p1', model('button'))).toBeUndefined()
59
+ expect(validateMenuParentRule('', model('directory'))).toBeUndefined()
60
+ })
61
+ })
62
+
63
+ describe('validateMenuComponentKeyRule', () => {
64
+ const allowed = ['dashboard', 'user-list']
65
+
66
+ it('rejects page nodes with unknown or blank component keys', () => {
67
+ expect(validateMenuComponentKeyRule('legacy-page', model('page'), allowed)).toBe(
68
+ 'system.menus.errors.componentRequired',
69
+ )
70
+ expect(validateMenuComponentKeyRule('', model('page'), allowed)).toBe(
71
+ 'system.menus.errors.componentRequired',
72
+ )
73
+ expect(validateMenuComponentKeyRule('dashboard', model('page'), allowed)).toBeUndefined()
74
+ })
75
+
76
+ it('ignores non-page kinds', () => {
77
+ expect(validateMenuComponentKeyRule('anything', model('directory'), allowed)).toBeUndefined()
78
+ })
79
+ })
80
+
81
+ describe('parseMenuDraft', () => {
82
+ it('converges button fields and strips route-only data', () => {
83
+ expect(
84
+ parseMenuDraft({
85
+ kind: 'button',
86
+ title: ' 导出 ',
87
+ code: 'export',
88
+ path: '/ignored',
89
+ icon: 'Download',
90
+ visible: true,
91
+ cache: true,
92
+ tabHidden: false,
93
+ permission: 'system:user:export',
94
+ parentId: 'p1',
95
+ order: 2,
96
+ status: 'active',
97
+ }),
98
+ ).toMatchObject({
99
+ kind: 'button',
100
+ title: '导出',
101
+ path: undefined,
102
+ icon: undefined,
103
+ visible: false,
104
+ cache: false,
105
+ tabHidden: true,
106
+ permission: 'system:user:export',
107
+ parentId: 'p1',
108
+ redirect: undefined,
109
+ })
110
+ })
111
+
112
+ it('defaults empty directory redirects to the home route', () => {
113
+ expect(
114
+ parseMenuDraft({ kind: 'directory', title: 'S', code: 'sys', path: '/sys' })?.redirect,
115
+ ).toBe('/dashboard')
116
+ expect(
117
+ parseMenuDraft({
118
+ kind: 'directory',
119
+ title: 'S',
120
+ code: 'sys',
121
+ path: '/sys',
122
+ redirect: '/sys/ops',
123
+ })?.redirect,
124
+ ).toBe('/sys/ops')
125
+ expect(
126
+ parseMenuDraft({ kind: 'page', title: 'P', code: 'p', path: '/p' })?.redirect,
127
+ ).toBeUndefined()
128
+ })
129
+
130
+ it('keeps page fields, trims text, and drops route-only data', () => {
131
+ expect(
132
+ parseMenuDraft({
133
+ kind: 'page',
134
+ title: ' 用户 ',
135
+ code: 'users',
136
+ path: ' users ',
137
+ componentKey: 'user-list',
138
+ icon: ' Users ',
139
+ visible: false,
140
+ order: 5,
141
+ status: 'active',
142
+ }),
143
+ ).toMatchObject({
144
+ kind: 'page',
145
+ title: '用户',
146
+ path: 'users',
147
+ componentKey: 'user-list',
148
+ icon: 'Users',
149
+ visible: false,
150
+ permission: undefined,
151
+ redirect: undefined,
152
+ })
153
+ })
154
+
155
+ it('normalizes order and status and falls back unknown kinds to page', () => {
156
+ expect(
157
+ parseMenuDraft({ kind: 'magic', title: 'T', code: 't', path: '/t', order: 'abc' }),
158
+ ).toMatchObject({
159
+ kind: 'page',
160
+ order: 1,
161
+ status: 'active',
162
+ })
163
+ expect(
164
+ parseMenuDraft({
165
+ kind: 'page',
166
+ title: 'T',
167
+ code: 't',
168
+ path: '/t',
169
+ order: 3.7,
170
+ status: 'suspended',
171
+ }),
172
+ ).toMatchObject({ order: 3, status: 'suspended' })
173
+ })
174
+ })
175
+
176
+ describe('parseMenuDraft titleKey', () => {
177
+ it('keeps trimmed keys for page and directory and drops them for buttons', () => {
178
+ expect(
179
+ parseMenuDraft({ kind: 'page', title: 'T', code: 't', path: '/t', titleKey: ' menus.t ' }),
180
+ ).toMatchObject({ titleKey: 'menus.t' })
181
+ expect(
182
+ parseMenuDraft({
183
+ kind: 'directory',
184
+ title: 'D',
185
+ code: 'd',
186
+ path: '/d',
187
+ titleKey: ' menus.d ',
188
+ }),
189
+ ).toMatchObject({ titleKey: 'menus.d' })
190
+ expect(
191
+ parseMenuDraft({
192
+ kind: 'button',
193
+ title: 'B',
194
+ code: 'b',
195
+ parentId: 'p',
196
+ permission: 'x:y',
197
+ titleKey: ' menus.b ',
198
+ }),
199
+ ).toMatchObject({ titleKey: undefined })
200
+ expect(
201
+ parseMenuDraft({ kind: 'page', title: 'T', code: 't', path: '/t', titleKey: ' ' }),
202
+ ).toMatchObject({ titleKey: undefined })
203
+ })
204
+ })
@@ -0,0 +1,116 @@
1
+ import type { MenuKind, RbacMenuDraft, RbacStatus } from '../access/api/rbac'
2
+ import { HOME_ROUTE } from '../../../router/paths'
3
+
4
+ /** 菜单编辑器的表单草稿形状;由表单组件与提交流程共享。 */
5
+ export interface MenuEditorDraft {
6
+ parentId: string
7
+ kind: MenuKind
8
+ title: string
9
+ titleKey: string
10
+ code: string
11
+ path: string
12
+ componentKey: string
13
+ permission: string
14
+ icon: string
15
+ order: number
16
+ status: RbacStatus
17
+ visible: boolean
18
+ cache: boolean
19
+ tabHidden: boolean
20
+ redirect: string
21
+ }
22
+
23
+ /**
24
+ * 菜单编辑器的纯校验与归一化逻辑:表单 schema 通过 validator 调用这里的
25
+ * 规则(返回文案 key,由表单翻译),提交前用 parseMenuDraft 归一化。
26
+ * 拆出纯函数以便在组件外做逻辑级测试。
27
+ */
28
+
29
+ export type MenuValidationErrorKey =
30
+ | 'system.menus.errors.formRequired' |
31
+ 'system.menus.errors.buttonRequiresPage' |
32
+ 'system.menus.errors.pathRequired' |
33
+ 'system.menus.errors.submenuRelativePath' |
34
+ 'system.menus.errors.rootPathSlash' |
35
+ 'system.menus.errors.componentRequired' |
36
+ 'system.menus.errors.titleKeyRequired'
37
+
38
+ export interface MenuRuleModel {
39
+ kind: MenuKind
40
+ parentId?: string
41
+ }
42
+
43
+ export function validateMenuTitleRule(value: unknown): MenuValidationErrorKey | undefined {
44
+ return String(value ?? '').trim() ? undefined : 'system.menus.errors.formRequired'
45
+ }
46
+
47
+ export function validateMenuTitleKeyRule(
48
+ value: unknown,
49
+ model: MenuRuleModel,
50
+ ): MenuValidationErrorKey | undefined {
51
+ if (model.kind === 'button') return undefined
52
+ return String(value ?? '').trim() ? undefined : 'system.menus.errors.titleKeyRequired'
53
+ }
54
+
55
+ export function validateMenuParentRule(
56
+ value: unknown,
57
+ model: MenuRuleModel,
58
+ ): MenuValidationErrorKey | undefined {
59
+ if (model.kind !== 'button') return undefined
60
+ return String(value ?? '').trim() ? undefined : 'system.menus.errors.buttonRequiresPage'
61
+ }
62
+
63
+ export function validateMenuPathRule(
64
+ value: unknown,
65
+ model: MenuRuleModel,
66
+ ): MenuValidationErrorKey | undefined {
67
+ const path = String(value ?? '').trim()
68
+ if (model.kind === 'button') return undefined
69
+ if (!path) return 'system.menus.errors.pathRequired'
70
+ const isRoot = !model.parentId?.trim()
71
+ if (isRoot ? !path.startsWith('/') : path.startsWith('/')) {
72
+ return isRoot ? 'system.menus.errors.rootPathSlash' : 'system.menus.errors.submenuRelativePath'
73
+ }
74
+ return undefined
75
+ }
76
+
77
+ export function validateMenuComponentKeyRule(
78
+ value: unknown,
79
+ model: MenuRuleModel,
80
+ allowedComponentKeys: readonly string[],
81
+ ): MenuValidationErrorKey | undefined {
82
+ if (model.kind !== 'page') return undefined
83
+ const key = String(value ?? '').trim()
84
+ // 目录收缩后,存量行的 componentKey 可能已不在目录内,提交前必须拦截。
85
+ if (!key || !allowedComponentKeys.includes(key)) return 'system.menus.errors.componentRequired'
86
+ return undefined
87
+ }
88
+
89
+ export function parseMenuDraft(value: Record<string, unknown> | undefined): RbacMenuDraft {
90
+ const draft = value as Partial<MenuEditorDraft> | undefined
91
+ const kind = draft?.kind
92
+ const normalizedKind: MenuKind =
93
+ kind === 'page' || kind === 'directory' || kind === 'button' ? kind : 'page'
94
+ const trimmed = (input: string | undefined): string | undefined => {
95
+ const text = input?.trim()
96
+ return text || undefined
97
+ }
98
+
99
+ return {
100
+ parentId: trimmed(draft?.parentId),
101
+ kind: normalizedKind,
102
+ title: draft?.title?.trim() ?? '',
103
+ titleKey: normalizedKind === 'button' ? undefined : trimmed(draft?.titleKey),
104
+ code: draft?.code?.trim() ?? '',
105
+ path: normalizedKind === 'button' ? undefined : trimmed(draft?.path),
106
+ componentKey: normalizedKind === 'page' ? trimmed(draft?.componentKey) : undefined,
107
+ permission: normalizedKind === 'button' ? trimmed(draft?.permission) : undefined,
108
+ icon: normalizedKind === 'button' ? undefined : trimmed(draft?.icon),
109
+ order: Math.max(1, Math.floor(Number(draft?.order) || 1)),
110
+ status: draft?.status === 'suspended' ? 'suspended' : 'active',
111
+ visible: normalizedKind === 'button' ? false : draft?.visible !== false,
112
+ cache: normalizedKind === 'button' ? false : draft?.cache !== false,
113
+ tabHidden: normalizedKind === 'button' ? true : draft?.tabHidden === true,
114
+ redirect: normalizedKind === 'directory' ? (trimmed(draft?.redirect) ?? HOME_ROUTE) : undefined,
115
+ }
116
+ }