Skip to content

Commit 58a3af8

Browse files
xiaobaoorYunaiV
authored andcommitted
!22 issue#I6KY0M 修复建议
* feat: 增加全局权限判断函数#I6KY0M
1 parent 7d43157 commit 58a3af8

File tree

1 file changed

+45
-0
lines changed

1 file changed

+45
-0
lines changed

src/utils/permission.ts

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
import { CACHE_KEY, useCache } from '@/hooks/web/useCache'
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 = '*:*:*'
15+
const permissions = wsCache.get(CACHE_KEY.USER).permissions
16+
const hasPermission = permissions.some((permission) => {
17+
return all_permission === permission || permissionDatas.includes(permission)
18+
})
19+
return !!hasPermission
20+
} else {
21+
console.error(t('permission.hasPermission'))
22+
return false
23+
}
24+
}
25+
26+
/**
27+
* 角色权限校验
28+
* @param {string[]} value 校验值
29+
* @returns {Boolean}
30+
*/
31+
export function checkRole(value: string[]) {
32+
if (value && value instanceof Array && value.length > 0) {
33+
const { wsCache } = useCache()
34+
const permissionRoles = value
35+
const super_admin = 'admin'
36+
const roles = wsCache.get(CACHE_KEY.USER).roles
37+
const hasRole = roles.some((role) => {
38+
return super_admin === role || permissionRoles.includes(role)
39+
})
40+
return !!hasRole
41+
} else {
42+
console.error(t('permission.hasRole'))
43+
return false
44+
}
45+
}

0 commit comments

Comments
 (0)