71 lines
2.2 KiB
JavaScript
71 lines
2.2 KiB
JavaScript
const app = getApp()
|
|
const { request } = require('../../utils/request')
|
|
|
|
Page({
|
|
data: {
|
|
hasPhone: false
|
|
},
|
|
onLoad() {
|
|
this.checkStatus();
|
|
},
|
|
checkStatus() {
|
|
// If the user is already here, we check if they are valid.
|
|
// However, app.js handles the redirection for existing users.
|
|
// If we are here, it means either app.js sent us here (New User),
|
|
// or app.js hasn't finished checking yet,
|
|
// or the user manually navigated here (unlikely).
|
|
|
|
const user = app.globalData.userInfo;
|
|
if (user) {
|
|
// We can't rely on is_new_user here because we don't persist it in globalData (though we could).
|
|
// But based on new logic:
|
|
// If user exists, app.js wouldn't redirect here.
|
|
// If we are here, user probably needs to authorize.
|
|
|
|
// Update UI state
|
|
const hasPhone = !!user.phone;
|
|
|
|
this.setData({
|
|
hasPhone: hasPhone
|
|
})
|
|
|
|
// If somehow we ended up here but profile is full, go home
|
|
if (hasPhone) {
|
|
wx.switchTab({ url: '/pages/index/index' })
|
|
}
|
|
} else {
|
|
// Wait for app.js login callback
|
|
app.loginCallback = (user) => {
|
|
// Re-run check
|
|
this.checkStatus();
|
|
// If app.js determines it's an existing user, it might redirect.
|
|
// But if not, we update UI here.
|
|
}
|
|
}
|
|
},
|
|
getPhoneNumber(e) {
|
|
console.log('getPhoneNumber', e)
|
|
if (e.detail.errMsg === 'getPhoneNumber:ok') {
|
|
request({
|
|
url: '/user/phone/',
|
|
method: 'POST',
|
|
data: {
|
|
code: e.detail.code
|
|
}
|
|
}).then(res => {
|
|
console.log('Get phone success', res)
|
|
if (res.phone) {
|
|
app.globalData.userInfo = { ...app.globalData.userInfo, ...res }
|
|
this.setData({ hasPhone: true })
|
|
wx.switchTab({ url: '/pages/index/index' })
|
|
}
|
|
}).catch(err => {
|
|
console.error('Get phone failed', err)
|
|
wx.showToast({ title: '获取手机号失败', icon: 'none' })
|
|
})
|
|
} else {
|
|
wx.showToast({ title: '需要授权手机号才能继续', icon: 'none' })
|
|
}
|
|
}
|
|
})
|