提交 | 用户 | 时间
|
820397
|
1 |
import type { App } from 'vue' |
H |
2 |
import { CACHE_KEY, useCache } from '@/hooks/web/useCache' |
|
3 |
|
|
4 |
const { t } = useI18n() // 国际化 |
|
5 |
|
|
6 |
export function hasPermi(app: App<Element>) { |
|
7 |
app.directive('hasPermi', (el, binding) => { |
|
8 |
const { wsCache } = useCache() |
|
9 |
const { value } = binding |
|
10 |
const all_permission = '*:*:*' |
9259c2
|
11 |
const userInfo = wsCache.get(CACHE_KEY.USER) |
H |
12 |
const permissions = userInfo?.permissions || [] |
820397
|
13 |
|
H |
14 |
if (value && value instanceof Array && value.length > 0) { |
|
15 |
const permissionFlag = value |
|
16 |
|
|
17 |
const hasPermissions = permissions.some((permission: string) => { |
|
18 |
return all_permission === permission || permissionFlag.includes(permission) |
|
19 |
}) |
|
20 |
|
|
21 |
if (!hasPermissions) { |
|
22 |
el.parentNode && el.parentNode.removeChild(el) |
|
23 |
} |
|
24 |
} else { |
|
25 |
throw new Error(t('permission.hasPermission')) |
|
26 |
} |
|
27 |
}) |
|
28 |
} |