Files
MBTI_wang/miniprogram/pages/phone-auth/index.js
Ghost d07235b64c feat: 小程序 AI 底栏与聊天页、出站推送调试与 tabbar 迁移
- 自定义 tabBar:AI 入口图标与样式,相关页面与审核门控调整

- API:MpTabbar、出站推送 Hook、后台设置与迁移 SQL(含 FAB 居中排序)

- 管理端:推送 Hook 面板、出站推送调试工具与生产环境注释说明

Made-with: Cursor
2026-04-20 15:29:10 +08:00

75 lines
2.6 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

// 手机号授权页:用户点击按钮授权后,用 code 换手机号并写回 userInfo再返回或跳转 next
const app = getApp()
Page({
data: {
next: '',
},
onLoad(options) {
this.setData({
next: options.next ? decodeURIComponent(options.next) : '',
})
},
onGetPhoneNumber(e) {
const { code, errMsg } = e.detail || {}
if (errMsg && errMsg.indexOf('getPhoneNumber:fail') === 0) {
wx.showToast({ title: '需要授权手机号才能继续', icon: 'none' })
return
}
if (!code) {
wx.showToast({ title: '获取手机号失败', icon: 'none' })
return
}
const token = app.globalData.token || wx.getStorageSync('token')
if (!token) {
wx.showToast({ title: '请先登录', icon: 'none' })
return
}
wx.showLoading({ title: '处理中...', mask: true })
wx.request({
url: `${app.globalData.apiBase.replace(/\/$/, '')}/api/auth/wechat/phone`,
method: 'POST',
header: {
'Authorization': 'Bearer ' + token,
'Content-Type': 'application/json',
},
data: { code },
success: (res) => {
wx.hideLoading()
if (res.statusCode === 200 && res.data && res.data.code === 200) {
const data = res.data.data || {}
const user = data.user || app.globalData.userInfo || {}
const phone = data.phone || user.phone || ''
const newUser = { ...user, phone }
app.globalData.userInfo = newUser
wx.setStorageSync('userInfo', newUser)
wx.showToast({ title: '授权成功', icon: 'success' })
const nextPath = this.data.next && this.data.next.startsWith('/') ? this.data.next : ''
const tabBarPaths = ['/pages/index/index', '/pages/index/camera', '/pages/ai-chat/index', '/pages/profile/index']
const isTabBar = tabBarPaths.some(p => nextPath === p || nextPath.startsWith(p + '?'))
if (nextPath) {
setTimeout(() => {
if (isTabBar) {
const pathOnly = nextPath.split('?')[0]
wx.switchTab({ url: pathOnly, fail: () => wx.navigateBack() })
} else {
wx.redirectTo({ url: nextPath, fail: () => wx.navigateBack() })
}
}, 500)
} else {
setTimeout(() => wx.navigateBack(), 500)
}
} else {
wx.showToast({ title: res.data && res.data.message ? res.data.message : '获取手机号失败', icon: 'none' })
}
},
fail: () => {
wx.hideLoading()
wx.showToast({ title: '网络请求失败', icon: 'none' })
},
})
},
})