feat: 实现登录登出的逻辑

This commit is contained in:
valarchie
2023-07-04 21:55:03 +08:00
parent 8f46526cc0
commit a021e7f9d5
10 changed files with 85 additions and 147 deletions

View File

@@ -1,7 +1,7 @@
// import "@/utils/sso";
import { getConfig } from "@/config";
import NProgress from "@/utils/progress";
import { sessionKey, type DataInfo } from "@/utils/auth";
import { sessionKey } from "@/utils/auth";
import { useMultiTagsStoreHook } from "@/store/modules/multiTags";
import { usePermissionStoreHook } from "@/store/modules/permission";
import {
@@ -25,6 +25,7 @@ import { buildHierarchyTree } from "@/utils/tree";
import { isUrl, openLink, storageSession, isAllEmpty } from "@pureadmin/utils";
import remainingRouter from "./modules/remaining";
import { TokenDTO } from "@/api/common";
/** 自动导入全部静态路由,无需再手动引入!匹配 src/router/modules 目录(任何嵌套级别)中具有 .ts 扩展名的所有文件,除了 remaining.ts 文件
* 如何匹配所有文件请看https://github.com/mrmlnc/fast-glob#basic-syntax
@@ -108,7 +109,7 @@ router.beforeEach((to: ToRouteType, _from, next) => {
handleAliveRoute(to);
}
}
const userInfo = storageSession().getItem<DataInfo<number>>(sessionKey);
const userInfo = storageSession().getItem<TokenDTO>(sessionKey)?.currentUser;
NProgress.start();
const externalLink = isUrl(to?.name as string);
if (!externalLink) {
@@ -125,7 +126,7 @@ router.beforeEach((to: ToRouteType, _from, next) => {
}
if (userInfo) {
// 无权限跳转403页面
if (to.meta?.roles && !isOneOfArray(to.meta?.roles, userInfo?.roles)) {
if (to.meta?.roles && !isOneOfArray(to.meta?.roles, [userInfo.roleKey])) {
next({ path: "/error/403" });
}
// 开启隐藏首页后在浏览器地址栏手动输入首页welcome路由则跳转到404页面

View File

@@ -19,7 +19,7 @@ import {
import { getConfig } from "@/config";
import { menuType } from "@/layout/types";
import { buildHierarchyTree } from "@/utils/tree";
import { sessionKey, type DataInfo } from "@/utils/auth";
import { sessionKey } from "@/utils/auth";
import { useMultiTagsStoreHook } from "@/store/modules/multiTags";
import { usePermissionStoreHook } from "@/store/modules/permission";
const IFrame = () => import("@/layout/frameView.vue");
@@ -28,6 +28,7 @@ const modulesRoutes = import.meta.glob("/src/views/**/*.{vue,tsx}");
// 动态路由
import { getAsyncRoutes } from "@/api/routes";
import { TokenDTO } from "@/api/common";
function handRank(routeInfo: any) {
const { name, path, parentId, meta } = routeInfo;
@@ -83,8 +84,9 @@ function isOneOfArray(a: Array<string>, b: Array<string>) {
/** 从sessionStorage里取出当前登陆用户的角色roles过滤无权限的菜单 */
function filterNoPermissionTree(data: RouteComponent[]) {
const currentRoles =
storageSession().getItem<DataInfo<number>>(sessionKey)?.roles ?? [];
const roleKey =
storageSession().getItem<TokenDTO>(sessionKey).currentUser.roleKey;
const currentRoles = roleKey ? [roleKey] : [];
const newTree = cloneDeep(data).filter((v: any) =>
isOneOfArray(v.meta?.roles, currentRoles)
);