2025-01-21 09:23:37 +08:00
|
|
|
import type {App} from 'vue'
|
|
|
|
import {useUserStore} from "@/store/modules/user";
|
2023-02-11 00:44:00 +08:00
|
|
|
|
|
|
|
const { t } = useI18n() // 国际化
|
|
|
|
|
|
|
|
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-21 09:23:37 +08:00
|
|
|
const userStore = useUserStore();
|
|
|
|
const all_permission = '*:*:*'
|
2025-01-04 11:14:33 +08:00
|
|
|
export const hasPermission = (permission: string[]) => {
|
2025-01-21 09:23:37 +08:00
|
|
|
return userStore.permissionsSet.has(all_permission) ||
|
|
|
|
permission.some(permission => userStore.permissionsSet.has(permission))
|
|
|
|
}
|