基本重构完成
This commit is contained in:
@@ -198,7 +198,9 @@ Page({
|
||||
})
|
||||
.then((data) => {
|
||||
if (data.code === 0) {
|
||||
app.globalData.user = data.data.user
|
||||
// 与 profileedit 同一处坑:/user/update 的响应不含 security.token,
|
||||
// 整替会抹掉本机 token,security 必须保留本地那份
|
||||
app.globalData.user = { ...user, ...data.data.user, security: user.security || {} }
|
||||
wx.showToast({ title: '保存成功', icon: 'success' })
|
||||
} else {
|
||||
wx.showToast({ title: data.msg || '保存失败', icon: 'none' })
|
||||
|
||||
+30
-26
@@ -230,33 +230,37 @@ Page({
|
||||
},
|
||||
|
||||
handleLogin(e) {
|
||||
// 微信手机号 code 是一次性凭证(5 分钟内有效、只能用一次),只能在服务端消费一次。
|
||||
// 所以不能先调 /user/wxgetphonenumber 把它用掉、再在 wxSignin 里复用——
|
||||
// 那样第二次向微信换号必然报 invalid code。
|
||||
// wxSignin 内部已做可信的服务端换号(api/user/handler/users.js),直接交给它即可。
|
||||
const phoneCode = e.detail.code ? e.detail.code : ''
|
||||
API.user.wxGetPhoneNumber({ code: phoneCode })
|
||||
.then((data) => {
|
||||
const phoneNumber = data.data.phoneNumber
|
||||
wx.setStorageSync('user_phonenumber', phoneNumber)
|
||||
wx.login({
|
||||
success: (res) => {
|
||||
if (res.code) {
|
||||
API.user.wxSignin({ phoneNumber, phoneCode, code: res.code })
|
||||
.then((data) => {
|
||||
if (data.code == 0) {
|
||||
const app = getApp()
|
||||
app.globalData.user = data.data.user
|
||||
// 用户重新授权登录,解除对静默重登的拦截
|
||||
wx.removeStorageSync('has_logout')
|
||||
this.loadUserInfo()
|
||||
}
|
||||
})
|
||||
} else {
|
||||
console.log('登录失败!' + res.errMsg)
|
||||
wx.login({
|
||||
success: (res) => {
|
||||
if (!res.code) {
|
||||
console.log('登录失败!' + res.errMsg)
|
||||
return
|
||||
}
|
||||
API.user.wxSignin({ phoneCode, code: res.code })
|
||||
.then((data) => {
|
||||
if (data.code !== 0) {
|
||||
// 后端失败(如 code 过期、该手机号已注册等)此前被静默吞掉,
|
||||
// 用户点了授权却毫无反馈,这里补一个提示
|
||||
wx.showToast({ title: data.msg || '登录失败', icon: 'none' })
|
||||
return
|
||||
}
|
||||
}
|
||||
})
|
||||
})
|
||||
.catch((err) => {
|
||||
wx.showToast({ title: '网络请求失败', icon: 'none' })
|
||||
console.log('请求失败', err)
|
||||
})
|
||||
const app = getApp()
|
||||
app.globalData.user = data.data.user
|
||||
wx.setStorageSync('user_phonenumber', data.data.user?.profile?.mobile || '')
|
||||
// 用户重新授权登录,解除对静默重登的拦截
|
||||
wx.removeStorageSync('has_logout')
|
||||
this.loadUserInfo()
|
||||
})
|
||||
.catch((err) => {
|
||||
wx.showToast({ title: '网络请求失败', icon: 'none' })
|
||||
console.log('请求失败', err)
|
||||
})
|
||||
}
|
||||
})
|
||||
}
|
||||
})
|
||||
@@ -130,8 +130,11 @@ Page({
|
||||
API.user.update(updateData)
|
||||
.then((data) => {
|
||||
if (data.code === 0) {
|
||||
// 回写全局对象:「我的」页 onShow 依赖它刷新头像行与资料
|
||||
app.globalData.user = data.data.user
|
||||
// 回写全局对象:「我的」页 onShow 依赖它刷新头像行与资料。
|
||||
// 不能整替——/user/update 的响应是脱敏用户(security.token 已被删掉),
|
||||
// 整替会把本机 token 抹掉,之后所有接口都变成未登录。security 保留本地那份
|
||||
const prev = app.globalData.user || {}
|
||||
app.globalData.user = { ...prev, ...data.data.user, security: prev.security || {} }
|
||||
wx.showToast({ title: '保存成功', icon: 'success' })
|
||||
// 返回上一页(「我的」),由它的 onShow 重新读取全局对象
|
||||
wx.navigateBack()
|
||||
|
||||
Reference in New Issue
Block a user