/**
 * @description: 请求
 * @author: Adrian
 */
/* eslint-disable */
import Store from '@/store'
import axios from "axios";
import {Message } from "element-ui";
import { getToken } from './auth'
const service = axios.create({
  timeout:0
});
// request interceptor
service.interceptors.request.use(
  config => {
    config['url']=encodeURI(config['url'])//解决中文乱码问题
    config.headers['Content-Type'] = 'application/json;charset=UTF-8'//请求类型
    if (config.accessToken||getToken()) {
      config.headers['X-AUTH-TOKEN'] = config.accessToken||getToken()//token
      delete config.accessToken
    }
    if (config.responseType==='blob'){
        config.onDownloadProgress = progressEvent => {
            let complete = (progressEvent.loaded / progressEvent.total * 100 | 0);
            Store.commit('UPLOAD_USER_PROGRESS',complete);
        }
    }
    return config
  },
  error => {
    Promise.reject(error);
  }
);
// respone interceptor
service.interceptors.response.use(
  response => {
    if (response.data.header&&response.data.header.ret === "0"||response.data.size>-1) {
      return response.data;
    }else if(response.data.header&&response.data.header.ret==='1000'){
      Store.dispatch('logout')
      Message({
        message: response.data.header.msg,
        type: "error",
      });
      return;
    } else {
      Message({
        message: response.data.header.msg,
        type: "error",
      });
      return Promise.reject(response.data.header.msg);
    }
  },
  error => {
    if(error.message.includes('status code')){
      Message({
        message:error.message,
        type: "error",
      });
    }else{
      Message({
          message:'请求超时，请重新操作',
          type: "error",
        });
    }
    return Promise.reject(error);
  }
);

export default service;