2023-08-02 17:54:12 +08:00
|
|
|
import { RouteLocationNormalized, RouteRecordNormalized, RouteRecordRaw } from 'vue-router';
|
|
|
|
|
import { useAppStore, useUserStore } from '@/store';
|
|
|
|
|
import { WHITE_ROUTER_LIST } from '@/router/constants';
|
2023-07-24 10:05:07 +08:00
|
|
|
|
|
|
|
|
export default function usePermission() {
|
2023-08-02 17:54:12 +08:00
|
|
|
const appStore = useAppStore();
|
2023-07-24 10:05:07 +08:00
|
|
|
const userStore = useUserStore();
|
|
|
|
|
return {
|
2023-08-02 17:08:40 +08:00
|
|
|
/**
|
|
|
|
|
* 是否可访问路由
|
|
|
|
|
*/
|
2023-07-24 10:05:07 +08:00
|
|
|
accessRouter(route: RouteLocationNormalized | RouteRecordRaw) {
|
2023-08-02 17:54:12 +08:00
|
|
|
// 检查路由是否存在于授权路由中
|
|
|
|
|
const menuConfig = [...appStore.appAsyncMenus, ...WHITE_ROUTER_LIST];
|
|
|
|
|
let exist = false;
|
|
|
|
|
while (menuConfig.length && !exist) {
|
|
|
|
|
const element = menuConfig.shift();
|
|
|
|
|
if (element?.name === route.name) exist = true;
|
|
|
|
|
if (element?.children) {
|
|
|
|
|
menuConfig.push(
|
|
|
|
|
...(element.children as unknown as RouteRecordNormalized[])
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return exist;
|
2023-07-24 10:05:07 +08:00
|
|
|
},
|
2023-08-02 17:08:40 +08:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 是否有权限
|
|
|
|
|
*/
|
|
|
|
|
hasPermission(permission: string) {
|
|
|
|
|
return userStore.permission?.includes('*') ||
|
|
|
|
|
userStore.permission?.includes(permission);
|
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 是否有权限
|
|
|
|
|
*/
|
|
|
|
|
hasAnyPermission(permission: string[]) {
|
|
|
|
|
return userStore.permission?.includes('*') ||
|
|
|
|
|
permission.map(s => userStore.permission?.includes(s))
|
|
|
|
|
.filter(Boolean).length > 0;
|
2023-07-24 10:05:07 +08:00
|
|
|
},
|
2023-08-02 17:08:40 +08:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 是否有角色
|
|
|
|
|
*/
|
|
|
|
|
hasRole(role: string) {
|
|
|
|
|
return userStore.roles?.includes('admin') ||
|
|
|
|
|
userStore.roles?.includes(role);
|
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 是否有角色
|
|
|
|
|
*/
|
|
|
|
|
hasAnyRole(role: string[]) {
|
|
|
|
|
return userStore.roles?.includes('*') ||
|
|
|
|
|
role.map(s => userStore.roles?.includes(s))
|
|
|
|
|
.filter(Boolean).length > 0;
|
|
|
|
|
}
|
2023-07-24 10:05:07 +08:00
|
|
|
};
|
|
|
|
|
}
|