feat: 逻辑完善,对接后端菜单接口完成

This commit is contained in:
李寻欢 2024-02-01 18:10:55 +08:00
parent 9f1b4ed85b
commit 43517f9c3f
11 changed files with 30 additions and 26 deletions

View File

@ -1,10 +1,6 @@
import { http } from "@/utils/http";
type Result = {
success: boolean;
data: Array<any>;
};
import { BaseResponse } from "@/api/base";
export const getAsyncRoutes = () => {
return http.request<Result>("get", "/getAsyncRoutes");
return http.request<BaseResponse<Array<any>>>("get", "/admin/v1/menu/self");
};

View File

@ -8,7 +8,15 @@ import { addIcon } from "@iconify/vue/dist/offline";
import HomeFilled from "@iconify-icons/ep/home-filled";
import InformationLine from "@iconify-icons/ri/information-line";
import Lollipop from "@iconify-icons/ep/lollipop";
import Tools from "@iconify-icons/ep/tools";
import User from "@iconify-icons/ep/user";
import Guide from "@iconify-icons/ep/guide";
import View from "@iconify-icons/ep/view";
addIcon("homeFilled", HomeFilled);
addIcon("informationLine", InformationLine);
addIcon("lollipop", Lollipop);
addIcon("tools", Tools);
addIcon("user", User);
addIcon("guide", Guide);
addIcon("view", View);

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 { LoginResult } from "@/api/login";
/** 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<LoginResult>(sessionKey);
NProgress.start();
const externalLink = isUrl(to?.name as string);
if (!externalLink) {

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 { LoginResult } from "@/api/login";
function handRank(routeInfo: any) {
const { name, path, parentId, meta } = routeInfo;
@ -76,15 +77,13 @@ function filterChildrenTree(data: RouteComponent[]) {
function isOneOfArray(a: Array<string>, b: Array<string>) {
return Array.isArray(a) && Array.isArray(b)
? intersection(a, b).length > 0
? true
: false
: true;
}
/** 从sessionStorage里取出当前登陆用户的角色roles过滤无权限的菜单 */
function filterNoPermissionTree(data: RouteComponent[]) {
const currentRoles =
storageSession().getItem<DataInfo<number>>(sessionKey)?.roles ?? [];
storageSession().getItem<LoginResult>(sessionKey)?.roles ?? [];
const newTree = cloneDeep(data).filter((v: any) =>
isOneOfArray(v.meta?.roles, currentRoles)
);

View File

@ -23,7 +23,7 @@ export function getToken(): LoginResult {
export function setToken(data: LoginResult) {
let expires = 0;
const { access_token, refresh_token } = data;
expires = new Date(data.expires).getTime(); // 如果后端直接设置时间戳将此处代码改为expires = data.expires然后把上面的DataInfo<Date>改成DataInfo<number>即可
expires = new Date(data.expires).getTime();
const cookieString = JSON.stringify({ access_token, expires });
expires > 0

View File

@ -73,22 +73,22 @@ class PureHttp {
return config;
}
/** 请求白名单放置一些不需要token的接口通过设置请求白名单防止token过期后再请求造成的死循环问题 */
const whiteList = ["/refreshToken", "/login"];
const whiteList = ["/admin/v1/login", "/admin/v1/login/refresh"];
return whiteList.some(v => config.url.indexOf(v) > -1)
? config
: new Promise(resolve => {
const data = getToken();
if (data) {
const now = new Date().getTime();
const expired = parseInt(data.expires) - now <= 0;
const expired = new Date(data.expires).getTime() - now <= 0;
if (expired) {
if (!PureHttp.isRefreshing) {
PureHttp.isRefreshing = true;
// token过期刷新
useUserStoreHook()
.handRefreshToken(data.refreshToken)
.handRefreshToken(data.refresh_token)
.then(res => {
const token = res.data.accessToken;
const token = res.access_token;
config.headers["Authorization"] = formatToken(token);
PureHttp.requests.forEach(cb => cb(token));
PureHttp.requests = [];
@ -100,7 +100,7 @@ class PureHttp {
resolve(PureHttp.retryOriginalRequest(config));
} else {
config.headers["Authorization"] = formatToken(
data.accessToken
data.access_token
);
resolve(config);
}

View File

@ -1,5 +1,6 @@
import { removeToken, setToken, type DataInfo } from "./auth";
import { removeToken, setToken } from "./auth";
import { subBefore, getQueryMap } from "@pureadmin/utils";
import { LoginResult } from "@/api/login";
/**
*
@ -12,7 +13,7 @@ import { subBefore, getQueryMap } from "@pureadmin/utils";
*/
(function () {
// 获取 url 中的参数
const params = getQueryMap(location.href) as DataInfo<Date>;
const params = getQueryMap(location.href) as LoginResult;
const must = ["username", "roles", "accessToken"];
const mustLength = must.length;
if (Object.keys(params).length !== mustLength) return;

View File

@ -57,7 +57,6 @@ const onLogin = async (formEl: FormInstance | undefined) => {
useUserStoreHook()
.loginByUsername(ruleForm)
.then(res => {
console.log("登录返回", res);
//
initRouter().then(() => {
router.push(getTopMenu(true).path);

View File

@ -1,12 +1,12 @@
<script setup lang="ts">
defineOptions({
name: "AdminRole"
name: "SystemMenu"
});
</script>
<template>
<div class="flex justify-center items-center h-[640px]">
角色管理
菜单管理
</div>
</template>

View File

@ -1,12 +1,12 @@
<script setup lang="ts">
defineOptions({
name: "AdminUser"
name: "AdminRole"
});
</script>
<template>
<div class="flex justify-center items-center h-[640px]">
用户管理
角色管理
</div>
</template>

View File

@ -5,5 +5,5 @@ defineOptions({
</script>
<template>
<h1>Pure-Admin-Thin非国际化版本</h1>
<h1>我是首页</h1>
</template>