import axios from 'axios'
//import CancelStore from '@/axios/store';
import {
  Message,
  MessageBox
} from 'element-ui'
let baseURL = process.env.baseURL1;
// if (process.env.NODE_ENV === "development") {
// baseURL = process.env.baseURL;
// } else {
// baseURL = process.env.baseURL;
// }
// axios.defaults.timeout = 30000;
// axios.defaults.retry = 1;
// axios.defaults.retryDelay = 1000;
//const CancelToken = axios.CancelToken;
// 创建axios实例
const service = axios.create({
  baseURL: baseURL, // api 的 base_url
  timeout: 30000 // 请求超时时间
})
// request拦截器
service.interceptors.request.use(
  config => {
    // 这个是 取消重点
    //config.cancelToken = new CancelToken((cancel) => {
    // CancelStore._axiosPromiseCancel.push(cancel);
    //});
    let url = config.url.replace(config.baseURL, '');
    let code = config.code;
    config.headers = getHeader(url, code) // 让每个请求携带自定义签名
    return config
  },
  error => {
    console.log(error) // for debug 11
    Promise.reject(error)
  }
)
// response 拦截器
service.interceptors.response.use(
  response => {
    if (response.config.responseType == 'blob') {
      if (response.status == 200) {
        return response;
      } else {
        return Promise.reject(error);
      }
    } else {
      const res = response.data;
      if (res.code === 200) {
        return response.data
      } else if (res.code === 407) {
        // console.log("Token失效跳转登陆页面");
        // sessionStorage.clear();
        // location.href="/ecloud-sp/logout";
      } else {
        Message({
          message: res.message,
          type: 'error',
          duration: 5 * 1000
        })
        return Promise.reject('error')
      }
    }
  },
  error => {
    console.log('err' + error) // for debug
    // if (axios.isCancel(error)) {
    // // 为了终结promise链 就是实际请求 不会走到.catch(rej=>{});这样就不会触发错误提示之
    // return new Promise(() => {});
    // } else {
    Message({
      message: error.message,
      type: 'error',
      duration: 5 * 1000
    })
    return Promise.reject(error)
    // }
  }
)
function getHeader(url, code) {
    let timestamp = new Date() * 1;
    let token = sessionStorage.getItem('user_token') === null? "" : sessionStorage.getI
    let source = 'web';
    let sign = $md5(source + url + code + timestamp + token);
    return {'Content-Type': 'application/json; charset=UTF-8', 'token': token,'timestam':timestamp, 'code': code, 'sign':sign, 'source':source };
   }
export default service
