import {
  userprofileBaseUrl
} from '../config/env'
import {
  Loading,
  Message
} from 'element-ui';
import axios from 'axios'

let req = axios.create({
  baseURL: userprofileBaseUrl,
  timeout: 3000
})
let loadingInstance = null;
req.interceptors.request.use(config => {
  loadingInstance = Loading.service({
    text: '拼命加载中',
    background: 'rgba(0,0,0,0.5)',
    spinner: 'el-icon-loading'
  });
  return config
}, error => {
  return Promise.reject(error)
});
req.interceptors.response.use(config => {
  loadingInstance.close();
  let {
    code,
    message,
    msg
  } = config.data
  if (code == 1 || code == 100 || code == 0) {
    return config;
  } else {
    Message({
      message: message || msg || '请求异常',
      type: 'error',
      duration: 2000
    });
    return Promise.reject(config.data)
  }
}, error => {
  loadingInstance.close();
  return Promise.reject(new Error(error));
})
export default req
