"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 api_resource_oss = require("../../api/resource/oss.js"); const _sfc_main = { data() { return { assets: utils_assets.assets, isAgreed: false, activeModal: "", avatarOssId: null, avatarPreviewUrl: "", 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.access_token) { common_vendor.index.setStorageSync("token", res.data.access_token); 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:190", "登录错误:", error); common_vendor.index.showToast({ title: "登录失败,请重试", icon: "none" }); } }, async onChooseAvatar(e) { const tempPath = e.detail.avatarUrl; common_vendor.index.__f__("log", "at pages/login/index.vue:196", "[微信信息] 头像临时路径:", tempPath); this.avatarPreviewUrl = tempPath; try { common_vendor.index.showLoading({ title: "上传头像..." }); const res = await api_resource_oss.uploadFile(tempPath); common_vendor.index.hideLoading(); this.avatarOssId = res.ossId; this.avatarPreviewUrl = res.url; common_vendor.index.__f__("log", "at pages/login/index.vue:204", "[微信信息] 头像OSS上传成功, ossId:", this.avatarOssId); } catch (err) { common_vendor.index.hideLoading(); common_vendor.index.__f__("error", "at pages/login/index.vue:207", "[微信信息] 头像上传失败:", err); common_vendor.index.showToast({ title: "头像上传失败", icon: "none" }); } }, onNicknameBlur(e) { this.userName = e.detail.value; common_vendor.index.__f__("log", "at pages/login/index.vue:213", "[微信信息] 昵称(blur):", this.userName); }, onNicknameChange(e) { this.userName = e.detail.value; common_vendor.index.__f__("log", "at pages/login/index.vue:217", "[微信信息] 昵称(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.avatarOssId }); common_vendor.index.hideLoading(); this.performLogin(); } catch (error) { common_vendor.index.hideLoading(); common_vendor.index.__f__("error", "at pages/login/index.vue:256", "注册错误:", 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:275", "[协议] 加载失败", 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.avatarPreviewUrl || "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), "64"), t: common_vendor.o((...args) => $options.onNicknameChange && $options.onNicknameChange(...args), "dc"), v: common_vendor.o((...args) => $options.goToPhoneAuth && $options.goToPhoneAuth(...args), "c4") } : {}, { w: $data.activeModal === "phone" }, $data.activeModal === "phone" ? { x: $data.assets.logo, y: common_vendor.o(($event) => $data.activeModal = "", "9a"), z: common_vendor.o((...args) => $options.handleGetPhoneNumber && $options.handleGetPhoneNumber(...args), "c5") } : {}, { A: $data.activeModal === "protocol" }, $data.activeModal === "protocol" ? { B: common_vendor.t($data.currentProtocol.title), C: common_vendor.o(($event) => $data.activeModal = "", "4d"), D: $data.currentProtocol.content, E: common_vendor.o(($event) => $data.activeModal = "", "4e") } : {}); } 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