/* * @Author: luoxi * @Date: 2022-01-25 09:51:12 * @LastEditors: luoxi * @LastEditTime: 2022-01-25 12:25:51 * @FilePath: \vue-admin-box\src\router\index.ts * @Description: */ /** * @description 所有人可使用的参数配置列表 * @params hideMenu: 是否隐藏当前路由结点不在导航中展示 * @params alwayShow: 只有一个子路由时是否总是展示菜单,默认false */ import { reactive } from 'vue' import { createRouter, createWebHashHistory } from 'vue-router' import {store} from '../store' import i18n from '../locale' //import NProgress from '@/utils/system/nprogress' import { ElMessage } from 'element-plus' import NProgress from 'nprogress' import 'nprogress/nprogress.css' import { getToken } from '../utils/auth' NProgress.configure({ showSpinner: false }) // 引入不需要权限的modules import System from './modules/system' import Layout from "../AppLayout/index.vue"; import {getAuthRoutes} from "./permission"; /** * @name 初始化必须要的路由 * @description 使用reactive属性使得modules可以在路由菜单里面实时响应,搞定菜单回显的问题 * @detail 针对modules的任何修改,均会同步至菜单级别,记住,是针对变量名为:moduels的修改 **/ const modules = reactive([ ...System ]) // const { t } = i18n.global // console.log(t) const router = createRouter({ history: createWebHashHistory(), routes: modules }) export function reBuildRoutes () { // const appModules = store.getters[AppMutationTypes.getAppModules]; let appModules= localStorage.getItem('locaMenu') || []; appModules = JSON.parse(appModules as any) const appRoot = { path: '/app', component: Layout as any, redirect: '/404', hideMenu: true, meta: {title: 'message.menu.system.name'}, children: appModules } modules.push(appRoot as any) router.addRoute(appRoot as any) } // 未授权时可访问的白名单 const whiteList = ['/login'] router.beforeEach((to, from, next) => { NProgress.start() if (getToken()) { if (to.path === '/login') { next({ path: '/' }) NProgress.done() } else { // eslint-disable-next-line no-debugger const loginInfo =store.getters.loginInfo;//[UserMutationTypes.GETTER_MODULE_MENUS] if (!loginInfo) { store.commit('setIsRelogin',true) // 判断当前用户是否已拉取完user_info信息 store.dispatch('getInfo').then(() => { store.commit('setIsRelogin',false) getAuthRoutes() // 因为部分菜单配置了query,所以需重新再整理一下路由 next({ ...to, replace: true }) }).catch((err:any) => { store.dispatch('logout').then(() => { ElMessage.error(err) next({ path: '/' }) }) }) } else { next() // let appModules= localStorage.getItem('locaMenu') as any; // appModules = JSON.parse(appModules as any) // // 如果没有跳转404 // const routeValue = to.path.substring(1,to.path.length) // console.log('=====routeValue==========', routeValue) // console.log('=====appModules==========', appModules) // const arr = appModules.map((item: any)=>{ // return item.path // }) // if (arr.includes(routeValue)) { // next() // }else{ // next({ path: '/user' }) // } } } } else { // 没有token if (whiteList.indexOf(to.path) !== -1) { // 在免登录白名单,直接进入 next() } else { next(`/login?redirect=${to.fullPath}`) // 否则全部重定向到登录页 NProgress.done() } } }) // router.beforeEach((to, _from, next) => { // console.log('-----to----------', to) // // next('/') // }) // 路由跳转前的监听操作 /* router.beforeEach((to, _from, next) => { NProgress.start(); if (store.state.user.token) { to.meta.title ? (changeTitle(to.meta.title)) : "" // 动态title if (to.path === '/login') { next('/') return } next() } else if (whiteList.includes(to.path)) { to.meta.title ? (changeTitle(to.meta.title)) : "" // 动态title next() } else { next("/login"); // 全部重定向到登录页 to.meta.title ? (changeTitle(to.meta.title)) : "" // 动态title } }); */ // 路由跳转后的监听操作 router.afterEach((to, _from) => { if(to.matched.length<=0) return; const keepAliveComponentsName = store.getters.keepAliveComponentsName || [] const nameData = to.matched[to.matched.length - 1].components || {} const name = nameData.default.name if (to.meta && to.meta.cache && name && !keepAliveComponentsName.includes(name)) { store.commit('addKeepAliveComponentsName', name) } NProgress.done() }); export { modules } export default router