fetch(modify):修改bug
All checks were successful
构建并部署前端到测试环境 / build-and-deploy (push) Successful in 5m20s
All checks were successful
构建并部署前端到测试环境 / build-and-deploy (push) Successful in 5m20s
This commit is contained in:
@@ -1,46 +1,57 @@
|
||||
import { useRoute } from 'vue-router'
|
||||
import { storeToRefs } from 'pinia'
|
||||
import { useUserStore } from '@/store/modules/user'
|
||||
import { useCommon } from '@/composables/useCommon'
|
||||
import type { AppRouteRecord } from '@/types/router'
|
||||
|
||||
type AuthItem = NonNullable<AppRouteRecord['meta']['authList']>[number]
|
||||
|
||||
const userStore = useUserStore()
|
||||
|
||||
/**
|
||||
* 按钮权限(前后端模式通用)
|
||||
* 权限判断组合式函数
|
||||
* 用于在 setup 函数或表格 formatter 中判断权限
|
||||
*
|
||||
* 用法:
|
||||
* const { hasAuth } = useAuth()
|
||||
* hasAuth('add') // 检查是否拥有新增权限
|
||||
* const { hasAuth, hasAllAuth } = useAuth()
|
||||
* hasAuth('role:add') // 检查是否拥有权限
|
||||
* hasAuth(['role:add', 'role:edit']) // 检查是否拥有任意一个权限
|
||||
* hasAllAuth(['role:add', 'role:edit']) // 检查是否拥有全部权限
|
||||
*/
|
||||
export const useAuth = () => {
|
||||
const route = useRoute()
|
||||
const { isFrontendMode } = useCommon()
|
||||
const { info } = storeToRefs(userStore)
|
||||
|
||||
// 前端按钮权限(例如:['add', 'edit'])
|
||||
const frontendAuthList = info.value?.buttons ?? []
|
||||
|
||||
// 后端路由 meta 配置的权限列表(例如:[{ auth_mark: 'add' }])
|
||||
const backendAuthList: AuthItem[] = Array.isArray(route.meta.authList)
|
||||
? (route.meta.authList as AuthItem[])
|
||||
: []
|
||||
const userStore = useUserStore()
|
||||
|
||||
/**
|
||||
* 检查是否拥有某权限标识
|
||||
* @param auth 权限标识
|
||||
* 检查是否有指定权限(满足任意一个即可)
|
||||
* @param permission 权限标识,可以是字符串或字符串数组
|
||||
* @returns 是否有权限
|
||||
*/
|
||||
const hasAuth = (auth: string): boolean => {
|
||||
if (isFrontendMode.value) {
|
||||
return frontendAuthList.includes(auth)
|
||||
const hasAuth = (permission: string | string[]): boolean => {
|
||||
if (!permission) return false
|
||||
|
||||
if (typeof permission === 'string') {
|
||||
// 单个权限(同时检查 permissions 和 buttons)
|
||||
return userStore.hasPermission(permission) || userStore.hasButton(permission)
|
||||
}
|
||||
|
||||
return backendAuthList.some((item) => item?.auth_mark === auth)
|
||||
if (Array.isArray(permission)) {
|
||||
// 多个权限(满足任意一个即可)
|
||||
return permission.some(
|
||||
(perm) => userStore.hasPermission(perm) || userStore.hasButton(perm)
|
||||
)
|
||||
}
|
||||
|
||||
return false
|
||||
}
|
||||
|
||||
/**
|
||||
* 检查是否有所有指定权限(需要全部满足)
|
||||
* @param permissions 权限标识数组
|
||||
* @returns 是否有全部权限
|
||||
*/
|
||||
const hasAllAuth = (permissions: string[]): boolean => {
|
||||
if (!permissions || !Array.isArray(permissions)) return false
|
||||
|
||||
// 需要全部满足
|
||||
return permissions.every(
|
||||
(perm) => userStore.hasPermission(perm) || userStore.hasButton(perm)
|
||||
)
|
||||
}
|
||||
|
||||
return {
|
||||
hasAuth
|
||||
hasAuth,
|
||||
hasAllAuth
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user