2020-09-28 20:19:10 +08:00
|
|
|
import type { AppRouteRecordRaw, Menu } from '/@/router/types';
|
|
|
|
|
|
2021-04-10 19:25:49 +08:00
|
|
|
import { defineStore } from 'pinia';
|
|
|
|
|
import { store } from '/@/store';
|
|
|
|
|
import { useI18n } from '/@/hooks/web/useI18n';
|
|
|
|
|
import { useUserStore } from './user';
|
|
|
|
|
import { useAppStoreWidthOut } from './app';
|
2021-03-23 22:31:23 +08:00
|
|
|
import { toRaw } from 'vue';
|
2021-04-10 19:25:49 +08:00
|
|
|
import { transformObjToRoute, flatMultiLevelRoutes } from '/@/router/helper/routeHelper';
|
|
|
|
|
import { transformRouteToMenu } from '/@/router/helper/menuHelper';
|
2020-09-28 20:19:10 +08:00
|
|
|
|
2021-04-10 19:25:49 +08:00
|
|
|
import projectSetting from '/@/settings/projectSetting';
|
2021-03-23 22:31:23 +08:00
|
|
|
|
2020-09-28 20:19:10 +08:00
|
|
|
import { PermissionModeEnum } from '/@/enums/appEnum';
|
|
|
|
|
|
2020-11-12 22:20:15 +08:00
|
|
|
import { asyncRoutes } from '/@/router/routes';
|
2021-03-23 22:31:23 +08:00
|
|
|
import { ERROR_LOG_ROUTE, PAGE_NOT_FOUND_ROUTE } from '/@/router/routes/basic';
|
|
|
|
|
|
2020-09-28 20:19:10 +08:00
|
|
|
import { filter } from '/@/utils/helper/treeHelper';
|
2021-03-23 22:31:23 +08:00
|
|
|
|
2020-09-28 20:19:10 +08:00
|
|
|
import { getMenuListById } from '/@/api/sys/menu';
|
2021-03-18 22:38:36 +08:00
|
|
|
import { getPermCodeByUserId } from '/@/api/sys/user';
|
2020-09-28 20:19:10 +08:00
|
|
|
|
|
|
|
|
import { useMessage } from '/@/hooks/web/useMessage';
|
2020-11-26 21:10:21 +08:00
|
|
|
|
2021-04-10 19:25:49 +08:00
|
|
|
interface PermissionState {
|
2020-10-30 21:32:05 +08:00
|
|
|
// Permission code list
|
2021-04-10 19:25:49 +08:00
|
|
|
permCodeList: string[];
|
2020-09-28 20:19:10 +08:00
|
|
|
// Whether the route has been dynamically added
|
2021-04-10 19:25:49 +08:00
|
|
|
isDynamicAddedRoute: boolean;
|
2020-10-30 21:32:05 +08:00
|
|
|
// To trigger a menu update
|
2021-04-10 19:25:49 +08:00
|
|
|
lastBuildMenuTime: number;
|
2020-10-30 21:32:05 +08:00
|
|
|
// Backstage menu list
|
2021-04-10 19:25:49 +08:00
|
|
|
backMenuList: Menu[];
|
|
|
|
|
}
|
|
|
|
|
export const usePermissionStore = defineStore({
|
|
|
|
|
id: 'app-permission',
|
|
|
|
|
state: (): PermissionState => ({
|
|
|
|
|
permCodeList: [],
|
|
|
|
|
// Whether the route has been dynamically added
|
|
|
|
|
isDynamicAddedRoute: false,
|
|
|
|
|
// To trigger a menu update
|
|
|
|
|
lastBuildMenuTime: 0,
|
|
|
|
|
// Backstage menu list
|
|
|
|
|
backMenuList: [],
|
|
|
|
|
}),
|
|
|
|
|
getters: {
|
2021-05-25 01:24:26 +08:00
|
|
|
getPermCodeList() {
|
2021-04-10 19:25:49 +08:00
|
|
|
return this.permCodeList;
|
|
|
|
|
},
|
2021-05-25 01:24:26 +08:00
|
|
|
getBackMenuList() {
|
2021-04-10 19:25:49 +08:00
|
|
|
return this.backMenuList;
|
|
|
|
|
},
|
2021-05-25 01:24:26 +08:00
|
|
|
getLastBuildMenuTime() {
|
2021-04-10 19:25:49 +08:00
|
|
|
return this.lastBuildMenuTime;
|
|
|
|
|
},
|
2021-05-25 01:24:26 +08:00
|
|
|
getIsDynamicAddedRoute() {
|
2021-04-10 19:25:49 +08:00
|
|
|
return this.isDynamicAddedRoute;
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
actions: {
|
|
|
|
|
setPermCodeList(codeList: string[]) {
|
|
|
|
|
this.permCodeList = codeList;
|
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
setBackMenuList(list: Menu[]) {
|
|
|
|
|
this.backMenuList = list;
|
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
setLastBuildMenuTime() {
|
|
|
|
|
this.lastBuildMenuTime = new Date().getTime();
|
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
setDynamicAddedRoute(added: boolean) {
|
|
|
|
|
this.isDynamicAddedRoute = added;
|
|
|
|
|
},
|
|
|
|
|
resetState(): void {
|
|
|
|
|
this.isDynamicAddedRoute = false;
|
|
|
|
|
this.permCodeList = [];
|
|
|
|
|
this.backMenuList = [];
|
|
|
|
|
this.lastBuildMenuTime = 0;
|
|
|
|
|
},
|
|
|
|
|
async changePermissionCode(userId: string) {
|
|
|
|
|
const codeList = await getPermCodeByUserId({ userId });
|
|
|
|
|
this.setPermCodeList(codeList);
|
|
|
|
|
},
|
|
|
|
|
async buildRoutesAction(id?: number | string): Promise<AppRouteRecordRaw[]> {
|
|
|
|
|
const { t } = useI18n();
|
|
|
|
|
const userStore = useUserStore();
|
|
|
|
|
const appStore = useAppStoreWidthOut();
|
|
|
|
|
|
|
|
|
|
let routes: AppRouteRecordRaw[] = [];
|
|
|
|
|
const roleList = toRaw(userStore.getRoleList);
|
|
|
|
|
const { permissionMode = projectSetting.permissionMode } = appStore.getProjectConfig;
|
|
|
|
|
// role permissions
|
|
|
|
|
if (permissionMode === PermissionModeEnum.ROLE) {
|
|
|
|
|
const routeFilter = (route: AppRouteRecordRaw) => {
|
|
|
|
|
const { meta } = route;
|
|
|
|
|
const { roles } = meta || {};
|
|
|
|
|
if (!roles) return true;
|
|
|
|
|
return roleList.some((role) => roles.includes(role));
|
|
|
|
|
};
|
|
|
|
|
routes = filter(asyncRoutes, routeFilter);
|
|
|
|
|
routes = routes.filter(routeFilter);
|
|
|
|
|
// Convert multi-level routing to level 2 routing
|
|
|
|
|
routes = flatMultiLevelRoutes(routes);
|
|
|
|
|
// If you are sure that you do not need to do background dynamic permissions, please comment the entire judgment below
|
|
|
|
|
} else if (permissionMode === PermissionModeEnum.BACK) {
|
|
|
|
|
const { createMessage } = useMessage();
|
|
|
|
|
|
|
|
|
|
createMessage.loading({
|
|
|
|
|
content: t('sys.app.menuLoading'),
|
|
|
|
|
duration: 1,
|
|
|
|
|
});
|
|
|
|
|
// Here to get the background routing menu logic to modify by yourself
|
|
|
|
|
const paramId = id || userStore.getUserInfo?.userId;
|
|
|
|
|
|
|
|
|
|
// !Simulate to obtain permission codes from the background,
|
|
|
|
|
// this function may only need to be executed once, and the actual project can be put at the right time by itself
|
2021-04-24 20:31:05 +08:00
|
|
|
let routeList: AppRouteRecordRaw[] = [];
|
2021-04-10 19:25:49 +08:00
|
|
|
try {
|
|
|
|
|
this.changePermissionCode('1');
|
2021-04-24 20:31:05 +08:00
|
|
|
routeList = (await getMenuListById({ id: paramId })) as AppRouteRecordRaw[];
|
|
|
|
|
} catch (error) {
|
|
|
|
|
console.error(error);
|
|
|
|
|
}
|
2021-04-10 19:25:49 +08:00
|
|
|
|
|
|
|
|
if (!paramId) {
|
|
|
|
|
throw new Error('paramId is undefined!');
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Dynamically introduce components
|
|
|
|
|
routeList = transformObjToRoute(routeList);
|
|
|
|
|
|
|
|
|
|
// Background routing to menu structure
|
|
|
|
|
const backMenuList = transformRouteToMenu(routeList);
|
|
|
|
|
this.setBackMenuList(backMenuList);
|
|
|
|
|
|
|
|
|
|
routeList = flatMultiLevelRoutes(routeList);
|
|
|
|
|
routes = [PAGE_NOT_FOUND_ROUTE, ...routeList];
|
2020-09-28 20:19:10 +08:00
|
|
|
}
|
2021-04-10 19:25:49 +08:00
|
|
|
routes.push(ERROR_LOG_ROUTE);
|
|
|
|
|
return routes;
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
// Need to be used outside the setup
|
|
|
|
|
export function usePermissionStoreWidthOut() {
|
|
|
|
|
return usePermissionStore(store);
|
2020-09-28 20:19:10 +08:00
|
|
|
}
|