import axios from 'axios';
import {
  Toast
} from "vant";
import {
  login
} from "../api";

Toast.setDefaultOptions({
  duration: 2000
});
const $service = axios.create({
  baseURL: process.env.VUE_APP_BASE_API,
  timeout: 60000
});

$service.interceptors.request.use(
  config => {
      if (config.url === '/vascap/informationReturn/v1' || config.url === '/ordersync') {
          config.baseURL = process.env.VUE_APP_BASE_APISORE
      }

    if (config.loading != true) {
      Toast.loading({
        message: '加载中',
        forbidClick: true,
        duration: 10000
      });
    }
    config.headers['Content-Type'] = 'application/json;charset=UTF-8';

    return config;
  },
  error => {
    return Promise.reject(error);
  });
$service.interceptors.response.use(
  response => {
    if (response.config.loading != true) {
      Toast.clear()
    }
    const res = response.data;
    if (res.code !== 10000) {
      if (res.code === 401 || res.code === 50012 || res.code === 50014) {
        Toast.fail('登录状态失效');
      }
      return Promise.reject(res);
    } else {
      return res;
    }
  },
  error => {
    Toast.clear()
    if (error.config.loading != true) {
      Toast.fail('网络请求异常');
    }
    return Promise.reject(error);
  }
);

export default $service;
