diff --git a/src/api/login.js b/src/api/login.js index e519dba..8302fcd 100644 --- a/src/api/login.js +++ b/src/api/login.js @@ -1,5 +1,32 @@ import request from '@/utils/request' +const permissionInfoCache = new Map() +const permissionInfoPending = new Map() + +function normalizePermissionParams(params = {}) { + return { + clientType: 2, + ...params + } +} + +function createPermissionCacheKey(params = {}) { + const normalizedParams = normalizePermissionParams(params) + return JSON.stringify( + Object.keys(normalizedParams) + .sort() + .reduce((result, key) => { + result[key] = normalizedParams[key] + return result + }, {}) + ) +} + +export function clearPermissionInfoCache() { + permissionInfoCache.clear() + permissionInfoPending.clear() +} + // 登录方法 export function login(username, password, code, uuid, tenantName, captchaVerification,rememberMe) { const data = { @@ -34,14 +61,34 @@ export function register(data) { } function getPermissionInfo(params = {}) { - return request({ + const normalizedParams = normalizePermissionParams(params) + const cacheKey = createPermissionCacheKey(normalizedParams) + + if (permissionInfoCache.has(cacheKey)) { + return Promise.resolve(permissionInfoCache.get(cacheKey)) + } + + if (permissionInfoPending.has(cacheKey)) { + return permissionInfoPending.get(cacheKey) + } + + const requestPromise = request({ url: '/admin-api/system/auth/get-permission-info', method: 'get', - params: { - clientType: 2, - ...params - } + params: normalizedParams }) + + const wrappedPromise = requestPromise + .then((res) => { + permissionInfoCache.set(cacheKey, res) + return res + }) + .finally(() => { + permissionInfoPending.delete(cacheKey) + }) + + permissionInfoPending.set(cacheKey, wrappedPromise) + return wrappedPromise } // 获取用户详细信息 diff --git a/src/store/modules/user.ts b/src/store/modules/user.ts index 816e241..1e2430b 100644 --- a/src/store/modules/user.ts +++ b/src/store/modules/user.ts @@ -1,4 +1,4 @@ -import { login, logout, getInfo } from "@/api/login"; +import { login, logout, getInfo, clearPermissionInfoCache } from "@/api/login"; import { getToken, setToken, removeToken } from "@/utils/auth"; import defAva from "@/static/images/profile.jpg"; import { defineStore } from "pinia"; @@ -40,6 +40,7 @@ const useUserStore = defineStore("user", { .then((res: any) => { setToken(res.data.accessToken); this.token = res.data.accessToken; + clearPermissionInfoCache(); resolve(null); }) .catch((error) => { @@ -80,6 +81,7 @@ const useUserStore = defineStore("user", { return new Promise((resolve, reject) => { logout() .then(() => { + clearPermissionInfoCache(); this.token = ""; this.roles = []; this.permissions = [];