123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180 |
- /* eslint-disable @typescript-eslint/restrict-template-expressions */
- /* eslint-disable @typescript-eslint/no-base-to-string */
- import { createRouter, createWebHistory, type RouteRecordRaw } from "vue-router"
- import appStore from "@s"
- import configs from "@@/config"
- import JwtService from "@@/services/JwtService"
- import { isReLogin } from "@@/services/RequestService"
- import message from "@@/utils/message"
- import { staticRouter } from "./_staticRouter"
- const whiteList = ["/login", "/register", "/icon", "/r"]
- export const to404: RouteRecordRaw = {
- path: "/:pathMatch(.*)*",
- name: "to404",
- redirect: "/404"
- }
- export const constantRoutes: RouteRecordRaw[] = [
- {
- path: "/",
- redirect: "/home",
- name: configs.MAIN_ROUTER_NAME,
- component: () => import("@/layouts/dynamic/Layout.vue"),
- children: [
- {
- path: "/home",
- name: "home",
- component: () => import("@/views/home.vue"),
- meta: {
- title: "首页",
- affix: true
- }
- },
- {
- path: "/profile",
- name: "profile",
- component: () => import("@/views/account/profile/index.vue"),
- meta: {
- title: "个人中心",
- noCache: false
- }
- }
- ]
- },
- ...staticRouter,
- {
- path: "/",
- children: [
- {
- path: "/login",
- name: "login",
- component: () => import("@/views/account/login.vue"),
- meta: {
- title: "登录"
- }
- }
- ]
- },
- {
- path: "/social-callback",
- component: () => import("@/views/account/social/callback.vue"),
- meta: {
- title: "登录"
- }
- },
- {
- path: "/",
- component: () => import("@/layouts/system/Index.vue"),
- children: [
- {
- // the 404 route, when none of the above matches
- path: "/404",
- name: "404",
- component: () => import("@/views/error/404.vue"),
- meta: {
- title: "404"
- }
- },
- {
- path: "/500",
- name: "500",
- component: () => import("@/views/error/500.vue"),
- meta: {
- title: "500"
- }
- }
- ]
- }
- ]
- const router = createRouter({
- //出现刷新页面404的问题 请在nginx配置root 下一行添加 try_files $uri $uri/ /index.html;
- history: createWebHistory(import.meta.env.VITE_APP_CONTEXT_PATH),
- routes: constantRoutes,
- scrollBehavior(to, _from, savedPosition) {
- if (savedPosition) {
- return savedPosition
- } else if (to.hash) {
- return {
- el: to.hash,
- top: 80,
- behavior: "smooth"
- }
- } else {
- return { top: 0, left: 0, behavior: "smooth" }
- }
- }
- })
- function doBefore(to: any) {
- //console.log("Router_Name", to.name)
- // 解决切换路由tooltip不消失
- document.querySelectorAll(".tooltip").forEach((v) => {
- v.remove()
- })
- }
- router.beforeEach((to, _from, next) => {
- doBefore(to)
- NProgressStart()
- const authStore = appStore.authStore
- if (JwtService.getToken()) {
- document.title = to.meta.title
- ? `${to.meta.title} - ${import.meta.env.VITE_APP_NAME}`
- : import.meta.env.VITE_APP_NAME
- appStore.bodyConfigStore.resetLayoutConfig()
- if (to.path === "/login") {
- next({ path: "/" })
- NProgressClose()
- } else {
- appStore.dictStore.initDict()
- if (!authStore.user?.userName) {
- isReLogin.show = true
- authStore
- .getInfo()
- .then(() => {
- isReLogin.show = false
- if (to.path.search("/redirect-tenant") == 0) {
- next({ ...to, replace: true })
- } else {
- appStore.menuStore.generateRoutes().then(() => {
- next({ ...to, replace: true })
- })
- }
- })
- .catch((err) => {
- isReLogin.show = false
- authStore.logout().then(() => {
- message.msgError(err)
- next({ path: import.meta.env.VITE_APP_CONTEXT_PATH + "home" })
- })
- })
- appStore.appConfigStore.loadConfig()
- } else {
- next()
- }
- }
- } else {
- // 没有token
- if (whiteList.includes(to.path)) {
- // 在免登录白名单,直接进入
- next()
- } else {
- next(`/login?redirect=${to.fullPath}`) // 否则全部重定向到登录页
- NProgressClose()
- }
- }
- // Scroll page to top on every route change
- // window.scrollTo({
- // top: 0,
- // left: 0,
- // behavior: "smooth",
- // })
- })
- router.afterEach((_to, _from) => {
- // console.log("AFTER", _to.path, _from.path)
- NProgressClose()
- })
- export default router
|