Files
school-developer/src/hooks/auth.ts
guochunsi c6da6e286f a
2026-01-30 16:29:15 +08:00

31 lines
1.0 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import { storeToRefs } from 'pinia'
import { useUserInfo } from '/@/stores/userInfo'
import { judementSameArr } from '/@/utils/arrayOperation'
/**
* 权限 composable用于「无权限即无节点」场景v-if + hasAuth替代 v-auth
* 多页面复用,保证响应式(权限变更后模板会更新)
*
* @example
* const { hasAuth, hasAuths, hasAuthAll } = useAuth()
* // 模板v-if="hasAuth('xxx')" v-if="hasAuths(['a','b'])" v-if="hasAuthAll(['a','b'])"
*/
export function useAuth() {
const { userInfos } = storeToRefs(useUserInfo())
const list = () => userInfos.value?.authBtnList ?? []
/** 单个权限:有该权限返回 true */
const hasAuth = (code: string) => list().includes(code)
/** 多个权限满足其一即返回 true */
const hasAuths = (codes: string[]) => {
const btnList = list()
return codes.some((c) => btnList.includes(c))
}
/** 多个权限全部满足才返回 true */
const hasAuthAll = (codes: string[]) => judementSameArr(codes, list())
return { hasAuth, hasAuths, hasAuthAll }
}