Skip to content

Commit

Permalink
fix: ensure permissionMode exists close #409
Browse files Browse the repository at this point in the history
  • Loading branch information
anncwb committed Mar 23, 2021
1 parent fedd9ca commit 8fb0396
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 16 deletions.
2 changes: 1 addition & 1 deletion src/settings/projectSetting.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ const setting: ProjectConfig = {
settingButtonPosition: SettingButtonPositionEnum.AUTO,

// Permission mode
permissionMode: PermissionModeEnum.ROLE,
permissionMode: PermissionModeEnum.BACK,

// Permission-related cache is stored in sessionStorage or localStorage
permissionCacheType: CacheTypeEnum.LOCAL,
Expand Down
28 changes: 15 additions & 13 deletions src/store/modules/permission.ts
Original file line number Diff line number Diff line change
@@ -1,28 +1,30 @@
import type { AppRouteRecordRaw, Menu } from '/@/router/types';
import store from '/@/store/index';
import { hotModuleUnregisterModule } from '/@/utils/helper/vuexHelper';

import store from '/@/store';
import { toRaw } from 'vue';
import { VuexModule, Mutation, Module, getModule, Action } from 'vuex-module-decorators';

import { hotModuleUnregisterModule } from '/@/utils/helper/vuexHelper';

import { PermissionModeEnum } from '/@/enums/appEnum';

import { appStore } from '/@/store/modules/app';
import { userStore } from '/@/store/modules/user';
import projectSetting from '/@/settings/projectSetting';

import { asyncRoutes } from '/@/router/routes';
import { ERROR_LOG_ROUTE, PAGE_NOT_FOUND_ROUTE } from '/@/router/routes/basic';
import { transformObjToRoute, flatMultiLevelRoutes } from '/@/router/helper/routeHelper';
import { transformRouteToMenu } from '/@/router/helper/menuHelper';

import { filter } from '/@/utils/helper/treeHelper';
import { toRaw } from 'vue';

import { getMenuListById } from '/@/api/sys/menu';
import { getPermCodeByUserId } from '/@/api/sys/user';

import { transformObjToRoute, flatMultiLevelRoutes } from '/@/router/helper/routeHelper';
import { transformRouteToMenu } from '/@/router/helper/menuHelper';

import { useMessage } from '/@/hooks/web/useMessage';
import { useI18n } from '/@/hooks/web/useI18n';
import { ERROR_LOG_ROUTE, PAGE_NOT_FOUND_ROUTE } from '/@/router/routes/basic';

const { createMessage } = useMessage();
const NAME = 'app-permission';
hotModuleUnregisterModule(NAME);
@Module({ dynamic: true, namespaced: true, store, name: NAME })
Expand Down Expand Up @@ -94,13 +96,11 @@ class Permission extends VuexModule {
const { t } = useI18n();
let routes: AppRouteRecordRaw[] = [];
const roleList = toRaw(userStore.getRoleListState);

const { permissionMode = PermissionModeEnum.ROLE } = appStore.getProjectConfig;

const { permissionMode = projectSetting.permissionMode } = appStore.getProjectConfig;
// role permissions
if (permissionMode === PermissionModeEnum.ROLE) {
const routeFilter = (route) => {
const { meta } = route as AppRouteRecordRaw;
const routeFilter = (route: AppRouteRecordRaw) => {
const { meta } = route;
const { roles } = meta || {};
if (!roles) return true;
return roleList.some((role) => roles.includes(role));
Expand All @@ -111,6 +111,8 @@ class Permission extends VuexModule {
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,
Expand Down
4 changes: 2 additions & 2 deletions src/utils/helper/vuexHelper.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import store from '/@/store';

export function hotModuleUnregisterModule(name: string) {
if (!name) return;
if ((store.state as any)[name]) {
if (!name || !import.meta.hot) return;
if ((store.state as Recordable)[name]) {
store.unregisterModule(name);
}
}

0 comments on commit 8fb0396

Please sign in to comment.