fix: 🐛 注销清除用户数据方法名修改

This commit is contained in:
ray
2024-11-13 13:36:29 +08:00
parent 9bc060b512
commit 5e90e5ca06

View File

@@ -1,8 +1,4 @@
import type { import type { NavigationGuardNext, RouteLocationNormalized, RouteRecordRaw } from "vue-router";
NavigationGuardNext,
RouteLocationNormalized,
RouteRecordRaw,
} from "vue-router";
import NProgress from "@/utils/nprogress"; import NProgress from "@/utils/nprogress";
import { getToken } from "@/utils/auth"; import { getToken } from "@/utils/auth";
import router from "@/router"; import router from "@/router";
@@ -22,15 +18,14 @@ export function setupPermission() {
next({ path: "/" }); next({ path: "/" });
} else { } else {
const permissionStore = usePermissionStore(); const permissionStore = usePermissionStore();
// 判断路由是否加载 // 判断路由是否加载完成
if (permissionStore.isRoutesLoaded) { if (permissionStore.isRoutesLoaded) {
if (to.matched.length === 0) { if (to.matched.length === 0) {
// 路由未匹配跳转到404 // 路由未匹配跳转到404
next("/404"); next("/404");
} else { } else {
// 动态设置页面标题 // 动态设置页面标题
const title = const title = (to.params.title as string) || (to.query.title as string);
(to.params.title as string) || (to.query.title as string);
if (title) { if (title) {
to.meta.title = title; to.meta.title = title;
} }
@@ -40,14 +35,12 @@ export function setupPermission() {
try { try {
// 生成动态路由 // 生成动态路由
const dynamicRoutes = await permissionStore.generateRoutes(); const dynamicRoutes = await permissionStore.generateRoutes();
dynamicRoutes.forEach((route: RouteRecordRaw) => dynamicRoutes.forEach((route: RouteRecordRaw) => router.addRoute(route));
router.addRoute(route) next({ ...to, replace: true });
);
next({ ...to, replace: true }); // 添加动态路由后重新导航
} catch (error) { } catch (error) {
console.error(error); console.error(error);
// 路由加载失败,重置 token 并重定向到登录页 // 路由加载失败,重置 token 并重定向到登录页
await useUserStore().clearUserSession(); await useUserStore().clearUserData();
redirectToLogin(to, next); redirectToLogin(to, next);
NProgress.done(); NProgress.done();
} }
@@ -60,7 +53,7 @@ export function setupPermission() {
} else { } else {
// 不在白名单,重定向到登录页 // 不在白名单,重定向到登录页
redirectToLogin(to, next); redirectToLogin(to, next);
NProgress.done(); // 关闭进度条 NProgress.done();
} }
} }
}); });
@@ -72,10 +65,7 @@ export function setupPermission() {
} }
/** 重定向到登录页 */ /** 重定向到登录页 */
function redirectToLogin( function redirectToLogin(to: RouteLocationNormalized, next: NavigationGuardNext) {
to: RouteLocationNormalized,
next: NavigationGuardNext
) {
const params = new URLSearchParams(to.query as Record<string, string>); const params = new URLSearchParams(to.query as Record<string, string>);
const queryString = params.toString(); const queryString = params.toString();
const redirect = queryString ? `${to.path}?${queryString}` : to.path; const redirect = queryString ? `${to.path}?${queryString}` : to.path;
@@ -83,10 +73,7 @@ function redirectToLogin(
} }
/** 判断是否有权限 */ /** 判断是否有权限 */
export function hasAuth( export function hasAuth(value: string | string[], type: "button" | "role" = "button") {
value: string | string[],
type: "button" | "role" = "button"
) {
const { roles, perms } = useUserStore().userInfo; const { roles, perms } = useUserStore().userInfo;
// 超级管理员 拥有所有权限 // 超级管理员 拥有所有权限