import axios from 'axios'
import {DOMIN} from './server.js'
import Vue from 'vue'
Vue.prototype.DOMIN = DOMIN

// axios.defaults.headers.post['Content-Type'] = 'application/x-www-form-urlencoded;charset=UTF-8';
// axios.defaults.timeout = 30000;
// axios.defaults.baseURL = DOMIN 
// axios.defaults.withCredentials = true //axios请求时携带session
// axios.defaults.crossDomain = true 

// axios请求前的拦截
axios.interceptors.request.use(req => {
  let session = localStorage.getItem('session');
  let url = req.url.split('=')[1]
  if (url != 'user.login.login_user' && url != 'pest.pests.insect_discern' &&
    url != 'pest.pests.plant_discern' &&
    url != 'pest.pests.pests_contrast' && url != 'pest.pests.pests_expert_img' &&
    url != 'pest.pests.pests_img' && url != 'recognizationSys' &&
    url != 'base.bases.base_photo' && url != 'pest.warning_record.rolemanage_img' &&
    url != 'home.homes.personal_photo' && url != 'ascend.ascend_manage.product_info' &&
    url != 'ascend.ascend_manage.quality_info' && url != 'ascend.ascend_manage.grow_info' &&
    url != 'ascend.ascend_manage.all_ascend' && url != 'after_sale.after_sale_manage.device_check' &&
    url != 'after_sale.after_sale_manage.aftersale_apply') {
      req.data = req.data ? req.data + '&token=' + session : 'token=' + session;
  }
  return req
}, 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)
})
