修复bug

This commit is contained in:
2026-05-22 11:36:25 +08:00
parent 0df38bd544
commit 5cc680f26d

View File

@@ -13,6 +13,7 @@
手机号快捷登录 手机号快捷登录
</button> </button>
<!-- 恢复v-model同时保留防错乱逻辑 -->
<view class="user-type"> <view class="user-type">
<u-radio-group v-model="loginType" active-color="#EA414A" shape="square"> <u-radio-group v-model="loginType" active-color="#EA414A" shape="square">
<u-radio name="0">个人</u-radio> <u-radio name="0">个人</u-radio>
@@ -38,7 +39,7 @@
export default { export default {
data() { data() {
return { return {
loginType: "0", loginType: "0", // 默认个人,恢复默认选中
agreeProtocol: false agreeProtocol: false
}; };
}, },
@@ -48,22 +49,18 @@
} }
}, },
methods: { methods: {
// 微信获取手机号(修复版)
async onGetPhone(e) { async onGetPhone(e) {
// 用户拒绝
if (e.detail.errMsg !== "getPhoneNumber:ok") { if (e.detail.errMsg !== "getPhoneNumber:ok") {
this.$mytip.toast("已取消授权"); this.$mytip.toast("已取消授权");
return; return;
} }
// 必须勾选协议
if (!this.agreeProtocol) { if (!this.agreeProtocol) {
this.$mytip.toast("请先同意用户协议与隐私政策"); this.$mytip.toast("请先同意用户协议与隐私政策");
return; return;
} }
try { try {
// 1. 先拿微信登录 code
const loginRes = await new Promise((resolve, reject) => { const loginRes = await new Promise((resolve, reject) => {
uni.login({ uni.login({
provider: "weixin", provider: "weixin",
@@ -72,12 +69,14 @@
}); });
}); });
// 2. 传给后端登录 // 🔥 关键:登录瞬间快照当前类型,防异步错乱
const currentType = this.loginType;
await this.doLogin({ await this.doLogin({
loginCode: loginRes.code, loginCode: loginRes.code,
encryptedData: e.detail.encryptedData, encryptedData: e.detail.encryptedData,
iv: e.detail.iv, iv: e.detail.iv,
loginType: this.loginType loginType: currentType
}); });
} catch (err) { } catch (err) {
@@ -85,12 +84,10 @@
} }
}, },
// 登录接口(修复:用户信息 + 全局刷新)
async doLogin(data) { async doLogin(data) {
uni.showLoading({ title: "登录中...", mask: true }); uni.showLoading({ title: "登录中...", mask: true });
try { try {
// 1. 登录拿 token
const authRes = await this.$u.post("/login/weChatLogin", data); const authRes = await this.$u.post("/login/weChatLogin", data);
const token = authRes.data; const token = authRes.data;
@@ -100,17 +97,14 @@
return; return;
} }
// 2. 保存 token全局
this.$u.vuex("vuex_token", token); this.$u.vuex("vuex_token", token);
// 3. 获取并保存用户信息(修复:必须 await 等它完成) // 🔥 用快照的类型不用this.loginType防切换错乱
await this.getUserOtherInfo(this.loginType, token); await this.getUserOtherInfo(data.loginType, token);
// 4. 登录成功 → 强制刷新全局状态 + 跳首页
uni.hideLoading(); uni.hideLoading();
this.$mytip.toast("登录成功"); this.$mytip.toast("登录成功");
// 🔥 关键:强制刷新页面,解决用户信息不显示
setTimeout(() => { setTimeout(() => {
uni.switchTab({ uni.switchTab({
url: "/pages/index/index", url: "/pages/index/index",
@@ -127,7 +121,6 @@
} }
}, },
// 获取用户信息(修复:必须返回 Promise
getUserOtherInfo(loginType, token) { getUserOtherInfo(loginType, token) {
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
this.$u.get("/login/userInfo", {}, { this.$u.get("/login/userInfo", {}, {
@@ -135,10 +128,7 @@
"USERTYPE": loginType "USERTYPE": loginType
}).then(obj => { }).then(obj => {
if (obj?.flag) { if (obj?.flag) {
// 🔥 保存用户信息到缓存
uni.setStorageSync('userInfo', obj.data); uni.setStorageSync('userInfo', obj.data);
// 🔥 同时存入 vuex 全局状态(首页就能实时刷新)
this.$u.vuex('vuex_userInfo', obj.data); this.$u.vuex('vuex_userInfo', obj.data);
} }
resolve(); resolve();
@@ -148,12 +138,10 @@
}); });
}, },
// 返回
goBack() { goBack() {
uni.navigateBack({ delta: 1 }); uni.navigateBack({ delta: 1 });
}, },
// 协议
goPrivacy(type) { goPrivacy(type) {
const url = type === "user" const url = type === "user"
? "/pages-biz/privacy/userAgreement" ? "/pages-biz/privacy/userAgreement"