添加公共缓存逻辑
This commit is contained in:
parent
d26a8fb38f
commit
8099732e28
@ -68,7 +68,9 @@
|
|||||||
import { ref, reactive, computed, onMounted, nextTick } from "vue";
|
import { ref, reactive, computed, onMounted, nextTick } from "vue";
|
||||||
import { dqpkApi, drpkkbApi } from "@/api/base/server";
|
import { dqpkApi, drpkkbApi } from "@/api/base/server";
|
||||||
import { useUserStore } from "@/store/modules/user";
|
import { useUserStore } from "@/store/modules/user";
|
||||||
|
import { useCommonStore } from "@/store/modules/common";
|
||||||
const { getCurXs } = useUserStore();
|
const { getCurXs } = useUserStore();
|
||||||
|
const { getCacheList }= useCommonStore();
|
||||||
import dayjs from "dayjs";
|
import dayjs from "dayjs";
|
||||||
import "dayjs/locale/zh-cn";
|
import "dayjs/locale/zh-cn";
|
||||||
import weekOfYear from "dayjs/plugin/weekOfYear";
|
import weekOfYear from "dayjs/plugin/weekOfYear";
|
||||||
@ -198,7 +200,8 @@ const getCourseColorClass = (subject: string | undefined): string => {
|
|||||||
};
|
};
|
||||||
|
|
||||||
onMounted(async () => {
|
onMounted(async () => {
|
||||||
const res = await dqpkApi();
|
// 获取当前排课设置
|
||||||
|
const res = await getCacheList(dqpkApi, "dqPkSz");
|
||||||
const result = res.result;
|
const result = res.result;
|
||||||
dqZc = res.result.zc;
|
dqZc = res.result.zc;
|
||||||
xqId = res.result.xq.id;
|
xqId = res.result.xq.id;
|
||||||
|
|||||||
@ -2,6 +2,44 @@ import { defineStore } from "pinia";
|
|||||||
import { jsFindByIdApi } from "@/api/base/jsApi";
|
import { jsFindByIdApi } from "@/api/base/jsApi";
|
||||||
import { xkkclxFindAllApi, getXkkcDetailByIdApi } from "@/api/base/xkApi";
|
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 {
|
interface CommonState {
|
||||||
data: any;
|
data: any;
|
||||||
}
|
}
|
||||||
@ -18,8 +56,77 @@ export const useCommonStore = defineStore({
|
|||||||
}
|
}
|
||||||
},
|
},
|
||||||
actions: {
|
actions: {
|
||||||
setData(data: any) {
|
setData(key: string, data: any) {
|
||||||
this.data = data;
|
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获取教师信息(带缓存机制)
|
// 根据教师ID获取教师信息(带缓存机制)
|
||||||
async getJsById(params: any): Promise<any> {
|
async getJsById(params: any): Promise<any> {
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user