"use strict";
const common_vendor = require("../common/vendor.js");
const BASE_URL = "https://api.example.com";
const TIMEOUT = 1e4;
const request = (options) => {
  return new Promise((resolve, reject) => {
    const token = common_vendor.index.getStorageSync("token") || "";
    common_vendor.index.request({
      url: BASE_URL + options.url,
      method: options.method || "GET",
      data: options.data || {},
      header: {
        "Content-Type": "application/json",
        "Authorization": token ? `Bearer ${token}` : ""
      },
      timeout: TIMEOUT,
      success: (res) => {
        const { statusCode, data } = res;
        if (statusCode === 200) {
          if (data.code === 0 || data.code === 200 || data.success) {
            resolve(data);
          } else {
            common_vendor.index.showToast({
              title: data.message || data.msg || "请求失败",
              icon: "none",
              duration: 2e3
            });
            reject(data);
          }
        } else if (statusCode === 401) {
          common_vendor.index.removeStorageSync("userInfo");
          common_vendor.index.removeStorageSync("token");
          common_vendor.index.reLaunch({
            url: "/pages/login/login"
          });
          reject({ message: "未授权，请重新登录" });
        } else {
          common_vendor.index.showToast({
            title: "网络请求失败",
            icon: "none",
            duration: 2e3
          });
          reject(res);
        }
      },
      fail: (err) => {
        common_vendor.index.showToast({
          title: "网络连接失败",
          icon: "none",
          duration: 2e3
        });
        reject(err);
      }
    });
  });
};
const post = (url, data) => {
  return request({ url, method: "POST", data });
};
exports.post = post;
//# sourceMappingURL=../../.sourcemap/mp-weixin/api/request.js.map
