fix: 修复角色数组为空时导致路由守卫无限循环的问题
This commit is contained in:
parent
4a58eb8e03
commit
32101689bf
@ -8,14 +8,14 @@ interface IAsyncRouteSettings {
|
|||||||
open: boolean
|
open: boolean
|
||||||
/** 当动态路由功能关闭时:
|
/** 当动态路由功能关闭时:
|
||||||
* 1. 应该将所有路由都写到常驻路由里面(表明所有登陆的用户能访问的页面都是一样的)
|
* 1. 应该将所有路由都写到常驻路由里面(表明所有登陆的用户能访问的页面都是一样的)
|
||||||
* 2. 系统自动给当前登录用户赋值一个默认的角色(默认为 admin,拥有所有页面权限)
|
* 2. 系统自动给当前登录用户赋值一个没有任何作用的默认角色
|
||||||
*/
|
*/
|
||||||
defaultRoles: Array<string>
|
defaultRoles: Array<string>
|
||||||
}
|
}
|
||||||
|
|
||||||
const asyncRouteSettings: IAsyncRouteSettings = {
|
const asyncRouteSettings: IAsyncRouteSettings = {
|
||||||
open: true,
|
open: true,
|
||||||
defaultRoles: ["admin"]
|
defaultRoles: ["DEFAULT_ROLE"]
|
||||||
}
|
}
|
||||||
|
|
||||||
export default asyncRouteSettings
|
export default asyncRouteSettings
|
||||||
|
@ -3,6 +3,7 @@ import store from "@/store"
|
|||||||
import { defineStore } from "pinia"
|
import { defineStore } from "pinia"
|
||||||
import { type RouteRecordRaw } from "vue-router"
|
import { type RouteRecordRaw } from "vue-router"
|
||||||
import { constantRoutes, asyncRoutes } from "@/router"
|
import { constantRoutes, asyncRoutes } from "@/router"
|
||||||
|
import asyncRouteSettings from "@/config/async-route"
|
||||||
|
|
||||||
const hasPermission = (roles: string[], route: RouteRecordRaw) => {
|
const hasPermission = (roles: string[], route: RouteRecordRaw) => {
|
||||||
if (route.meta && route.meta.roles) {
|
if (route.meta && route.meta.roles) {
|
||||||
@ -38,7 +39,7 @@ export const usePermissionStore = defineStore("permission", () => {
|
|||||||
|
|
||||||
const setRoutes = (roles: string[]) => {
|
const setRoutes = (roles: string[]) => {
|
||||||
let accessedRoutes
|
let accessedRoutes
|
||||||
if (roles.includes("admin")) {
|
if (!asyncRouteSettings.open) {
|
||||||
accessedRoutes = asyncRoutes
|
accessedRoutes = asyncRoutes
|
||||||
} else {
|
} else {
|
||||||
accessedRoutes = filterAsyncRoutes(asyncRoutes, roles)
|
accessedRoutes = filterAsyncRoutes(asyncRoutes, roles)
|
||||||
|
@ -8,6 +8,7 @@ import router, { resetRouter } from "@/router"
|
|||||||
import { loginApi, getUserInfoApi } from "@/api/login"
|
import { loginApi, getUserInfoApi } from "@/api/login"
|
||||||
import { type ILoginRequestData } from "@/api/login/types/login"
|
import { type ILoginRequestData } from "@/api/login/types/login"
|
||||||
import { type RouteRecordRaw } from "vue-router"
|
import { type RouteRecordRaw } from "vue-router"
|
||||||
|
import asyncRouteSettings from "@/config/async-route"
|
||||||
|
|
||||||
export const useUserStore = defineStore("user", () => {
|
export const useUserStore = defineStore("user", () => {
|
||||||
const token = ref<string>(getToken() || "")
|
const token = ref<string>(getToken() || "")
|
||||||
@ -44,8 +45,15 @@ export const useUserStore = defineStore("user", () => {
|
|||||||
return new Promise((resolve, reject) => {
|
return new Promise((resolve, reject) => {
|
||||||
getUserInfoApi()
|
getUserInfoApi()
|
||||||
.then((res) => {
|
.then((res) => {
|
||||||
roles.value = res.data.roles
|
const data = res.data
|
||||||
username.value = res.data.username
|
username.value = data.username
|
||||||
|
// 验证返回的 roles 是否是一个非空数组
|
||||||
|
if (data.roles && data.roles.length > 0) {
|
||||||
|
roles.value = data.roles
|
||||||
|
} else {
|
||||||
|
// 塞入一个没有任何作用的默认角色,不然路由守卫逻辑会无限循环
|
||||||
|
roles.value = asyncRouteSettings.defaultRoles
|
||||||
|
}
|
||||||
resolve(res)
|
resolve(res)
|
||||||
})
|
})
|
||||||
.catch((error) => {
|
.catch((error) => {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user