提交 | 用户 | 时间
|
820397
|
1 |
import { CACHE_KEY, useCache } from '@/hooks/web/useCache' |
H |
2 |
|
|
3 |
const { t } = useI18n() // 国际化 |
|
4 |
|
|
5 |
/** |
|
6 |
* 字符权限校验 |
|
7 |
* @param {Array} value 校验值 |
|
8 |
* @returns {Boolean} |
|
9 |
*/ |
|
10 |
export function checkPermi(value: string[]) { |
|
11 |
if (value && value instanceof Array && value.length > 0) { |
|
12 |
const { wsCache } = useCache() |
|
13 |
const permissionDatas = value |
|
14 |
const all_permission = '*:*:*' |
9259c2
|
15 |
const userInfo = wsCache.get(CACHE_KEY.USER) |
H |
16 |
const permissions = userInfo?.permissions || [] |
820397
|
17 |
const hasPermission = permissions.some((permission) => { |
H |
18 |
return all_permission === permission || permissionDatas.includes(permission) |
|
19 |
}) |
|
20 |
return !!hasPermission |
|
21 |
} else { |
|
22 |
console.error(t('permission.hasPermission')) |
|
23 |
return false |
|
24 |
} |
|
25 |
} |
|
26 |
|
|
27 |
/** |
|
28 |
* 角色权限校验 |
|
29 |
* @param {string[]} value 校验值 |
|
30 |
* @returns {Boolean} |
|
31 |
*/ |
|
32 |
export function checkRole(value: string[]) { |
|
33 |
if (value && value instanceof Array && value.length > 0) { |
|
34 |
const { wsCache } = useCache() |
|
35 |
const permissionRoles = value |
|
36 |
const super_admin = 'admin' |
9259c2
|
37 |
const userInfo = wsCache.get(CACHE_KEY.USER) |
H |
38 |
const roles = userInfo?.roles || [] |
820397
|
39 |
const hasRole = roles.some((role) => { |
H |
40 |
return super_admin === role || permissionRoles.includes(role) |
|
41 |
}) |
|
42 |
return !!hasRole |
|
43 |
} else { |
|
44 |
console.error(t('permission.hasRole')) |
|
45 |
return false |
|
46 |
} |
|
47 |
} |