From 43517f9c3f417bbaac86beef5ffbb3c2b89f115d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9D=8E=E5=AF=BB=E6=AC=A2?= Date: Thu, 1 Feb 2024 18:10:55 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E9=80=BB=E8=BE=91=E5=AE=8C=E5=96=84?= =?UTF-8?q?=EF=BC=8C=E5=AF=B9=E6=8E=A5=E5=90=8E=E7=AB=AF=E8=8F=9C=E5=8D=95?= =?UTF-8?q?=E6=8E=A5=E5=8F=A3=E5=AE=8C=E6=88=90?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/api/routes.ts | 8 ++------ src/components/ReIcon/src/offlineIcon.ts | 8 ++++++++ src/router/index.ts | 5 +++-- src/router/utils.ts | 7 +++---- src/utils/auth.ts | 2 +- src/utils/http/index.ts | 10 +++++----- src/utils/sso.ts | 5 +++-- src/views/login/index.vue | 1 - src/views/system/menu/index.vue | 4 ++-- src/views/system/role/index.vue | 4 ++-- src/views/welcome/index.vue | 2 +- 11 files changed, 30 insertions(+), 26 deletions(-) diff --git a/src/api/routes.ts b/src/api/routes.ts index 277c5f3..3b396ef 100644 --- a/src/api/routes.ts +++ b/src/api/routes.ts @@ -1,10 +1,6 @@ import { http } from "@/utils/http"; - -type Result = { - success: boolean; - data: Array; -}; +import { BaseResponse } from "@/api/base"; export const getAsyncRoutes = () => { - return http.request("get", "/getAsyncRoutes"); + return http.request>>("get", "/admin/v1/menu/self"); }; diff --git a/src/components/ReIcon/src/offlineIcon.ts b/src/components/ReIcon/src/offlineIcon.ts index bb0e020..d03fbcd 100644 --- a/src/components/ReIcon/src/offlineIcon.ts +++ b/src/components/ReIcon/src/offlineIcon.ts @@ -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); diff --git a/src/router/index.ts b/src/router/index.ts index ef66c29..8633ff7 100644 --- a/src/router/index.ts +++ b/src/router/index.ts @@ -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>(sessionKey); + const userInfo = storageSession().getItem(sessionKey); NProgress.start(); const externalLink = isUrl(to?.name as string); if (!externalLink) { diff --git a/src/router/utils.ts b/src/router/utils.ts index 2436e2b..3f2f6d6 100644 --- a/src/router/utils.ts +++ b/src/router/utils.ts @@ -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, b: Array) { 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>(sessionKey)?.roles ?? []; + storageSession().getItem(sessionKey)?.roles ?? []; const newTree = cloneDeep(data).filter((v: any) => isOneOfArray(v.meta?.roles, currentRoles) ); diff --git a/src/utils/auth.ts b/src/utils/auth.ts index 4e9b4ab..ba0c034 100644 --- a/src/utils/auth.ts +++ b/src/utils/auth.ts @@ -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改成DataInfo即可 + expires = new Date(data.expires).getTime(); const cookieString = JSON.stringify({ access_token, expires }); expires > 0 diff --git a/src/utils/http/index.ts b/src/utils/http/index.ts index dd8de95..1e284b9 100644 --- a/src/utils/http/index.ts +++ b/src/utils/http/index.ts @@ -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); } diff --git a/src/utils/sso.ts b/src/utils/sso.ts index 69b0509..b026d01 100644 --- a/src/utils/sso.ts +++ b/src/utils/sso.ts @@ -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; + const params = getQueryMap(location.href) as LoginResult; const must = ["username", "roles", "accessToken"]; const mustLength = must.length; if (Object.keys(params).length !== mustLength) return; diff --git a/src/views/login/index.vue b/src/views/login/index.vue index 23788f7..4210355 100644 --- a/src/views/login/index.vue +++ b/src/views/login/index.vue @@ -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); diff --git a/src/views/system/menu/index.vue b/src/views/system/menu/index.vue index 5ee101a..3d10611 100644 --- a/src/views/system/menu/index.vue +++ b/src/views/system/menu/index.vue @@ -1,12 +1,12 @@ diff --git a/src/views/system/role/index.vue b/src/views/system/role/index.vue index 34faac1..5ee101a 100644 --- a/src/views/system/role/index.vue +++ b/src/views/system/role/index.vue @@ -1,12 +1,12 @@ diff --git a/src/views/welcome/index.vue b/src/views/welcome/index.vue index 8db10d2..aeb14b4 100644 --- a/src/views/welcome/index.vue +++ b/src/views/welcome/index.vue @@ -5,5 +5,5 @@ defineOptions({