perf: 添加权限Set 替换检查权限数组 提高检查权限性能

This commit is contained in:
zws 2025-01-21 09:23:37 +08:00
parent 5c0ee866a0
commit d710c9d27e
3 changed files with 14 additions and 27 deletions

View File

@ -1,5 +1,5 @@
import type { App } from 'vue'
import { CACHE_KEY, useCache } from '@/hooks/web/useCache'
import type {App} from 'vue'
import {useUserStore} from "@/store/modules/user";
const { t } = useI18n() // 国际化
@ -18,14 +18,9 @@ export function hasPermi(app: App<Element>) {
}
})
}
const userStore = useUserStore();
const all_permission = '*:*:*'
export const hasPermission = (permission: string[]) => {
const { wsCache } = useCache()
const all_permission = '*:*:*'
const userInfo = wsCache.get(CACHE_KEY.USER)
const permissions = userInfo?.permissions || []
return permissions.some((p: string) => {
return all_permission === p || permission.includes(p)
})
}
return userStore.permissionsSet.has(all_permission) ||
permission.some(permission => userStore.permissionsSet.has(permission))
}

View File

@ -16,6 +16,7 @@ interface UserVO {
interface UserInfoVO {
// USER 缓存
permissions: string[]
permissionsSet: Set<string>
roles: string[]
isSetUser: boolean
user: UserVO
@ -24,6 +25,7 @@ interface UserInfoVO {
export const useUserStore = defineStore('admin-user', {
state: (): UserInfoVO => ({
permissions: [],
permissionsSet: new Set<string>(),
roles: [],
isSetUser: false,
user: {
@ -58,6 +60,7 @@ export const useUserStore = defineStore('admin-user', {
userInfo = await getInfo()
}
this.permissions = userInfo.permissions
this.permissionsSet = new Set(userInfo.permissions)
this.roles = userInfo.roles
this.user = userInfo.user
this.isSetUser = true

View File

@ -1,4 +1,6 @@
import { CACHE_KEY, useCache } from '@/hooks/web/useCache'
import {hasPermission} from "@/directives/permission/hasPermi";
const { t } = useI18n() // 国际化
@ -7,21 +9,8 @@ const { t } = useI18n() // 国际化
* @param {Array} value
* @returns {Boolean}
*/
export function checkPermi(value: string[]) {
if (value && value instanceof Array && value.length > 0) {
const { wsCache } = useCache()
const permissionDatas = value
const all_permission = '*:*:*'
const userInfo = wsCache.get(CACHE_KEY.USER)
const permissions = userInfo?.permissions || []
const hasPermission = permissions.some((permission: string) => {
return all_permission === permission || permissionDatas.includes(permission)
})
return !!hasPermission
} else {
console.error(t('permission.hasPermission'))
return false
}
export function checkPermi(permission: string[]) {
return hasPermission(permission)
}
/**