style:菜单接口调用次数优化

master
黄伟杰 3 weeks ago
parent df24ee20a2
commit 085fa5b297

@ -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
}
// 获取用户详细信息

@ -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<null>((resolve, reject) => {
logout()
.then(() => {
clearPermissionInfoCache();
this.token = "";
this.roles = [];
this.permissions = [];

Loading…
Cancel
Save