/**
 * 表码、枚举、url、数据请求接口
 * @Author: huangjq
 * @Date:   2019-02-26
 * @Project jz
 */
import request from "@/utils/request";
import Qs from "qs";
const { getSetting } = window.systemParamsUtils;
const baseApi = {
    fetchCode: function(codeName, module) {
        return request({
            url: "/duceap/v2/resource/codelist/code/" + codeName,
            method: "get",
            baseURL: this._getBaseURL(module)
        });
    },

    fetchEnum: function(enumName, module) {
        return request({
            url: "/duceap/v2/resource/codelist/enum/" + enumName,
            method: "get",
            baseURL: this._getBaseURL(module)
        });
    },

    // fetchTree: function(sqlKey, module) {
    //     return request({
    //         url: "/api/codelist/sql/" + sqlKey,
    //         method: "get",
    //         baseURL: this._getBaseURL(module)
    //     });
    // },

    fetchUrl: function(url, module) {
        return request({
            url: url,
            method: "get",
            baseURL: this._getBaseURL(module)
        });
    },

    reqTableData(url, data, module) {
        return request({
            url: url,
            method: "post",
            data: Qs.stringify(data),
            baseURL: this._getBaseURL(module)
        });
    },

    getData(url, params, module) {
        return request({
            url: url,
            method: "GET",
            params: params,
            loading: true,
            baseURL: this._getBaseURL(module)
        });
    },

    postData(url, data, module = "BASE_API") {
        return request({
            url: url,
            method: "POST",
            data: data,
            loading: true,
            baseURL: this._getBaseURL(module)
        });
    },

    putData(url, data, module = "BASE_API") {
        return request({
            url: url,
            method: "PUT",
            data: data,
            loading: true,
            baseURL: this._getBaseURL(module)
        });
    },

    deleteData(url, params, module = "BASE_API") {
        return request({
            url: url,
            method: "DELETE",
            params: params,
            loading: true,
            baseURL: this._getBaseURL(module)
        });
    },

    /**
     * 根据module获取上下文
     * @param module
     * @private
     */
    _getBaseURL(module) {
        if (module === "" || !module) {
            module = "BASE_API";
        }
        return getSetting(module);
    }
};
export default baseApi;
