var e = require("../common/vendor.js"),
  n = require("../hooks/useState.js"),
  o = require("../config/index.js"),
  t = require("../service/index.js"),
  r = require("./util.js");
require("../constant/index.js");
(exports.request = function a(c) {
  var s = c.url,
    i = c.method,
    u = c.formData,
    l = c.needToast,
    d = void 0 === l || l,
    m = c.count,
    h = void 0 === m ? 0 : m,
    g = n.useState(),
    f = g.channelName,
    p =
      (g.userLogout,
      {
        version: o.appVersion,
        token: e.index.getStorageSync("token"),
        channel: f.value ? f.value : o.defaultChannelName,
        phoneType: e.index.getSystemInfoSync().deviceModel,
      }),
    v = r.encrypt(e._.cloneDeep(u), p.channel);
  return (
    console.log("[请求开始] -- ", {
      渠道来源: p.channel,
      请求地址: "".concat(o.baseUrl).concat(s),
      请求参数: v || null,
      token: p.token || "",
      count: h,
    }),
    e.index
      .request({
        url: "".concat(o.baseUrl).concat(s),
        method: i,
        data: v,
        header: p,
        timeout: 1e4,
        dataType: "json",
      })
      .then(function (n) {
        var o = n.statusCode,
          c = n.data,
          d = c.code,
          m = c.message || '',
          g = c.data;
        if ((console.log("[请求结束] -- ", g, m, d), 200 === o)) {
          if (200 === d) return Promise.resolve(g);
          if (301 === d) {
            return (function () {
              return e.index
                .login({ provider: "weixin" })
                .then(function (e) {
                  var n = e.code;
                  return t.loginForMpWeixin({ code: n });
                })
                .then(function (n) {
                  var o = n.token,
                    t = n.openId;
                  return (
                    e.index.setStorageSync("token", o),
                    e.index.setStorageSync("openId", t),
                    a({ url: s, method: i, formData: u, needToast: !1 })
                  );
                });
            })();
          }
          return h < 3
            ? a({ url: s, method: i, formData: u, needToast: !1, count: h + 1 })
            : Promise.reject({ msg: m });
        }
        return Promise.reject({ msg: m });
      })
      .catch(function (e) {
        return (
          console.error("[请求报错] -- ", e),
          d && r.showToast(e.msg ? e.msg : "接口请求失败，请重新加载"),
          Promise.reject()
        );
      })
  );
}),
  (exports.upload = function (t) {
    var a = t.url,
      c = t.name,
      s = t.filePath,
      i = t.formData,
      u = t.needToast,
      l = void 0 === u || u,
      d = n.useState().channelName,
      m = {
        version: o.appVersion,
        token: e.index.getStorageSync("token"),
        channel: d.value ? d.value : o.defaultChannelName,
        phoneType: e.index.getSystemInfoSync().deviceModel,
      },
      h = r.encrypt(e._.cloneDeep(i), m.channel);
    return (
      console.log("[请求开始] -- ", {
        渠道来源: m.channel,
        请求地址: "".concat(o.baseUrl).concat(a),
        请求参数: h || null,
        token: m.token || "",
      }),
      e.index
        .uploadFile({
          url: "".concat(o.baseUrl).concat(a),
          filePath: s,
          name: c,
          formData: h,
          header: m,
        })
        .then(function (e) {
          var n = e.statusCode,
            c = n.data,
            a = c.code,
            c = c.message || '',
            s = c.data;
          return (
            console.log("[请求结束] -- ", s, c, a),
            200 === n && 200 === a
              ? Promise.resolve(s)
              : Promise.reject({ msg: c })
          );
        })
        .catch(function (e) {
          return (
            console.error("[请求报错] -- ", e),
            l && r.showToast(e.msg ? e.msg : "上传失败，请重新上传"),
            Promise.reject()
          );
        })
    );
  });
