From 91f388437368c1e34218d7d56421153a6d63019e Mon Sep 17 00:00:00 2001 From: houzhongjian <houzhongyi@126.com> Date: 星期四, 05 十二月 2024 16:57:16 +0800 Subject: [PATCH] 优化与平台的菜单对接 --- src/store/modules/permission.ts | 9 ++-- types/env.d.ts | 4 - src/hooks/web/useCache.ts | 16 ++++++++ src/config/axios/config.ts | 2 .env | 10 +---- src/store/modules/user.ts | 29 +++++++++++--- src/api/login/index.ts | 5 ++ 7 files changed, 51 insertions(+), 24 deletions(-) diff --git a/.env b/.env index 4e8c33e..7b8c1e1 100644 --- a/.env +++ b/.env @@ -16,22 +16,16 @@ # 接口地址 VITE_API_URL=/admin-api -# 平台接口地址 -VITE_PLAT_API_URL=/admin-api - # 租户开关 VITE_APP_TENANT_ENABLE=true # 文档地址的开关 VITE_APP_DOCALERT_ENABLE=true -# 默认账户密码 -VITE_APP_DEFAULT_LOGIN_TENANT = iailab -VITE_APP_DEFAULT_LOGIN_USERNAME = admin -VITE_APP_DEFAULT_LOGIN_PASSWORD = admin123 - VITE_TENANT_ID=174 +VITE_APP_CODE=xmcsms + ## 平台授权URL VITE_AUTH2_LOGIN_URL='http://172.16.8.100:48080/admin-api/system/oauth2/token' diff --git a/src/api/login/index.ts b/src/api/login/index.ts index bc3b924..c493951 100644 --- a/src/api/login/index.ts +++ b/src/api/login/index.ts @@ -41,6 +41,11 @@ return request.get({ url: plat_url + '/system/auth/get-permission-info' }) } +// 获取应用菜单信息 +export const getAppInfo = () => { + return request.get({ url: plat_url + '/system/auth/get-app-permission-info' }) +} + // 获取验证图片以及 token export const getCode = (data) => { return request.postOriginal({ url: plat_url + '/system/captcha/get', data }) diff --git a/src/config/axios/config.ts b/src/config/axios/config.ts index 45a8908..4e99330 100644 --- a/src/config/axios/config.ts +++ b/src/config/axios/config.ts @@ -12,7 +12,7 @@ /** * 平台请求基础路径 */ - plat_url: import.meta.env.VITE_PLAT_URL + import.meta.env.VITE_PLAT_API_URL, + plat_url: import.meta.env.VITE_PLAT_URL + import.meta.env.VITE_API_URL, /** * 接口成功返回状态码 */ diff --git a/src/hooks/web/useCache.ts b/src/hooks/web/useCache.ts index 4f39f30..f6b2bd1 100644 --- a/src/hooks/web/useCache.ts +++ b/src/hooks/web/useCache.ts @@ -37,3 +37,19 @@ wsCache.delete(CACHE_KEY.ROLE_ROUTERS) // 注意,不要清理 LoginForm 登录表单 } + +export const useSessionCache = (type: CacheType = 'sessionStorage') => { + const wsSessionCache: WebStorageCache = new WebStorageCache({ + storage: type + }) + + return { + wsSessionCache + } +} + +export const deleteUserSessionCache = () => { + const { wsSessionCache } = useSessionCache() + wsSessionCache.delete(CACHE_KEY.ROLE_ROUTERS) + // 注意,不要清理 用户和 LoginForm 登录表单 +} diff --git a/src/store/modules/permission.ts b/src/store/modules/permission.ts index 5a42f14..e0eea07 100644 --- a/src/store/modules/permission.ts +++ b/src/store/modules/permission.ts @@ -3,10 +3,10 @@ import { cloneDeep } from 'lodash-es' import remainingRouter from '@/router/modules/remaining' import { flatMultiLevelRoutes, generateRoute } from '@/utils/routerHelper' -import { CACHE_KEY, useCache } from '@/hooks/web/useCache' +import {CACHE_KEY, useSessionCache} from '@/hooks/web/useCache' import Layout from "@/layout/Layout.vue"; -const { wsCache } = useCache() +const { wsSessionCache } = useSessionCache() export interface PermissionState { routers: AppRouteRecordRaw[] @@ -36,8 +36,8 @@ return new Promise<void>(async (resolve) => { // 获得菜单列表,它在登录的时候,setUserInfoAction 方法中已经进行获取 let res: AppCustomRouteRecordRaw[] = [] - if (wsCache.get(CACHE_KEY.ROLE_ROUTERS)) { - res = wsCache.get(CACHE_KEY.ROLE_ROUTERS) as AppCustomRouteRecordRaw[] + if (wsSessionCache.get(CACHE_KEY.ROLE_ROUTERS)) { + res = wsSessionCache.get(CACHE_KEY.ROLE_ROUTERS) as AppCustomRouteRecordRaw[] } const routerMap: AppRouteRecordRaw[] = generateRoute(res) // 动态路由,404一定要放到最后面 @@ -53,7 +53,6 @@ } } ]) - console.log(this.addRouters) // 渲染菜单的所有路由 this.routers = cloneDeep(remainingRouter).concat(routerMap) resolve() diff --git a/src/store/modules/user.ts b/src/store/modules/user.ts index b386180..176152e 100644 --- a/src/store/modules/user.ts +++ b/src/store/modules/user.ts @@ -1,10 +1,18 @@ import { store } from '@/store' import { defineStore } from 'pinia' import { getAccessToken, removeToken } from '@/utils/auth' -import { CACHE_KEY, useCache, deleteUserCache } from '@/hooks/web/useCache' -import { getInfo, loginOut } from '@/api/login' +import { + CACHE_KEY, + useCache, + deleteUserCache, + useSessionCache, + deleteUserSessionCache +} from '@/hooks/web/useCache' +import {getAppInfo, getInfo, loginOut} from '@/api/login' +import * as AppApi from '@/api/system/app' const { wsCache } = useCache() +const { wsSessionCache } = useSessionCache() interface UserVO { id: number @@ -53,16 +61,21 @@ this.resetState() return null } - let userInfo = wsCache.get(CACHE_KEY.USER) - if (!userInfo) { - userInfo = await getInfo() - } + const userInfo = await getInfo() + const appInfo = await getAppInfo() this.permissions = userInfo.permissions this.roles = userInfo.roles this.user = userInfo.user this.isSetUser = true wsCache.set(CACHE_KEY.USER, userInfo) - wsCache.set(CACHE_KEY.ROLE_ROUTERS, userInfo.menus) + //如果localStorage中有应用code,说明是从平台点击应用跳转过来 + const appId = localStorage.getItem(import.meta.env.VITE_APP_CODE) + if(appId) { + const data = await AppApi.getAppMenuList(appId) + wsSessionCache.set(CACHE_KEY.ROLE_ROUTERS, data) + } else { + wsSessionCache.set(CACHE_KEY.ROLE_ROUTERS, appInfo.menus) + } }, async setUserAvatarAction(avatar: string) { const userInfo = wsCache.get(CACHE_KEY.USER) @@ -82,6 +95,8 @@ await loginOut() removeToken() deleteUserCache() // 删除用户缓存 + deleteUserSessionCache() + localStorage.clear() this.resetState() }, resetState() { diff --git a/types/env.d.ts b/types/env.d.ts index ce972f0..a49968f 100644 --- a/types/env.d.ts +++ b/types/env.d.ts @@ -14,9 +14,6 @@ readonly VITE_DEV: string readonly VITE_APP_CAPTCHA_ENABLE: string readonly VITE_APP_TENANT_ENABLE: string - readonly VITE_APP_DEFAULT_LOGIN_TENANT: string - readonly VITE_APP_DEFAULT_LOGIN_USERNAME: string - readonly VITE_APP_DEFAULT_LOGIN_PASSWORD: string readonly VITE_APP_DOCALERT_ENABLE: string readonly VITE_BASE_URL: string readonly VITE_PLAT_URL: string @@ -32,6 +29,7 @@ readonly VITE_SOURCEMAP: string readonly VITE_OUT_DIR: string readonly VITE_TENANT_ID: string + readonly VITE_APP_CODE: string } declare global { -- Gitblit v1.9.3