import axios from 'axios'
import { Loading } from 'element-ui';
import { Message } from 'element-ui';
import { REQUEST_URL } from '@/api/config'
import Router from '@/router/index'
import qs from 'qs'
//axios请求设置
axios.defaults.baseURL = REQUEST_URL;
let loadingCount = 0;

//element通知设定
let newMessage = {};

//element加载设定
let newLoading = {};

//过滤请求
axios.interceptors.request.use(config => {
  newLoading = Loading.service({
    lock: true,
    background: 'rgba(255, 255, 255, 0.7)'
  });
  loadingCount ++;
  return config
}, error => {
  loadingCount --;
  if(loadingCount===0){
    newLoading.close();
  }
  newMessage = Message({
    type:"error",
    message: "加载超时",
    duration: 2000
  });
  return error
});

// 添加响应拦截器
axios.interceptors.response.use(
  response => {
    loadingCount --;
    if(loadingCount===0) {
      newLoading.close();
    }
    if(response.data.code == 200){
      return response;
    }else if(response.data.code == 401){
      newMessage = Message({
        type:"error",
        message: "登录超时,请重新登录~",
        duration: 2000
      });
      localStorage.removeItem('token');
      Router.push({path: '/login'});
      return response;
    }else if(response.config.responseType == "arraybuffer"){
      return response;
    }else{
      newMessage = Message({
        showClose: true,
        type:"error",
        message: response.data.msg,
        duration: 2000
      });
      return response;
    }
  }, error => {
    loadingCount --;
    if(loadingCount===0){
      newLoading.close();
    }
    newMessage = Message({
      type:"error",
      message: '出错了,请稍候再试~',
      duration: 2000
    });
    return Promise.reject(error)
  }
);
export default function request(method, url, data) {  //暴露 request 给API 管理
  method = method.toLocaleLowerCase();   //封装API的各种请求方式 以 post get delete为例
  if (method === 'post') {
    return axios.post(url, data )    //axios的post 默认转化为json格式
  } else if (method === 'put') {
    return axios.put(url, data)
  } else if (method === 'get') {
    return axios.get(url, {
      params: data,
    })
  } else if (method === 'delete') {
    return axios.delete(url, {
      params: data
    })
  }else if (method === 'postheaders') {
    return axios.post(url, data,{
      headers: {
        "Authorization":localStorage.getItem('token'),
      }
    })
  }else if (method === 'getheaders') {
    return axios.get(url, {
      params: data,
      paramsSerializer: function(p) { //固定写法
        return qs.stringify(p, {arrayFormat: 'repeat'})
      },
      headers: {
        "Authorization":localStorage.getItem('token'),
      }
    })
  }else if (method === 'getheadersadd') {
    return axios.get(url+'/'+data, {
      headers: {
        "Authorization":localStorage.getItem('token'),
      }
    })
  }else if (method === 'postheadersadd') {
    return axios.post(url+'/'+data.id,"", {
      headers: {
        "Authorization":localStorage.getItem('token'),
      }
    })
  }else if (method === 'getheadersaddtwo') {
    return axios.get(url+'/'+data.id+'/'+data.isConfirm,"", {
      headers: {
        "Authorization":localStorage.getItem('token'),
      }
    })
  }else if (method === 'putheaders') {
    return axios.put(url, data,{
      headers: {
        "Authorization":localStorage.getItem('token'),
      }
    })
  } else if (method === 'deleteheaders') {
    return axios.delete(url+'/'+data, {
      headers: {
        "Authorization":localStorage.getItem('token'),
      }
    })
  }else if (method === 'download') {
    return axios.get(url,
      {
        params: data,
        paramsSerializer: function(p) { //固定写法
          return qs.stringify(p, {arrayFormat: 'repeat'})
        },
        responseType: 'arraybuffer',
        headers: {
          "Authorization":localStorage.getItem('token'),
        },
      },{
      responseType: 'blob',
    })
  }
}
/*
      newMessage = Message({
        type:"error",
        message: "登录超时,请重新登录~",
        duration: 2000
      });
      localStorage.removeItem('token');
      Router.push({path: '/login'});
* */
