fix:移除错误的逻辑判断

This commit is contained in:
tsui110 2026-01-02 17:18:29 +08:00
parent 152fe14aab
commit 5cbd30fcb7
9 changed files with 172 additions and 92 deletions

View File

@ -1272,8 +1272,8 @@ async function onParticipate() {
const iid = currentIssueId.value || '' const iid = currentIssueId.value || ''
if (!aid || !iid) { uni.showToast({ title: '期数未选择', icon: 'none' }); return } if (!aid || !iid) { uni.showToast({ title: '期数未选择', icon: 'none' }); return }
const token = uni.getStorageSync('token') const token = uni.getStorageSync('token')
const phoneBound = !!uni.getStorageSync('phone_bound') const phoneNumber = uni.getStorageSync('phone_number')
if (!token || !phoneBound) { if (!token || !phoneNumber) {
uni.showModal({ uni.showModal({
title: '提示', title: '提示',
content: '请先登录并绑定手机号', content: '请先登录并绑定手机号',

View File

@ -228,8 +228,8 @@ function openPayment(count) {
pendingCount.value = times pendingCount.value = times
paymentAmount.value = (pricePerDraw.value * times).toFixed(2) paymentAmount.value = (pricePerDraw.value * times).toFixed(2)
const token = uni.getStorageSync('token') const token = uni.getStorageSync('token')
const phoneBound = !!uni.getStorageSync('phone_bound') const phoneNumber = uni.getStorageSync('phone_number')
if (!token || !phoneBound) { if (!token || !phoneNumber) {
uni.showModal({ uni.showModal({
title: '提示', title: '提示',
content: '请先登录并绑定手机号', content: '请先登录并绑定手机号',
@ -340,8 +340,8 @@ async function onMachineDraw(count) {
} }
const token = uni.getStorageSync('token') const token = uni.getStorageSync('token')
const phoneBound = !!uni.getStorageSync('phone_bound') const phoneNumber = uni.getStorageSync('phone_number')
if (!token || !phoneBound) { if (!token || !phoneNumber) {
uni.showModal({ uni.showModal({
title: '提示', title: '提示',
content: '请先登录并绑定手机号', content: '请先登录并绑定手机号',

View File

@ -88,14 +88,14 @@ const error = ref('')
async function fetchList() { async function fetchList() {
const user_id = uni.getStorageSync('user_id') const user_id = uni.getStorageSync('user_id')
const token = uni.getStorageSync('token') const token = uni.getStorageSync('token')
const phoneBound = !!uni.getStorageSync('phone_bound') const phoneNumber = uni.getStorageSync('phone_number')
// //
if (!user_id || !token) { if (!user_id || !token) {
// //
return return
} }
loading.value = true loading.value = true
error.value = '' error.value = ''
try { try {

View File

@ -163,18 +163,19 @@ function onGetPhoneNumber(e) {
if (user_info.invite_code) uni.setStorageSync('invite_code', user_info.invite_code) if (user_info.invite_code) uni.setStorageSync('invite_code', user_info.invite_code)
const openid = data && (data.openid || data.open_id) const openid = data && (data.openid || data.open_id)
if (openid) uni.setStorageSync('openid', openid) if (openid) uni.setStorageSync('openid', openid)
try { try {
await new Promise(r => setTimeout(r, 600)) await new Promise(r => setTimeout(r, 600))
const bindRes = await bindPhone(user_id, phoneCode, { 'X-Suppress-Auth-Modal': true }) const bindRes = await bindPhone(user_id, phoneCode, { 'X-Suppress-Auth-Modal': true })
const phoneNumber = (bindRes && (bindRes.phone || bindRes.phone_number || bindRes.mobile)) || '' const phoneNumber = (bindRes && (bindRes.phone || bindRes.phone_number || bindRes.mobile)) || ''
if (phoneNumber) uni.setStorageSync('phone_number', phoneNumber) if (phoneNumber) {
uni.setStorageSync('phone_number', phoneNumber)
console.log('[Invite Landing] 已缓存手机号:', phoneNumber)
}
} catch (bindErr) { } catch (bindErr) {
console.warn('Bind phone failed', bindErr) console.warn('Bind phone failed', bindErr)
} }
uni.setStorageSync('phone_bound', true)
try { try {
const stats = await getUserStats(user_id) const stats = await getUserStats(user_id)
uni.setStorageSync('user_stats', stats) uni.setStorageSync('user_stats', stats)

View File

@ -310,9 +310,9 @@ function filterOrders(items) {
async function fetchOrders(append) { async function fetchOrders(append) {
const user_id = uni.getStorageSync('user_id') const user_id = uni.getStorageSync('user_id')
const token = uni.getStorageSync('token') const token = uni.getStorageSync('token')
const phoneBound = !!uni.getStorageSync('phone_bound') const phoneNumber = uni.getStorageSync('phone_number')
if (!user_id || !token || !phoneBound) { if (!user_id || !token || !phoneNumber) {
uni.showModal({ uni.showModal({
title: '提示', title: '提示',
content: '请先登录并绑定手机号', content: '请先登录并绑定手机号',

View File

@ -106,8 +106,8 @@ function getActionText(action) {
async function fetchRecords(append = false) { async function fetchRecords(append = false) {
const user_id = uni.getStorageSync('user_id') const user_id = uni.getStorageSync('user_id')
const token = uni.getStorageSync('token') const token = uni.getStorageSync('token')
const phoneBound = !!uni.getStorageSync('phone_bound') const phoneNumber = uni.getStorageSync('phone_number')
if (!user_id || !token || !phoneBound) { if (!user_id || !token || !phoneNumber) {
uni.showModal({ uni.showModal({
title: '提示', title: '提示',
content: '请先登录并绑定手机号', content: '请先登录并绑定手机号',

View File

@ -347,30 +347,30 @@ async function handleSmsLogin() {
vibrateShort() vibrateShort()
return return
} }
if (!canLogin.value) return if (!canLogin.value) return
loading.value = true loading.value = true
try { try {
const inviterCode = uni.getStorageSync('inviter_code') const inviterCode = uni.getStorageSync('inviter_code')
const data = await smsLogin(mobile.value, smsCode.value, inviterCode) const data = await smsLogin(mobile.value, smsCode.value, inviterCode)
console.log('[DEBUG] 短信登录响应原始数据:', data) console.log('[DEBUG] 短信登录响应原始数据:', data)
saveUserData(data) saveUserData(data)
const isNew = data && data.is_new_user const isNew = data && data.is_new_user
uni.showToast({ uni.showToast({
title: isNew ? '🎉 注册成功!' : '✨ 登录成功!', title: isNew ? '🎉 注册成功!' : '✨ 登录成功!',
icon: 'none', icon: 'none',
duration: 1200 duration: 1200
}) })
// //
if (data && data.user_id) { if (data && data.user_id) {
fetchExtraData(data.user_id) fetchExtraData(data.user_id)
} }
setTimeout(() => uni.reLaunch({ url: '/pages/mine/index' }), 600) setTimeout(() => uni.reLaunch({ url: '/pages/mine/index' }), 600)
} catch (err) { } catch (err) {
uni.showToast({ title: err.message || '登录失败', icon: 'none' }) uni.showToast({ title: err.message || '登录失败', icon: 'none' })
@ -380,7 +380,7 @@ async function handleSmsLogin() {
} }
// //
function onGetPhoneNumber(e) { async function onGetPhoneNumber(e) {
if (!agreementChecked.value) { if (!agreementChecked.value) {
uni.showToast({ title: '请先同意用户协议', icon: 'none' }) uni.showToast({ title: '请先同意用户协议', icon: 'none' })
vibrateShort() vibrateShort()
@ -395,58 +395,145 @@ function onGetPhoneNumber(e) {
loading.value = true loading.value = true
uni.login({ try {
provider: 'weixin', // 1.
success: async (res) => { const token = uni.getStorageSync('token')
const phoneNumber = uni.getStorageSync('phone_number')
console.log('[DEBUG] 微信登录 - 检查登录状态:', {
hasToken: !!token,
hasPhoneNumber: !!phoneNumber,
phoneNumber: phoneNumber || '未找到'
})
let userId = null
// 2. token
if (token && phoneNumber) {
console.log('[DEBUG] 已登录且已绑定手机号,跳过登录流程')
userId = uni.getStorageSync('user_id')
//
uni.showToast({ title: '✨ 已登录!', icon: 'none', duration: 1200 })
setTimeout(() => uni.reLaunch({ url: '/pages/mine/index' }), 600)
loading.value = false
return
}
// 3. token
if (token && !phoneNumber) {
console.log('[DEBUG] 已登录但未绑定手机号,开始绑定')
userId = uni.getStorageSync('user_id')
try { try {
const inviterCode = uni.getStorageSync('inviter_code') const bindRes = await bindPhone(userId, phoneCode)
const data = await wechatLogin(res.code, inviterCode) console.log('[DEBUG] 绑定手机号成功:', bindRes)
// //
const isBound = data.phone || data.phone_number || data.mobile const boundPhone = bindRes.phone || bindRes.phone_number || bindRes.mobile
console.log('[DEBUG] 微信登录返回数据手机号状态:', isBound ? '已绑定' : '未绑定', data) if (boundPhone) {
uni.setStorageSync('phone_number', boundPhone)
// console.log('[DEBUG] 已缓存手机号到 phone_number:', boundPhone)
if (!isBound) {
try {
console.log('[DEBUG] 开始绑定手机号')
await bindPhone(data.user_id, phoneCode)
//
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) try {
const updatedUserInfo = await authRequest({ url: '/api/app/users/info', method: 'GET' })
if (updatedUserInfo) {
uni.setStorageSync('user_info', updatedUserInfo)
}
} catch (err) {
console.warn('[DEBUG] 绑定后获取用户信息失败:', err)
}
// //
fetchExtraData(data.user_id) fetchExtraData(userId)
uni.showToast({ title: '✨ 登录成功!', icon: 'none', duration: 1200 }) uni.showToast({ title: '✨ 绑定成功!', icon: 'none', duration: 1200 })
setTimeout(() => uni.reLaunch({ url: '/pages/mine/index' }), 600) setTimeout(() => uni.reLaunch({ url: '/pages/mine/index' }), 600)
} catch (err) { } catch (bindErr) {
uni.showToast({ title: err.message || '登录失败', icon: 'none' }) console.error('[DEBUG] 绑定手机号失败:', bindErr)
} finally { uni.showToast({ title: bindErr.message || '绑定失败', icon: 'none' })
loading.value = false
} }
},
fail: () => {
loading.value = false loading.value = false
return
} }
})
// 4. token
console.log('[DEBUG] 未登录或已过期,执行微信登录')
uni.login({
provider: 'weixin',
success: async (res) => {
try {
const inviterCode = uni.getStorageSync('inviter_code')
const data = await wechatLogin(res.code, inviterCode)
console.log('[DEBUG] 微信登录成功:', data)
//
saveUserData(data)
userId = data.user_id
//
const hasPhone = data.mobile || data.phone || data.phone_number
if (!hasPhone) {
// 5.
console.log('[DEBUG] 登录未返回手机号,开始绑定')
try {
const bindRes = await bindPhone(userId, phoneCode)
console.log('[DEBUG] 绑定手机号成功:', bindRes)
//
const boundPhone = bindRes.phone || bindRes.phone_number || bindRes.mobile
if (boundPhone) {
uni.setStorageSync('phone_number', boundPhone)
console.log('[DEBUG] 已缓存手机号到 phone_number:', boundPhone)
}
//
try {
const updatedUserInfo = await authRequest({ url: '/api/app/users/info', method: 'GET' })
if (updatedUserInfo) {
Object.assign(data, updatedUserInfo)
uni.setStorageSync('user_info', updatedUserInfo)
}
} catch (err) {
console.warn('[DEBUG] 绑定后获取用户信息失败:', err)
}
} catch (bindErr) {
console.error('[DEBUG] 绑定手机号失败:', bindErr)
}
} else {
// 6.
const phoneToCache = data.mobile || data.phone || data.phone_number
uni.setStorageSync('phone_number', phoneToCache)
console.log('[DEBUG] 登录已返回手机号,已缓存:', phoneToCache)
}
//
fetchExtraData(userId)
uni.showToast({ title: '✨ 登录成功!', icon: 'none', duration: 1200 })
setTimeout(() => uni.reLaunch({ url: '/pages/mine/index' }), 600)
} catch (err) {
console.error('[DEBUG] 微信登录失败:', err)
uni.showToast({ title: err.message || '登录失败', icon: 'none' })
} finally {
loading.value = false
}
},
fail: () => {
loading.value = false
uni.showToast({ title: '微信登录失败', icon: 'none' })
}
})
} catch (err) {
console.error('[DEBUG] 微信登录流程错误:', err)
uni.showToast({ title: err.message || '登录失败', icon: 'none' })
loading.value = false
}
} }
// - // -
@ -523,13 +610,11 @@ function saveUserData(data) {
hasPhone: hasPhone ? '已绑定' : '未绑定' hasPhone: hasPhone ? '已绑定' : '未绑定'
}) })
// phone_bound // phone_number
if (hasPhone) { if (hasPhone) {
uni.setStorageSync('phone_bound', true) const phoneToCache = data.mobile || data.phone || data.phone_number
console.log('[DEBUG] 已设置 phone_bound = true') uni.setStorageSync('phone_number', phoneToCache)
} else { console.log('[DEBUG] 已缓存手机号到 phone_number:', phoneToCache)
uni.setStorageSync('phone_bound', false)
console.log('[DEBUG] 已设置 phone_bound = false')
} }
// openid ( openid) // openid ( openid)
@ -541,8 +626,8 @@ function saveUserData(data) {
console.warn('[DEBUG] 登录接口未返回 openid, 请检查后端或联系管理员') console.warn('[DEBUG] 登录接口未返回 openid, 请检查后端或联系管理员')
} }
// tab
if (!hasPhone) { if (!hasPhone) {
// tab
console.log('[DEBUG] 未检测到手机号切换到短信登录tab') console.log('[DEBUG] 未检测到手机号切换到短信登录tab')
uni.showModal({ uni.showModal({
title: '绑定手机号', title: '绑定手机号',

View File

@ -4,20 +4,14 @@
* @returns {boolean} 是否已绑定手机号 * @returns {boolean} 是否已绑定手机号
*/ */
export function checkPhoneBound() { export function checkPhoneBound() {
// 获取用户信息 // 直接检查 phone_number 缓存中是否有手机号
const userInfo = uni.getStorageSync('user_info') || {} const phoneNumber = uni.getStorageSync('phone_number') || ''
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 || '' console.log('[checkPhoneBound] 检查 phone_number 缓存:', phoneNumber ? phoneNumber : '未找到')
// 如果已绑定手机号,直接返回 // 如果已绑定手机号,直接返回
if (mobile) { if (phoneNumber) {
console.log('[checkPhoneBound] 已检测到手机号,允许通过:', mobile) console.log('[checkPhoneBound] 已检测到手机号,允许通过:', phoneNumber)
return true return true
} }

View File

@ -98,10 +98,10 @@ export async function executePaymentFlow({ createOrder, openid, onOrderCreated }
*/ */
export function checkLoginStatus() { export function checkLoginStatus() {
const token = uni.getStorageSync('token') const token = uni.getStorageSync('token')
const phoneBound = !!uni.getStorageSync('phone_bound') const phoneNumber = uni.getStorageSync('phone_number')
const openid = uni.getStorageSync('openid') const openid = uni.getStorageSync('openid')
if (!token || !phoneBound) { if (!token || !phoneNumber) {
return { ok: false, message: '请先登录并绑定手机号' } return { ok: false, message: '请先登录并绑定手机号' }
} }
if (!openid) { if (!openid) {