create-lumal-admin 0.0.5-beta → 0.0.6-beta

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 (194) hide show
  1. package/CHANGELOG.md +13 -0
  2. package/README.md +39 -10
  3. package/dist/cli.d.ts +9 -3
  4. package/dist/cli.d.ts.map +1 -1
  5. package/dist/cli.js +236 -19
  6. package/dist/cli.js.map +1 -1
  7. package/dist/create.d.ts +10 -1
  8. package/dist/create.d.ts.map +1 -1
  9. package/dist/create.js +18 -5
  10. package/dist/create.js.map +1 -1
  11. package/dist/index.d.ts +2 -1
  12. package/dist/index.d.ts.map +1 -1
  13. package/dist/index.js +1 -0
  14. package/dist/index.js.map +1 -1
  15. package/dist/presets.d.ts +29 -3
  16. package/dist/presets.d.ts.map +1 -1
  17. package/dist/presets.js +38 -10
  18. package/dist/presets.js.map +1 -1
  19. package/dist/project-cli.d.ts +3 -0
  20. package/dist/project-cli.d.ts.map +1 -0
  21. package/dist/project-cli.js +13 -0
  22. package/dist/project-cli.js.map +1 -0
  23. package/dist/projects.d.ts +21 -0
  24. package/dist/projects.d.ts.map +1 -0
  25. package/dist/projects.js +124 -0
  26. package/dist/projects.js.map +1 -0
  27. package/dist/templates.d.ts +2 -0
  28. package/dist/templates.d.ts.map +1 -1
  29. package/dist/templates.js +42 -13
  30. package/dist/templates.js.map +1 -1
  31. package/dist/types.d.ts +39 -0
  32. package/dist/types.d.ts.map +1 -1
  33. package/package.json +11 -3
  34. package/templates/base/API-CONTRACT.md +67 -20
  35. package/templates/base/README.md +20 -3
  36. package/templates/base/src/App.vue +71 -79
  37. package/templates/base/src/app/components/AppHeaderActions.vue +46 -0
  38. package/templates/base/src/{components/app → app/components}/AppSettingsDrawer.vue +15 -15
  39. package/templates/base/src/app/menu-policy.ts +163 -0
  40. package/templates/base/src/app/module.ts +46 -0
  41. package/templates/base/src/app/router/index.ts +107 -0
  42. package/templates/base/src/app/router/views.ts +12 -0
  43. package/templates/base/src/{services → app/services}/access.ts +1 -1
  44. package/templates/base/src/app/services/contracts.ts +54 -0
  45. package/templates/base/src/app/services/local-contracts.ts +18 -0
  46. package/templates/base/src/{services → app/services}/preferences.ts +8 -8
  47. package/templates/base/src/app/services/session-runtime.ts +186 -0
  48. package/templates/base/src/{services → app/services}/session.ts +4 -4
  49. package/templates/base/src/app/stored-menus.ts +72 -0
  50. package/templates/base/src/app/views/SafeIframeView.vue +34 -0
  51. package/templates/base/src/{views → app/views}/auth/LoginView.vue +29 -34
  52. package/templates/base/src/app/views/error/ForbiddenView.vue +20 -0
  53. package/templates/base/src/{views → app/views}/error/NotFoundView.vue +5 -7
  54. package/templates/base/src/main.ts +21 -22
  55. package/templates/base/src/modules/business/README.md +33 -0
  56. package/templates/base/src/{views → modules}/dashboard/DashboardView.vue +3 -3
  57. package/templates/base/src/modules/dashboard/module.ts +14 -0
  58. package/templates/base/src/shared/api.ts +53 -0
  59. package/templates/base/src/shared/components/BaseEntityDialog.vue +39 -0
  60. package/templates/base/src/shared/composables/use-async-data.ts +35 -0
  61. package/templates/base/src/shared/composables/use-entity-editor.ts +77 -0
  62. package/templates/base/src/shared/status.ts +4 -0
  63. package/templates/base/src/shared/tree.ts +41 -0
  64. package/templates/base/src/styles.scss +1 -6
  65. package/templates/base/tests/menu-policy.test.ts.template +89 -0
  66. package/templates/base/tests/session.test.ts.template +38 -27
  67. package/templates/base/vite.config.ts +4 -4
  68. package/templates/blank/README.md +6 -0
  69. package/templates/blank/src/App.vue +11 -0
  70. package/templates/common/AGENTS.md +13 -0
  71. package/templates/common/_editorconfig +15 -0
  72. package/templates/common/_gitattributes +6 -0
  73. package/templates/{base → common}/_gitignore +1 -0
  74. package/templates/common/_prettierignore +6 -0
  75. package/templates/common/docs/DEVELOPMENT.md +26 -0
  76. package/templates/common/eslint.config.mjs +60 -0
  77. package/templates/common/package.json +39 -0
  78. package/templates/common/prettier.config.mjs +8 -0
  79. package/templates/{base → common}/tsconfig.json +2 -5
  80. package/templates/common/vitest.config.ts +3 -0
  81. package/templates/features/examples/src/modules/examples/CrudExampleView.vue +51 -0
  82. package/templates/features/examples/src/{views → modules}/examples/FormExampleView.vue +15 -8
  83. package/templates/features/examples/src/modules/examples/module.ts +27 -0
  84. package/templates/features/system/src/modules/system/config/ConfigView.vue +104 -0
  85. package/templates/features/system/src/modules/system/config/api.ts +38 -0
  86. package/templates/features/system/src/modules/system/config/routes.ts +21 -0
  87. package/templates/features/system/src/modules/system/dictionary/DictionaryItemView.vue +141 -0
  88. package/templates/features/system/src/modules/system/dictionary/DictionaryTypeView.vue +107 -0
  89. package/templates/features/system/src/modules/system/dictionary/api.ts +61 -0
  90. package/templates/features/system/src/modules/system/dictionary/cache.ts +9 -0
  91. package/templates/features/system/src/modules/system/dictionary/routes.ts +32 -0
  92. package/templates/features/system/src/modules/system/dictionary/types.ts +22 -0
  93. package/templates/features/system/src/modules/system/local.ts +348 -0
  94. package/templates/features/system/src/modules/system/menu/MenuView.vue +133 -0
  95. package/templates/features/system/src/modules/system/menu/api.ts +73 -0
  96. package/templates/features/system/src/modules/system/menu/components/MenuEditorDialog.vue +106 -0
  97. package/templates/features/system/src/modules/system/menu/routes.ts +23 -0
  98. package/templates/features/system/src/modules/system/menu/types.ts +18 -0
  99. package/templates/features/system/src/modules/system/module.ts +22 -0
  100. package/templates/features/system/src/modules/system/organization/OrganizationView.vue +134 -0
  101. package/templates/features/system/src/modules/system/organization/api.ts +38 -0
  102. package/templates/features/system/src/modules/system/organization/routes.ts +23 -0
  103. package/templates/features/system/src/modules/system/organization/types.ts +17 -0
  104. package/templates/features/system/src/modules/system/role/RoleView.vue +90 -0
  105. package/templates/features/system/src/modules/system/role/api.ts +40 -0
  106. package/templates/features/system/src/modules/system/role/components/RoleAuthorizationDialog.vue +110 -0
  107. package/templates/features/system/src/modules/system/role/permission-tree.ts +31 -0
  108. package/templates/features/system/src/modules/system/role/routes.ts +24 -0
  109. package/templates/features/system/src/modules/system/role/types.ts +12 -0
  110. package/templates/features/system/src/modules/system/user/UserView.vue +297 -0
  111. package/templates/features/system/src/modules/system/user/api.ts +45 -0
  112. package/templates/features/system/src/modules/system/user/components/UserOrganizationFilter.vue +61 -0
  113. package/templates/features/system/src/modules/system/user/components/UserRoleDialog.vue +93 -0
  114. package/templates/features/system/src/modules/system/user/routes.ts +27 -0
  115. package/templates/features/system/src/modules/system/user/types.ts +28 -0
  116. package/templates/features/system/tests/system.test.ts.template +266 -0
  117. package/templates/modes/http/src/app/services/data-source.ts +124 -0
  118. package/templates/modes/http/src/app/services/http-adapter.ts +128 -0
  119. package/templates/modes/http/tests/http.test.ts.template +186 -133
  120. package/templates/modes/local/src/app/services/data-source.ts +113 -0
  121. package/templates/modes/local/src/app/services/local-database.ts +67 -0
  122. package/templates/modes/local/tests/local.test.ts.template +117 -77
  123. package/templates/presets/standard/src/modules/dashboard/DashboardView.vue +52 -0
  124. package/templates/presets/standard/src/modules/dashboard/components/DashboardSessionStatus.vue +41 -0
  125. package/templates/presets/standard/src/modules/dashboard/components/DashboardShortcuts.vue +51 -0
  126. package/templates/presets/standard/src/modules/profile/ProfileView.vue +109 -0
  127. package/templates/presets/standard/src/modules/profile/api.ts +49 -0
  128. package/templates/presets/standard/src/modules/profile/components/ProfilePasswordForm.vue +68 -0
  129. package/templates/presets/standard/src/modules/profile/local.ts +40 -0
  130. package/templates/presets/standard/src/modules/profile/module.ts +11 -0
  131. package/templates/presets/standard/tests/profile.test.ts.template +72 -0
  132. package/templates/screen/README.md +6 -0
  133. package/templates/screen/src/App.vue +7 -0
  134. package/templates/screen/src/screen/ScreenEntry.vue +18 -0
  135. package/templates/screen/src/screen/ScreenView.vue +48 -0
  136. package/templates/screen/src/screen/components/ScreenMetrics.vue +40 -0
  137. package/templates/screen-auth/src/screen/ScreenEntry.vue +52 -0
  138. package/templates/screen-auth/src/screen/components/ScreenLoginForm.vue +44 -0
  139. package/templates/screen-auth/src/services/session.ts +61 -0
  140. package/templates/screen-auth/tests/session.test.ts.template +54 -0
  141. package/templates/screen-auth-http/src/services/auth-adapter.ts +14 -0
  142. package/templates/screen-auth-http/tests/auth-adapter.test.ts.template +17 -0
  143. package/templates/screen-auth-local-demo/src/services/auth-adapter.ts +15 -0
  144. package/templates/screen-auth-local-demo/tests/auth-adapter.test.ts.template +18 -0
  145. package/templates/screen-data/src/screen/ScreenView.vue +69 -0
  146. package/templates/screen-data/src/services/metrics-types.ts +4 -0
  147. package/templates/screen-data-http/src/services/metrics.ts +20 -0
  148. package/templates/screen-data-http/src/services/request.ts +3 -0
  149. package/templates/screen-data-http/tests/metrics.test.ts.template +21 -0
  150. package/templates/screen-data-local/src/services/metrics.ts +11 -0
  151. package/templates/screen-data-local/tests/metrics.test.ts.template +9 -0
  152. package/templates/screen-http/API-CONTRACT.md +20 -0
  153. package/templates/screen-http/src/services/http.ts +17 -0
  154. package/templates/screen-request-auth/src/services/request.ts +15 -0
  155. package/templates/screen-router/src/App.vue +7 -0
  156. package/templates/screen-router/src/main.ts +13 -0
  157. package/templates/screen-router/src/router.ts +7 -0
  158. package/templates/screen-router/tests/app.test.ts.template +20 -0
  159. package/templates/screen-viewport/src/screen/ScreenViewport.vue +30 -0
  160. package/templates/screen-visualization-charts/src/screen/components/ScreenVisualization.vue +22 -0
  161. package/templates/screen-visualization-cockpit/src/screen/components/ScreenVisualization.vue +33 -0
  162. package/templates/screen-visualization-cockpit/src/screen/components/ScreenWidget.vue +10 -0
  163. package/templates/screen-visualization-datav/src/screen/components/ScreenVisualization.vue +11 -0
  164. package/templates/vben-compatible/MIGRATION.md +2 -0
  165. package/templates/vben-compatible/src/{views/migration → modules/vben-migration}/VbenFormView.vue +2 -1
  166. package/templates/vben-compatible/src/{views/migration → modules/vben-migration}/VbenUsersView.vue +3 -2
  167. package/templates/vben-compatible/src/modules/vben-migration/components/VbenFormExample.vue +64 -0
  168. package/templates/vben-compatible/src/{components/migration → modules/vben-migration/components}/VbenUsers.vue +73 -64
  169. package/templates/vben-compatible/src/modules/vben-migration/local.ts +68 -0
  170. package/templates/vben-compatible/src/modules/vben-migration/module.ts +42 -0
  171. package/templates/vue/index.html +12 -0
  172. package/templates/vue/src/main.ts +5 -0
  173. package/templates/vue/src/styles.css +19 -0
  174. package/templates/vue/tests/app.test.ts.template +10 -0
  175. package/templates/vue/vite.config.ts +9 -0
  176. package/templates/vue/vitest.config.ts +4 -0
  177. package/templates/base/eslint.config.mjs +0 -8
  178. package/templates/base/src/components/app/AppHeaderActions.vue +0 -34
  179. package/templates/base/src/router/index.ts +0 -86
  180. package/templates/base/src/router/views.ts +0 -16
  181. package/templates/base/src/services/contracts.ts +0 -29
  182. package/templates/base/src/services/session-runtime.ts +0 -137
  183. package/templates/base/src/views/error/ForbiddenView.vue +0 -25
  184. package/templates/base/vitest.config.ts +0 -3
  185. package/templates/features/examples/src/modules/examples.ts +0 -10
  186. package/templates/features/examples/src/views/examples/CrudExampleView.vue +0 -30
  187. package/templates/features/system/src/business/system/entities.ts +0 -93
  188. package/templates/features/system/src/modules/system.ts +0 -28
  189. package/templates/features/system/src/views/system/SystemEntityView.vue +0 -85
  190. package/templates/modes/http/src/services/data-source.ts +0 -120
  191. package/templates/modes/http/src/services/http-adapter.ts +0 -124
  192. package/templates/modes/local/src/services/data-source.ts +0 -176
  193. package/templates/vben-compatible/src/components/migration/VbenFormExample.vue +0 -34
  194. package/templates/vben-compatible/src/modules/vben.ts +0 -17
