2024-04-28 16:20:45 +08:00
|
|
|
import type { App } from 'vue'
|
2024-08-01 18:41:37 +08:00
|
|
|
import { CACHE_KEY } from '@/hooks/web/useCache'
|
|
|
|
|
import cache from '@/plugins/cache'
|
2024-04-28 16:20:45 +08:00
|
|
|
|
|
|
|
|
const { t } = useI18n() // 国际化
|
|
|
|
|
|
|
|
|
|
export function hasPermi(app: App<Element>) {
|
|
|
|
|
app.directive('hasPermi', (el, binding) => {
|
|
|
|
|
const { value } = binding
|
|
|
|
|
const all_permission = '*:*:*'
|
2024-08-01 18:41:37 +08:00
|
|
|
const permissions = cache.local.get(CACHE_KEY.USER)?.permissions || []
|
2024-04-28 16:20:45 +08:00
|
|
|
|
|
|
|
|
if (value && value instanceof Array && value.length > 0) {
|
|
|
|
|
const permissionFlag = value
|
|
|
|
|
|
|
|
|
|
const hasPermissions = permissions.some((permission: string) => {
|
|
|
|
|
return all_permission === permission || permissionFlag.includes(permission)
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
if (!hasPermissions) {
|
|
|
|
|
el.parentNode && el.parentNode.removeChild(el)
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
throw new Error(t('permission.hasPermission'))
|
|
|
|
|
}
|
|
|
|
|
})
|
|
|
|
|
}
|