| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152 |
- /**
- * 路由配置
- */
- import Vue from 'vue';
- import NProgress from 'nprogress';
- import VueRouter from 'vue-router';
- import { WHITE_LIST, REDIRECT_PATH, LAYOUT_PATH } from '@/config/setting';
- import store from '@/store';
- import { getToken, setToken, getCurrentUser } from '@/utils/token-util';
- import { routes, getMenuRoutes } from './routes';
- import { SYSTEM_NAME } from '@/config/setting';
- import { getLoginUser } from '@/api/login';
- import { changeRole } from '@/api/layout/index';
- Vue.use(VueRouter);
- const router = new VueRouter({
- base: window.__POWERED_BY_QIANKUN__
- ? `/page-${SYSTEM_NAME}/`
- : `/${SYSTEM_NAME}/`,
- routes,
- mode: 'history',
- scrollBehavior() {
- return { y: 0 };
- }
- });
- /**
- * 路由守卫
- */
- router.beforeEach((to, from, next) => {
- // console.log(store.state.user);
- // console.log(to, 999);
- // console.log(from);
- if (!from.path.includes(REDIRECT_PATH)) {
- NProgress.start();
- }
- // 判断是否登录
- if (getToken()) {
- if (!store.state.user.info?.userId) {
- getLoginUser().then((res) => {
- store.commit('user/setUserInfo', res);
- });
- }
- console.log(!store.state.user.menus);
- // 还未注册动态路由则先获取
- if (!store.state.user.menus) {
- store
- .dispatch('user/fetchUserInfo')
- .then(({ menus, homePath, authoritiesRouter }) => {
- menus.forEach((item) => {
- //匹配第一层路由名称
- if (item.name == '配置管理') {
- item.children.push(
- {
- path: '/bpm/definition',
- component: 'bpm/definition',
- name: '流程定义',
- meta: {
- title: '流程定义',
- hide: true,
- routePath: '/bpm/definition'
- }
- },
- {
- path: '/bpm/modelEditor',
- component: 'bpm/model/modelEditor',
- name: '设计流程',
- meta: {
- title: '设计流程',
- hide: true,
- routePath: '/bpm/modelEditor'
- }
- },
- {
- path: '/bpm/processInstance',
- component: 'bpm/processInstance/detail',
- name: '流程详情',
- meta: {
- title: '流程详情',
- hide: true,
- routePath: '/bpm/processInstance'
- }
- }
- );
- }
- });
- if (menus) {
- router.addRoute(
- getMenuRoutes([...menus, ...authoritiesRouter], homePath)
- );
- next({ ...to, replace: true });
- }
- console.log(router);
- })
- .catch((e) => {
- console.error(e);
- next();
- });
- } else {
- if (routes.length >= router.getRoutes()?.length) {
- router.addRoute(
- getMenuRoutes([
- ...store.state.user.menus,
- ...store.state.user.authoritiesRouter,
- to.fullPath
- ])
- );
- next({ ...to });
- } else {
- next();
- }
- }
- } else if (WHITE_LIST.includes(to.path)) {
- next();
- } else {
- next({
- path: '/login',
- query: to.path === LAYOUT_PATH ? {} : { from: to.path }
- });
- }
- });
- router.afterEach((to) => {
- if (!to.path.includes(REDIRECT_PATH) && NProgress.isStarted()) {
- setTimeout(() => {
- NProgress.done(true);
- }, 200);
- }
- });
- router.roleChange = async ({ menus, homePath, authoritiesRouter }) => {
- const currentUser = getCurrentUser()
- if (menus && menus.length > 0) {
- router.addRoute(getMenuRoutes([...menus, ...authoritiesRouter], homePath));
- if (router.currentRoute.path != (menus[0].redirect || menus[0].path)) {
- await router.replace({
- path: menus[0].redirect || menus[0].path
- });
- }
- const newToken = await changeRole({
- groupId: currentUser.currentGroupId,
- roleId: currentUser.currentRoleId
- });
- setToken(newToken);
- setTimeout(() => {
- window.location.reload();
- }, 100);
- }
- // next({ ...to, replace: true });
- };
- export default router;
|