"use strict";
const common_vendor = require("../common/vendor.js");
const config_user = require("../config/user.js");
const config_url = require("../config/url.js");
const wxLogin = () => {
  return new Promise((resolve, reject) => {
    common_vendor.index.login({
      provider: "weixin",
      success: async (loginRes) => {
        try {
          if (loginRes.code) {
            const result = await config_user.login({
              code: loginRes.code,
              appid: "wx2165f0be356ae7a9"
            });
            if (result.code === 200) {
              common_vendor.index.setStorageSync("token", result.data.LoginWxVo.access_token);
              common_vendor.index.setStorageSync("client_id", result.data.LoginWxVo.client_id);
              resolve(result.data);
            } else {
              throw new Error(result.message || "登录失败");
            }
          } else {
            throw new Error("获取微信登录凭证失败");
          }
        } catch (error) {
          common_vendor.index.showToast({
            title: error.message || "登录失败",
            icon: "none"
          });
          reject(error);
        }
      },
      fail: (error) => {
        common_vendor.index.showToast({
          title: "微信登录失败",
          icon: "none"
        });
        reject(error);
      }
    });
  });
};
const getUserInfo = () => {
  return new Promise(async (res, rej) => {
    const result = await config_user.getMyIndexInfo({
      isHide: false
    });
    res(result);
  });
};
const getUserPhoneNumber = (code) => {
  return new Promise(async (resolve, reject) => {
    try {
      const token = common_vendor.index.getStorageSync("token");
      if (!token) {
        reject(new Error("用户未登录"));
        return;
      }
      common_vendor.index.__f__("log", "at util/index.js:88", "开始请求获取手机号，code=", code, "请求地址=", config_url.URL + "/app/user/getPhoneNumber");
      common_vendor.index.request({
        url: config_url.URL + "/app/user/getPhoneNumber",
        method: "POST",
        header: {
          "Authorization": "Bearer " + token,
          "clientId": common_vendor.index.getStorageSync("client_id"),
          "Content-Type": "application/json"
        },
        data: {
          code,
          // 微信获取手机号授权后的临时code
          appid: config_url.appid
        },
        success: (res) => {
          common_vendor.index.__f__("log", "at util/index.js:104", "请求获取手机号接口成功，原始响应:", JSON.stringify(res));
          if (res.statusCode !== 200) {
            reject(new Error(`HTTP错误: ${res.statusCode}`));
            return;
          }
          if (!res.data) {
            reject(new Error("服务器返回空数据"));
            return;
          }
          resolve(res.data);
        },
        fail: (err) => {
          common_vendor.index.__f__("error", "at util/index.js:122", "请求获取手机号接口网络错误:", err);
          reject(new Error("请求获取手机号接口失败: " + (err.errMsg || JSON.stringify(err))));
        }
      });
    } catch (error) {
      common_vendor.index.__f__("error", "at util/index.js:127", "获取手机号过程中出现异常:", error);
      reject(new Error("获取手机号异常: " + (error.message || JSON.stringify(error))));
    }
  });
};
const initiateWeChatScorePayment = (paymentData) => {
  return new Promise((resolve, reject) => {
    if (!paymentData || !paymentData.data || !paymentData.data.package) {
      reject(new Error("支付参数不完整"));
      return;
    }
    common_vendor.wx$1.openBusinessView({
      businessType: "wxpayScoreUse",
      extraData: {
        mch_id: paymentData.data.mch_id,
        package: paymentData.data.package
      },
      success: (businessRes) => {
        if (businessRes.errMsg === "openBusinessView:ok") {
          resolve(businessRes);
        } else {
          reject(new Error("支付流程未完成"));
        }
      },
      fail: (error) => {
        common_vendor.index.__f__("error", "at util/index.js:158", "微信支付分小程序调用失败", error);
        common_vendor.index.showToast({
          title: error.errMsg || "支付分接口调用失败",
          icon: "none"
        });
        reject(error);
      }
    });
  });
};
const getQueryString = function(url, name) {
  var reg = new RegExp("(^|&|/?)" + name + "=([^&|/?]*)(&|/?|$)", "i");
  var r = url.substr(1).match(reg);
  if (r != null) {
    return r[2];
  }
  return null;
};
exports.getQueryString = getQueryString;
exports.getUserInfo = getUserInfo;
exports.getUserPhoneNumber = getUserPhoneNumber;
exports.initiateWeChatScorePayment = initiateWeChatScorePayment;
exports.wxLogin = wxLogin;
//# sourceMappingURL=../../.sourcemap/mp-weixin/util/index.js.map
