You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

98 lines
3.3 KiB

import type { Router } from 'vue-router';
import { NsMessage } from '/nerv-lib/component/message';
import { authorizationService } from '/nerv-base/store/modules/authorization-service';
import { appConfigStore } from '/nerv-base/store/modules/app-config';
import { Cookies } from '/nerv-lib/util/cookie';
import { setRouteChange } from '/nerv-lib/util/routeChange';
export function createPermissionGuard(router: Router, whiteNameList: string[]) {
const authorizationStore = authorizationService();
const appConfig = appConfigStore();
router.beforeEach(async (to, from, next) => {
if (!appConfig.userCustomRouterGuard) {
setRouteChange(to);
if (to?.query?.nervsid) {
if (to?.query?.nervsid !== Cookies.get('nervsid')) {
Cookies.set('nervsid', to?.query?.nervsid);
try {
const info = await appConfig.userInfo();
await authorizationStore.initUserResource();
authorizationStore.initMenuResource();
appConfig.setUserInfo(info.data);
info.success
? window.sessionStorage.setItem('userInfo', JSON.stringify(info.data))
: '';
} catch (err) {
Cookies.remove('nervsid');
}
}
}
if (
!Cookies.get('nervsid') &&
to.fullPath !== '/login' &&
to.name &&
!whiteNameList.includes(to.name as string)
) {
NsMessage.error('登陆信息已过期,请重新登录!', 1);
next({ name: 'login' });
} else {
// 存储用户信息
if (to.fullPath !== '/login') {
const initUserInfo = window.sessionStorage['userInfo'];
if (!initUserInfo && Cookies.get('nervsid')) {
try {
const info = await appConfig.userInfo();
info.data ? window.sessionStorage.setItem('userInfo', JSON.stringify(info.data)) : '';
} catch (err) {
Cookies.remove('nervsid');
}
} else {
if (initUserInfo) {
const userInfo = JSON.parse(initUserInfo);
authorizationStore.setEnterpriseName(
!userInfo.organizationalName ? '' : userInfo.organizationalName,
);
}
}
}
if (to.name && !whiteNameList.includes(to.name as string)) {
if (appConfig.enablePermissions !== undefined && appConfig.enablePermissions) {
if (authorizationStore.getInitRouterList.length === 0) {
await authorizationStore.initUserResource();
}
if (
authorizationStore.checkPermissionRouter(
to.meta?.bindView ? to.meta?.bindView : to.name,
)
) {
next();
} else {
if (to.path === '/') {
next({ name: authorizationStore.getInitRouterList[0] });
} else {
// console.log(authorizationStore.getInitRouterList);
NsMessage.error('无该页面权限!');
next({ name: 'error403' });
}
}
} else {
next();
}
} else {
next();
}
}
} else {
await appConfig.userCustomRouterGuard(
to,
from,
next,
whiteNameList,
authorizationStore,
appConfig,
);
}
});
}