const userApi = require('../../api/user') const authApi = require('../../api/auth') const auth = require('../../utils/auth') const profileCache = require('../../utils/profile-cache') Page({ data: { profile: {}, loading: true, binding: false, smsForm: { phone: '', code: '' }, codeLoading: false, countdown: 0, timer: null, smsExpanded: false }, onShow() { this.loadProfile() }, async loadProfile(options = {}) { this.setData({ loading: true }) try { const profile = await profileCache.getProfile(options) this.setData({ profile, 'smsForm.phone': profile.phone || '' }) const app = getApp() const session = auth.getSession() app.setSession({ ...session, userInfo: { ...(session.userInfo || {}), ...profile } }) } catch (error) { wx.showToast({ title: error.message || '账号信息加载失败', icon: 'none' }) } finally { this.setData({ loading: false }) } }, onInput(event) { const field = event.currentTarget.dataset.field const value = field === 'phone' ? String(event.detail.value || '').replace(/\D/g, '').slice(0, 11) : event.detail.value this.setData({ [`smsForm.${field}`]: value }) }, toggleSms() { this.setData({ smsExpanded: !this.data.smsExpanded }) }, async bindWechatPhone(event) { const phoneCode = event.detail && event.detail.code if (!phoneCode) { wx.showToast({ title: '未获得手机号授权', icon: 'none' }); return } this.setData({ binding: true }) try { await userApi.bindWechatPhone(phoneCode) profileCache.invalidate() await this.loadProfile({ force: true }) wx.showToast({ title: '手机号已绑定' }) } catch (error) { wx.showToast({ title: error.message || '绑定失败', icon: 'none' }) } finally { this.setData({ binding: false }) } }, async sendCode() { const phone = String(this.data.smsForm.phone || '') if (!/^1[3-9]\d{9}$/.test(phone)) { wx.showToast({ title: '请输入正确手机号', icon: 'none' }); return } if (this.data.codeLoading || this.data.countdown) return this.setData({ codeLoading: true }) try { await userApi.sendBindPhoneCode(phone) this.startCountdown() wx.showToast({ title: '验证码已发送' }) } catch (error) { wx.showToast({ title: error.message || '验证码发送失败', icon: 'none' }) } finally { this.setData({ codeLoading: false }) } }, startCountdown() { if (this.data.timer) clearInterval(this.data.timer) this.setData({ countdown: 60 }) const timer = setInterval(() => { const next = this.data.countdown - 1 if (next <= 0) { clearInterval(timer); this.setData({ countdown: 0, timer: null }); return } this.setData({ countdown: next }) }, 1000) this.setData({ timer }) }, async bindSmsPhone() { const form = this.data.smsForm if (!/^1[3-9]\d{9}$/.test(form.phone) || !/^\d{6}$/.test(String(form.code || ''))) { wx.showToast({ title: '请输入正确手机号和6位验证码', icon: 'none' }); return } this.setData({ binding: true }) try { await userApi.bindPhone({ phone: form.phone, verificationCode: form.code }) profileCache.invalidate() await this.loadProfile({ force: true }) wx.showToast({ title: '手机号已绑定' }) } catch (error) { wx.showToast({ title: error.message || '绑定失败', icon: 'none' }) } finally { this.setData({ binding: false }) } }, async logout() { const confirmed = await new Promise(resolve => wx.showModal({ title: '退出登录', content: '退出后将关闭自动登录,并清除本机提醒设置。', success: result => resolve(result.confirm), fail: () => resolve(false) })) if (!confirmed) return try { await authApi.logout() } catch (error) {} getApp().logoutDevice() wx.reLaunch({ url: '/pages/login/index' }) }, onUnload() { if (this.data.timer) clearInterval(this.data.timer) } })