import axios from "axios";
import store from "@/store";

// 中心化请求
export function request(config) {
  const instance = axios.create({
    baseURL: "",
    // 表示跨域请求时是否需要使用凭证
    withCredentials: false,
    timeout: 20000,
  });

  // 请求拦截器
  instance.interceptors.request.use(
    (config) => {
      return config;
    },
    (err) => Promise.reject(err)
  );

  // 响应拦截器
  instance.interceptors.response.use(
    (res) => {
      return res;
    },
    (err) => {
      return Promise.reject(err);
    }
  );
  return instance(config);
}

export function $get(url, data) {
  return request({
    url: url,
    method: "get",
    params: data,
  });
}

export function $post(url, data) {
  return request({
    url: url,
    method: "post",
    data,
  });
}

export function $put(url, data) {
  return request({
    url: url,
    method: "put",
    data,
  });
}
