2025-09-29 22:47:33 +08:00

160 lines
4.8 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

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;
}
export const useCommonStore = defineStore({
id: "app-common",
state: (): CommonState => ({
// 字典数据
data: {}
}),
getters: {
getData(): any {
return this.data;
}
},
actions: {
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> {
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);
},
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]);
},
},
persist: {
enabled: true,
detached: true,
H5Storage: localStorage
},
});