"use strict"; const common_vendor = require("../../common/vendor.js"); const common_assets = require("../../common/assets.js"); const _sfc_main = { data() { return { isAgreed: false, showServiceAgreement: false, showPrivacyPolicy: false }; }, methods: { // 处理协议勾选 handleAgreementChange(e) { this.isAgreed = e.detail.value.length > 0; }, // 微信登录 async handleWechatLogin() { if (!this.isAgreed) { common_vendor.index.showToast({ title: "请先同意用户协议和隐私政策", icon: "none" }); return; } try { const loginResult = await new Promise((resolve, reject) => { common_vendor.index.login({ provider: "weixin", success: (res) => resolve(res), fail: (err) => reject(err) }); }); if (!loginResult.code) { common_vendor.index.showToast({ title: "获取登录凭证失败", icon: "none" }); return; } common_vendor.index.__f__("log", "at pages/login/index.vue:88", "获取到的code:", loginResult.code); const userInfoResult = await new Promise((resolve, reject) => { common_vendor.index.getUserInfo({ provider: "weixin", withCredentials: true, success: (res) => resolve(res), fail: (err) => reject(err) }); }); if (!userInfoResult.userInfo) { common_vendor.index.showToast({ title: "获取用户信息失败", icon: "none" }); return; } const loginRes = await common_vendor.index.request({ url: "http://localhost:9527/api/wx-login", method: "POST", data: { code: loginResult.code, nickName: userInfoResult.userInfo.nickName, avatarUrl: userInfoResult.userInfo.avatarUrl, gender: userInfoResult.userInfo.gender.toString(), country: userInfoResult.userInfo.country, province: userInfoResult.userInfo.province, city: userInfoResult.userInfo.city, language: userInfoResult.userInfo.language, rawData: userInfoResult.rawData, signature: userInfoResult.signature, encryptedData: userInfoResult.encryptedData, iv: userInfoResult.iv }, header: { "content-type": "application/json" } }); if (loginRes.data.errcode === 40029) { common_vendor.index.__f__("error", "at pages/login/index.vue:130", "微信登录code无效,尝试重新获取"); const newLoginResult = await new Promise((resolve, reject) => { common_vendor.index.login({ provider: "weixin", success: (res) => resolve(res), fail: (err) => reject(err) }); }); if (!newLoginResult.code) { throw new Error("重新获取登录凭证失败"); } const retryRes = await common_vendor.index.request({ url: "http://localhost:9527/api/wx-login", method: "POST", data: { code: newLoginResult.code, nickName: userInfoResult.userInfo.nickName, avatarUrl: userInfoResult.userInfo.avatarUrl, gender: userInfoResult.userInfo.gender.toString(), country: userInfoResult.userInfo.country, province: userInfoResult.userInfo.province, city: userInfoResult.userInfo.city, language: userInfoResult.userInfo.language, rawData: userInfoResult.rawData, signature: userInfoResult.signature, encryptedData: userInfoResult.encryptedData, iv: userInfoResult.iv }, header: { "content-type": "application/json" } }); if (retryRes.statusCode === 200 && !retryRes.data.errcode) { common_vendor.index.setStorageSync("token", retryRes.data.data.token); common_vendor.index.setStorageSync("userInfo", retryRes.data.data.userInfo); common_vendor.index.switchTab({ url: "/pages/discover/index" }); } else { throw new Error(retryRes.data.errmsg || "登录失败"); } } else if (loginRes.statusCode === 200 && !loginRes.data.errcode) { common_vendor.index.setStorageSync("token", loginRes.data.data.token); common_vendor.index.setStorageSync("userInfo", loginRes.data.data.userInfo); common_vendor.index.switchTab({ url: "/pages/discover/index" }); } else { throw new Error(loginRes.data.errmsg || "登录失败"); } } catch (error) { common_vendor.index.__f__("error", "at pages/login/index.vue:192", "登录过程出错:", error); common_vendor.index.showToast({ title: error.message || "登录失败,请重试", icon: "none" }); } }, // 跳转到验证码登录页面 navigateToVerifyCodeLogin() { common_vendor.index.navigateTo({ url: "/pages/login/verify-code" }); }, // 打开服务协议 openServiceAgreement() { common_vendor.index.navigateTo({ url: "/pages/agreement/service" }); }, // 打开隐私政策 openPrivacyPolicy() { common_vendor.index.navigateTo({ url: "/pages/agreement/privacy" }); } } }; function _sfc_render(_ctx, _cache, $props, $setup, $data, $options) { return { a: common_assets._imports_0, b: common_vendor.o((...args) => $options.handleWechatLogin && $options.handleWechatLogin(...args)), c: common_vendor.o((...args) => $options.navigateToVerifyCodeLogin && $options.navigateToVerifyCodeLogin(...args)), d: $data.isAgreed, e: common_vendor.o((...args) => $options.openServiceAgreement && $options.openServiceAgreement(...args)), f: common_vendor.o((...args) => $options.openPrivacyPolicy && $options.openPrivacyPolicy(...args)), g: common_vendor.o((...args) => $options.handleAgreementChange && $options.handleAgreementChange(...args)) }; } const MiniProgramPage = /* @__PURE__ */ common_vendor._export_sfc(_sfc_main, [["render", _sfc_render]]); wx.createPage(MiniProgramPage); //# sourceMappingURL=../../../.sourcemap/mp-weixin/pages/login/index.js.map