import axios from 'axios';
import Qs from 'qs';

// 请求超时时间
axios.defaults.timeout = 1000000;

axios.defaults.baseURL = 'http://122.112.252.100:8085';

// post 请求头
axios.defaults.headers.post['Content-Type'] = 'application/x-www-form-urlencoded;charset=UTF-8';

//请求拦截器
axios.interceptors.request.use(
  config => {

    config.headers = {
      'Content-Type' : 'application/x-www-form-urlencoded',
    }
    config.headers['Content-Type'] = "application/x-www-form-urlencoded";
    return config
  },
  error => {
    return Promise.error(error);
  }
);

// 相应拦截器

axios.interceptors.response.use(
  response => {
    if(response.status === 200) {
      return Promise.resolve(response)
    } else {
      return Promise.reject(response)
    }
  },
  error => {
    return Promise.error(error);
  }
);

// get 方法

export function get(url, params) {
  return new Promise((resolve, reject) => {
    axios.get(url, {params: params})
      .then(res => {
        resolve(res.data);
      })
      .catch(err => {
        reject(err.data);
      })
  })
}

// post 方法

export function post(url, params) {
  return new Promise((resolve, reject) => {
    axios.post(url, Qs.stringify(params))
      .then(res => {
        resolve(res.data);
      })
      .catch(err => {
        reject(err.data);
      })
  })

}


export default  {
  get,
  post
}
