增加空判断保护

This commit is contained in:
shizhong 2024-03-21 15:09:03 +08:00
parent 8f368774ef
commit 75d0bdcc49
3 changed files with 25 additions and 4 deletions

View File

@ -8,7 +8,7 @@ export function hasPermi(app: App<Element>) {
const { wsCache } = useCache() const { wsCache } = useCache()
const { value } = binding const { value } = binding
const all_permission = '*:*:*' const all_permission = '*:*:*'
const permissions = wsCache.get(CACHE_KEY.USER).permissions const permissions = wsCache.get(CACHE_KEY.USER)?.permissions ? wsCache.get(CACHE_KEY.USER)?.permissions : []
if (value && value instanceof Array && value.length > 0) { if (value && value instanceof Array && value.length > 0) {
const permissionFlag = value const permissionFlag = value
@ -25,3 +25,24 @@ export function hasPermi(app: App<Element>) {
} }
}) })
} }
export function hasPermiEvery(app: App<Element>) {
app.directive('hasPermiEvery', (el, binding) => {
const { wsCache } = useCache()
const { value } = binding
const all_permission = '*:*:*'
const permissions = wsCache.get(CACHE_KEY.USER)?.permissions ? wsCache.get(CACHE_KEY.USER)?.permissions : []
if (value && value instanceof Array && value.length > 0) {
const permissionFlag = value
const hasPermissions = permissionFlag.every((permission: string) => {
return all_permission === permission || permissions.includes(permission)
})
if (!hasPermissions) {
el.parentNode && el.parentNode.removeChild(el)
}
} else {
throw new Error(t('permission.hasPermission'))
}
})
}

View File

@ -8,7 +8,7 @@ export function hasRole(app: App<Element>) {
const { wsCache } = useCache() const { wsCache } = useCache()
const { value } = binding const { value } = binding
const super_admin = 'admin' const super_admin = 'admin'
const roles = wsCache.get(CACHE_KEY.USER).roles const roles = wsCache.get(CACHE_KEY.USER)?.roles ? wsCache.get(CACHE_KEY.USER)?.roles : []
if (value && value instanceof Array && value.length > 0) { if (value && value instanceof Array && value.length > 0) {
const roleFlag = value const roleFlag = value

View File

@ -12,7 +12,7 @@ export function checkPermi(value: string[]) {
const { wsCache } = useCache() const { wsCache } = useCache()
const permissionDatas = value const permissionDatas = value
const all_permission = '*:*:*' const all_permission = '*:*:*'
const permissions = wsCache.get(CACHE_KEY.USER).permissions const permissions = wsCache.get(CACHE_KEY.USER)?.permissions ? wsCache.get(CACHE_KEY.USER)?.permissions : []
const hasPermission = permissions.some((permission) => { const hasPermission = permissions.some((permission) => {
return all_permission === permission || permissionDatas.includes(permission) return all_permission === permission || permissionDatas.includes(permission)
}) })
@ -33,7 +33,7 @@ export function checkRole(value: string[]) {
const { wsCache } = useCache() const { wsCache } = useCache()
const permissionRoles = value const permissionRoles = value
const super_admin = 'admin' const super_admin = 'admin'
const roles = wsCache.get(CACHE_KEY.USER).roles const roles = wsCache.get(CACHE_KEY.USER)?.roles ? wsCache.get(CACHE_KEY.USER)?.roles : []
const hasRole = roles.some((role) => { const hasRole = roles.some((role) => {
return super_admin === role || permissionRoles.includes(role) return super_admin === role || permissionRoles.includes(role)
}) })