import request from '@/utils/request'
import user from '@/store/modules/user';

function get(url, params) {
  return new Promise((resolve, reject) => {
    request({
      method: "GET",
      url,
      params: params,
      headers: {
        token: user.state.token,
      },
    })
      .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.state.token,
        "Content-Type": "multipart/form-data;",
      },
    })
      .then((res) => {
        resolve(res);
      })
      .catch((err) => {
        reject(err);
      });
  });
}

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

function del(url, data) {
  return new Promise((resolve, reject) => {
    request({
      method: "DELETE",
      url,
      data: data,
      headers: {
        token: user.state.token,
        "Content-Type": "multipart/form-data;",
        // "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,
  put,
  del,
  delform
}