import axios from "axios";
import {timeout, baseUrl, baseUrl_Address,withCredentials} from "@/service/config";
import {UserInfo} from "@/unit/UserInfo";
import {showMessage} from "@/unit/element-ui/tip";
import {closeShowLoading, showLoading} from "@/data/element-loading/element-loader";
import router from "@/router";

const requestInstance = axios.create({
    baseURL:import.meta.env.VITE_BASE_API,
    timeout,
    method:"post",
    headers:{
        Systemtype:'ebb42096345340359741dcb9c039f15c'
    },
    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) => {
    if (!config.url.includes('/user/updateLoginLasttime')){
        showLoading({
            fullscreen:true
        })
    }

    let reg = /appkey=.*/
    //let isMatch = reg.test(config.url.split('?')[1])
    let isMatch = config.data.hasOwnProperty('appkey');
    return {
        ...config,
        data:new UserInfo().GetUserKeyCode()?(isMatch?config.data:{...config.data,appkey:new UserInfo().GetUserKeyCode()}):config.data
    }
},(error) => {
    return new Error(error)
})

requestInstance.interceptors.response.use(function (res) {
    if (!res.config.url.includes('/user/updateLoginLasttime')){
        closeShowLoading()
    }
    let isAppkeyIsFailed = false
    try {
        if (+res.data.msg[0].code === 401){
            isAppkeyIsFailed=  true
        }else {
            isAppkeyIsFailed=  false
        }
    }catch {
        isAppkeyIsFailed = false
    }
    if (isAppkeyIsFailed){
        showMessage({
            type:'warning',
            message:'登录验证过期，请重新登录'
        })
       router.replace('/login')
        router.go(0)
    }



    return res.data
},function (error) {
    closeShowLoading()
    return new Error(error)

})

export {
    requestInstance
}



const requestInstance_Get = axios.create({
    baseURL:import.meta.env.VITE_BASE_API,
    timeout,
    method:"get",
    withCredentials,
    responseType: "blob",
    headers:{
        Systemtype:'ebb42096345340359741dcb9c039f15c'
    },
    // 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_Get.interceptors.request.use((config) => {
    return config
},(error) => {
    return new Error(error)
})

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

export {
    requestInstance_Get
}



