const userApi = require('../../api/user') const auth = require('../../utils/auth') const navigation = require('../../utils/navigation') const theme = require('../../utils/theme') const { runWithLoading } = require('../../utils/loading') const passwordRegex = /^(?=.*[A-Za-z])(?=.*\d)[A-Za-z\d@$!%*#?&]{8,}$/ Page({ data: Object.assign(theme.pageThemeData(getApp().globalData.shopTheme), { loading: false, saving: false, isLoggedIn: false, userInfo: null, form: { verificationCode: '', password: '', confirmPassword: '' } }), onShow() { this.loadProfile() }, handleBack() { navigation.goBack('/pages/settings/index') }, async loadProfile() { const isLoggedIn = auth.isLoggedIn() this.setData({ isLoggedIn }) if (!isLoggedIn) { navigation.openAuth(navigation.getCurrentPageUrl()) return } this.setData({ loading: true }) try { this.setData({ userInfo: await userApi.getUserInfo() }) } catch (error) { wx.showToast({ title: error.message || '账号信息加载失败', icon: 'none' }) } finally { this.setData({ loading: false }) } }, handleInput(event) { const field = event.currentTarget.dataset.field this.setData({ [`form.${field}`]: event.detail.value }) }, async sendCode() { const phone = String(this.data.userInfo && this.data.userInfo.phone || '').trim() if (!phone) { wx.showToast({ title: '请先绑定手机号', icon: 'none' }) return } try { await runWithLoading(() => userApi.sendVerificationCode({ phone, sceneCode: 'USER_CHANGE_PASSWORD_SMS_CODE' })) wx.showToast({ title: '验证码已发送', icon: 'none' }) } catch (error) { wx.showToast({ title: error.message || '验证码发送失败', icon: 'none' }) } }, async savePassword() { const verificationCode = String(this.data.form.verificationCode || '').trim() const password = String(this.data.form.password || '') const confirmPassword = String(this.data.form.confirmPassword || '') if (!verificationCode) { wx.showToast({ title: '请输入验证码', icon: 'none' }) return } if (!passwordRegex.test(password)) { wx.showToast({ title: '密码至少8位且包含字母和数字', icon: 'none' }) return } if (password !== confirmPassword) { wx.showToast({ title: '两次密码不一致', icon: 'none' }) return } this.setData({ saving: true }) try { await runWithLoading(() => userApi.updatePassword({ password, verificationCode })) auth.setNeedSetPassword(false) wx.showToast({ title: '密码已更新', icon: 'none' }) navigation.goBack('/pages/settings/index') } catch (error) { wx.showToast({ title: error.message || '密码更新失败', icon: 'none' }) } finally { this.setData({ saving: false }) } } })