| | |
| | | import { createRouter, createWebHashHistory } from 'vue-router'; |
| | | import NProgress from 'nprogress'; |
| | | import 'nprogress/nprogress.css'; |
| | | import pinia from '/@/stores/index'; |
| | | import { storeToRefs } from 'pinia'; |
| | | import { useKeepALiveNames } from '/@/stores/keepAliveNames'; |
| | | import { useRoutesList } from '/@/stores/routesList'; |
| | | import { useThemeConfig } from '/@/stores/themeConfig'; |
| | | import { Local, Session } from '/@/utils/storage'; |
| | | import { staticRoutes, notFoundAndNoPower } from '/@/router/route'; |
| | | import { initFrontEndControlRoutes } from '/@/router/frontEnd'; |
| | | import { initBackEndControlRoutes } from '/@/router/backEnd'; |
| | | import { createRouter, createWebHashHistory } from 'vue-router'; |
| | | import { MenuTypeEnum } from '/@/api/menu/type'; |
| | | import { accessTokenKey, clearAccessTokens } from '/@/utils/request'; |
| | | import { notFoundAndNoPower, staticRoutes } from '/@/router/route'; |
| | | import pinia from '/@/stores/index'; |
| | | import { useKeepALiveNames } from '/@/stores/keepAliveNames'; |
| | | import { useThemeConfig } from '/@/stores/themeConfig'; |
| | | import { Local } from '../utils/storage'; |
| | | import { accessSessionKey } from '../utils/request'; |
| | | import emitter from '../utils/mitt'; |
| | | import { SHARE_PAGE_NAME, activeChatRoom, isSharePage } from '../stores/chatRoom'; |
| | | import { nextTick } from 'vue'; |
| | | |
| | | /** |
| | | * 1、前端控制路由时:isRequestRoutes 为 false,需要写 roles,需要走 setFilterRoute 方法。 |
| | |
| | | } |
| | | |
| | | // 路由加载前 |
| | | router.beforeEach(async (to, from, next) => { |
| | | NProgress.configure({ showSpinner: false }); |
| | | if (to.meta.title) NProgress.start(); |
| | | const token = Local.get(accessTokenKey); |
| | | if (to.path === '/login' && !token) { |
| | | next(); |
| | | NProgress.done(); |
| | | } else { |
| | | if (!token) { |
| | | next(`/login?redirect=${to.path}¶ms=${JSON.stringify(to.query ? to.query : to.params)}`); |
| | | clearAccessTokens(); |
| | | NProgress.done(); |
| | | } else if (token && to.path === '/login') { |
| | | next('/'); |
| | | NProgress.done(); |
| | | } else { |
| | | const storesRoutesList = useRoutesList(pinia); |
| | | const { routesList } = storeToRefs(storesRoutesList); |
| | | if (routesList.value.length === 0) { |
| | | if (isRequestRoutes) { |
| | | // 后端控制路由:路由数据初始化,防止刷新时丢失 |
| | | await initBackEndControlRoutes(); |
| | | // 解决刷新时,一直跳 404 页面问题,关联问题 No match found for location with path 'xxx' |
| | | // to.query 防止页面刷新时,普通路由带参数时,参数丢失。动态路由(xxx/:id/:name")isDynamic 无需处理 |
| | | next({ path: to.path, query: to.query }); |
| | | } else { |
| | | // https://gitee.com/lyt-top/vue-next-admin/issues/I5F1HP |
| | | await initFrontEndControlRoutes(); |
| | | next({ path: to.path, query: to.query }); |
| | | } |
| | | } else { |
| | | next(); |
| | | } |
| | | router.beforeEach((to, from, next) => { |
| | | const accessSession = Local.get(accessSessionKey); |
| | | if (!accessSession && to.name !==SHARE_PAGE_NAME) { |
| | | emitter.emit('openLoginDlg'); |
| | | if(to.name==='Home'){ |
| | | next(); |
| | | }else{ |
| | | next({ |
| | | name: 'Home', |
| | | }); |
| | | } |
| | | |
| | | } |
| | | next(); |
| | | }); |
| | | |
| | | // 路由加载后 |
| | | router.afterEach(() => { |
| | | NProgress.done(); |
| | | }); |
| | | |
| | | // 导出路由 |
| | | export default router; |