add:增加两个用于获取枚举的公共方法
This commit is contained in:
@@ -38,3 +38,46 @@ export const dict = async ({
|
||||
}
|
||||
return Promise.resolve({ data: { data: get(dictMap, key) } });
|
||||
};
|
||||
|
||||
/**
|
||||
* 获取所有枚举(无需传参)
|
||||
*/
|
||||
export const getAllEnum = async ({
|
||||
api = `${BASE_URL}/operation/enum/getAllEnum`,
|
||||
params = {},
|
||||
keyField = 'dicKey',
|
||||
transform = (res: any) => res,
|
||||
}: dictHttpConfig) => {
|
||||
const dictMap = JSON.parse(sessionStorage.getItem('dictMap') || '{}') as Object;
|
||||
const key = get(params, keyField) as keyof typeof dictMap;
|
||||
|
||||
if (!dictMap.hasOwnProperty(key)) {
|
||||
const res = await http.post(api, params);
|
||||
const options = get(transform(res), `data.${key}`);
|
||||
dictMap[key] = options;
|
||||
sessionStorage.setItem('dictMap', JSON.stringify(dictMap));
|
||||
}
|
||||
return Promise.resolve({ data: { data: get(dictMap, key) } });
|
||||
};
|
||||
|
||||
|
||||
/**
|
||||
* 获取单个枚举(需传参,参数 enumType)
|
||||
*/
|
||||
export const getEnum = async ({
|
||||
api = `${BASE_URL}/operation/enum/getEnum`,
|
||||
params = {},
|
||||
keyField = 'dicKey',
|
||||
transform = (res: any) => res,
|
||||
}: dictHttpConfig) => {
|
||||
const dictMap = JSON.parse(sessionStorage.getItem('dictMap') || '{}') as Object;
|
||||
const key = get(params, keyField) as keyof typeof dictMap;
|
||||
|
||||
if (!dictMap.hasOwnProperty(key)) {
|
||||
const res = await http.post(api, params);
|
||||
const options = get(transform(res), `data.${key}`);
|
||||
dictMap[key] = options;
|
||||
sessionStorage.setItem('dictMap', JSON.stringify(dictMap));
|
||||
}
|
||||
return Promise.resolve({ data: { data: get(dictMap, key) } });
|
||||
};
|
Reference in New Issue
Block a user