This commit is contained in:
2023-06-05 21:02:25 +08:00
parent bdbda8b68e
commit d778ceab61
41 changed files with 1259 additions and 94 deletions

View File

@@ -0,0 +1,15 @@
import { appRoutes, appExternalRoutes } from "@/router/routes"
const mixinRoutes = [...appRoutes, appExternalRoutes]
const appClientMenus = mixinRoutes.map((el) => {
const { name, path, meta, redirect, children } = el
return {
name,
path,
meta,
redirect,
children
}
})
export default appClientMenus

View File

@@ -23,7 +23,7 @@ export const DEFAULT_ROUTE_NAME = "Workplace"
* @type: object
*/
export const DEFAULT_ROUTE = {
title: "menu.dashboard.workplace",
title: "工作台",
name: DEFAULT_ROUTE_NAME,
fullPath: "/dashboard/workplace"
}

View File

@@ -0,0 +1,16 @@
import { setRouteEmitter } from "@/utils/route-listener"
import setupUserLoginInfoGuard from "./userLoginInfo"
import setupPermissionGuard from "@/router/guard/permisstion"
function setupPageGuard(router) {
router.beforeEach(async (to) => {
// 发出路由改变的事件
setRouteEmitter(to)
})
}
export default function createRouteGuard(router) {
setupPageGuard(router)
setupUserLoginInfoGuard(router)
setupPermissionGuard(router)
}

View File

@@ -0,0 +1,48 @@
import NProgress from "nprogress" // progress bar
import usePermission from "@/hooks/permission"
import { useUserStore, useAppStore } from "@/store"
import { appRoutes } from "../routes"
import { WHITE_LIST, NOT_FOUND } from "../constants"
// 权限守卫
export default function setupPermissionGuard(router) {
router.beforeEach(async (to, from, next) => {
const appStore = useAppStore()
const userStore = useUserStore()
const Permission = usePermission()
// 该函数根据角色返回是否有这个路由
const permissionsAllow = Permission.accessRouter(to)
if (appStore.menuFromServer) {
// 针对来自服务端的菜单配置进行处理
// Handle routing configuration from the server
// 根据需要自行完善来源于服务端的菜单配置的permission逻辑
// Refine the permission logic from the server's menu configuration as needed
if (!appStore.appAsyncMenus.length && !WHITE_LIST.find((el) => el.name === to.name)) {
await appStore.fetchServerMenuConfig()
}
const serverMenuConfig = [...appStore.appAsyncMenus, ...WHITE_LIST]
let exist = false
while (serverMenuConfig.length && !exist) {
const element = serverMenuConfig.shift()
if (element?.name === to.name) exist = true
if (element?.children) {
serverMenuConfig.push(...element.children)
}
}
if (exist && permissionsAllow) {
next()
} else next(NOT_FOUND)
} else {
// eslint-disable-next-line no-lonely-if
if (permissionsAllow) next()
else {
const destination = Permission.findFirstPermissionRoute(appRoutes, userStore.role) || NOT_FOUND
next(destination)
}
}
NProgress.done()
})
}

View File

@@ -0,0 +1,40 @@
import NProgress from "nprogress" // progress bar
import { useUserStore } from "@/store"
// userInfo守卫
export default function setupUserLoginInfoGuard(router) {
router.beforeEach(async (to, from, next) => {
NProgress.start()
const userStore = useUserStore()
if (userStore.isLogin()) {
if (userStore.role) {
next()
} else {
try {
await userStore.info()
next()
} catch (error) {
await userStore.logout()
next({
name: "login",
query: {
redirect: to.name,
...to.query
}
})
}
}
} else {
if (to.name === "login") {
next()
return
}
next({
name: "login",
query: {
redirect: to.name,
...to.query
}
})
}
})
}

View File

@@ -3,6 +3,7 @@ import { createRouter, createWebHistory } from "vue-router"
import { appRoutes } from "./routes"
// 引入基本重定向路由和notFound路由
import { REDIRECT_MAIN, NOT_FOUND_ROUTE } from "./routes/base"
import createRouteGuard from "@/router/guard/index"
const router = createRouter({
history: createWebHistory(),
@@ -28,4 +29,5 @@ const router = createRouter({
return { top: 0 }
}
})
createRouteGuard(router)
export default router

View File

@@ -1,15 +1,15 @@
import { REDIRECT_ROUTE_NAME } from "@/router/constants"
/**
* @description: 返回import("@/layout/default-layout.vue")
* @description: import('@/layout/default-layout.vue')
* @type: Promise对象
*/
export const DEFAULT_LAYOUT = () => import("@/layout/default-layout.vue")
export const DEFAULT_LAYOUT = () => import('@/layout/default-layout.vue')
export const REDIRECT_MAIN = {
path: "/redirect",
name: "redirectWrapper",
component: DEFAULT_LAYOUT, // () => import("@/layout/default-layout.vue")
component: DEFAULT_LAYOUT, // import('@/layout/default-layout.vue')
meta: {
requiresAuth: true,
hideInMenu: true
@@ -30,5 +30,5 @@ export const REDIRECT_MAIN = {
export const NOT_FOUND_ROUTE = {
path: "/:pathMatch(.*)*",
name: "notFound",
component: () => import("@/views/not-found/index.vue")
component: () => import("@/layout/404.vue")
}

View File

@@ -1 +0,0 @@
import { setRouteEmitter } from "@/utils/route-listener"

View File

@@ -1 +0,0 @@
import NProgress from 'nprogress'

View File

@@ -5,10 +5,10 @@ const DASHBOARD = {
name: "dashboard",
component: DEFAULT_LAYOUT, // () => import("@/layout/default-layout.vue")
meta: {
locale: "menu.dashboard",
requiresAuth: true,
icon: "icon-dashboard",
order: 0
order: 0,
locale:"图标展示"
},
children: [
{
@@ -16,9 +16,9 @@ const DASHBOARD = {
name: "Workplace",
component: () => import("@/views/dashboard/workplace/index.vue"),
meta: {
locale: "menu.dashboard.workplace",
requiresAuth: true,
roles: ["*"]
roles: ["*"],
locale:"工作台"
}
},
]