import axios from "axios";
import Qs from 'qs';
import { Toast } from 'vant';
import Vue from "vue";
Vue.use(Toast);

let globalUrl = "http://uat.m.jzlives.cn/servantequityservice";
let globalUrlImg = "";


console.log("http", globalUrl, globalUrlImg);

Vue.prototype.imgUrl = globalUrlImg;

axios.defaults.baseURLImg= globalUrlImg;
axios.defaults.baseURL = globalUrl; //设置默认接口地址
axios.defaults.headers = { "X-Requested-With": "XMLHttpRequest" };
axios.defaults.timeout = 60000; //设置请求时间
axios.defaults.headers["Content-Type"] = "application/json;charset=utf-8";
// axios.defaults.headers={"Content-Type":"application/x-www-form-urlencoded;charset=utf-8"};

let loading;
function startLoading() {
      loading = Toast.loading({
        message: '加载中...',
        forbidClick: true,
      });
}

function endLoading() {
    Toast.clear();
}
// 请求拦截
axios.interceptors.request.use(
      config => {
            // 加载动画
            startLoading();
            // if (localStorage.token) {
            //       // 存在设置同意请求头
            //       config.headers["seller-token"] = localStorage.token;
            // }
            return config;
      },
      err => {
        Toast('请求超时!');
            return Promise.reject(err);
      }
);

// 响应拦截
axios.interceptors.response.use(
      response => {
            // 结束加载动画
            endLoading();
            console.log(response)
            return response;
      },
      err => {
            endLoading();
            console.log(err);
            if (err && err.response && err.response.status) {
                  // 获取错误状态吗
                  const { status } = err.response;
                  if (status == 401) {
                    Toast("token失效，请重新登录!");
                        // 清除token
                        // localStorage.removeItem("token");
                        // 跳转到登录页面
                        // router.push("/login");
                  } else {
                    Toast(err.response.data.desc);
                  }
            } else {
                Toast('Network Error');
            }
            return Promise.reject(err);
      }
);

export default axios;
