@kevairy/template 0.1.3 → 0.1.5
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.
- package/package.json +1 -1
- package/template/.env +2 -5
- package/template/.env.production +6 -0
- package/template/README.md +12 -0
- package/template/package.json +5 -5
- package/template/scripts/build-info.ts +76 -0
- package/template/src/adapters/auth/index.ts +1 -1
- package/template/src/adapters/auth/types.ts +6 -1
- package/template/src/api/auth.ts +3 -2
- package/template/src/components/core/layouts/art-guide/index.vue +9 -1
- package/template/src/components/core/layouts/art-page-content/index.vue +8 -1
- package/template/src/config/index.ts +4 -0
- package/template/src/directives/core/access.ts +125 -0
- package/template/src/directives/core/auth.ts +7 -62
- package/template/src/directives/core/roles.ts +5 -83
- package/template/src/env.d.ts +2 -0
- package/template/src/hooks/core/useAuth.ts +2 -74
- package/template/src/hooks/core/useGuide.ts +6 -0
- package/template/src/hooks/core/usePermission.ts +41 -0
- package/template/src/hooks/index.ts +1 -0
- package/template/src/locales/langs/en.json +11 -1
- package/template/src/locales/langs/zh.json +11 -1
- package/template/src/main.ts +18 -10
- package/template/src/router/core/IframeRouteManager.ts +4 -2
- package/template/src/router/core/MenuProcessor.ts +5 -40
- package/template/src/router/core/RoutePermissionValidator.ts +6 -149
- package/template/src/router/core/access.ts +79 -0
- package/template/src/router/guards/afterEach.ts +6 -10
- package/template/src/router/guards/beforeEach.ts +143 -100
- package/template/src/router/guards/error.ts +42 -0
- package/template/src/router/index.ts +2 -0
- package/template/src/router/modules/examples.ts +4 -3
- package/template/src/router/routes/staticRoutes.ts +5 -3
- package/template/src/services/auth.ts +32 -10
- package/template/src/services/permission.ts +21 -0
- package/template/src/store/modules/menu.ts +28 -8
- package/template/src/store/modules/user.ts +72 -35
- package/template/src/store/modules/worktab.ts +1 -1
- package/template/src/types/api/api.d.ts +4 -1
- package/template/src/types/build-info.ts +11 -0
- package/template/src/types/config/index.ts +5 -2
- package/template/src/types/directive/directive.d.ts +1 -0
- package/template/src/types/router/index.ts +6 -0
- package/template/src/utils/http/contract.ts +51 -19
- package/template/src/utils/http/error.ts +73 -26
- package/template/src/utils/http/index.ts +160 -167
- package/template/src/utils/http/request-manager.ts +124 -0
- package/template/src/utils/http/status.ts +1 -0
- package/template/src/utils/permission/index.ts +66 -0
- package/template/src/utils/session.ts +24 -0
- package/template/src/utils/storage/storage-config.ts +15 -0
- package/template/src/utils/storage/storage.ts +5 -3
- package/template/src/utils/sys/console.ts +63 -7
- package/template/src/utils/sys/diagnostics.ts +180 -0
- package/template/src/utils/sys/error-handle.ts +56 -126
- package/template/src/utils/sys/recovery.ts +31 -0
- package/template/src/views/examples/permissions/index.vue +69 -13
- package/template/src/views/exception/403/index.vue +33 -0
- package/template/src/views/exception/500/index.vue +52 -10
- package/template/tests/e2e/permissions.spec.ts +53 -0
- package/template/tests/e2e/session-recovery.spec.ts +49 -0
- package/template/tests/unit/build/build-info.spec.ts +151 -0
- package/template/tests/unit/diagnostics/diagnostics.spec.ts +132 -0
- package/template/tests/unit/diagnostics/router.spec.ts +84 -0
- package/template/tests/unit/http/auth-lifecycle.spec.ts +203 -0
- package/template/tests/unit/http/contract.spec.ts +46 -0
- package/template/tests/unit/http/request.spec.ts +487 -0
- package/template/tests/unit/permissions/guard.spec.ts +261 -0
- package/template/tests/unit/permissions/guide.spec.ts +30 -0
- package/template/tests/unit/permissions/rules.spec.ts +106 -0
- package/template/tests/unit/permissions/state-and-directives.spec.ts +176 -0
- package/template/tests/unit/router/examples.spec.ts +5 -1
- package/template/tsconfig.json +8 -1
- package/template/vite.config.ts +19 -9
package/package.json
CHANGED
package/template/.env
CHANGED
|
@@ -1,8 +1,5 @@
|
|
|
1
1
|
# 【通用】环境变量
|
|
2
2
|
|
|
3
|
-
# 版本号
|
|
4
|
-
VITE_VERSION = 0.1.0
|
|
5
|
-
|
|
6
3
|
# 端口号
|
|
7
4
|
VITE_PORT = 3006
|
|
8
5
|
|
|
@@ -18,8 +15,8 @@ VITE_WITH_CREDENTIALS = false
|
|
|
18
15
|
# Authorization 头前缀;接入业务后端时可按约定设置为 Bearer 等值。
|
|
19
16
|
VITE_AUTH_SCHEME =
|
|
20
17
|
|
|
21
|
-
#
|
|
22
|
-
#
|
|
18
|
+
# GET/HEAD 在网络故障、超时或指定 HTTP 5xx 下的额外重试次数,默认 0。
|
|
19
|
+
# 写请求默认不重试;确认后端幂等后,按单请求 retry.allowUnsafe 显式开启。
|
|
23
20
|
VITE_HTTP_MAX_RETRIES = 0
|
|
24
21
|
|
|
25
22
|
# 是否打开路由信息
|
package/template/.env.production
CHANGED
package/template/README.md
CHANGED
|
@@ -20,3 +20,15 @@ __RUN_COMMAND__ typecheck
|
|
|
20
20
|
__RUN_COMMAND__ test
|
|
21
21
|
__RUN_COMMAND__ build
|
|
22
22
|
```
|
|
23
|
+
|
|
24
|
+
## 版本与构建
|
|
25
|
+
|
|
26
|
+
发布版本由项目根目录 package.json 的 version 管理。每次执行构建命令会自动生成独立的构建编号、UTC 构建时间、Git 提交和未提交状态,不修改版本文件。
|
|
27
|
+
|
|
28
|
+
浏览器控制台在生产环境也会显示一次构建信息;dist/version.json 和错误诊断使用相同的发布版本与构建编号。没有 Git 也可以正常构建,无法确认的提交和工作区状态标为 null。
|
|
29
|
+
|
|
30
|
+
CI 可以通过 VITE_BUILD_ID 覆盖构建编号(应保证每次构建唯一),通过进程环境变量 BUILD_GIT_COMMIT 和 BUILD_GIT_DIRTY 注入提交与工作区状态。BUILD_GIT_DIRTY 仅接受 true 或 false;只注入提交时,工作区状态标为未知。
|
|
31
|
+
|
|
32
|
+
部署时将 version.json 与同一批静态资源一起发布,并对该文件配置 Cache-Control: no-store。浏览器输出代表当前页面已加载的版本,服务器的 version.json 代表服务器当前提供的版本。
|
|
33
|
+
|
|
34
|
+
准备正式发布时再升级 package.json 的 version,例如运行 npm version patch --no-git-tag-version;自动构建编号不参与本地存储版本迁移。
|
package/template/package.json
CHANGED
|
@@ -17,7 +17,7 @@
|
|
|
17
17
|
"@vueuse/core": "13.9.0",
|
|
18
18
|
"@wangeditor/editor": "5.1.23",
|
|
19
19
|
"@wangeditor/editor-for-vue": "5.1.12",
|
|
20
|
-
"axios": "1.
|
|
20
|
+
"axios": "1.21.1",
|
|
21
21
|
"driver.js": "1.9.0",
|
|
22
22
|
"echarts": "6.1.0",
|
|
23
23
|
"element-plus": "2.14.4",
|
|
@@ -29,8 +29,8 @@
|
|
|
29
29
|
"pinia-plugin-persistedstate": "4.7.1",
|
|
30
30
|
"qrcode.vue": "3.10.0",
|
|
31
31
|
"tailwindcss": "4.3.3",
|
|
32
|
-
"vue": "3.5.
|
|
33
|
-
"vue-i18n": "11.4.
|
|
32
|
+
"vue": "3.5.45",
|
|
33
|
+
"vue-i18n": "11.4.18",
|
|
34
34
|
"vue-router": "4.6.4",
|
|
35
35
|
"xgplayer": "3.0.26"
|
|
36
36
|
},
|
|
@@ -65,7 +65,7 @@
|
|
|
65
65
|
"@playwright/test": "1.62.1",
|
|
66
66
|
"@types/node": "24.13.3",
|
|
67
67
|
"@vitejs/plugin-vue": "6.0.8",
|
|
68
|
-
"@vue/compiler-sfc": "3.5.
|
|
68
|
+
"@vue/compiler-sfc": "3.5.45",
|
|
69
69
|
"@vue/test-utils": "2.4.11",
|
|
70
70
|
"happy-dom": "20.11.2",
|
|
71
71
|
"rollup-plugin-visualizer": "5.14.0",
|
|
@@ -74,7 +74,7 @@
|
|
|
74
74
|
"unplugin-auto-import": "20.3.0",
|
|
75
75
|
"unplugin-element-plus": "0.10.0",
|
|
76
76
|
"unplugin-vue-components": "29.2.0",
|
|
77
|
-
"vite": "7.3.
|
|
77
|
+
"vite": "7.3.8",
|
|
78
78
|
"vitest": "4.1.10",
|
|
79
79
|
"vue-demi": "0.14.10",
|
|
80
80
|
"vue-img-cutter": "3.1.1",
|
|
@@ -0,0 +1,76 @@
|
|
|
1
|
+
import { execFileSync } from 'node:child_process'
|
|
2
|
+
import { randomUUID } from 'node:crypto'
|
|
3
|
+
import { readFileSync } from 'node:fs'
|
|
4
|
+
import path from 'node:path'
|
|
5
|
+
import type { Plugin } from 'vite'
|
|
6
|
+
import type { BuildInfo } from '../src/types/build-info'
|
|
7
|
+
|
|
8
|
+
interface BuildInfoOptions {
|
|
9
|
+
root: string
|
|
10
|
+
mode: string
|
|
11
|
+
buildId?: string
|
|
12
|
+
gitCommit?: string
|
|
13
|
+
gitDirty?: string
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
function readGit(root: string, args: string[]): string | null {
|
|
17
|
+
try {
|
|
18
|
+
return execFileSync('git', args, {
|
|
19
|
+
cwd: root,
|
|
20
|
+
encoding: 'utf8',
|
|
21
|
+
stdio: ['ignore', 'pipe', 'ignore'],
|
|
22
|
+
timeout: 3000
|
|
23
|
+
}).trim()
|
|
24
|
+
} catch {
|
|
25
|
+
return null
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
export function createBuildInfo(options: BuildInfoOptions): BuildInfo {
|
|
30
|
+
const { version } = JSON.parse(readFileSync(path.join(options.root, 'package.json'), 'utf8'))
|
|
31
|
+
if (typeof version !== 'string' || !version.trim()) {
|
|
32
|
+
throw new Error('package.json 必须包含非空的 version 字段')
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
const gitCommit = options.gitCommit?.trim() || readGit(options.root, ['rev-parse', 'HEAD'])
|
|
36
|
+
// CI 指定提交时,不把构建机工作区状态误当成该提交的状态。
|
|
37
|
+
const status = options.gitCommit?.trim()
|
|
38
|
+
? null
|
|
39
|
+
: readGit(options.root, ['status', '--porcelain', '--untracked-files=normal'])
|
|
40
|
+
if (options.gitDirty && !['true', 'false'].includes(options.gitDirty)) {
|
|
41
|
+
throw new Error('BUILD_GIT_DIRTY 只允许 true 或 false')
|
|
42
|
+
}
|
|
43
|
+
const gitDirty = options.gitDirty
|
|
44
|
+
? options.gitDirty === 'true'
|
|
45
|
+
: status === null
|
|
46
|
+
? null
|
|
47
|
+
: status.length > 0
|
|
48
|
+
const builtAt = new Date().toISOString()
|
|
49
|
+
const timestamp = builtAt.replace(/[-:.]/g, '')
|
|
50
|
+
const buildId =
|
|
51
|
+
options.buildId?.trim() ||
|
|
52
|
+
`${timestamp}-${gitCommit?.slice(0, 12) || 'nogit'}${gitDirty ? '-dirty' : ''}-${randomUUID().slice(0, 8)}`
|
|
53
|
+
|
|
54
|
+
return { version, buildId, gitCommit, gitDirty, builtAt, mode: options.mode }
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
/** 常量与静态清单复用同一份快照,不在输出阶段重新生成编号。 */
|
|
58
|
+
export function buildInfoPlugin(info: BuildInfo): Plugin {
|
|
59
|
+
return {
|
|
60
|
+
name: 'app-build-info',
|
|
61
|
+
config: () => ({
|
|
62
|
+
define: {
|
|
63
|
+
__APP_VERSION__: JSON.stringify(info.version),
|
|
64
|
+
__BUILD_ID__: JSON.stringify(info.buildId),
|
|
65
|
+
__BUILD_INFO__: JSON.stringify(info)
|
|
66
|
+
}
|
|
67
|
+
}),
|
|
68
|
+
generateBundle() {
|
|
69
|
+
this.emitFile({
|
|
70
|
+
type: 'asset',
|
|
71
|
+
fileName: 'version.json',
|
|
72
|
+
source: `${JSON.stringify(info, null, 2)}\n`
|
|
73
|
+
})
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
}
|
|
@@ -3,9 +3,14 @@
|
|
|
3
3
|
*
|
|
4
4
|
* 页面、Store 和路由守卫只依赖此契约;项目接口字段必须在适配器内部转换。
|
|
5
5
|
*/
|
|
6
|
+
export interface LogoutContext {
|
|
7
|
+
/** HTTP 退出请求必须透传此 scope,普通请求在退出期间会被取消。 */
|
|
8
|
+
requestScope: symbol
|
|
9
|
+
}
|
|
10
|
+
|
|
6
11
|
export interface AuthAdapter {
|
|
7
12
|
readonly name: string
|
|
8
13
|
login(params: Api.Auth.LoginParams): Promise<Api.Auth.LoginResponse>
|
|
9
14
|
getUserInfo(): Promise<Api.Auth.UserInfo>
|
|
10
|
-
logout(): Promise<void>
|
|
15
|
+
logout(context?: LogoutContext): Promise<void>
|
|
11
16
|
}
|
package/template/src/api/auth.ts
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import { authAdapter } from '@/adapters/auth'
|
|
2
|
+
import type { LogoutContext } from '@/adapters/auth'
|
|
2
3
|
|
|
3
4
|
/**
|
|
4
5
|
* 登录
|
|
@@ -18,6 +19,6 @@ export function fetchGetUserInfo() {
|
|
|
18
19
|
}
|
|
19
20
|
|
|
20
21
|
/** 退出当前账号 */
|
|
21
|
-
export function fetchLogout() {
|
|
22
|
-
return authAdapter.logout()
|
|
22
|
+
export function fetchLogout(context?: LogoutContext) {
|
|
23
|
+
return authAdapter.logout(context)
|
|
23
24
|
}
|
|
@@ -6,6 +6,7 @@
|
|
|
6
6
|
</template>
|
|
7
7
|
|
|
8
8
|
<script setup lang="ts">
|
|
9
|
+
import { nextTick, onMounted, onUnmounted } from 'vue'
|
|
9
10
|
import 'driver.js/dist/driver.css'
|
|
10
11
|
import { useGuide } from '@/hooks/core/useGuide'
|
|
11
12
|
import { useSettingStore } from '@/store/modules/setting'
|
|
@@ -16,6 +17,8 @@
|
|
|
16
17
|
|
|
17
18
|
const settingStore = useSettingStore()
|
|
18
19
|
const { start, destroy } = useGuide()
|
|
20
|
+
let startTimer: ReturnType<typeof setTimeout> | undefined
|
|
21
|
+
let unmounted = false
|
|
19
22
|
|
|
20
23
|
onMounted(() => {
|
|
21
24
|
// 用户菜单「新手引导」入口:手动重新播放
|
|
@@ -24,12 +27,17 @@
|
|
|
24
27
|
// 首次进入且功能开启时自动播放;延迟等待顶栏各按钮渲染完成
|
|
25
28
|
if (AppConfig.features.guide && !settingStore.hasCompletedGuide) {
|
|
26
29
|
nextTick(() => {
|
|
27
|
-
|
|
30
|
+
if (unmounted) return
|
|
31
|
+
startTimer = setTimeout(() => {
|
|
32
|
+
if (!settingStore.hasCompletedGuide) start()
|
|
33
|
+
}, 600)
|
|
28
34
|
})
|
|
29
35
|
}
|
|
30
36
|
})
|
|
31
37
|
|
|
32
38
|
onUnmounted(() => {
|
|
39
|
+
unmounted = true
|
|
40
|
+
clearTimeout(startTimer)
|
|
33
41
|
mittBus.off('startGuide', start)
|
|
34
42
|
destroy()
|
|
35
43
|
})
|
|
@@ -14,7 +14,12 @@
|
|
|
14
14
|
</div>
|
|
15
15
|
</div>
|
|
16
16
|
|
|
17
|
-
<RouterView
|
|
17
|
+
<RouterView
|
|
18
|
+
v-if="isRefresh"
|
|
19
|
+
:key="sessionEpoch"
|
|
20
|
+
v-slot="{ Component, route }"
|
|
21
|
+
:style="contentStyle"
|
|
22
|
+
>
|
|
18
23
|
<!-- 缓存路由动画 -->
|
|
19
24
|
<Transition :name="showTransitionMask ? '' : actualTransition" mode="out-in" appear>
|
|
20
25
|
<KeepAlive :max="10" :exclude="keepAliveExclude">
|
|
@@ -53,6 +58,7 @@
|
|
|
53
58
|
import { useAutoLayoutHeight } from '@/hooks/core/useLayoutHeight'
|
|
54
59
|
import { useSettingStore } from '@/store/modules/setting'
|
|
55
60
|
import { useWorktabStore } from '@/store/modules/worktab'
|
|
61
|
+
import { useUserStore } from '@/store/modules/user'
|
|
56
62
|
import AppConfig from '@/config'
|
|
57
63
|
|
|
58
64
|
defineOptions({ name: 'ArtPageContent' })
|
|
@@ -61,6 +67,7 @@
|
|
|
61
67
|
const { containerMinHeight } = useAutoLayoutHeight()
|
|
62
68
|
const { pageTransition, containerWidth, refresh } = storeToRefs(useSettingStore())
|
|
63
69
|
const { keepAliveExclude } = storeToRefs(useWorktabStore())
|
|
70
|
+
const { sessionEpoch } = storeToRefs(useUserStore())
|
|
64
71
|
|
|
65
72
|
const isRefresh = shallowRef(true)
|
|
66
73
|
const isOpenRouteInfo = import.meta.env.VITE_OPEN_ROUTE_INFO
|
|
@@ -50,6 +50,10 @@ const appConfig: SystemConfig = {
|
|
|
50
50
|
loginRouteName: 'Login',
|
|
51
51
|
titleTemplate: '{page} - {app}'
|
|
52
52
|
},
|
|
53
|
+
accessControl: {
|
|
54
|
+
// 设置为 false 可关闭超级权限通配符;不根据角色名称推断超级权限。
|
|
55
|
+
wildcardPermission: '*:*:*'
|
|
56
|
+
},
|
|
53
57
|
features: {
|
|
54
58
|
menuButton: true,
|
|
55
59
|
refreshButton: true,
|
|
@@ -0,0 +1,125 @@
|
|
|
1
|
+
import {
|
|
2
|
+
shallowRef,
|
|
3
|
+
watchEffect,
|
|
4
|
+
type ObjectDirective,
|
|
5
|
+
type ShallowRef,
|
|
6
|
+
type WatchStopHandle
|
|
7
|
+
} from 'vue'
|
|
8
|
+
import { usePermission } from '@/hooks/core/usePermission'
|
|
9
|
+
import type { PermissionRequirement } from '@/utils/permission'
|
|
10
|
+
|
|
11
|
+
interface ElementState {
|
|
12
|
+
binding: ShallowRef<{ value: PermissionRequirement; disabled: boolean; all: boolean }>
|
|
13
|
+
stop: WatchStopHandle
|
|
14
|
+
restore: () => void
|
|
15
|
+
capture: () => void
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
/** 隐藏而非移除节点,权限恢复时 Vue 仍能管理、更新原节点。 */
|
|
19
|
+
export function createAccessDirective(
|
|
20
|
+
role = false
|
|
21
|
+
): ObjectDirective<HTMLElement, PermissionRequirement> {
|
|
22
|
+
const states = new WeakMap<HTMLElement, ElementState>()
|
|
23
|
+
return {
|
|
24
|
+
beforeMount(el, binding) {
|
|
25
|
+
const permission = usePermission()
|
|
26
|
+
const original = {
|
|
27
|
+
display: el.style.display,
|
|
28
|
+
displayPriority: el.style.getPropertyPriority('display'),
|
|
29
|
+
disabled: el.getAttribute('disabled'),
|
|
30
|
+
ariaDisabled: el.getAttribute('aria-disabled'),
|
|
31
|
+
disabledClass: el.classList.contains('is-disabled'),
|
|
32
|
+
pointerEvents: el.style.pointerEvents
|
|
33
|
+
}
|
|
34
|
+
let applied = false
|
|
35
|
+
const current = shallowRef({
|
|
36
|
+
value: binding.value,
|
|
37
|
+
disabled: !!binding.modifiers.disabled,
|
|
38
|
+
all: !!binding.modifiers.all
|
|
39
|
+
})
|
|
40
|
+
const restore = () => {
|
|
41
|
+
if (!applied) return
|
|
42
|
+
el.style.setProperty('display', original.display, original.displayPriority)
|
|
43
|
+
el.style.pointerEvents = original.pointerEvents
|
|
44
|
+
if (original.disabled === null) el.removeAttribute('disabled')
|
|
45
|
+
else el.setAttribute('disabled', original.disabled)
|
|
46
|
+
if (original.ariaDisabled === null) el.removeAttribute('aria-disabled')
|
|
47
|
+
else el.setAttribute('aria-disabled', original.ariaDisabled)
|
|
48
|
+
el.classList.toggle('is-disabled', original.disabledClass)
|
|
49
|
+
applied = false
|
|
50
|
+
}
|
|
51
|
+
const allowed = () => {
|
|
52
|
+
const { value, all } = current.value
|
|
53
|
+
return role
|
|
54
|
+
? all
|
|
55
|
+
? permission.hasAllRoles(value)
|
|
56
|
+
: permission.hasRole(value)
|
|
57
|
+
: all
|
|
58
|
+
? permission.hasAllPermissions(value)
|
|
59
|
+
: permission.hasPermission(value)
|
|
60
|
+
}
|
|
61
|
+
// disabled 属性只对原生表单元素生效,其他元素同时拦截鼠标和键盘激活。
|
|
62
|
+
const block = (event: Event) => {
|
|
63
|
+
if (event instanceof KeyboardEvent && !['Enter', ' '].includes(event.key)) return
|
|
64
|
+
if (!allowed()) {
|
|
65
|
+
event.preventDefault()
|
|
66
|
+
event.stopImmediatePropagation()
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
el.addEventListener('click', block, true)
|
|
70
|
+
el.addEventListener('keydown', block, true)
|
|
71
|
+
const stop = watchEffect(
|
|
72
|
+
() => {
|
|
73
|
+
restore()
|
|
74
|
+
if (allowed()) return
|
|
75
|
+
applied = true
|
|
76
|
+
if (current.value.disabled) {
|
|
77
|
+
el.setAttribute('disabled', '')
|
|
78
|
+
el.setAttribute('aria-disabled', 'true')
|
|
79
|
+
el.classList.add('is-disabled')
|
|
80
|
+
el.style.pointerEvents = 'none'
|
|
81
|
+
} else {
|
|
82
|
+
el.style.setProperty('display', 'none', 'important')
|
|
83
|
+
}
|
|
84
|
+
},
|
|
85
|
+
{ flush: 'sync' }
|
|
86
|
+
)
|
|
87
|
+
states.set(el, {
|
|
88
|
+
binding: current,
|
|
89
|
+
restore,
|
|
90
|
+
capture: () => {
|
|
91
|
+
original.display = el.style.display
|
|
92
|
+
original.displayPriority = el.style.getPropertyPriority('display')
|
|
93
|
+
original.disabled = el.getAttribute('disabled')
|
|
94
|
+
original.ariaDisabled = el.getAttribute('aria-disabled')
|
|
95
|
+
original.disabledClass = el.classList.contains('is-disabled')
|
|
96
|
+
original.pointerEvents = el.style.pointerEvents
|
|
97
|
+
},
|
|
98
|
+
stop: () => {
|
|
99
|
+
stop()
|
|
100
|
+
el.removeEventListener('click', block, true)
|
|
101
|
+
el.removeEventListener('keydown', block, true)
|
|
102
|
+
restore()
|
|
103
|
+
}
|
|
104
|
+
})
|
|
105
|
+
},
|
|
106
|
+
beforeUpdate(el) {
|
|
107
|
+
states.get(el)?.restore()
|
|
108
|
+
},
|
|
109
|
+
updated(el, binding) {
|
|
110
|
+
const state = states.get(el)
|
|
111
|
+
if (state) {
|
|
112
|
+
state.capture()
|
|
113
|
+
state.binding.value = {
|
|
114
|
+
value: binding.value,
|
|
115
|
+
disabled: !!binding.modifiers.disabled,
|
|
116
|
+
all: !!binding.modifiers.all
|
|
117
|
+
}
|
|
118
|
+
}
|
|
119
|
+
},
|
|
120
|
+
unmounted(el) {
|
|
121
|
+
states.get(el)?.stop()
|
|
122
|
+
states.delete(el)
|
|
123
|
+
}
|
|
124
|
+
}
|
|
125
|
+
}
|
|
@@ -1,66 +1,11 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
* 适用于后端权限控制模式,基于权限标识控制 DOM 元素的显示和隐藏。
|
|
5
|
-
* 如果用户没有对应权限,元素将从 DOM 中移除。
|
|
6
|
-
*
|
|
7
|
-
* ## 主要功能
|
|
8
|
-
*
|
|
9
|
-
* - 权限验证 - 根据路由 meta 中的权限列表验证用户权限
|
|
10
|
-
* - DOM 控制 - 无权限时自动移除元素,而非隐藏
|
|
11
|
-
* - 响应式更新 - 权限变化时自动更新元素状态
|
|
12
|
-
*
|
|
13
|
-
* ## 使用示例
|
|
14
|
-
*
|
|
15
|
-
* ```vue
|
|
16
|
-
* <!-- 只有拥有 'add' 权限的用户才能看到新增按钮 -->
|
|
17
|
-
* <el-button v-auth="'add'">新增</el-button>
|
|
18
|
-
*
|
|
19
|
-
* <!-- 只有拥有 'edit' 权限的用户才能看到编辑按钮 -->
|
|
20
|
-
* <el-button v-auth="'edit'">编辑</el-button>
|
|
21
|
-
*
|
|
22
|
-
* <!-- 只有拥有 'delete' 权限的用户才能看到删除按钮 -->
|
|
23
|
-
* <el-button v-auth="'delete'">删除</el-button>
|
|
24
|
-
* ```
|
|
25
|
-
*
|
|
26
|
-
* ## 注意事项
|
|
27
|
-
*
|
|
28
|
-
* - 该指令会直接移除 DOM 元素,而不是使用 v-if 隐藏
|
|
29
|
-
* - 权限列表从当前路由的 meta.authList 中获取
|
|
30
|
-
*
|
|
31
|
-
* @module directives/auth
|
|
32
|
-
* @author Art Design Pro Team
|
|
33
|
-
*/
|
|
1
|
+
import type { App, Directive } from 'vue'
|
|
2
|
+
import type { PermissionRequirement } from '@/utils/permission'
|
|
3
|
+
import { createAccessDirective } from './access'
|
|
34
4
|
|
|
35
|
-
|
|
36
|
-
import { App, Directive, DirectiveBinding } from 'vue'
|
|
37
|
-
|
|
38
|
-
export type AuthDirective = Directive<HTMLElement, string>
|
|
39
|
-
|
|
40
|
-
function checkAuthPermission(el: HTMLElement, binding: DirectiveBinding<string>): void {
|
|
41
|
-
// 获取当前路由的权限列表
|
|
42
|
-
const authList = (router.currentRoute.value.meta.authList as Array<{ authMark: string }>) || []
|
|
43
|
-
|
|
44
|
-
// 检查是否有对应的权限标识
|
|
45
|
-
const hasPermission = authList.some((item) => item.authMark === binding.value)
|
|
46
|
-
|
|
47
|
-
// 如果没有权限,移除元素
|
|
48
|
-
if (!hasPermission) {
|
|
49
|
-
removeElement(el)
|
|
50
|
-
}
|
|
51
|
-
}
|
|
52
|
-
|
|
53
|
-
function removeElement(el: HTMLElement): void {
|
|
54
|
-
if (el.parentNode) {
|
|
55
|
-
el.parentNode.removeChild(el)
|
|
56
|
-
}
|
|
57
|
-
}
|
|
58
|
-
|
|
59
|
-
const authDirective: AuthDirective = {
|
|
60
|
-
mounted: checkAuthPermission,
|
|
61
|
-
updated: checkAuthPermission
|
|
62
|
-
}
|
|
5
|
+
export type AuthDirective = Directive<HTMLElement, PermissionRequirement>
|
|
63
6
|
|
|
7
|
+
/** v-permission 为标准入口,v-auth 保留兼容;支持 .all 与 .disabled。 */
|
|
64
8
|
export function setupAuthDirective(app: App): void {
|
|
65
|
-
app.directive('
|
|
9
|
+
app.directive('permission', createAccessDirective())
|
|
10
|
+
app.directive('auth', createAccessDirective())
|
|
66
11
|
}
|
|
@@ -1,87 +1,9 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
* 基于用户角色控制 DOM 元素的显示和隐藏。
|
|
5
|
-
* 只要用户拥有指定角色中的任意一个,元素就会显示,否则从 DOM 中移除。
|
|
6
|
-
*
|
|
7
|
-
* ## 主要功能
|
|
8
|
-
*
|
|
9
|
-
* - 角色验证 - 检查用户是否拥有指定角色
|
|
10
|
-
* - 多角色支持 - 支持单个角色或多个角色(满足其一即可)
|
|
11
|
-
* - DOM 控制 - 无权限时自动移除元素,而非隐藏
|
|
12
|
-
* - 响应式更新 - 角色变化时自动更新元素状态
|
|
13
|
-
*
|
|
14
|
-
* ## 使用示例
|
|
15
|
-
*
|
|
16
|
-
* ```vue
|
|
17
|
-
* <template>
|
|
18
|
-
* <!-- 单个角色 - 只有超级管理员可见 -->
|
|
19
|
-
* <el-button v-roles="'R_SUPER'">超级管理员功能</el-button>
|
|
20
|
-
*
|
|
21
|
-
* <!-- 多个角色 - 超级管理员或普通管理员可见 -->
|
|
22
|
-
* <el-button v-roles="['R_SUPER', 'R_ADMIN']">管理员功能</el-button>
|
|
23
|
-
*
|
|
24
|
-
* <!-- 应用到任意元素 -->
|
|
25
|
-
* <div v-roles="['R_SUPER', 'R_ADMIN', 'R_USER']">
|
|
26
|
-
* 所有登录用户可见的内容
|
|
27
|
-
* </div>
|
|
28
|
-
* </template>
|
|
29
|
-
* ```
|
|
30
|
-
*
|
|
31
|
-
* ## 权限逻辑
|
|
32
|
-
*
|
|
33
|
-
* - 用户角色从 userStore.getUserInfo.roles 获取
|
|
34
|
-
* - 只要用户拥有指定角色中的任意一个,元素就会显示
|
|
35
|
-
* - 如果用户没有任何角色或不满足条件,元素将被移除
|
|
36
|
-
*
|
|
37
|
-
* ## 注意事项
|
|
38
|
-
*
|
|
39
|
-
* - 该指令会直接移除 DOM 元素,而不是使用 v-if 隐藏
|
|
40
|
-
* - 适用于基于角色的粗粒度权限控制
|
|
41
|
-
* - 如需基于具体操作的细粒度权限控制,请使用 v-auth 指令
|
|
42
|
-
*
|
|
43
|
-
* @module directives/roles
|
|
44
|
-
* @author Art Design Pro Team
|
|
45
|
-
*/
|
|
1
|
+
import type { App, Directive } from 'vue'
|
|
2
|
+
import type { PermissionRequirement } from '@/utils/permission'
|
|
3
|
+
import { createAccessDirective } from './access'
|
|
46
4
|
|
|
47
|
-
|
|
48
|
-
import { App, Directive, DirectiveBinding } from 'vue'
|
|
49
|
-
|
|
50
|
-
export type RolesDirective = Directive<HTMLElement, string | string[]>
|
|
51
|
-
|
|
52
|
-
function checkRolePermission(el: HTMLElement, binding: DirectiveBinding<string | string[]>): void {
|
|
53
|
-
const userStore = useUserStore()
|
|
54
|
-
const userRoles = userStore.getUserInfo.roles
|
|
55
|
-
|
|
56
|
-
// 如果用户角色为空或未定义,移除元素
|
|
57
|
-
if (!userRoles?.length) {
|
|
58
|
-
removeElement(el)
|
|
59
|
-
return
|
|
60
|
-
}
|
|
61
|
-
|
|
62
|
-
// 确保指令值为数组格式
|
|
63
|
-
const requiredRoles = Array.isArray(binding.value) ? binding.value : [binding.value]
|
|
64
|
-
|
|
65
|
-
// 检查用户是否具有所需角色之一
|
|
66
|
-
const hasPermission = requiredRoles.some((role: string) => userRoles.includes(role))
|
|
67
|
-
|
|
68
|
-
// 如果没有权限,安全地移除元素
|
|
69
|
-
if (!hasPermission) {
|
|
70
|
-
removeElement(el)
|
|
71
|
-
}
|
|
72
|
-
}
|
|
73
|
-
|
|
74
|
-
function removeElement(el: HTMLElement): void {
|
|
75
|
-
if (el.parentNode) {
|
|
76
|
-
el.parentNode.removeChild(el)
|
|
77
|
-
}
|
|
78
|
-
}
|
|
79
|
-
|
|
80
|
-
const rolesDirective: RolesDirective = {
|
|
81
|
-
mounted: checkRolePermission,
|
|
82
|
-
updated: checkRolePermission
|
|
83
|
-
}
|
|
5
|
+
export type RolesDirective = Directive<HTMLElement, PermissionRequirement>
|
|
84
6
|
|
|
85
7
|
export function setupRolesDirective(app: App): void {
|
|
86
|
-
app.directive('roles',
|
|
8
|
+
app.directive('roles', createAccessDirective(true))
|
|
87
9
|
}
|
package/template/src/env.d.ts
CHANGED
|
@@ -1,74 +1,2 @@
|
|
|
1
|
-
/**
|
|
2
|
-
|
|
3
|
-
*
|
|
4
|
-
* 提供统一的权限验证功能,支持前端和后端两种权限模式。
|
|
5
|
-
* 用于控制页面按钮、操作等功能的显示和访问权限。
|
|
6
|
-
*
|
|
7
|
-
* ## 主要功能
|
|
8
|
-
*
|
|
9
|
-
* 1. 权限检查 - 检查用户是否拥有指定的权限标识
|
|
10
|
-
* 2. 双模式支持 - 自动适配前端模式和后端模式的权限验证
|
|
11
|
-
* 3. 前端模式 - 从用户信息中获取按钮权限列表(如 ['add', 'edit', 'delete'])
|
|
12
|
-
* 4. 后端模式 - 从路由 meta 配置中获取权限列表(如 [{ authMark: 'add' }])
|
|
13
|
-
*
|
|
14
|
-
* ## 使用示例
|
|
15
|
-
*
|
|
16
|
-
* ```typescript
|
|
17
|
-
* const { hasAuth } = useAuth()
|
|
18
|
-
*
|
|
19
|
-
* // 检查是否有新增权限
|
|
20
|
-
* if (hasAuth('add')) {
|
|
21
|
-
* // 显示新增按钮
|
|
22
|
-
* }
|
|
23
|
-
*
|
|
24
|
-
* // 在模板中使用
|
|
25
|
-
* <el-button v-if="hasAuth('edit')">编辑</el-button>
|
|
26
|
-
* <el-button v-if="hasAuth('delete')">删除</el-button>
|
|
27
|
-
* ```
|
|
28
|
-
*
|
|
29
|
-
* @module useAuth
|
|
30
|
-
* @author Art Design Pro Team
|
|
31
|
-
*/
|
|
32
|
-
|
|
33
|
-
import { useRoute } from 'vue-router'
|
|
34
|
-
import { storeToRefs } from 'pinia'
|
|
35
|
-
import { useUserStore } from '@/store/modules/user'
|
|
36
|
-
import { useAppMode } from '@/hooks/core/useAppMode'
|
|
37
|
-
import type { AppRouteRecord } from '@/types/router'
|
|
38
|
-
|
|
39
|
-
type AuthItem = NonNullable<AppRouteRecord['meta']['authList']>[number]
|
|
40
|
-
|
|
41
|
-
const userStore = useUserStore()
|
|
42
|
-
|
|
43
|
-
export const useAuth = () => {
|
|
44
|
-
const route = useRoute()
|
|
45
|
-
const { isFrontendMode } = useAppMode()
|
|
46
|
-
const { info } = storeToRefs(userStore)
|
|
47
|
-
|
|
48
|
-
// 前端按钮权限(例如:['add', 'edit'])
|
|
49
|
-
const frontendAuthList = info.value?.buttons ?? []
|
|
50
|
-
|
|
51
|
-
// 后端路由 meta 配置的权限列表(例如:[{ authMark: 'add' }])
|
|
52
|
-
const backendAuthList: AuthItem[] = Array.isArray(route.meta.authList)
|
|
53
|
-
? (route.meta.authList as AuthItem[])
|
|
54
|
-
: []
|
|
55
|
-
|
|
56
|
-
/**
|
|
57
|
-
* 检查是否拥有某权限标识(前后端模式通用)
|
|
58
|
-
* @param auth 权限标识
|
|
59
|
-
* @returns 是否有权限
|
|
60
|
-
*/
|
|
61
|
-
const hasAuth = (auth: string): boolean => {
|
|
62
|
-
// 前端模式
|
|
63
|
-
if (isFrontendMode.value) {
|
|
64
|
-
return frontendAuthList.includes(auth)
|
|
65
|
-
}
|
|
66
|
-
|
|
67
|
-
// 后端模式
|
|
68
|
-
return backendAuthList.some((item) => item?.authMark === auth)
|
|
69
|
-
}
|
|
70
|
-
|
|
71
|
-
return {
|
|
72
|
-
hasAuth
|
|
73
|
-
}
|
|
74
|
-
}
|
|
1
|
+
/** 兼容原有 hasAuth 调用;新页面优先使用 usePermission。 */
|
|
2
|
+
export { usePermission as useAuth } from './usePermission'
|