2025-01-23 19:40:49 +08:00
|
|
|
import type { App } from 'vue'
|
|
|
|
import { useUserStore } from '@/store/modules/user'
|
2023-02-11 00:44:00 +08:00
|
|
|
|
|
|
|
const { t } = useI18n() // 国际化
|
|
|
|
|
2025-01-23 19:40:49 +08:00
|
|
|
/** 判断权限的指令 directive */
|
2023-02-11 00:44:00 +08:00
|
|
|
export function hasPermi(app: App<Element>) {
|
|
|
|
app.directive('hasPermi', (el, binding) => {
|
|
|
|
const { value } = binding
|
|
|
|
|
|
|
|
if (value && value instanceof Array && value.length > 0) {
|
2025-01-04 11:14:33 +08:00
|
|
|
const hasPermissions = hasPermission(value)
|
2023-02-11 00:44:00 +08:00
|
|
|
|
|
|
|
if (!hasPermissions) {
|
|
|
|
el.parentNode && el.parentNode.removeChild(el)
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
throw new Error(t('permission.hasPermission'))
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
2025-01-23 19:40:49 +08:00
|
|
|
|
|
|
|
/** 判断权限的方法 function */
|
|
|
|
const userStore = useUserStore()
|
2025-01-21 09:23:37 +08:00
|
|
|
const all_permission = '*:*:*'
|
2025-01-04 11:14:33 +08:00
|
|
|
export const hasPermission = (permission: string[]) => {
|
2025-01-23 19:40:49 +08:00
|
|
|
return (
|
|
|
|
userStore.permissions.has(all_permission) ||
|
|
|
|
permission.some((permission) => userStore.permissions.has(permission))
|
|
|
|
)
|
2025-01-21 09:23:37 +08:00
|
|
|
}
|