UNPKG

create-chuntianxiaozhu

Version:

春天小猪模板工具

72 lines (67 loc) 1.85 kB
import { proxyApi } from "@/apis"; import { utils } from "@chuntianxiaozhu/adminreactcomps"; const { cache } = utils; /** * 缓存函数结构体 */ const dictCacheMap = {}; /** * 字典数据缓存,不随着路由的变化而重新加载 * 借助dataCache对各个字典数据进行非持久化缓存 * @param dictType * @param otherParams * @returns */ export const loadDictCache = (dictType, otherParams?: any): Promise<any> => { let mergeParams = { ...(otherParams || {}), }; if (typeof dictType == "string" || typeof dictType == "number") { mergeParams.dictType = dictType; } else { mergeParams = { ...(otherParams || {}), ...(dictType || {}), }; } const uniqueKey = JSON.stringify(mergeParams); const { dictType: type, ...reset } = mergeParams; // 存在函数定义则直接进行数据请求 if (dictCacheMap[uniqueKey]) { return dictCacheMap[uniqueKey](type, reset); } // 不存在则先定义缓存起来再进行数据请求 dictCacheMap[uniqueKey] = cache.dataCache((params) => { const dictType = params[0]; const otherParams = params[1] || {}; return proxyApi.common.queryDictValue({ dictType, isChild: true, ...otherParams, }); }, false); return dictCacheMap[uniqueKey](type, reset); }; /** * 省市区map解析 */ export const loadProvinceCity = (() => { const areaMap = {}; return async () => { if (Object.keys(areaMap).length > 0) { return areaMap; } return loadDictCache(1).then((res: JSONObject[]) => { function loopChild(data) { data.forEach((item) => { areaMap[item.dictKey] = item.dictValue; if (item.subDict && item.subDict.length) { loopChild(item.subDict); } }); } loopChild(res); return areaMap; }); }; })();