From 5cc680f26d7bf8b6792549d152cc0bf0bd12f3df Mon Sep 17 00:00:00 2001 From: RuicyWu <1063154311@qq.com> Date: Fri, 22 May 2026 11:36:25 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E5=A4=8Dbug?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- pages-biz/login/login.vue | 28 ++++++++-------------------- 1 file changed, 8 insertions(+), 20 deletions(-) diff --git a/pages-biz/login/login.vue b/pages-biz/login/login.vue index 4395b0b..6e5ec34 100644 --- a/pages-biz/login/login.vue +++ b/pages-biz/login/login.vue @@ -13,6 +13,7 @@ 手机号快捷登录 + 个人 @@ -38,7 +39,7 @@ export default { data() { return { - loginType: "0", + loginType: "0", // 默认个人,恢复默认选中 agreeProtocol: false }; }, @@ -48,22 +49,18 @@ } }, methods: { - // 微信获取手机号(修复版) async onGetPhone(e) { - // 用户拒绝 if (e.detail.errMsg !== "getPhoneNumber:ok") { this.$mytip.toast("已取消授权"); return; } - // 必须勾选协议 if (!this.agreeProtocol) { this.$mytip.toast("请先同意用户协议与隐私政策"); return; } try { - // 1. 先拿微信登录 code const loginRes = await new Promise((resolve, reject) => { uni.login({ provider: "weixin", @@ -72,12 +69,14 @@ }); }); - // 2. 传给后端登录 + // 🔥 关键:登录瞬间快照当前类型,防异步错乱 + const currentType = this.loginType; + await this.doLogin({ loginCode: loginRes.code, encryptedData: e.detail.encryptedData, iv: e.detail.iv, - loginType: this.loginType + loginType: currentType }); } catch (err) { @@ -85,12 +84,10 @@ } }, - // 登录接口(修复:用户信息 + 全局刷新) async doLogin(data) { uni.showLoading({ title: "登录中...", mask: true }); try { - // 1. 登录拿 token const authRes = await this.$u.post("/login/weChatLogin", data); const token = authRes.data; @@ -100,17 +97,14 @@ return; } - // 2. 保存 token(全局) this.$u.vuex("vuex_token", token); - // 3. 获取并保存用户信息(修复:必须 await 等它完成) - await this.getUserOtherInfo(this.loginType, token); + // 🔥 用快照的类型,不用this.loginType,防切换错乱 + await this.getUserOtherInfo(data.loginType, token); - // 4. 登录成功 → 强制刷新全局状态 + 跳首页 uni.hideLoading(); this.$mytip.toast("登录成功"); - // 🔥 关键:强制刷新页面,解决用户信息不显示 setTimeout(() => { uni.switchTab({ url: "/pages/index/index", @@ -127,7 +121,6 @@ } }, - // 获取用户信息(修复:必须返回 Promise) getUserOtherInfo(loginType, token) { return new Promise((resolve, reject) => { this.$u.get("/login/userInfo", {}, { @@ -135,10 +128,7 @@ "USERTYPE": loginType }).then(obj => { if (obj?.flag) { - // 🔥 保存用户信息到缓存 uni.setStorageSync('userInfo', obj.data); - - // 🔥 同时存入 vuex 全局状态(首页就能实时刷新) this.$u.vuex('vuex_userInfo', obj.data); } resolve(); @@ -148,12 +138,10 @@ }); }, - // 返回 goBack() { uni.navigateBack({ delta: 1 }); }, - // 协议 goPrivacy(type) { const url = type === "user" ? "/pages-biz/privacy/userAgreement"