import axios from 'axios'
import {
  DOMIN,
  mapJson
} from './server.js'
// import router from '@/router'

import Vue from 'vue'
Vue.prototype.DOMIN = DOMIN
Vue.prototype.mapJson = mapJson //请求地图json,不带端口号
console.log(DOMIN)
// 打包解开   跑项目注释
// axios.defaults.headers.post['Content-Type'] = 'application/x-www-form-urlencoded;charset=UTF-8';
// axios.defaults.timeout = 30000;
// axios.defaults.withCredentials = true //axios请求时携带session
// axios.defaults.crossDomain = true
// axios.defaults.baseURL = DOMIN

// axios请求前的拦截
axios.interceptors.request.use(req => {
  // 判断当前请求url为weatherCondition或ipLocation时就不将axios的请求地址更换
  // let session = localStorage.getItem('session');
  // let session = "OSs0lS9lq/6X1Mp4ZF9ekU3W/6 BVG4XgECuecB QLk="
  let session = localStorage.getItem("keys")
  let url;
  if (req.url.split('=')[1]) {
    url = req.url.split('=')[1]
  } else {
    url = req.url.split('api/')[1]
  }
  // console.log(url)
  if (url !== 'user.login.zg_login_user' &&
    url !== 'pest.pests.plant_discern' &&
    url !== 'pest.pests.insect_discern'&& 
    url !== 'user.login.login_user') {
    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 => {
  return Promise.reject(error)
})
