import axios from 'axios';
import { Notification ,Message } from 'element-ui';
import qs from 'qs'

/**
 * axios 请求结果异常统一处理
 * @params error
 * 
 */
const error = (error) => {
    if(error.response){ //请求有响应
        const { status } = error.response;
        const statusCode = [{
                status: 400,
                description: '请求参数有误'
            },
            {
                status: 403,
                description: '资源不可用'
            },
            {
                status: 500,
                description: '服务器内部错误'
            }
        ];
    
        for (let i = 0; i < statusCode.length; i++) {
            if (statusCode[i].status == status) {
             
                Notification .error({
                    title: '异常',
                    message: statusCode[i].description,
                    duration: 2000
                });
                break;
            }
        }
    
    }else if(error.request){ //请求无响应
        Notification .error({
            title: '异常',
            message: '请求暂无响应,请稍后再试',
            duration: 2000
        });
    }else{
        Notification .error({
            title: '异常',
            message: error.message,
            duration: 2000
        });
       
    }
   
    return {
        status: -404,
        message: '请求出错.'
    }
}

/**
 * axios 请求结果统一处理
 * @params axiosResponse
 * 
 */
const successResponse = (axiosResponse) =>{
      const {data} = axiosResponse;
      let res = null;
      if(data.code == "0000"){
          res = data;
      }else{
        res = {
            code: data.code,
            message:data.message
        }
        Message.warning({
            showClose: true,
            message: res.message,
            type: 'warning',
            duration:1500
        })
      }
      return res
}


const service = axios.create({
    baseURL: 'http://101.37.20.82:8081/backUser', // 服务器地址公用前缀 
    timeout: 8000, // 请求超时时间毫秒
    responseType: 'json'
})


service.interceptors.request.use(config=>{
    return config;
})

service.interceptors.response.use(response=>{
   return response
},error)


export default service