let baseUrl = "";
const $get = (uri, data) => {
  let app = getApp();
  let tenant = app.tenant || "test";
  return new Promise((resolve, reject) => {
    wx.request({
      url: `${baseUrl}${uri}`,
      method: "get",
      data,
      header: { "x-tenant": tenant },
      success: (res) => toResolve(resolve, res),
      error: (e) => toReject(reject, e),
    });
  });
};
const $post = (uri, data) => {
  let app = getApp();
  let tenant = app.tenant || "test";
  return new Promise((resolve, reject) => {
    wx.request({
      url: `${baseUrl}${uri}`,
      method: "post",
      data,
      header: { "x-tenant": tenant },
      success: (res) => toResolve(resolve, res),
      error: (e) => toReject(reject, e),
    });
  });
};
const $delete = (uri, data) => {
  let app = getApp();
  let tenant = app.tenant || "test";
  return new Promise((resolve, reject) => {
    wx.request({
      url: `${baseUrl}${uri}`,
      method: "delete",
      data,
      header: { "x-tenant": tenant },
      success: (res) => toResolve(resolve, res),
      error: (e) => toReject(reject, e),
    });
  });
};

const toResolve = (resolve, result) => {
  if (result.statusCode === 200) resolve(result.data);
  else {
    console.error("请求失败");
  }
};

const toReject = (reject, result) => {
  console.error(result);
  reject(result);
};

module.exports = { $get, $post, $delete };
