import request from '@/utils/request'
import user from "@/store/user"

let currentToken = "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1c2VyX2lkIjoxLCJ1c2VyX25hbWUiOiJcdTVmMjBcdTRlMDkiLCJleHAiOjE2ODA3NzEzOTV9.aQ51C_OIOoHDGi-ua6aGPdF2WqNGCgEp1IdqK_zdfOo"

function get(url, params) {
  return new Promise((resolve, reject) => {
    request({
      method: "GET",
      url,
      params: params,
      headers: {
        token: user.token || currentToken,
      },
    })
      .then((res) => {
        resolve(res);
      })
      .catch((err) => {
        reject(err);
      });
  });
}

function post(url, data) {
  return new Promise((resolve, reject) => {
    request({
      method: "POST",
      url,
      data: data,
      headers: {
        token: user.token || "",
        "Content-Type": "multipart/form-data;",
      },
    })
      .then((res) => {
        resolve(res);
      })
      .catch((err) => {
        reject(err);
      });
  });
}

function del(url, data) {
  return new Promise((resolve, reject) => {
    request({
      method: "DELETE",
      url,
      data: data,
      headers: {
        "Content-Type": "application/json;",
      },
    })
      .then((res) => {
        resolve(res);
      })
      .catch((err) => {
        reject(err);
      });
  });
}


function delform(url, data) {
  return new Promise((resolve, reject) => {
    request({
      method: "DELETE",
      url,
      data: JSON.stringify(data),
      headers: {
        "Content-Type": "application/x-www-form-urlencoded;",
      },
    })
      .then((res) => {
        resolve(res);
      })
      .catch((err) => {
        reject(err);
      });
  });
}

export {
  get,
  post
}