123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263 |
- import { createRouter, createWebHashHistory, RouteRecordRaw } from 'vue-router';
- import NProgress from 'nprogress';
- import 'nprogress/nprogress.css';
- import { store } from '/@/store/index';
- import { Session } from '/@/utils/storage';
- import { NextLoading } from '/@/utils/loading';
- import { staticRoutes, dynamicRoutes } from '/@/router/route';
- import { initFrontEndControlRoutes } from '/@/router/frontEnd';
- import { initBackEndControlRoutes } from '/@/router/backEnd';
- /**
- * 创建一个可以被 Vue 应用程序使用的路由实例
- * @method createRouter(options: RouterOptions): Router
- * @link 参考:https://next.router.vuejs.org/zh/api/#createrouter
- */
- const router = createRouter({
- history: createWebHashHistory(),
- routes: staticRoutes,
- });
- /**
- * 定义404界面
- * @link 参考:https://next.router.vuejs.org/zh/guide/essentials/history-mode.html#netlify
- */
- const pathMatch = {
- path: '/:path(.*)*',
- redirect: '/404',
- };
- /**
- * 路由多级嵌套数组处理成一维数组
- * @param arr 传入路由菜单数据数组
- * @returns 返回处理后的一维路由菜单数组
- */
- export function formatFlatteningRoutes(arr: any) {
- if (arr.length <= 0) return false;
- for (let i = 0; i < arr.length; i++) {
- if (arr[i].children) {
- arr = arr.slice(0, i + 1).concat(arr[i].children, arr.slice(i + 1));
- }
- }
- return arr;
- }
- /**
- * 一维数组处理成多级嵌套数组(只保留二级:也就是二级以上全部处理成只有二级,keep-alive 支持二级缓存)
- * @description isKeepAlive 处理 `name` 值,进行缓存。顶级关闭,全部不缓存
- * @link 参考:https://v3.cn.vuejs.org/api/built-in-components.html#keep-alive
- * @param arr 处理后的一维路由菜单数组
- * @returns 返回将一维数组重新处理成 `定义动态路由(dynamicRoutes)` 的格式
- */
- export function formatTwoStageRoutes(arr: any) {
- if (arr.length <= 0) return false;
- const newArr: any = [];
- const cacheList: Array<string> = [];
- arr.forEach((v: any) => {
- if (v.path === '/') {
- newArr.push({ component: v.component, name: v.name, path: v.path, redirect: v.redirect, meta: v.meta, children: [] });
- } else {
- // 判断是否是动态路由(xx/:id/:name),用于 tagsView 等中使用
- // 修复:https://gitee.com/lyt-top/vue-next-admin/issues/I3YX6G
- if (v.path.indexOf('/:') > -1) {
- v.meta['isDynamic'] = true;
- v.meta['isDynamicPath'] = v.path;
- }
- newArr[0].children.push({ ...v });
- // 存 name 值,keep-alive 中 include 使用,实现路由的缓存
- // 路径:/@/layout/routerView/parent.vue
- if (newArr[0].meta?.isKeepAlive && v.meta?.isKeepAlive) {
- cacheList.push(v.name);
- store.dispatch('keepAliveNames/setCacheKeepAlive', cacheList);
- }
- }
- });
- return newArr;
- }
- /**
- * 缓存多级嵌套数组处理后的一维数组
- * @description 用于 tagsView、菜单搜索中:未过滤隐藏的(isHide)
- */
- export function setCacheTagsViewRoutes() {
- // 获取有权限的路由,否则 tagsView、菜单搜索中无权限的路由也将显示
- let rolesRoutes = dynamicRoutes
- // 添加到 vuex setTagsViewRoutes 中
- store.dispatch('tagsViewRoutes/setTagsViewRoutes', formatTwoStageRoutes(formatFlatteningRoutes(rolesRoutes))[0].children);
- }
- /**
- * 判断路由 `meta.roles` 中是否包含当前登录用户权限字段
- * @param roles 用户权限标识,在 userInfos(用户信息)的 roles(登录页登录时缓存到浏览器)数组
- * @param route 当前循环时的路由项
- * @returns 返回对比后有权限的路由项
- */
- export function hasRoles(roles: any, route: any) {
- if (route.meta && route.meta.roles) return roles.some((role: any) => route.meta.roles.includes(role));
- else return true;
- }
- /**
- * 获取当前用户权限标识去比对路由表,设置递归过滤有权限的路由
- * @param routes 当前路由 children
- * @param roles 用户权限标识,在 userInfos(用户信息)的 roles(登录页登录时缓存到浏览器)数组
- * @returns 返回有权限的路由数组 `meta.roles` 中控制
- */
- export function setFilterHasRolesMenu(routes: any, roles: any) {
- const menu: any = [];
- routes.forEach((route: any) => {
- const item = { ...route };
- if (hasRoles(roles, item)) {
- menu.push(item);
- }
- });
- return menu;
- }
- /**
- * 设置递归过滤有权限的路由到 vuex routesList 中(已处理成多级嵌套路由)及缓存多级嵌套数组处理后的一维数组
- * @description 用于左侧菜单、横向菜单的显示
- * @description 用于 tagsView、菜单搜索中:未过滤隐藏的(isHide)
- */
- export function setFilterMenuAndCacheTagsViewRoutes() {
- store.dispatch('routesList/setRoutesList', dynamicRoutes[0].children);
- setCacheTagsViewRoutes();
- }
- /**
- * 获取当前用户权限标识去比对路由表(未处理成多级嵌套路由)
- * @description 这里主要用于动态路由的添加,router.addRoute
- * @link 参考:https://next.router.vuejs.org/zh/api/#addroute
- * @param chil dynamicRoutes(/@/router/route)第一个顶级 children 的下路由集合
- * @returns 返回有当前用户权限标识的路由数组
- */
- export function setFilterRoute(chil: any) {
- let filterRoute: any = [];
- chil.forEach((route: any) => {
- if (route.meta.roles) {
- route.meta.roles.forEach((metaRoles: any) => {
- store.state.userInfos.userInfos.roles.forEach((roles: any) => {
- if (metaRoles === roles) filterRoute.push({ ...route });
- });
- });
- }
- });
- return filterRoute;
- }
- /**
- * 获取有当前用户权限标识的路由数组,进行对原路由的替换
- * @description 替换 dynamicRoutes(/@/router/route)第一个顶级 children 的路由
- * @returns 返回替换后的路由数组
- */
- export function setFilterRouteEnd() {
- let filterRouteEnd: any = formatTwoStageRoutes(formatFlatteningRoutes(dynamicRoutes));
- filterRouteEnd[0].children = [...filterRouteEnd[0].children, { ...pathMatch }];
- return filterRouteEnd;
- }
- /**
- * 添加动态路由
- * @method router.addRoute
- * @description 此处循环为 dynamicRoutes(/@/router/route)第一个顶级 children 的路由一维数组,非多级嵌套
- * @link 参考:https://next.router.vuejs.org/zh/api/#addroute
- */
- export async function setAddRoute() {
- await setFilterRouteEnd().forEach((route: RouteRecordRaw) => {
- const routeName: any = route.name;
- if (!router.hasRoute(routeName)) router.addRoute(route);
- });
- // 修改首页重定向的地址,从后台配置中获取首页的地址并在登录之后和刷新页面时进行修改
- const sysinfo = JSON.parse(localStorage.sysinfo || '{}');
- const homePage = router.getRoutes().find((item) => item.path === '/');
- homePage && sysinfo.systemHomePageRoute && (homePage.redirect = sysinfo.systemHomePageRoute) || '/home';
- }
- /**
- * 删除/重置路由
- * @method router.removeRoute
- * @description 此处循环为 dynamicRoutes(/@/router/route)第一个顶级 children 的路由一维数组,非多级嵌套
- * @link 参考:https://next.router.vuejs.org/zh/api/#push
- */
- export async function resetRoute() {
- await setFilterRouteEnd().forEach((route: RouteRecordRaw) => {
- const routeName: any = route.name;
- router.hasRoute(routeName) && router.removeRoute(routeName);
- });
- }
- // isRequestRoutes 为 true,则开启后端控制路由,路径:`/src/store/modules/themeConfig.ts`
- const { isRequestRoutes } = store.state.themeConfig.themeConfig;
- // 前端控制路由:初始化方法,防止刷新时路由丢失
- if (!isRequestRoutes) initFrontEndControlRoutes();
- // import { isInit } from "/@/api/system/dbInit"
- // 路由加载前
- router.beforeEach(async (to, from, next) => {
- NProgress.configure({ showSpinner: false });
- if (to.meta?.title) NProgress.start();
- // 系统初始化
- if (to.path === '/dbInit') {
- next();
- NProgress.done();
- }
- // if (Session.get("isInit") !== true) {
- // const res: any = await isInit()
- // let { code, data } = res
- // if (code === 0) {
- // if (data === false) {
- // next('/dbInit');
- // NProgress.done();
- // return
- // } else {
- // Session.set("isInit", true)
- // }
- // }
- // }
- // 正常流程
- const token = localStorage.token;
- if (to.path === '/login' && !token) {
- next();
- NProgress.done();
- } else {
- if (!token) {
- const params = Object.keys(to.query).length || Object.keys(to.params).length
- let paramsStr = ''
- if (params) {
- paramsStr = `¶ms=${JSON.stringify(Object.keys(to.query).length ? to.query : to.params)}`
- }
- next(`/login?redirect=${to.path}${paramsStr}`);
- Session.clear();
- resetRoute();
- NProgress.done();
- } else if (token && to.path === '/login') {
- next('/');
- NProgress.done();
- } else {
- if (store.state.routesList.routesList.length === 0) {
- if (isRequestRoutes) {
- // 后端控制路由:路由数据初始化,防止刷新时丢失
- await initBackEndControlRoutes();
- // 动态添加路由:防止非首页刷新时跳转回首页的问题
- // 确保 addRoute() 时动态添加的路由已经被完全加载上去
- next({ ...to, replace: true });
- }
- } else {
- next();
- }
- }
- }
- });
- // 路由加载后
- router.afterEach(() => {
- NProgress.done();
- NextLoading.done();
- });
- // 导出路由
- export default router;
|