"use strict";
const common_vendor = require("../../common/vendor.js");
const utils_assets = require("../../utils/assets.js");
const api_system_agreement = require("../../api/system/agreement.js");
const api_auth_index = require("../../api/auth/index.js");
const _sfc_main = {
  data() {
    return {
      assets: utils_assets.assets,
      isAgreed: false,
      activeModal: "",
      avatarBase64: "",
      userName: "微信用户",
      currentProtocol: { title: "", content: "" },
      protocols: {
        user: { title: "", content: "" },
        privacy: { title: "", content: "" }
      },
      openId: "",
      unionId: "",
      phoneNumber: ""
    };
  },
  methods: {
    toggleAgreed() {
      this.isAgreed = !this.isAgreed;
    },
    startLoginFlow() {
      if (!this.isAgreed)
        this.activeModal = "confirm";
      else
        this.performLogin();
    },
    agreeAndClose() {
      this.isAgreed = true;
      this.activeModal = "";
    },
    async performLogin() {
      try {
        common_vendor.index.showLoading({ title: "登录中..." });
        const loginRes = await new Promise((resolve, reject) => {
          common_vendor.wx$1.login({
            success: resolve,
            fail: reject
          });
        });
        if (!loginRes.code) {
          common_vendor.index.hideLoading();
          common_vendor.index.showToast({ title: "获取登录凭证失败", icon: "none" });
          return;
        }
        const res = await api_auth_index.wechatLogin({ loginCode: loginRes.code });
        common_vendor.index.hideLoading();
        if (res.data && res.data.accessToken) {
          common_vendor.index.setStorageSync("token", res.data.accessToken);
          common_vendor.index.setStorageSync("isLogin", true);
          common_vendor.index.showToast({ title: "登录成功", icon: "success" });
          setTimeout(() => {
            common_vendor.index.reLaunch({ url: "/pages/order/index" });
          }, 1e3);
        } else if (res.data && res.data.openid) {
          this.openId = res.data.openid;
          this.unionId = res.data.unionid || "";
          this.activeModal = "profile";
        } else {
          common_vendor.index.showToast({ title: "登录失败", icon: "none" });
        }
      } catch (error) {
        common_vendor.index.hideLoading();
        common_vendor.index.__f__("error", "at pages/login/index.vue:196", "登录错误：", error);
        common_vendor.index.showToast({ title: "登录失败，请重试", icon: "none" });
      }
    },
    onChooseAvatar(e) {
      const tempPath = e.detail.avatarUrl;
      common_vendor.index.__f__("log", "at pages/login/index.vue:202", "[微信信息] 头像临时路径:", tempPath);
      const fs = common_vendor.index.getFileSystemManager();
      fs.readFile({
        filePath: tempPath,
        encoding: "base64",
        success: (res) => {
          common_vendor.index.__f__("log", "at pages/login/index.vue:208", "[微信信息] 头像 base64 长度:", res.data.length);
          this.avatarBase64 = "data:image/jpeg;base64," + res.data;
          common_vendor.index.__f__("log", "at pages/login/index.vue:210", "[微信信息] avatarBase64 已赋值，前缀:", this.avatarBase64.substring(0, 30));
        },
        fail: (err) => {
          common_vendor.index.__f__("error", "at pages/login/index.vue:213", "[微信信息] 读取头像 base64 失败:", err);
        }
      });
    },
    onNicknameBlur(e) {
      this.userName = e.detail.value;
      common_vendor.index.__f__("log", "at pages/login/index.vue:219", "[微信信息] 昵称(blur):", this.userName);
    },
    onNicknameChange(e) {
      this.userName = e.detail.value;
      common_vendor.index.__f__("log", "at pages/login/index.vue:223", "[微信信息] 昵称(input):", this.userName);
    },
    async goToPhoneAuth() {
      this.phoneNumber = "";
      this.activeModal = "phone";
    },
    async handleGetPhoneNumber(e) {
      if (e.detail.errMsg !== "getPhoneNumber:ok") {
        common_vendor.index.showToast({ title: "获取手机号失败，请重试", icon: "none" });
        return;
      }
      try {
        common_vendor.index.showLoading({ title: "获取手机号中..." });
        const phoneRes = await api_auth_index.getWechatPhone({
          phoneCode: e.detail.code,
          openId: this.openId
        });
        common_vendor.index.hideLoading();
        this.phoneNumber = phoneRes.data;
        common_vendor.index.showLoading({ title: "注册中..." });
        const registerRes = await api_auth_index.wechatRegister({
          openId: this.openId,
          unionId: this.unionId,
          phone: this.phoneNumber,
          nickname: this.userName,
          avatar: this.avatarBase64
        });
        common_vendor.index.hideLoading();
        this.performLogin();
      } catch (error) {
        common_vendor.index.hideLoading();
        common_vendor.index.__f__("error", "at pages/login/index.vue:262", "注册错误：", error);
        common_vendor.index.showToast({ title: "注册失败，请重试", icon: "none" });
      }
    },
    showProtocol(type) {
      this.currentProtocol = this.protocols[type];
      this.activeModal = "protocol";
    },
    closeAllModals() {
      this.activeModal = "";
    }
  },
  async mounted() {
    try {
      const [userRes, privacyRes] = await Promise.all([
        api_system_agreement.getAgreement(1),
        api_system_agreement.getAgreement(2)
      ]);
      this.protocols.user = { title: userRes.data.title, content: userRes.data.content };
      this.protocols.privacy = { title: privacyRes.data.title, content: privacyRes.data.content };
    } catch (e) {
      common_vendor.index.__f__("error", "at pages/login/index.vue:281", "[协议] 加载失败", e);
    }
  }
};
function _sfc_render(_ctx, _cache, $props, $setup, $data, $options) {
  return common_vendor.e({
    a: $data.assets.logo,
    b: $data.assets.wechat,
    c: common_vendor.o((...args) => $options.startLoginFlow && $options.startLoginFlow(...args), "ec"),
    d: $data.isAgreed,
    e: common_vendor.o(($event) => $options.showProtocol("user"), "a3"),
    f: common_vendor.o(($event) => $options.showProtocol("privacy"), "86"),
    g: common_vendor.o((...args) => $options.toggleAgreed && $options.toggleAgreed(...args), "56"),
    h: $data.activeModal
  }, $data.activeModal ? {
    i: common_vendor.o((...args) => $options.closeAllModals && $options.closeAllModals(...args), "a3")
  } : {}, {
    j: $data.activeModal === "confirm"
  }, $data.activeModal === "confirm" ? {
    k: common_vendor.o(($event) => $data.activeModal = "", "e1"),
    l: common_vendor.o((...args) => $options.agreeAndClose && $options.agreeAndClose(...args), "1f")
  } : {}, {
    m: $data.activeModal === "profile"
  }, $data.activeModal === "profile" ? {
    n: common_vendor.o(($event) => $data.activeModal = "", "c9"),
    o: common_vendor.o(($event) => $data.activeModal = "phone", "14"),
    p: $data.avatarBase64 || "https://img.icons8.com/color/144/user.png",
    q: common_vendor.o((...args) => $options.onChooseAvatar && $options.onChooseAvatar(...args), "14"),
    r: $data.userName,
    s: common_vendor.o((...args) => $options.onNicknameBlur && $options.onNicknameBlur(...args), "60"),
    t: common_vendor.o((...args) => $options.onNicknameChange && $options.onNicknameChange(...args), "df"),
    v: common_vendor.o((...args) => $options.goToPhoneAuth && $options.goToPhoneAuth(...args), "65")
  } : {}, {
    w: $data.activeModal === "phone"
  }, $data.activeModal === "phone" ? {
    x: $data.assets.logo,
    y: common_vendor.t($data.phoneNumber || "获取中..."),
    z: common_vendor.o(($event) => $data.activeModal = "", "8c"),
    A: common_vendor.o((...args) => $options.handleGetPhoneNumber && $options.handleGetPhoneNumber(...args), "f8")
  } : {}, {
    B: $data.activeModal === "protocol"
  }, $data.activeModal === "protocol" ? {
    C: common_vendor.t($data.currentProtocol.title),
    D: common_vendor.o(($event) => $data.activeModal = "", "d4"),
    E: $data.currentProtocol.content,
    F: common_vendor.o(($event) => $data.activeModal = "", "7e")
  } : {});
}
const MiniProgramPage = /* @__PURE__ */ common_vendor._export_sfc(_sfc_main, [["render", _sfc_render], ["__scopeId", "data-v-d08ef7d4"]]);
wx.createPage(MiniProgramPage);
//# sourceMappingURL=../../../.sourcemap/mp-weixin/pages/login/index.js.map
