提交 | 用户 | 时间
|
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 { value } = binding |
|
9 |
|
|
10 |
if (value && value instanceof Array && value.length > 0) { |
c9a6f7
|
11 |
const hasPermissions = hasPermission(value) |
820397
|
12 |
|
H |
13 |
if (!hasPermissions) { |
|
14 |
el.parentNode && el.parentNode.removeChild(el) |
|
15 |
} |
|
16 |
} else { |
|
17 |
throw new Error(t('permission.hasPermission')) |
|
18 |
} |
|
19 |
}) |
|
20 |
} |
c9a6f7
|
21 |
|
H |
22 |
export const hasPermission = (permission: string[]) => { |
|
23 |
const { wsCache } = useCache() |
|
24 |
const all_permission = '*:*:*' |
|
25 |
const userInfo = wsCache.get(CACHE_KEY.USER) |
|
26 |
const permissions = userInfo?.permissions || [] |
|
27 |
|
|
28 |
return permissions.some((p: string) => { |
|
29 |
return all_permission === p || permission.includes(p) |
|
30 |
}) |
|
31 |
} |