import axios from "axios";
import { Toast } from "vant";

let loading = null;

const service = axios.create({
  baseURL: "http://192.168.204.152:8997",
  timeout: 5000
});

service.interceptors.request.use(
  config => {
    config.headers["Authorization"] =
      "eyJhbGciOiJIUzUxMiJ9.eyJzdWIiOiJ7XCJpZFwiOjEsXCJpbnZpdGVDb2RlXCI6XCIxXCIsXCJwYXNzd29yZFwiOlwiMTIzXCIsXCJ1c2VySWRcIjpcIjFcIixcInVzZXJMZXZlbFwiOjR9IiwiaWF0IjoxNjE0MzI3OTY0fQ.f9RPBOMo0BMMbWPnQIhvfLe535as60dk_SZoXMYOT3bHYjoYST-mivTcKC-u4grEpYZMax9RgyuHXCYydZPzQw";
    if (!config.loading) {
      loading = Toast.loading({
        forbidClick: true,
        message: "加载中..."
      });
    }
    return config;
  },
  error => {
    if (loading) Toast.clear();
    console.log("rqquest====错误", error);
  }
);

service.interceptors.response.use(
  response => {
    if (loading) Toast.clear();
    const res = response.data;
    return res;
  },
  error => {
    if (loading) Toast.clear();
    console.log("response===错误", error);
  }
);

export default service;
