import axios from "axios";
import {timeout, baseUrl, baseUrl_Address,withCredentials} from "@/service/config";
import {UserInfo} from "@/units/UserInfo";
const requestInstance = axios.create({
    baseURL:baseUrl,
    timeout,
    method:"post",

    // withCredentials,
    // headers:{
    //
    //     "Content-Type":"application/x-www-form-urlencoded"
    // },
    // transformRequest:[(data) => {
    //
    //     if(data) {
    //         let params = new FormData()
    //         let keys = Object.keys(data)
    //         keys.forEach(key => {
    //             params.append(key,data[key])
    //         })
    //         return params
    //     }
    //     return data
    // }
    // ]
})
export function setConfigInstance(config) {
    let instance = axios.create({
        baseURL:baseUrl,
        timeout,
        // method:"post",
        ...config
    })
    return instance
}
requestInstance.interceptors.request.use((config) => {

    let reg = /appkey=.*/
    let isMatch = reg.test(config.url.split('?')[1])
    return {
        ...config,
        // url:isMatch?config.url:config.url+='?appkey='+localStorage.getItem("xyxx_userkeycode")
        url:isMatch?config.url:config.url
    }
},(error) => {
    return new Error(error)
})

requestInstance.interceptors.response.use(function (res) {
    return res.data
},function (error) {
    return new Error(error)
})

export {
    requestInstance
}


const requestInstance_Address = axios.create({
    baseURL:baseUrl_Address,
    timeout,
    method:"post",
    withCredentials,
    responseType: "blob",
    headers:{

        "Content-Type":"application/x-www-form-urlencoded"
    },
    transformRequest:[(data) => {

        if(data) {
            let params = new FormData()
            let keys = Object.keys(data)
            keys.forEach(key => {
                params.append(key,data[key])
            })
            return params
        }
        return data
    }
    ]
})
requestInstance_Address.interceptors.request.use((config) => {
    return config
},(error) => {
    return new Error(error)
})

requestInstance_Address.interceptors.response.use(function (res) {
    return res.data
},function (error) {
    return new Error(error)
})

export {
    requestInstance_Address
}
