import Vue from 'vue';
// import axios from 'axios';
import qs from 'qs';
import { Message } from 'element-ui';

Vue.axios.defaults.timeout = 25000;
let local = window.location.origin;
/* if (local.indexOf('localhost') != -1) {
  local = 'http://www.gicdev.com';
} */

Vue.axios.interceptors.request.use(
  config => {
    return config;
  },
  err => {
    Message.error({ message: '请求超时!' });
    return Promise.resolve(err);
  }
);

Vue.axios.interceptors.response.use(
  data => {
    // console.log(data);
    if (data.status && data.status == 200 && data.data.code != '0000') {
      // Message.error({message: data.data.message});
      if (data.data.code == 4 || data.data.code == 2003) {
        window.location.href = local + '/gateway-web/#/login';
      }
      if (data.data.code == 10 || data.data.code == 7) {
        window.location.href = local + '/gateway-web/#/overview';
      }
      return data;
    }
    return data;
  },
  err => {
    // Message.error({message: err.response.message});
    if (err.response.status == 504 || err.response.status == 404) {
      // window.location.href= local + "/haoban-web/#/login"
      // Message.error({message: '服务异常⊙﹏⊙∥'});
    } else if (err.response.status == 403) {
      // window.location.href= local + "/haoban-web/#/login"
      // Message.error({message: '权限不足,请联系管理员!'});
    } else {
      // window.location.href= local + "/haoban-web/#/login"
      // Message.error({message: '未知错误!'});
    }
    return Promise.resolve(err);
  }
);

/**
 * @description: get
 * @param {String} url
 * @param {Object} params
 * @return {Function}
 * @author: 无尘
 */
export const getRequest = (url, params) => {
  params.requestProject = 'api-gateway-web';
  return Vue.axios({
    method: 'get',
    url: `${local}${url}`,
    data: {},
    params: params,
    headers: {
      'Content-type': 'application/x-www-form-urlencoded'
    }
  });
};

/**
 * @description: post formdata
 * @param {String} url
 * @param {Object} params
 * @return {Function}
 * @author: 无尘
 */
export const postRequest = (url, params) => {
  params.requestProject = 'api-gateway-web';
  return Vue.axios({
    method: 'post',
    url: `${local}${url}`,
    data: qs.stringify(params),
    headers: {
      'Content-type': 'application/x-www-form-urlencoded'
    }
  });
};

export const postJsonRequest = (url, params) => {
  params.requestProject = 'api-gateway-web';
  return Vue.axios({
    method: 'post',
    url: `${local}${url}`,
    data: '{}',
    params: params,
    headers: { 'Content-Type': 'application/json;charset=UTF-8' }
  });
};

/**
 * @description: post json
 * @param {String} url
 * @param {Object} params
 * @return {Function}
 * @author: 无尘
 */
export const postJson = (url, params) => {
  // params.requestProject = "api-gateway-web";
  return Vue.axios({
    method: 'post',
    url: `${local}${url}`,
    data: params,
    params: { requestProject: 'api-gateway-web' },
    headers: { 'Content-Type': 'application/json;charset=UTF-8' }
  });
};

/**
 * @description: post
 * @param {String} url
 * @param {Object} params
 * @return {Function}
 * @author: 无尘
 */
export const postForm = (url, params) => {
  params.requestProject = 'api-gateway-web';
  return Vue.axios({
    method: 'post',
    url: `${local}${url}`,
    data: params,
    headers: {}
  });
};

/**
 * @description: post excel
 * @param {String} url
 * @param {Object} params
 * @return {Function}
 * @author: 无尘
 */
export const postExcel = (url, params) => {
  params.requestProject = 'api-gateway-web';
  return Vue.axios({
    method: 'post',
    url: `${local}${url}`,
    data: qs.stringify(params),
    responseType: 'blob',
    headers: {
      'content-type': 'application/x-www-form-urlencoded'
    }
  });
};
