修复bug
This commit is contained in:
@@ -13,6 +13,7 @@
|
||||
手机号快捷登录
|
||||
</button>
|
||||
|
||||
<!-- 恢复v-model,同时保留防错乱逻辑 -->
|
||||
<view class="user-type">
|
||||
<u-radio-group v-model="loginType" active-color="#EA414A" shape="square">
|
||||
<u-radio name="0">个人</u-radio>
|
||||
@@ -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"
|
||||
|
||||
Reference in New Issue
Block a user