import setting from './config';
import util from 'dvcp-wui/utils/util';
import store from "../store"
import instance from "dvcp-wui/utils/http";

instance.defaults.baseURL = setting.baseUrl

instance.interceptors.request.use(config => {
  if (config.baseURL === 'http://192.168.1.87:59998') {
    config.url = config.url.replace(/(app|auth|admin|api)\//, "")
  }

  return config
})

instance.interceptors.response.use(response => {
  util.$hideLoading();
  if (response.data.code === 1) {
    util.$toast({title: response.data.msg, duration: 3000});
  } else if (response.data.code == 2) {
    //首次静默登录异常不做任何返回
  } else if (response.data.code == 401) {
    return store.commit('logout')
  } else {
    return response.data;
  }
}, err => {
  console.error(err);
});

export default instance
