import * as _ from "lodash"; import paramCache from "../common/param-cache" import RequestPromise from "./axios-service/RequestPromise"; export const fetchSystemParams = () => { return RequestPromise({ url: '/api/system-parameters' }) } const parameters = paramCache.getItem("system-params") export const getParam = async (types: string[]) => { if (parameters) { return mergeSpecailParams(parameters, types) } else { return fetchSystemParams().then(({ data }) => { paramCache.setItem("system-params", data) return mergeSpecailParams(data, types) }) } } export const getParamKeys = async (type: string) => { if (parameters) { return setParamKeys(parameters) } else { return fetchSystemParams().then(({ data }) => { paramCache.setItem("system-params", data) return setParamKeys(data[type]) }) } } const setParamKeys = (params: any) => { const paramKeys: string[] = []; for (const key in params) { if (params.hasOwnProperty(key)) { paramKeys.push(key) } } return paramKeys } export const getParamOptions = async (types: string[]) => { if (parameters) { return setParamOptions(mergeSpecailParams(parameters, types)) } else { return fetchSystemParams().then(({ data }) => { paramCache.setItem("system-params", data) return setParamOptions(mergeSpecailParams(data, types)) }) } } const setParamOptions = (params: any) => { const paramOptions: any[] = []; _.forIn(params, (value, key) => { paramOptions.push({value: key, text: value}) }) return paramOptions; } const mergeSpecailParams = (parameters: any, types: string[]) => { let targetParam = {}; _.forIn(parameters, (value, key) => { if (types.includes(key)) { targetParam = { ...targetParam, ...value } } }) return targetParam }