添加公共缓存逻辑

This commit is contained in:
ywyonui 2025-09-29 22:47:33 +08:00
parent d26a8fb38f
commit 8099732e28
2 changed files with 113 additions and 3 deletions

View File

@ -68,7 +68,9 @@
import { ref, reactive, computed, onMounted, nextTick } from "vue";
import { dqpkApi, drpkkbApi } from "@/api/base/server";
import { useUserStore } from "@/store/modules/user";
import { useCommonStore } from "@/store/modules/common";
const { getCurXs } = useUserStore();
const { getCacheList }= useCommonStore();
import dayjs from "dayjs";
import "dayjs/locale/zh-cn";
import weekOfYear from "dayjs/plugin/weekOfYear";
@ -198,7 +200,8 @@ const getCourseColorClass = (subject: string | undefined): string => {
};
onMounted(async () => {
const res = await dqpkApi();
//
const res = await getCacheList(dqpkApi, "dqPkSz");
const result = res.result;
dqZc = res.result.zc;
xqId = res.result.xq.id;

View File

@ -2,6 +2,44 @@ import { defineStore } from "pinia";
import { jsFindByIdApi } from "@/api/base/jsApi";
import { xkkclxFindAllApi, getXkkcDetailByIdApi } from "@/api/base/xkApi";
/**
*
* @param params
* @returns
*/
const getIdList = (params: any) => {
if (!params || !params.length) {
return [];
}
// 判断传入参数params是逗号分隔的字符串还是数组如果是字符串拆分成数组
if (typeof params == "string") {
return params.split(",");
} else if (Array.isArray(params)) {
return [...params];
}
}
/**
* ID列表
* @param idList ID列表
* @param data
* @returns
*/
const getCacheAndNewId = (idList: any[], data: any) => {
let cacheList: any = [];
let newIdList: any = [];
// 接下来判断bjPkkb里面有没有对应的排课科目数据如果有则记录如果没有则记录班级id
for (let i = 0; i < idList.length; i++) {
const bjId = idList[i];
if (data[bjId]) {
cacheList.push(...data[bjId]);
} else {
newIdList.push(bjId);
}
}
return { cacheList, newIdList };
}
interface CommonState {
data: any;
}
@ -18,8 +56,77 @@ export const useCommonStore = defineStore({
}
},
actions: {
setData(data: any) {
this.data = data;
setData(key: string, data: any) {
this.data[key] = data;
},
/**
*
* @param api api接口
* @param key
*/
async getCacheList (api: any, key: string) : Promise<any> {
if (!this.data[key]) {
this.data[key] = await api();
}
return Promise.resolve(this.data[key]);
},
/**
*
* @param api api接口
* @param params
* @param key
* @param subKey
*/
async getCacheSubList (api: any, params: any, key: string, subKey: string) : Promise<any> {
// 如果没有参数之间返回
if (!params || !params[subKey]) {
return Promise.resolve();
}
if (!this.data[key] || !this.data[key][subKey]) {
this.data[key] = this.data[key] || {};
this.data[key][subKey] = await api(params);
}
return Promise.resolve(this.data[key][subKey]);
},
/**
* ID列表
* @param api api接口
* @param params
* @param srcIdList ID列表
* @param key
* @param subKey
* @param paramKey idList对应的参数键
*/
async getCacheSubListByIdList (api: any, params: any, srcIdList: any, key: string, subKey: string, paramKey: string) : Promise<any> {
// 如果没有参数之间返回
let idList: any = getIdList(srcIdList);
if (!idList.length) {
return Promise.resolve([]);
}
let dataKey: any = this.data[key] || {};
const { cacheList, newIdList } = getCacheAndNewId(idList, dataKey);
let retList = [...cacheList];
// 判断如果有新的班级id则请求接口获取排课科目数据
if (newIdList.length) {
// 把newIdList转换成逗号分隔的字符串
const newIdStr = newIdList.join(',');
params[paramKey] = newIdStr;
const res = await api(params);
let newList:any = [];
if (res.resultCode == 1) {
newList = res.result || [];
} else if (Array.isArray(res)) {
newList = res || [];
}
// 缓存数据
newList.map((item:any) => {
dataKey[item[subKey]] = dataKey[item[subKey]] || [];
dataKey[item[subKey]].push(item);
retList.push(item);
});
this.data[key] = dataKey;
}
return Promise.resolve(retList);
},
// 根据教师ID获取教师信息带缓存机制
async getJsById(params: any): Promise<any> {