Vben
2021-03-18 67962f1deea31d695d20ae0ea7fc39b39c1eea47
提交 | 用户 | age
2f6253 1 import type { AppRouteRecordRaw, Menu } from '/@/router/types';
2 import store from '/@/store/index';
3 import { hotModuleUnregisterModule } from '/@/utils/helper/vuexHelper';
4
5 import { VuexModule, Mutation, Module, getModule, Action } from 'vuex-module-decorators';
6
7 import { PermissionModeEnum } from '/@/enums/appEnum';
8
9 import { appStore } from '/@/store/modules/app';
10 import { userStore } from '/@/store/modules/user';
11
ecfb70 12 import { asyncRoutes } from '/@/router/routes';
2f6253 13 import { filter } from '/@/utils/helper/treeHelper';
14 import { toRaw } from 'vue';
15 import { getMenuListById } from '/@/api/sys/menu';
67962f 16 import { getPermCodeByUserId } from '/@/api/sys/user';
2f6253 17
e12c58 18 import { transformObjToRoute, flatRoutes } from '/@/router/helper/routeHelper';
c303ec 19 import { transformRouteToMenu } from '/@/router/helper/menuHelper';
2f6253 20
21 import { useMessage } from '/@/hooks/web/useMessage';
d5d4c4 22 import { useI18n } from '/@/hooks/web/useI18n';
e12c58 23 import { ERROR_LOG_ROUTE, PAGE_NOT_FOUND_ROUTE } from '/@/router/routes/basic';
66acb2 24
2f6253 25 const { createMessage } = useMessage();
15567e 26 const NAME = 'app-permission';
2f6253 27 hotModuleUnregisterModule(NAME);
28 @Module({ dynamic: true, namespaced: true, store, name: NAME })
29 class Permission extends VuexModule {
e23336 30   // Permission code list
2f6253 31   private permCodeListState: string[] = [];
32
33   // Whether the route has been dynamically added
34   private isDynamicAddedRouteState = false;
35
e23336 36   // To trigger a menu update
2f6253 37   private lastBuildMenuTimeState = 0;
38
e23336 39   // Backstage menu list
2f6253 40   private backMenuListState: Menu[] = [];
41
42   get getPermCodeListState() {
43     return this.permCodeListState;
44   }
45
46   get getBackMenuListState() {
47     return this.backMenuListState;
48   }
49
50   get getLastBuildMenuTimeState() {
51     return this.lastBuildMenuTimeState;
52   }
53
54   get getIsDynamicAddedRouteState() {
55     return this.isDynamicAddedRouteState;
56   }
57
58   @Mutation
59   commitPermCodeListState(codeList: string[]): void {
60     this.permCodeListState = codeList;
61   }
62
63   @Mutation
64   commitBackMenuListState(list: Menu[]): void {
65     this.backMenuListState = list;
66   }
67
68   @Mutation
69   commitLastBuildMenuTimeState(): void {
70     this.lastBuildMenuTimeState = new Date().getTime();
71   }
72
73   @Mutation
74   commitDynamicAddedRouteState(added: boolean): void {
75     this.isDynamicAddedRouteState = added;
76   }
77
78   @Mutation
79   commitResetState(): void {
80     this.isDynamicAddedRouteState = false;
81     this.permCodeListState = [];
82     this.backMenuListState = [];
83     this.lastBuildMenuTimeState = 0;
84   }
85
86   @Action
67962f 87   async changePermissionCode(userId: string) {
V 88     const codeList = await getPermCodeByUserId({ userId });
89     this.commitPermCodeListState(codeList);
90   }
91
92   @Action
2f6253 93   async buildRoutesAction(id?: number | string): Promise<AppRouteRecordRaw[]> {
d5d4c4 94     const { t } = useI18n();
2f6253 95     let routes: AppRouteRecordRaw[] = [];
96     const roleList = toRaw(userStore.getRoleListState);
97
f6cef1 98     const { permissionMode = PermissionModeEnum.ROLE } = appStore.getProjectConfig;
2f6253 99
100     // role permissions
101     if (permissionMode === PermissionModeEnum.ROLE) {
102       routes = filter(asyncRoutes, (route) => {
c303ec 103         const { meta } = route as AppRouteRecordRaw;
V 104         const { roles } = meta || {};
2f6253 105         if (!roles) return true;
106         return roleList.some((role) => roles.includes(role));
107       });
da0491 108       //  If you are sure that you do not need to do background dynamic permissions, please comment the entire judgment below
2f6253 109     } else if (permissionMode === PermissionModeEnum.BACK) {
110       createMessage.loading({
d5d4c4 111         content: t('sys.app.menuLoading'),
2f6253 112         duration: 1,
113       });
da0491 114       // Here to get the background routing menu logic to modify by yourself
2f6253 115       const paramId = id || userStore.getUserInfoState.userId;
67962f 116
V 117       // !Simulate to obtain permission codes from the background,
118       // this function may only need to be executed once, and the actual project can be put at the right time by itself
119       this.changePermissionCode('1');
2f6253 120       if (!paramId) {
121         throw new Error('paramId is undefined!');
122       }
10cd4f 123       let routeList = (await getMenuListById({ id: paramId })) as AppRouteRecordRaw[];
V 124
da0491 125       // Dynamically introduce components
2f6253 126       routeList = transformObjToRoute(routeList);
e12c58 127
da0491 128       //  Background routing to menu structure
2f6253 129       const backMenuList = transformRouteToMenu(routeList);
130       this.commitBackMenuListState(backMenuList);
c303ec 131
e12c58 132       flatRoutes(routeList);
10cd4f 133       routes = [PAGE_NOT_FOUND_ROUTE, ...routeList];
2f6253 134     }
b335e7 135     routes.push(ERROR_LOG_ROUTE);
2f6253 136     return routes;
137   }
138 }
139 export const permissionStore = getModule<Permission>(Permission);