import axios from 'axios'


import { ElMessage } from 'element-plus'
import { da } from 'element-plus/es/locales.mjs'

// const defaultError = ()=> ElMessage.error('发生了一些错误，请联系管理员')
// const defaultFailure = (message:any,status:any) => ElMessage.warning(message)


// export function post(url:any,data:any,success:any,failure = defaultFailure,error = defaultError){
//     axios.post(url,data,{
//         headers:{
//             'Content-Type':'application/x-www-form-urlencoded'
//         },
//         withCredentials:true
//     }).then(({data})=>{
//         if(data.success)
//             success(data.message, data.status)
//         else
//             failure(data.message, data.status)
//     }).catch(error)
// }
// export function get(url:any,success:any,failure = defaultFailure,error = defaultError){
//     axios.post(url,{
//        withCredentials:true
//     }).then(({data})=>{
//         if(data.success)
//             success(data.message, data.status)
//         else
//             failure(data.message, data.status)
//     }).catch(error)
// }

// 创建axios实例
const service = axios.create({
    baseURL:"http://192.168.2.107:8080",
    timeout:5000,
    headers:{
        'Content-Type':'application/x-www-form-urlencoded;charset=utf-8'
        // 'Content-Type':'application/json;charset=utf-8'
    },
    withCredentials:true

})



// 请求拦截
service.interceptors.request.use((config)=>{
    config.headers=config.headers || {}
    if(localStorage.getItem('token')){
        config.headers.token=localStorage.getItem('token') || ""
    }
    return config
})


// 相应拦截
service.interceptors.response.use((res) => {
    
    const status:number = res.data.status
    if(status!=200){
        //  console.log("dddd");
        //  ElMessage.error(res.data.message)
        //  return Promise.reject(res.data)
        
    }
    return res
},(err)=>{
    // ElMessage.error(err);
})


export default service; 