@@ -0,0 +1,23 @@
1
+ import type { AppModule } from '../../../app/module';
2
+
3
+ export const menus: AppModule['menus'] = [
4
+ {
5
+ path: '/system/menus',
6
+ name: 'SystemMenus',
7
+ component: 'system/menus',
8
+ meta: { title: '菜单管理', authority: ['system:menu:list'], keepAlive: true, order: 3 },
9
+ },
10
+ ];
11
+ export const views: AppModule['views'] = { 'system/menus': () => import('./MenuView.vue') };
12
+ export const permissions: AppModule['permissions'] = [
13
+ {
14
+ id: 'system-menu',
15
+ label: '菜单管理',
16
+ children: [
17
+ { id: 'system:menu:list', label: '查询菜单', permission: 'system:menu:list' },
18
+ { id: 'system:menu:create', label: '新增节点', permission: 'system:menu:create' },
19
+ { id: 'system:menu:update', label: '修改节点', permission: 'system:menu:update' },
20
+ { id: 'system:menu:delete', label: '删除节点', permission: 'system:menu:delete' },
21
+ ],
22
+ },
23
+ ];
@@ -0,0 +1,18 @@
1
+ export type MenuType = 'directory' | 'menu' | 'button' | 'external' | 'embedded';
2
+ export interface MenuInput {
3
+ parentId: string;
4
+ type: MenuType;
5
+ title: string;
6
+ path: string;
7
+ name: string;
8
+ component: string;
9
+ icon: string;
10
+ order: number;
11
+ hidden: boolean;
12
+ permissions: string[];
13
+ externalLink: string;
14
+ }
15
+ export interface Menu extends MenuInput {
16
+ id: string;
17
+ children?: Menu[];
18
+ }
@@ -0,0 +1,22 @@
1
+ import type { AppModule } from '../../app/module';
2
+ import { assembleModules } from '../../app/module';
3
+ import * as config from './config/routes';
4
+ import * as dictionary from './dictionary/routes';
5
+ import * as menu from './menu/routes';
6
+ import * as organization from './organization/routes';
7
+ import * as role from './role/routes';
8
+ import * as user from './user/routes';
9
+
10
+ const children = assembleModules([user, role, menu, organization, dictionary, config]);
11
+ export const menus: AppModule['menus'] = [
12
+ {
13
+ path: '/system',
14
+ name: 'System',
15
+ meta: { title: '系统管理', icon: 'lumal:settings', order: 20 },
16
+ children: children.menus,
17
+ },
18
+ ];
19
+ export const views = children.views;
20
+ export const permissions: AppModule['permissions'] = [
21
+ { id: 'system', label: '系统管理', children: children.permissions },
22
+ ];
@@ -0,0 +1,134 @@
1
+ <script setup lang="ts">
2
+ import type { SchemaFormItem, SchemaTableColumn } from '@lumal/core/components';
3
+ import type { Organization } from './types';
4
+ import { LumalPage, LumalSchemaTable } from '@lumal/core/components';
5
+ import { LumalIcon } from '@lumal/icons-vue';
6
+ import { ElAlert, ElButton, ElInput } from 'element-plus';
7
+ import { computed, shallowRef } from 'vue';
8
+ import { permissionStore, service } from '../../../app/services/session';
9
+ import BaseEntityDialog from '../../../shared/components/BaseEntityDialog.vue';
10
+ import { useAsyncData } from '../../../shared/composables/use-async-data';
11
+ import { useEntityEditor } from '../../../shared/composables/use-entity-editor';
12
+ import { statusOptions } from '../../../shared/status';
13
+ import { excludedDescendants, flattenTree } from '../../../shared/tree';
14
+ import { createOrganizationService, organizationInput, parseOrganization } from './api';
15
+
16
+ /***********************机构树、关联限制与筛选*********************/
17
+ const api = createOrganizationService(service);
18
+ const state = useAsyncData(api.list, [] as Organization[]);
19
+ const keyword = shallowRef('');
20
+ const can = (action: string) => permissionStore.hasPermission(`system:organization:${action}`);
21
+ const editor = useEntityEditor<Organization, ReturnType<typeof organizationInput>>({
22
+ create: api.create,
23
+ update: api.update,
24
+ remove: api.remove,
25
+ toInput: organizationInput,
26
+ reload: state.load,
27
+ });
28
+ function filter(items: Organization[]): Organization[] {
29
+ return items.flatMap((item) => {
30
+ const children = filter(item.children ?? []);
31
+ return !keyword.value || `${item.name} ${item.code}`.includes(keyword.value) || children.length
32
+ ? [{ ...item, children }]
33
+ : [];
34
+ });
35
+ }
36
+ const displayed = computed(() => filter(state.data.value));
37
+ const columns: SchemaTableColumn<Organization>[] = [
38
+ { field: 'name', label: '机构名称', minWidth: 220 },
39
+ { field: 'code', label: '编码', minWidth: 130 },
40
+ { field: 'leader', label: '负责人', minWidth: 100 },
41
+ { field: 'phone', label: '联系电话', minWidth: 140 },
42
+ { field: 'status', label: '状态', options: statusOptions, width: 90 },
43
+ { field: 'userCount', label: '关联用户', width: 100, formatter: (value) => value ?? '-' },
44
+ { field: 'order', label: '排序', width: 80 },
45
+ ];
46
+ const schemas = computed<SchemaFormItem[]>(() => {
47
+ const all = flattenTree(state.data.value);
48
+ const excluded = excludedDescendants(all, editor.editing.value?.id);
49
+ return [
50
+ { field: 'name', label: '机构名称', required: true },
51
+ { field: 'code', label: '机构编码', required: true },
52
+ {
53
+ field: 'parentId',
54
+ label: '上级机构',
55
+ component: 'select',
56
+ options: [
57
+ { label: '无', value: '' },
58
+ ...all.filter((row) => !excluded.has(row.id)).map((row) => ({ label: row.name, value: row.id })),
59
+ ],
60
+ },
61
+ { field: 'leader', label: '负责人' },
62
+ { field: 'phone', label: '联系电话' },
63
+ { field: 'email', label: '邮箱', rules: [{ type: 'email', message: '邮箱格式错误' }] },
64
+ { field: 'order', label: '排序', component: 'number', defaultValue: 0 },
65
+ { field: 'status', label: '状态', component: 'select', options: statusOptions, defaultValue: 'enabled' },
66
+ ];
67
+ });
68
+ </script>
69
+
70
+ <template>
71
+ <LumalPage class="lumal-admin-page" title="机构管理" no-padding>
72
+ <ElAlert v-if="editor.error.value" :title="editor.error.value" type="error" :closable="false" />
73
+ <LumalSchemaTable
74
+ :columns="columns"
75
+ :rows="displayed"
76
+ :loading="state.loading.value"
77
+ :error="state.error.value"
78
+ default-expand-all
79
+ :action-width="240"
80
+ @retry="state.reload"
81
+ >
82
+ <template #toolbar>
83
+ <ElInput
84
+ v-model="keyword"
85
+ clearable
86
+ placeholder="搜索机构名称或编码"
87
+ aria-label="搜索机构"
88
+ style="max-width: 260px"
89
+ />
90
+ <ElButton
91
+ v-if="can('create')"
92
+ type="primary"
93
+ @click="editor.openCreate({ parentId: '', status: 'enabled', order: 0 })"
94
+ >
95
+ 新增机构
96
+ </ElButton>
97
+ </template>
98
+ <template #toolbar-tools>
99
+ <ElButton title="重新加载" aria-label="重新加载" @click="state.reload">
100
+ <LumalIcon name="lumal:refresh" />
101
+ </ElButton>
102
+ </template>
103
+ <template #actions="{ row }">
104
+ <ElButton
105
+ v-if="can('create')"
106
+ link
107
+ @click="editor.openCreate({ parentId: row.id, status: 'enabled', order: 0 })"
108
+ >
109
+ 新增子机构
110
+ </ElButton>
111
+ <ElButton v-if="can('update')" link @click="editor.openEdit(parseOrganization(row))"> 编辑 </ElButton>
112
+ <ElButton
113
+ v-if="can('delete')"
114
+ link
115
+ type="danger"
116
+ :disabled="editor.saving.value"
117
+ @click="editor.remove(parseOrganization(row))"
118
+ >
119
+ 删除
120
+ </ElButton>
121
+ </template>
122
+ </LumalSchemaTable>
123
+ <BaseEntityDialog
124
+ v-model:visible="editor.visible.value"
125
+ v-model:model="editor.model.value"
126
+ :title="editor.editing.value ? '编辑机构' : '新增机构'"
127
+ :schemas="schemas"
128
+ :editing="!!editor.editing.value"
129
+ :saving="editor.saving.value"
130
+ :error="editor.error.value"
131
+ @submit="editor.submit"
132
+ />
133
+ </LumalPage>
134
+ </template>
@@ -0,0 +1,38 @@
1
+ import type { SchemaFormModel } from '@lumal/core/components';
2
+ import type { BusinessTransport } from '../../../app/services/contracts';
3
+ import type { Organization, OrganizationInput } from './types';
4
+ import { formStatus, integer, object, rows, text } from '../../../shared/api';
5
+
6
+ export function organizationInput(model: SchemaFormModel): OrganizationInput {
7
+ return {
8
+ name: text(model.name, '机构名称', true),
9
+ code: text(model.code, '机构编码', true),
10
+ parentId: text(model.parentId, '父机构'),
11
+ leader: text(model.leader, '负责人'),
12
+ phone: text(model.phone, '联系电话'),
13
+ email: text(model.email, '邮箱'),
14
+ order: integer(model.order ?? 0),
15
+ status: formStatus(model),
16
+ };
17
+ }
18
+ export function parseOrganization(value: unknown): Organization {
19
+ const raw = object(value);
20
+ return {
21
+ ...organizationInput(raw),
22
+ id: text(raw.id, '机构 ID', true),
23
+ children: raw.children === undefined ? undefined : rows(raw.children, parseOrganization),
24
+ userCount: typeof raw.userCount === 'number' ? raw.userCount : undefined,
25
+ };
26
+ }
27
+ export function createOrganizationService(transport: BusinessTransport) {
28
+ return {
29
+ list: async () => rows(await transport.request('GET', '/system/organizations'), parseOrganization),
30
+ create: async (input: OrganizationInput) =>
31
+ parseOrganization(await transport.request('POST', '/system/organizations', { body: input })),
32
+ update: async (id: string, input: OrganizationInput) =>
33
+ parseOrganization(
34
+ await transport.request('PUT', `/system/organizations/${encodeURIComponent(id)}`, { body: input }),
35
+ ),
36
+ remove: (id: string) => transport.request<void>('DELETE', `/system/organizations/${encodeURIComponent(id)}`),
37
+ };
38
+ }
@@ -0,0 +1,23 @@
1
+ import type { AppModule } from '../../../app/module';
2
+
3
+ export const menus: AppModule['menus'] = [
4
+ {
5
+ path: '/system/organizations',
6
+ name: 'SystemOrganizations',
7
+ component: 'system/organizations',
8
+ meta: { title: '机构管理', authority: ['system:organization:list'], keepAlive: true, order: 4 },
9
+ },
10
+ ];
11
+ export const views: AppModule['views'] = { 'system/organizations': () => import('./OrganizationView.vue') };
12
+ export const permissions: AppModule['permissions'] = [
13
+ {
14
+ id: 'system-organization',
15
+ label: '机构管理',
16
+ children: [
17
+ { id: 'system:organization:list', label: '查询机构', permission: 'system:organization:list' },
18
+ { id: 'system:organization:create', label: '新增机构', permission: 'system:organization:create' },
19
+ { id: 'system:organization:update', label: '修改机构', permission: 'system:organization:update' },
20
+ { id: 'system:organization:delete', label: '删除机构', permission: 'system:organization:delete' },
21
+ ],
22
+ },
23
+ ];
@@ -0,0 +1,17 @@
1
+ import type { Status } from '../../../shared/api';
2
+
3
+ export interface OrganizationInput {
4
+ name: string;
5
+ code: string;
6
+ parentId: string;
7
+ leader: string;
8
+ phone: string;
9
+ email: string;
10
+ order: number;
11
+ status: Status;
12
+ }
13
+ export interface Organization extends OrganizationInput {
14
+ id: string;
15
+ children?: Organization[];
16
+ userCount?: number;
17
+ }
@@ -0,0 +1,90 @@
1
+ <script setup lang="ts">
2
+ import type { CrudDataSource, SchemaFormItem, SchemaTableColumn } from '@lumal/core/components';
3
+ import type { Role } from './types';
4
+ import { LumalCrudTable, LumalPage } from '@lumal/core/components';
5
+ import { ElAlert, ElButton } from 'element-plus';
6
+ import { shallowRef, useTemplateRef } from 'vue';
7
+ import { permissionStore, service } from '../../../app/services/session';
8
+ import { statusOptions } from '../../../shared/status';
9
+ import { createRoleService, parseRole, roleInput } from './api';
10
+ import RoleAuthorizationDialog from './components/RoleAuthorizationDialog.vue';
11
+
12
+ /***********************角色资料与授权分离*********************/
13
+ const api = createRoleService(service);
14
+ const table = useTemplateRef<InstanceType<typeof LumalCrudTable>>('table');
15
+ const authorizing = shallowRef<Role>();
16
+ const authorizationVisible = shallowRef(false);
17
+ const failure = shallowRef('');
18
+ const can = (action: string) => permissionStore.hasPermission(`system:role:${action}`);
19
+ const dataSource: CrudDataSource = {
20
+ fetch: (input) => api.list(input),
21
+ create: (model) => api.create(roleInput(model)),
22
+ update: (row, model) => api.update(parseRole(row).id, roleInput(model)),
23
+ remove: (row) => api.remove(parseRole(row).id),
24
+ };
25
+ const columns: SchemaTableColumn[] = [
26
+ { field: 'name', label: '角色名称', minWidth: 160 },
27
+ { field: 'code', label: '角色编码', minWidth: 140 },
28
+ { field: 'status', label: '状态', options: statusOptions, width: 100 },
29
+ { field: 'description', label: '说明', minWidth: 200 },
30
+ ];
31
+ const schemas: SchemaFormItem[] = [
32
+ { field: 'name', label: '角色名称', required: true },
33
+ {
34
+ field: 'code',
35
+ label: '角色编码',
36
+ required: true,
37
+ editDisabled: true,
38
+ rules: [{ pattern: /^[a-z][\w-]*$/, message: '使用小写字母开头的编码' }],
39
+ },
40
+ {
41
+ field: 'status',
42
+ label: '状态',
43
+ component: 'select',
44
+ options: statusOptions,
45
+ defaultValue: 'enabled',
46
+ disabled: ({ model }) => model.builtIn === true,
47
+ },
48
+ { field: 'description', label: '说明', component: 'textarea', span: 24 },
49
+ ];
50
+ const querySchemas: SchemaFormItem[] = [
51
+ { field: 'keyword', label: '关键词' },
52
+ { field: 'status', label: '状态', component: 'select', options: statusOptions },
53
+ ];
54
+ function authorize(role: Role): void {
55
+ authorizing.value = role;
56
+ authorizationVisible.value = true;
57
+ }
58
+ </script>
59
+
60
+ <template>
61
+ <LumalPage class="lumal-admin-page" title="角色管理" no-padding>
62
+ <ElAlert v-if="failure" :title="failure" type="error" :closable="false" />
63
+ <LumalCrudTable
64
+ ref="table"
65
+ :data-source="dataSource"
66
+ :form-schemas="schemas"
67
+ :table="{ columns, rowKey: 'id', actionWidth: 260 }"
68
+ :query="{ schemas: querySchemas, columns: 2, collapsible: true }"
69
+ :toolbar="{ create: can('create'), batchDelete: false }"
70
+ :actions="{ view: false, edit: false, remove: false }"
71
+ @operation-error="failure = $event instanceof Error ? $event.message : String($event)"
72
+ >
73
+ <template #row-actions="{ row }">
74
+ <ElButton link @click="table?.openView(row)"> 查看 </ElButton>
75
+ <ElButton v-if="can('update')" link @click="table?.openEdit(row)"> 编辑 </ElButton>
76
+ <ElButton v-if="can('authorize')" link @click="authorize(parseRole(row))"> 授权 </ElButton>
77
+ <ElButton
78
+ v-if="can('delete')"
79
+ link
80
+ type="danger"
81
+ :disabled="row.builtIn === true"
82
+ @click="table?.removeRow(row)"
83
+ >
84
+ 删除
85
+ </ElButton>
86
+ </template>
87
+ </LumalCrudTable>
88
+ <RoleAuthorizationDialog v-if="authorizing" v-model="authorizationVisible" :role="authorizing" />
89
+ </LumalPage>
90
+ </template>
@@ -0,0 +1,40 @@
1
+ import type { SchemaFormModel } from '@lumal/core/components';
2
+ import type { ApplicationService } from '../../../app/services/contracts';
3
+ import type { PageInput } from '../../../shared/api';
4
+ import type { Role, RoleInput } from './types';
5
+ import { formStatus, object, stringArray, text } from '../../../shared/api';
6
+ import { parsePermissionTree } from './permission-tree';
7
+
8
+ export function roleInput(model: SchemaFormModel): RoleInput {
9
+ return {
10
+ code: text(model.code, '角色编码', true),
11
+ name: text(model.name, '角色名称', true),
12
+ description: text(model.description, '说明'),
13
+ status: formStatus(model),
14
+ };
15
+ }
16
+ export function parseRole(value: unknown): Role {
17
+ const raw = object(value);
18
+ return { ...roleInput(raw), id: text(raw.id, '角色 ID', true), builtIn: raw.builtIn === true };
19
+ }
20
+ export function createRoleService(transport: Pick<ApplicationService, 'list' | 'request'>) {
21
+ return {
22
+ async list(input: PageInput) {
23
+ const page = await transport.list('roles', { ...input, query: { ...input.query } });
24
+ return { items: page.items.map(parseRole), total: page.total };
25
+ },
26
+ create: async (input: RoleInput) => parseRole(await transport.request('POST', '/system/roles', { body: input })),
27
+ update: async (id: string, input: RoleInput) =>
28
+ parseRole(await transport.request('PUT', `/system/roles/${encodeURIComponent(id)}`, { body: input })),
29
+ remove: (id: string) => transport.request<void>('DELETE', `/system/roles/${encodeURIComponent(id)}`),
30
+ tree: async () => parsePermissionTree(await transport.request('GET', '/system/permissions/tree')),
31
+ permissions: async (code: string) =>
32
+ stringArray(await transport.request('GET', `/system/roles/${encodeURIComponent(code)}/permissions`)),
33
+ authorize: async (code: string, permissions: string[]) =>
34
+ stringArray(
35
+ await transport.request('PUT', `/system/roles/${encodeURIComponent(code)}/permissions`, {
36
+ body: { permissions },
37
+ }),
38
+ ),
39
+ };
40
+ }
@@ -0,0 +1,110 @@
1
+ <script setup lang="ts">
2
+ import type { PermissionNode } from '../../../../app/module';
3
+ import type { Role } from '../types';
4
+ import { ElAlert, ElButton, ElDialog, ElTree } from 'element-plus';
5
+ import { nextTick, shallowRef, useTemplateRef, watch } from 'vue';
6
+ import { collectPermissions } from '../../../../app/module';
7
+ import { application, service } from '../../../../app/services/session';
8
+ import { createRoleService } from '../api';
9
+
10
+ /***********************授权树加载与回显*********************/
11
+ const props = defineProps<{ role: Role }>();
12
+ const visible = defineModel<boolean>({ required: true });
13
+ const api = createRoleService(service);
14
+ const tree = useTemplateRef<InstanceType<typeof ElTree>>('tree');
15
+ const nodes = shallowRef<PermissionNode[]>([]);
16
+ const loading = shallowRef(false);
17
+ const saving = shallowRef(false);
18
+ const saved = shallowRef(false);
19
+ const failure = shallowRef('');
20
+ let generation = 0;
21
+ function flatten(items: PermissionNode[]): PermissionNode[] {
22
+ return items.flatMap((item) => [item, ...flatten(item.children ?? [])]);
23
+ }
24
+ async function load(): Promise<void> {
25
+ const version = ++generation;
26
+ loading.value = true;
27
+ failure.value = '';
28
+ saved.value = false;
29
+ try {
30
+ const [next, granted] = await Promise.all([api.tree(), api.permissions(props.role.code)]);
31
+ if (version !== generation || !visible.value) return;
32
+ nodes.value = next;
33
+ await nextTick();
34
+ tree.value?.setCheckedKeys(
35
+ flatten(next)
36
+ .filter((node) => node.permission && granted.includes(node.permission))
37
+ .map((node) => node.id),
38
+ );
39
+ } catch (reason) {
40
+ if (version === generation) failure.value = reason instanceof Error ? reason.message : '授权加载失败';
41
+ } finally {
42
+ if (version === generation) loading.value = false;
43
+ }
44
+ }
45
+ watch(
46
+ visible,
47
+ (open) => {
48
+ if (open) void load();
49
+ else generation += 1;
50
+ },
51
+ { immediate: true },
52
+ );
53
+
54
+ /***********************保存与部分成功恢复*********************/
55
+ async function save(): Promise<void> {
56
+ if (saving.value || loading.value || !tree.value) return;
57
+ saving.value = true;
58
+ failure.value = '';
59
+ try {
60
+ if (!saved.value) {
61
+ const keys = new Set([...tree.value.getCheckedKeys(false), ...tree.value.getHalfCheckedKeys()].map(String));
62
+ const known = new Set(collectPermissions(nodes.value));
63
+ const permissions = flatten(nodes.value)
64
+ .filter((node) => keys.has(node.id) && node.permission && known.has(node.permission))
65
+ .map((node) => node.permission!);
66
+ await api.authorize(props.role.code, permissions);
67
+ saved.value = true;
68
+ }
69
+ // 授权写入和当前账号权限重读不是事务,失败时只重试重读。
70
+ await application.refreshAccess();
71
+ visible.value = false;
72
+ } catch (reason) {
73
+ failure.value = `${saved.value ? '授权已保存,但会话刷新失败。可重试刷新:' : ''}${reason instanceof Error ? reason.message : '授权保存失败'}`;
74
+ } finally {
75
+ saving.value = false;
76
+ }
77
+ }
78
+ </script>
79
+
80
+ <template>
81
+ <ElDialog
82
+ v-model="visible"
83
+ :title="`角色授权:${role.name}`"
84
+ width="min(600px, 94vw)"
85
+ append-to-body
86
+ :close-on-click-modal="!saving"
87
+ :close-on-press-escape="!saving"
88
+ >
89
+ <ElAlert v-if="failure" :title="failure" type="error" :closable="false" role="alert" />
90
+ <ElButton :disabled="loading || saving" @click="load"> 重新读取 </ElButton>
91
+ <ElTree
92
+ ref="tree"
93
+ :data="nodes"
94
+ node-key="id"
95
+ show-checkbox
96
+ default-expand-all
97
+ :props="{
98
+ label: 'label',
99
+ children: 'children',
100
+ disabled: () => loading || saving || saved || role.code === 'admin',
101
+ }"
102
+ />
103
+ <template #footer>
104
+ <ElButton :disabled="saving" @click="visible = false"> 关闭 </ElButton>
105
+ <ElButton type="primary" :loading="saving" :disabled="loading || !nodes.length" @click="save">
106
+ {{ saved ? '重试刷新' : '保存授权' }}
107
+ </ElButton>
108
+ </template>
109
+ </ElDialog>
110
+ </template>
@@ -0,0 +1,31 @@
1
+ import type { PermissionNode } from '../../../app/module';
2
+ import { object, text } from '../../../shared/api';
3
+
4
+ /** 将非叶节点的访问权限拆成独立叶子,避免回显父权限时误授予全部按钮权限。 */
5
+ export function parsePermissionTree(value: unknown): PermissionNode[] {
6
+ const ids = new Set<string>();
7
+ const seen = new WeakSet<object>();
8
+ let count = 0;
9
+ function visit(value: unknown, depth: number): PermissionNode[] {
10
+ if (!Array.isArray(value) || depth > 30) throw new Error('权限树必须是有限层级的数组');
11
+ return value.map((item) => {
12
+ const raw = object(item);
13
+ const id = text(raw.id, '权限节点 ID', true);
14
+ if (++count > 1000 || seen.has(raw) || ids.has(id)) throw new Error('权限树存在循环、重复节点或超过数量限制');
15
+ ids.add(id);
16
+ seen.add(raw);
17
+ const label = text(raw.label, '权限标题', true);
18
+ const permission = raw.permission === undefined ? undefined : text(raw.permission, '权限码', true);
19
+ const children = raw.children === undefined ? [] : visit(raw.children, depth + 1);
20
+ if (!children.length) return { id: `permission:${id}`, label, permission };
21
+ return {
22
+ id: `group:${id}`,
23
+ label,
24
+ children: permission
25
+ ? [{ id: `permission:${id}`, label: `${label}(访问)`, permission }, ...children]
26
+ : children,
27
+ };
28
+ });
29
+ }
30
+ return visit(value, 0);
31
+ }
@@ -0,0 +1,24 @@
1
+ import type { AppModule } from '../../../app/module';
2
+
3
+ export const menus: AppModule['menus'] = [
4
+ {
5
+ path: '/system/roles',
6
+ name: 'SystemRoles',
7
+ component: 'system/roles',
8
+ meta: { title: '角色管理', authority: ['system:role:list'], keepAlive: true, order: 2 },
9
+ },
10
+ ];
11
+ export const views: AppModule['views'] = { 'system/roles': () => import('./RoleView.vue') };
12
+ export const permissions: AppModule['permissions'] = [
13
+ {
14
+ id: 'system-role',
15
+ label: '角色管理',
16
+ children: [
17
+ { id: 'system:role:list', label: '查询角色', permission: 'system:role:list' },
18
+ { id: 'system:role:create', label: '新增角色', permission: 'system:role:create' },
19
+ { id: 'system:role:update', label: '修改角色', permission: 'system:role:update' },
20
+ { id: 'system:role:delete', label: '删除角色', permission: 'system:role:delete' },
21
+ { id: 'system:role:authorize', label: '角色授权', permission: 'system:role:authorize' },
22
+ ],
23
+ },
24
+ ];
@@ -0,0 +1,12 @@
1
+ import type { Status } from '../../../shared/api';
2
+
3
+ export interface RoleInput {
4
+ code: string;
5
+ name: string;
6
+ description: string;
7
+ status: Status;
8
+ }
9
+ export interface Role extends RoleInput {
10
+ id: string;
11
+ builtIn: boolean;
12
+ }