"use strict";
const common_vendor = require("../../common/vendor.js");
const config_request = require("../../config/request.js");
const useIsExpiration = async () => {
  var timestamp = Date.parse(/* @__PURE__ */ new Date());
  var data_expiration = common_vendor.index.getStorageSync("data_expiration");
  if (data_expiration) {
    if (timestamp > data_expiration) {
      common_vendor.index.setStorageSync("data_expiration", 0);
      return false;
    } else {
      return true;
    }
  }
  return false;
};
const useSmallProgramLogin = async (app) => {
  return new Promise(async (resolve, reject) => {
    if (await useIsExpiration() && common_vendor.index.getStorageSync("token")) {
      resolve(true);
      return;
    }
    common_vendor.index.login({
      success(res) {
        common_vendor.index.request({
          url: config_request.BASE_URL + "wsgw/" + app.globalData.channelId + "/" + app.globalData.configKey + "/" + app.globalData.hosId + "/smallProgramLogin_v2.do?cfgKey=" + app.globalData.wechatConfigKey,
          method: "POST",
          data: {
            appId: app.globalData.appId,
            smallPro_authCode: res.code,
            smallPro_systemInfo: app.globalData.smallPro_systemInfo,
            smallPro_version: app.globalData.smallPro_version
          },
          header: {
            "content-type": "application/x-www-form-urlencoded"
          },
          success(resp) {
            if (resp.data.RespCode == "10000") {
              common_vendor.index.setStorageSync("token", resp.data.token);
              getApp().globalData.token = resp.data.token;
              common_vendor.index.setStorageSync("openid", resp.data.openId);
              common_vendor.index.setStorageSync("unionid", resp.data.unionId);
              common_vendor.index.setStorageSync("smallProOpenId", resp.data.smallProOpenId);
              common_vendor.index.setStorageSync("wechatOpenid", resp.data.wechatOpenid);
              common_vendor.index.setStorageSync("isCall", 0);
              var timestamp = Date.parse(/* @__PURE__ */ new Date());
              var expiration = timestamp + resp.data.expireTime;
              common_vendor.index.setStorageSync("data_expiration", expiration);
              resolve(resp);
            } else {
              if (resp.data.RespCode == "-14019" && resp.data.RespMessage && resp.data.RespMessage.indexOf("非白名单用户") >= 0) {
                common_vendor.index.showLoading({
                  title: "系统升级中...",
                  mask: true
                });
              } else {
                common_vendor.index.showLoading({
                  title: resp.data.RespMessage || "网络异常！",
                  mask: true
                });
              }
              resolve(false);
            }
          },
          fail(error) {
            common_vendor.index.showLoading({
              title: "网络异常！",
              mask: true
            });
            reject(error);
          }
        });
      },
      fail(error) {
        common_vendor.index.showLoading({
          title: "网络异常！",
          mask: true
        });
        reject(error);
      }
    });
  });
};
exports.useSmallProgramLogin = useSmallProgramLogin;
//# sourceMappingURL=../../../.sourcemap/mp-alipay/hook/use-small-program-login/index.js.map
