import axios from 'axios'

// axios.defaults.headers.post['Content-Type'] = 'application/x-www-form-urlencoded;charset=UTF-8';
// axios.defaults.timeout = 30000;
// axios.defaults.baseURL =process.env.API_HOST  //韩佑波
// // axios.defaults.baseURL = 'http://47.104.218.216:8000' //曹世祥
// axios.defaults.withCredentials = true //axios请求时携带session
// axios.defaults.crossDomain = true 

// axios请求前的拦截
axios.interceptors.request.use(req => {
  let session = localStorage.getItem('session');
  if (req.url.split('=')[1] != 'user.login.login_user') {
    if (req.url.split('=')[1] == 'pest.pests.insect_discern' || req.url.split('=')[1] == 'pest.pests.plant_discern' || req.url.split('=')[1] == 'pest.pests.pests_contrast' || req.url.split('=')[1] == 'pest.pests.pests_expert_img' || req.url.split('=')[1] == 'pest.pests.pests_img' || req.url.split('/')[1] == 'recognizationSys' || req.url.split('=')[1] == 'base.bases.base_photo' || req.url.split('=')[1] == 'pest.warning_record.rolemanage_img' || req.url.split('=')[1] == 'home.homes.personal_photo') {
      // req.data=req.data?req.data+'&token='+session:'token='+session;
      // console.log(req.url.split('=')[1] != 'pest.pests.insect_discern/')
    } else {
      req.data = req.data ? req.data + '&token=' + session : 'token=' + session;
    }
  }
  return req
}, error => {
  // console.log('request_error');
  // console.log(error); 
  return Promise.reject(error)
})

//axios请求后的拦截
axios.interceptors.response.use(res => {
  if (res.data.data) {
    res.data.data.session_key && localStorage.setItem('session', res.data.data.session_key);
  }
  return res
}, error => {
  let _res = error.response;
  // console.log('response_error');
  // console.log(error);
  // switch(_res.states){
  //     case 401:
  //         sessionStorage.removeItem('session');
  //         //跳转到登录页面
  // }
  // router.push('/login')
  return Promise.reject(error)
})

