小程序修正

This commit is contained in:
tianmo committed 2026-09-29 20:51:39 +08:00
1 parent 5a92bab835
commit 6ae4c109ce
12 files changed
+232 -62

No files matched your search

@@ -41,21 +41,37 @@ Page({
});
},
/**
* 登录 / 注册。
*
* 关键:不能凭本地缓存决定昵称。
* 退出登录会清空本地缓存,此时若直接生成随机昵称并提交,
* 会把老用户在服务端的真实昵称覆盖掉。正确做法是先取服务端资料:
* - 已有昵称 → 老用户,原样保留,不提交任何修改
* - 没有昵称 → 首次注册,生成一个随机昵称
*/
onLoginBtnClick() {
const nickname = this.data.userInfo.nickName || randomNickname();
wx.showLoading({ title: '请稍候', mask: true });
// getMe 在未登录时会自动完成静默登录,无需手动判断 token
api
.updateMe({ nickname: nickname })
.getMe()
.then((user) => {
store.setUser(user);
if (user.nickname) return user;
return api.updateMe({ nickname: randomNickname() }).then((created) => {
store.setUser(created);
return created;
});
})
.then(() => {
wx.hideLoading();
wx.showToast({ title: '成功', icon: 'success', mask: true, duration: 1500 });
setTimeout(() => wx.switchTab({ url: '/pages/wode/wode' }), 1500);
})
.catch((err) => {
wx.hideLoading();
wx.showToast({ title: err.message || '登录失败', icon: 'none', mask: true });
wx.showToast({ title: (err && err.message) || '登录失败', icon: 'none', mask: true });
});
},
@@ -76,4 +92,3 @@ Page({
});
},
});