• react: typescript system params method optimize


    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
    }
  • 相关阅读:
    机器学习&数据挖掘笔记_16(常见面试之机器学习算法思想简单梳理)
    linux_shell_根据网站来源分桶
    mac_Alfred_快捷设置
    linux_无密登录
    crawler_Docker_解决用 JavaScript 框架开发的 Web 站点抓取
    linux下查看最消耗CPU、内存的进程
    绕过登陆常用万能密码
    ctf比赛linux文件监控和恢复shell
    Python爬虫之Selenium的常用方法
    CTF比赛时准备的一些shell命令
  • 原文地址:https://www.cnblogs.com/Nyan-Workflow-FC/p/11274511.html
Copyright © 2020-2023  润新知