160 lines
4.8 KiB
TypeScript
Raw Normal View History

2025-08-27 21:02:29 +08:00
import { defineStore } from "pinia";
import { jsFindByIdApi } from "@/api/base/jsApi";
2025-09-02 23:10:03 +08:00
import { xkkclxFindAllApi, getXkkcDetailByIdApi } from "@/api/base/xkApi";
2025-08-27 21:02:29 +08:00
2025-09-29 22:47:33 +08:00
/**
*
* @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 };
}
2025-08-27 21:02:29 +08:00
interface CommonState {
data: any;
}
export const useCommonStore = defineStore({
id: "app-common",
state: (): CommonState => ({
// 字典数据
data: {}
}),
getters: {
getData(): any {
return this.data;
}
},
actions: {
2025-09-29 22:47:33 +08:00
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);
2025-08-27 21:02:29 +08:00
},
// 根据教师ID获取教师信息带缓存机制
async getJsById(params: any): Promise<any> {
if (!this.data.js || !this.data.js[params.id]) {
this.data.js = this.data.js || {};
this.data.js[params.id] = await jsFindByIdApi(params);
}
return Promise.resolve(this.data.js[params.id]);
},
async getAllXkkcLx() : Promise<any> {
if (!this.data.xkkcLx) {
this.data.xkkcLx = await xkkclxFindAllApi();
}
return Promise.resolve(this.data.xkkcLx);
},
2025-09-02 23:10:03 +08:00
async getXkkcDetailByIdApi(id: string) : Promise<any> {
if (!this.data.xkkc || !this.data.xkkc[id]) {
this.data.xkkc = this.data.xkkc || {};
const res = await getXkkcDetailByIdApi(id);
console.log(res);
}
return Promise.resolve(this.data.xkkc[id]);
},
2025-08-27 21:02:29 +08:00
},
persist: {
enabled: true,
detached: true,
H5Storage: localStorage
},
});