31 lines
1.0 KiB
TypeScript
31 lines
1.0 KiB
TypeScript
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 }
|
||
}
|