import { getUserProfile } from '../api/appUser' /** * 检查用户是否已绑定手机号(同步,仅检查本地) * @returns {boolean} 是否已绑定手机号 */ export function hasPhoneBound() { // 优先检查登录方式,如果是微信手机号登录或短信登录,则已绑定手机号 const loginMethod = uni.getStorageSync('login_method') if (loginMethod === 'wechat_phone' || loginMethod === 'sms') { return true } // 降级检查 phone_number 缓存 const phoneNumber = uni.getStorageSync('phone_number') || '' return !!phoneNumber } /** * 检查手机号绑定状态 * 如果未绑定手机号,则跳转到登录页面进行绑定 * @returns {Promise} 是否已绑定手机号 */ export async function checkPhoneBound() { try { // 优先使用同步检查 if (hasPhoneBound()) { console.log('[checkPhoneBound] 用户已通过手机号登录,跳过绑定检查') return true } // 调用新的用户资料接口 const profile = await getUserProfile() console.log('[checkPhoneBound] 用户资料:', profile) // 检查是否已绑定手机号 const mobile = profile?.mobile if (mobile) { console.log('[checkPhoneBound] 已检测到手机号,允许通过:', mobile) // 缓存手机号 uni.setStorageSync('phone_number', mobile) return true } // 未绑定手机号,显示提示并跳转 console.warn('[checkPhoneBound] 未检测到手机号,提示用户绑定') uni.showModal({ title: '需要绑定手机号', content: '为了账号安全,请先绑定手机号', showCancel: false, confirmText: '去绑定', success: () => { uni.navigateTo({ url: '/pages/login/index?mode=sms' }) } }) return false } catch (err) { console.error('[checkPhoneBound] 获取用户信息失败:', err) // 请求失败时,降级检查本地缓存 const phoneNumber = uni.getStorageSync('phone_number') || '' console.log('[checkPhoneBound] 降级检查本地缓存:', phoneNumber ? phoneNumber : '未找到') if (phoneNumber) { return true } // 本地也没有,提示重新登录 uni.showModal({ title: '提示', content: '获取用户信息失败,请重新登录', showCancel: false, confirmText: '去登录', success: () => { uni.navigateTo({ url: '/pages/login/index' }) } }) return false } } /** * 同步检查手机号绑定状态(仅检查本地缓存) * @returns {boolean} 是否已绑定手机号 */ export function checkPhoneBoundSync() { if (hasPhoneBound()) { console.log('[checkPhoneBoundSync] 用户已通过手机号登录,跳过绑定检查') return true } const phoneNumber = uni.getStorageSync('phone_number') || '' console.log('[checkPhoneBoundSync] 检查 phone_number 缓存:', phoneNumber ? phoneNumber : '未找到') if (phoneNumber) { console.log('[checkPhoneBoundSync] 已检测到手机号,允许通过:', phoneNumber) return true } console.warn('[checkPhoneBoundSync] 未检测到手机号') return false }