import axios from 'axios'
import {
  DOMIN,
  mapJson
} from './server.js'
import router from '@/router'
import {
  Message
} from "element-ui"
import qs from 'qs'

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');
  // console.log(session)
  // let session = 'd5npOg/04dWL6f2YiywzUSyOzqayqnj7Qx8GNMyJhXMc='
  let url;
  if (req.url.split('=')[1]) {
    url = req.url.split('=')[1]
  } else {
    url = req.url.split('api/')[1]
  }
  // if (req.method === 'post') {
  //   // req.data = qs.stringify(req.data)
  //   if (req.url !== '/api/api_gateway?method=user.login.login_user') {
  //     req.data = req.data ? req.data + '&token=' + session : 'token=' + session;
  //   }
  // }
  url = req.url.split('=')[1]
  if (url != 'sysmenage.usermanager.user_login' && url !== 'monitor_manage.cbd_manage.add_img' && url != '/api/trap_export') {
    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);
  }
  // console.log(res.data.message)
  if (res.data.message != "" && res.data.message != undefined) {
    Message.warning(res.data.message);
  }
  return res
}, error => {
  return Promise.reject(error)
})
