import axios from "axios";
import router from "../router/index.js";
import { Undertheshelf } from "../view/namespace.js";
import { undefinedCollect } from './index.js'

// var instance = axios.create(
//   process.env.NODE_ENV === "development"
//     ? {
//         baseURL: "http://site1.aizhuishu.com/api",
//         withCredentials: true
//       }
//     : {
//         baseURL: "/api"
//       }
// );

var instance = axios.create({
  baseURL: "/api"
});

instance.interceptors.response.use(function (res) {
  if (res.data.code === 0) {
    return res.data.data;
  }
  if (res.data.code === 10018) {
    router.replace(Undertheshelf.route);
  } else {
    return Promise.reject(res);
  }
});
instance.interceptors.request.use(function (config) {
  if (config.method === 'get' && config.url && config.url.indexOf('undefined') !== -1) {
    undefinedCollect({
      name: 'undefined',
      content: JSON.stringify({
        current_page: location.href,
        next_page: window.nextPage,
        prev_page: window.prevPage,
        api: config.url,
        user_id: window.options && window.options.uid,
        channel_id: window.options && window.options.distribution_channel_id,
      })
    })
  }

  return config;
});

export default instance;
