Files
basic/src/utils/composables/useAuth.ts
T
Hooray df9fd2ae65 feat: 用户权限变更后,自动更新导航菜单和页面权限,无需刷新页面或者重新登录
重构了路由注册的逻辑,无权限的路由也会被注册,在访问页面时候进行权限控制
2023-03-05 18:05:44 +08:00

36 lines
861 B
TypeScript

import useSettingsStore from '@/store/modules/settings'
import useUserStore from '@/store/modules/user'
export default function useAuth() {
function hasPermission(permission: string) {
const settingsStore = useSettingsStore()
const userStore = useUserStore()
if (settingsStore.settings.app.enablePermission) {
return userStore.permissions.includes(permission)
}
else {
return true
}
}
function auth(value: string | string[]) {
let auth
if (typeof value === 'string') {
auth = value !== '' ? hasPermission(value) : true
}
else {
auth = value.length > 0 ? value.some(item => hasPermission(item)) : true
}
return auth
}
function authAll(value: string[]) {
return value.length > 0 ? value.every(item => hasPermission(item)) : true
}
return {
auth,
authAll,
}
}