fix:修复微信登录的错误

This commit is contained in:
tsui110 2026-01-02 16:40:48 +08:00
parent a8fa8bf557
commit 152fe14aab
2 changed files with 46 additions and 7 deletions

View File

@ -174,7 +174,8 @@
import { ref, computed, onMounted, onUnmounted } from 'vue' import { ref, computed, onMounted, onUnmounted } from 'vue'
import { onLoad } from '@dcloudio/uni-app' import { onLoad } from '@dcloudio/uni-app'
import { request } from '../../utils/request' import { request } from '../../utils/request'
import { wechatLogin, douyinLogin, bindPhone, bindDouyinPhone, getUserStats, getPointsBalance, sendSmsCode, smsLogin } from '../../api/appUser' import { wechatLogin, douyinLogin, bindPhone, bindDouyinPhone, getUserStats, getPointsBalance, sendSmsCode, smsLogin, getUserInfo } from '../../api/appUser'
import { authRequest } from '../../utils/request'
import { vibrateShort } from '@/utils/vibrate.js' import { vibrateShort } from '@/utils/vibrate.js'
const loading = ref(false) const loading = ref(false)
@ -401,16 +402,36 @@ function onGetPhoneNumber(e) {
const inviterCode = uni.getStorageSync('inviter_code') const inviterCode = uni.getStorageSync('inviter_code')
const data = await wechatLogin(res.code, inviterCode) const data = await wechatLogin(res.code, inviterCode)
saveUserData(data) //
// ()
const isBound = data.phone || data.phone_number || data.mobile const isBound = data.phone || data.phone_number || data.mobile
console.log('[DEBUG] 微信登录返回数据手机号状态:', isBound ? '已绑定' : '未绑定', data)
//
if (!isBound) { if (!isBound) {
try { try {
console.log('[DEBUG] 开始绑定手机号')
await bindPhone(data.user_id, phoneCode) await bindPhone(data.user_id, phoneCode)
} catch (e) {} //
try {
const updatedUserInfo = await authRequest({ url: '/api/app/users/info', method: 'GET' })
console.log('[DEBUG] 绑定后从服务器获取用户信息:', updatedUserInfo)
// data
if (updatedUserInfo) {
Object.assign(data, updatedUserInfo)
}
} catch (err) {
console.warn('[DEBUG] 绑定后获取用户信息失败:', err)
}
} catch (e) {
console.error('[DEBUG] 绑定手机号失败:', e)
}
} else {
console.log('[DEBUG] 微信登录已包含手机号,跳过绑定')
} }
//
saveUserData(data)
// //
fetchExtraData(data.user_id) fetchExtraData(data.user_id)
@ -493,15 +514,22 @@ function saveUserData(data) {
if (data.invite_code) uni.setStorageSync('invite_code', data.invite_code) if (data.invite_code) uni.setStorageSync('invite_code', data.invite_code)
if (data.mobile) uni.setStorageSync('last_login_mobile', data.mobile) if (data.mobile) uni.setStorageSync('last_login_mobile', data.mobile)
// //
const hasPhone = data.mobile || data.phone || data.phone_number const hasPhone = data.mobile || data.phone || data.phone_number
console.log('[DEBUG] 检查手机号绑定状态:', hasPhone ? '已绑定' : '未绑定') console.log('[DEBUG] 检查手机号绑定状态:', {
mobile: data.mobile,
phone: data.phone,
phone_number: data.phone_number,
hasPhone: hasPhone ? '已绑定' : '未绑定'
})
// phone_bound // phone_bound
if (hasPhone) { if (hasPhone) {
uni.setStorageSync('phone_bound', true) uni.setStorageSync('phone_bound', true)
console.log('[DEBUG] 已设置 phone_bound = true')
} else { } else {
uni.setStorageSync('phone_bound', false) uni.setStorageSync('phone_bound', false)
console.log('[DEBUG] 已设置 phone_bound = false')
} }
// openid ( openid) // openid ( openid)
@ -526,6 +554,8 @@ function saveUserData(data) {
loginMode.value = 'sms' loginMode.value = 'sms'
} }
}) })
} else {
console.log('[DEBUG] 已检测到手机号,正常登录流程')
} }
} }

View File

@ -6,14 +6,23 @@
export function checkPhoneBound() { export function checkPhoneBound() {
// 获取用户信息 // 获取用户信息
const userInfo = uni.getStorageSync('user_info') || {} const userInfo = uni.getStorageSync('user_info') || {}
console.log('[checkPhoneBound] 检查用户信息:', {
mobile: userInfo.mobile,
phone: userInfo.phone,
phone_number: userInfo.phone_number,
userInfoKeys: Object.keys(userInfo)
})
const mobile = userInfo.mobile || userInfo.phone || userInfo.phone_number || '' const mobile = userInfo.mobile || userInfo.phone || userInfo.phone_number || ''
// 如果已绑定手机号,直接返回 // 如果已绑定手机号,直接返回
if (mobile) { if (mobile) {
console.log('[checkPhoneBound] 已检测到手机号,允许通过:', mobile)
return true return true
} }
// 未绑定手机号,显示提示并跳转 // 未绑定手机号,显示提示并跳转
console.warn('[checkPhoneBound] 未检测到手机号,提示用户绑定')
uni.showModal({ uni.showModal({
title: '需要绑定手机号', title: '需要绑定手机号',
content: '为了账号安全,请先绑定手机号', content: '为了账号安全,请先绑定手机号',