You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
83 lines
2.3 KiB
83 lines
2.3 KiB
import { get } from 'lodash-es';
|
|
import { http } from '/nerv-lib/util/http';
|
|
|
|
/***
|
|
*配置接口 格式 module:Array<resource>
|
|
*/
|
|
export const apiModule = {
|
|
parking: ['User', 'CurrentUser', 'Organizational'],
|
|
};
|
|
|
|
export const BASE_URL = '/carbon-smart';
|
|
|
|
interface dictHttpConfig {
|
|
api?: string;
|
|
keyField?: string;
|
|
params: object;
|
|
transform?: Function;
|
|
}
|
|
|
|
/**
|
|
* 获取字典数据(首次获取,后续读缓存)
|
|
*/
|
|
|
|
export const dict = async ({
|
|
api = `${BASE_URL}/client/dict/listByKey`,
|
|
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) } });
|
|
};
|
|
|
|
/**
|
|
* 获取所有枚举(无需传参)
|
|
*/
|
|
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) } });
|
|
};
|