import instance from "./fetch"

import { Message } from 'element-ui';
import router from '@/router/index.js'

/**
 * 
 * @param {项目的filters} obj
 * 
 * @return {处理后的字符串}  string
 */
function delFilters(obj) {
    let str = "",
        key
    for (key in obj) {
        if (!obj[key] && typeof obj[key] != "undefined") {
            str += key + " isNull;"
        } else if (typeof obj[key] == 'string') {
            str += key + "='" + obj[key] + "';"
        } else {
            str += key + "=" + obj[key] + ";"
        }
    }
    console.log(str, "str")
    return str.split(';').map(item => {
        return !!item ? item : undefined

    }).filter(d => d).join(';')
}

function successResponse(response, success, failed) {
    let res = response.data;
    let result = res.result || res.Result;
    if (result == "success" || result == "Success") {
        success(res)
    } else if (result == "failure" || result == "Failure") {
        let msg = res.ResultMsg || res.message || res.Message || res.resultMsg
        Message.error({ message: msg })
    } else {
        router.replace('/login')
    }
}

function errorResponse(response, err) {
    let json = JSON.stringify(err),
        lastData = JSON.parse(json)
    console.log(err)
    if (lastData.hasOwnProperty('config')) {
        Message.error({ message: '接口：' + lastData.config.url + '请求错误' })
    } else {
        Message.error({ message: '请求错误:' + err });
    }
}


/**
 * post 请求方法
 * @param url
 * @param data
 * @returns {Promise}
 */
export function post(url, data = {}, success) {
    if (data.hasOwnProperty('Filters')) {
        data.Filters = delFilters(data.Filters)
    }
    instance.post(url, data).then(response => {
        //对接口错误码做处理
        successResponse(response, success)
    }, err => {
        errorResponse({}, err)
    })
}


/**
 * get 请求方法
 * @param url
 * @param data
 * @returns {Promise}
 */
export function get(url, data = {}, success) {
    instance.get(url, {
            params: data
        })
        .then(response => {
            successResponse(response, success)
        })
        .catch(err => {
            errorResponse({}, err)
        })
}

/**
 * 封装所有请求
 * @param methed
 * @param url
 * @param data 
 * @param headers
 * @returns {Promise}
 */
export function request(methed, url, data = {}, headers, success) {
    instance({
            method: methed || 'post',
            url: url,
            data: methed === 'get' ? { params: data } : data,
            headers: headers || { 'Content-Type': 'application/json' },
        })
        .then(response => {
            //对接口错误码做处理
            success(response)
        })
        .catch(err => {
            errorResponse(response, err)
        })
}