/*
* axios定义
* */
import Vue from 'vue';
import axios from 'axios';
import ElementUI from 'element-ui';
import store from '@/store/index.js';
import { getCookie, setCookie } from '@/utils/util.js';
import { ajaxCtx, ajaxCtx2, ajaxCtx0 } from '@/config/config.js';
// import { ajaxCtx, ajaxCtx2, ajaxCtx0 } from '../../static/js/config.js';
//   平台拦截处理
import { apiIsPassProject, requestConfig, responseConfig, APIPro } from './axiosPass.js';
// axios支持跨域cookie
axios.defaults.withCredentials = true;
// axios添加一个请求拦截器
axios.interceptors.request.use((config) => {
//   console.log('nb', config.url)
//   console.log('hz', new Date().getTime())
//   var returnFlag = false;
//   let allPrivilege = window.localStorage.getItem('allPrivilege') ? JSON.parse(window.localStorage.getItem('allPrivilege')) : [];
//   let ownPrivilege = window.localStorage.getItem('ownPrivilege') && window.localStorage.getItem('ownPrivilege') !== 'undefined' ? JSON.parse(window.localStorage.getItem('ownPrivilege')) : [];
//   let menuID = '';
//   if (allPrivilege != null && allPrivilege.length !== 0) {
//     var tempArr = allPrivilege.filter(function (x) {
//       var setStratIndex = config.url.indexOf('pass') === -1 ? 0 : config.url.indexOf('pass') + 4;
//       var setEndIndex = config.url.indexOf('?') === -1 ? config.url.length : config.url.indexOf('?');
//       var a = config.url.substring(setStratIndex, setEndIndex);
//       // console.log('a', a);
//       // console.log('x.menuUrl', x.menuUrl);
//       // 判断当前菜单ID是否等于接口所在的菜单id
//       if (x.menuUrl && x.menuUrl.indexOf(a) !== -1) {
//         menuID = x.pId
//       }
//       return x.menuUrl ? x.menuUrl.indexOf(a) !== -1 : false;
//     });
//     if (tempArr.length > 0) {
//       var setArr = ownPrivilege.filter(function (x) {
//         var setStratIndex = config.url.indexOf('pass') === -1 ? 0 : config.url.indexOf('pass') + 4;
//         var setEndIndex = config.url.indexOf('?') === -1 ? config.url.length : config.url.indexOf('?');
//         var a = config.url.substring(setStratIndex, setEndIndex);
//         return x.menuUrl ? x.menuUrl.indexOf(a) !== -1 : false;
//       });
//       if (setArr.length === 0) {
//         returnFlag = menuID === window.localStorage.getItem('activeMenuId');
//       }
//     }
//   }
//   if (returnFlag) {
//     console.log('这个接口叫' + config.url)
//     if (config.method === 'get') {
//       config.method = 'post'
//     }
//     config.url = '';
//     return config;
//   }
  //  判断是否平台接口
  if (config.url.indexOf('configCenter') === 0) {
    config.url = 'pass/' + config.url
  }
  config = requestConfig(config);
  return config;
}, (error) => {
  return Promise.reject(error);
});
// axios添加一个响应拦截器
axios.interceptors.response.use(function (response) {
  //  判断是否是平台接口
//   if (response.config.url === '') {
//     response.status = '405'
//   }
  if (apiIsPassProject(response.config.url, APIPro)) {
    //  是平台接口
    //  处理配置
    response = responseConfig(response);
  } else {
    //  不是
    let flag = false, msg = '';
    if (response && response.data) {
      if (response.data.success === false) {
        flag = true;
        msg = response.data.message;
      }
      let resCode = response.data.state;
      if (resCode === -1 || resCode === '-1') {
        flag = true;
        msg = response.data.message;
      } else if (resCode === 500 || resCode === '500') {
        flag = true;
        msg = response.data.msgInfo ? response.data.msgInfo : response.data.errorInfo;
      } else if (resCode === 300 || resCode === '300') {
        // 后台session失效
        flag = true;
        msg = '登录已过期，请重新登录';
        window.top.location.href = store.getters.ctx;
        // window.location.href = this.$store.state.ctx + '/views/index.html';
        // window.location.href = './index.html';
      }
    }
    if (flag) {
      if (msg) { ElementUI.Message({ message: msg, type: 'error', dangerouslyUseHTMLString: true }); }
      return null;
    }
  }
  return response;
}, function (error) {
  let errorMsg = '<span style="font-weight: bold; font-size: 13px; display: inline-block; padding-bottom: 5px;">操作失败</span><br/>' +
    '<span>请刷新页面重新操作，如果问题依旧存在，请联系管理员</span>';
    if ((error.response.data.exception && error.response.data.exception.indexOf('ShiroJwtException') >= 0) || (error.response.data.message && (error.response.data.message.indexOf('Token') >= 0))) {
      ElementUI.Message({
        // message: '登录失效,请重新登录',
        // dangerouslyUseHTMLString: true,
        // type: 'error'
      });
      setTimeout(() => {
        window.top.location.href = store.getters.ctx;
        setCookie('accessToken', '', -1, '/');
        setCookie('refreshToken', '', -1, '/');
        setCookie('workDate', '', -1);
      }, 500)
      return
    }
  if (error && error.response && error.response.data) {
    let errorData;
    if (typeof error.response.data === 'string') {
      errorData = error.response.data.substring(0, error.response.data.length - 2);
      try {
        errorData = JSON.parse(errorData);
        let index = errorData.message.indexOf(':') + 1
        if (errorData.message) { errorMsg = errorData.message.substring(index); }
      } catch (err) {
        errorData = {
          message: error.response.data,
          code: error.response.status
        }
      }
    } else {
      errorData = error.response.data
      if (errorData.message) { errorMsg = errorData.message; }
    }
    let mCode = errorData.code;
    // data.error
    if (errorData.error) {
      if (errorData.error.message) { errorMsg = errorData.error.message; }
      mCode = errorData.error.code;
    }
    if (mCode === '-99' || mCode === -99) {
      errorMsg = '登录已过期，请重新登录';
      window.top.location.href = store.getters.ctx;
      // window.location.href = './index.html';
    } else if (mCode === '405' || mCode === 405) {
      errorMsg = '无权限访问，请联系系统管理员';
    }
    // console.log('执行2', errorData.message, errorMsg)
  }
  // 提示
  ElementUI.Message({
    message: errorMsg,
    dangerouslyUseHTMLString: true,
    type: 'error'
  });
  return Promise.reject(error);
});
Vue.prototype.axios = axios;

export default axios;
