push
This commit is contained in:
90
lib/paas/router/guard/permission-guard.ts
Normal file
90
lib/paas/router/guard/permission-guard.ts
Normal file
@@ -0,0 +1,90 @@
|
||||
|
||||
|
||||
import type { Router } from 'vue-router';
|
||||
|
||||
import { authorizationService } from '/nerv-lib/paas/store/modules/authorization-service';
|
||||
import { loginService } from '/nerv-lib/paas/store/modules/login-service';
|
||||
|
||||
import { NsMessage } from '/nerv-lib/component/message';
|
||||
import { CatalogConfigService } from '/nerv-lib/paas/store/modules/catalog-config';
|
||||
import { routerConfig } from '/nerv-lib/paas/config/router.config';
|
||||
import { appConfigStore } from '/nerv-base/store/modules/app-config';
|
||||
import { identity, isArray } from 'lodash-es';
|
||||
export function createPermissionGuard(router: Router, whiteNameList: string[]) {
|
||||
const authService = authorizationService();
|
||||
const user = loginService();
|
||||
const catalogConfig = CatalogConfigService();
|
||||
const appConfig = appConfigStore();
|
||||
|
||||
router.beforeEach(async (to) => {
|
||||
// const regionStr = localStorage.getItem('region');
|
||||
// if (regionStr && !to.fullPath.includes('region=')) {
|
||||
// const region = JSON.parse(regionStr);
|
||||
// to.query.region = region?.value;
|
||||
// const urlArray = to.fullPath.split('?');
|
||||
// if (urlArray.length === 2 && urlArray[1]) {
|
||||
// to.path = `${to.path}®ion=${region?.value}`;
|
||||
// to.fullPath = `${to.fullPath}®ion=${region?.value}`;
|
||||
// } else {
|
||||
// to.path = `${to.path}?region=${region?.value}`;
|
||||
// to.fullPath = `${to.fullPath}?region=${region?.value}`;
|
||||
// }
|
||||
// }
|
||||
|
||||
if (!appConfig.pagePermission) {
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
let catalogs;
|
||||
if (to.name && !whiteNameList.includes(to.name as string)) {
|
||||
if (!user.isLogin()) {
|
||||
await router.push({ name: 'login' });
|
||||
// routerConfig.login();
|
||||
return false;
|
||||
} else {
|
||||
await user.checkUserInfo();
|
||||
await authService.checkAuthMap();
|
||||
catalogs = await catalogConfig.getCatalogs();
|
||||
}
|
||||
|
||||
if (
|
||||
authService.checkPermission(
|
||||
to.meta?.app ? to.meta?.app : (to.matched[0].name as string),
|
||||
to.meta?.bindView ? to.meta?.bindView : (to.name as string),
|
||||
)
|
||||
) {
|
||||
return true;
|
||||
} else {
|
||||
// redirect(to, catalogs);
|
||||
// NsMessage.error('无该页面权限!');
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
});
|
||||
|
||||
/** 针对从产品与服务点击某项目图标应跳转到第一个有权限的菜单场景,
|
||||
* 从非vue项目点击进去,含有层级菜单项目获取的第一个有权限的菜单有误,需要在此处重定向,待非vue项目重新装过最新lib版本,即可去除*/
|
||||
function redirect(to: any, catalogs: any) {
|
||||
const moduleName = to.meta?.app ? to.meta?.app : (to.matched[0].name as string);
|
||||
let target = null;
|
||||
for (const catalog of catalogs) {
|
||||
const items = catalog['items'];
|
||||
if (isArray(items) && items.length > 0) {
|
||||
for (const item of items) {
|
||||
if (item['name'] == moduleName) {
|
||||
target = item;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
if (target) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (target && target['url']) {
|
||||
window.location.replace(target['url']);
|
||||
}
|
||||
}
|
||||
}
|
||||
40
lib/paas/router/index.ts
Normal file
40
lib/paas/router/index.ts
Normal file
@@ -0,0 +1,40 @@
|
||||
import type { App } from 'vue';
|
||||
import type { RouteRecord } from 'vue-router';
|
||||
|
||||
import { createRouter, createWebHistory } from 'vue-router';
|
||||
import { ALLRoute, WHITE_NAME_LIST } from './routes';
|
||||
import { createPermissionGuard } from '/nerv-lib/paas/router/guard/permission-guard';
|
||||
|
||||
// app router
|
||||
let router: any = undefined;
|
||||
|
||||
// reset router
|
||||
export function resetRouter() {
|
||||
router.getRoutes().forEach((route: RouteRecord) => {
|
||||
const { name } = route;
|
||||
if (name && !WHITE_NAME_LIST.includes(name as string)) {
|
||||
router.hasRoute(name) && router.removeRoute(name);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
// config router
|
||||
export function setupRouter(app: App<Element>, modules?: any) {
|
||||
const routers: any[] = [];
|
||||
if (!modules) {
|
||||
modules = import.meta.globEager('/src/router/**/*.ts');
|
||||
}
|
||||
Object.keys(modules).forEach((key) => {
|
||||
const mod = modules[key].default || {};
|
||||
const modList = Array.isArray(mod) ? [...mod] : [mod];
|
||||
modList[0].path && routers.push(...modList);
|
||||
});
|
||||
router = createRouter({
|
||||
history: createWebHistory(import.meta.env.VITE_PUBLIC_PATH as string),
|
||||
routes: [...ALLRoute, ...routers],
|
||||
strict: true,
|
||||
scrollBehavior: () => ({ left: 0, top: 0 }),
|
||||
});
|
||||
app.use(router);
|
||||
createPermissionGuard(router, WHITE_NAME_LIST);
|
||||
}
|
||||
123
lib/paas/router/routes/index.ts
Normal file
123
lib/paas/router/routes/index.ts
Normal file
@@ -0,0 +1,123 @@
|
||||
import type { RouteRecordRaw } from 'vue-router';
|
||||
import { isArray } from 'lodash-es';
|
||||
import { appConfig } from '/@/config/app.config.ts';
|
||||
const modules = import.meta.globEager('/src/router/**/*.ts');
|
||||
|
||||
// const jsonModules = import.meta.globEager('/src/data/router/*.json');
|
||||
|
||||
const routers: any[] = [];
|
||||
// Object.keys(modules).forEach((key) => {
|
||||
// const mod = modules[key].default || {};
|
||||
// const modList = Array.isArray(mod) ? [...mod] : [mod];
|
||||
// modList[0].path && routers.push(...modList);
|
||||
// });
|
||||
|
||||
// Object.keys(jsonModules).forEach((key) => {
|
||||
// const mod = jsonModules[key].default || {};
|
||||
// const modList = Array.isArray(mod) ? [...mod] : [mod];
|
||||
// modList.forEach((item, index) => {
|
||||
// modList[index] = setComponent(item);
|
||||
// });
|
||||
// routers.push(...modList);
|
||||
// });
|
||||
|
||||
/**
|
||||
* 检测路由中是否包含name为root的路由(root是默认路由)
|
||||
* @param routers
|
||||
*/
|
||||
function hasRoot(routers: RouteRecordRaw[]): boolean {
|
||||
let root = undefined;
|
||||
function checkRoot(route: RouteRecordRaw | RouteRecordRaw[]) {
|
||||
if (isArray(route)) {
|
||||
for (let i = 0, l = route.length; i < l; i++) {
|
||||
checkRoot(route[i]);
|
||||
}
|
||||
} else {
|
||||
if (route.name === 'root') {
|
||||
return (root = route);
|
||||
}
|
||||
if (route.children) {
|
||||
for (let i = 0, l = route.children.length; i < l; i++) {
|
||||
checkRoot(route.children[i]);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
checkRoot(routers);
|
||||
return !!root;
|
||||
}
|
||||
// if (!hasRoot(routers)) console.error('Route named root is required as the default route');
|
||||
|
||||
function setComponent(route: any) {
|
||||
/* @vite-ignore */
|
||||
const component = route.component;
|
||||
if (component) {
|
||||
route.component = () =>
|
||||
import(/* @vite-ignore */ `/nerv-lib/paas/view/service/${component}.vue`);
|
||||
} else {
|
||||
route.component = () =>
|
||||
import(/* @vite-ignore */ '/nerv-lib/paas/view/system/layout/content.vue');
|
||||
}
|
||||
if (route.children) {
|
||||
route.children.forEach((item: Object, index: number) => {
|
||||
route.children[index] = setComponent(item);
|
||||
});
|
||||
}
|
||||
return route;
|
||||
}
|
||||
|
||||
/**
|
||||
* 登陆页面路由
|
||||
*/
|
||||
export const LoginRoute = {
|
||||
path: '/login',
|
||||
name: 'login',
|
||||
component: appConfig.customLogin
|
||||
? appConfig.customLogin
|
||||
: () => import('/nerv-lib/paas/view/system/login.vue'),
|
||||
meta: {
|
||||
title: '登录',
|
||||
},
|
||||
};
|
||||
|
||||
/**
|
||||
* 页面不存在
|
||||
*/
|
||||
export const Error404Route = {
|
||||
path: '/:pathMatch(.*)*',
|
||||
name: 'error404',
|
||||
component: () => import('/nerv-lib/paas/view/service/error-404.vue'),
|
||||
};
|
||||
|
||||
/**
|
||||
* 页面无权限
|
||||
*/
|
||||
export const Error403Route = {
|
||||
path: '/403',
|
||||
name: 'error403',
|
||||
component: () => import('/nerv-lib/paas/view/service/error-403.vue'),
|
||||
};
|
||||
|
||||
/**
|
||||
* 默认路由 转发至root
|
||||
*/
|
||||
export const RootRoute = {
|
||||
path: '/',
|
||||
name: 'rootRoute',
|
||||
redirect: 'root',
|
||||
};
|
||||
const outControlPageName: any[] = [];
|
||||
Object.keys(modules).forEach((key) => {
|
||||
const mod = modules[key].default || {};
|
||||
if (mod.name && mod.outContrl) {
|
||||
outControlPageName.push(mod.name);
|
||||
}
|
||||
});
|
||||
export const WHITE_NAME_LIST = [
|
||||
Error404Route.name,
|
||||
Error403Route.name,
|
||||
LoginRoute.name,
|
||||
...outControlPageName,
|
||||
];
|
||||
|
||||
export const ALLRoute = [...routers, LoginRoute, Error403Route, Error404Route, RootRoute];
|
||||
Reference in New Issue
Block a user