From 262d97554a8f04a41014a7edfbc63decaaa0edfa Mon Sep 17 00:00:00 2001
From: houzhongjian <houzhongyi@126.com>
Date: 星期四, 05 十二月 2024 16:58:09 +0800
Subject: [PATCH] 优化与平台的菜单对接,需要更新平台最新代码

---
 .env.dev                        |    7 ++-
 src/store/modules/permission.ts |    9 ++--
 types/env.d.ts                  |    5 --
 src/hooks/web/useCache.ts       |   16 ++++++++
 src/config/axios/config.ts      |    2 
 .env                            |   14 ++-----
 src/store/modules/user.ts       |   29 +++++++++++---
 .env.test                       |    6 +-
 src/api/login/index.ts          |    5 ++
 9 files changed, 61 insertions(+), 32 deletions(-)

diff --git a/.env b/.env
index 4e7a6e8..f5710d5 100644
--- a/.env
+++ b/.env
@@ -11,13 +11,10 @@
 VITE_BASE_URL='http://localhost:8089'
 
 # 平台路径
-VITE_PLAT_URL='http://172.16.8.100:48080'
+VITE_PLAT_URL='http://localhost:48080'
 
 # 接口地址
 VITE_API_URL=/admin-api
-
-# 平台接口地址
-VITE_PLAT_API_URL=/admin-api
 
 # 租户开关
 VITE_APP_TENANT_ENABLE=true
@@ -25,15 +22,12 @@
 # 文档地址的开关
 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=173
 
+VITE_APP_CODE=xmcpms
+
 ## 平台授权URL
-VITE_AUTH2_LOGIN_URL='http://172.16.8.100:48080/admin-api/system/oauth2/token'
+VITE_AUTH2_LOGIN_URL='http://localhost:48080/admin-api/system/oauth2/token'
 
 # 平台授权AUTH2 clientId
 VITE_AUTH2_CLIENT_ID='xmcpms'
diff --git a/.env.dev b/.env.dev
index 1cb7301..389bc98 100644
--- a/.env.dev
+++ b/.env.dev
@@ -4,7 +4,10 @@
 VITE_DEV=true
 
 # 请求路径
-VITE_BASE_URL='http://localhost:8089'
+VITE_BASE_URL='http://localhost:48080'
+
+# 平台路径
+VITE_PLAT_URL='http://localhost:48080'
 
 # 接口地址
 VITE_API_URL=/admin-api
@@ -19,7 +22,7 @@
 VITE_SOURCEMAP=true
 
 # 打包路径
-VITE_BASE_PATH=/xmcpms
+VITE_BASE_PATH=/pms
 
 # 输出路径
 VITE_OUT_DIR=dist
diff --git a/.env.test b/.env.test
index 76ab74c..8c2e725 100644
--- a/.env.test
+++ b/.env.test
@@ -4,10 +4,10 @@
 VITE_DEV=true
 
 # 请求路径
-VITE_BASE_URL='http://172.16.8.100:8089'
+VITE_BASE_URL='http://172.16.8.100'
 
 # 平台路径
-VITE_PLAT_URL='http://172.16.8.100:48080'
+VITE_PLAT_URL='http://172.16.8.100'
 
 # 接口地址
 VITE_API_URL=/admin-api
@@ -22,7 +22,7 @@
 VITE_SOURCEMAP=true
 
 # 打包路径
-VITE_BASE_PATH=/xmcpms
+VITE_BASE_PATH=/pms
 
 # 输出路径
 VITE_OUT_DIR=dist
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..cf6da8e 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
@@ -25,13 +22,13 @@
   readonly VITE_AUTH2_CLIENT_SECRET: string
   readonly VITE_UPLOAD_URL: string
   readonly VITE_API_URL: string
-  readonly VITE_PLAT_API_URL: string
   readonly VITE_BASE_PATH: string
   readonly VITE_DROP_DEBUGGER: string
   readonly VITE_DROP_CONSOLE: string
   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