Files
JayuCloud/JC-Salon-WeChat/pages/account-settings/index.js
T
xiaowang 385c4b06ec
自动发布开发环境 Nacos 配置 / publish (push) Successful in 25s
环境分支晋级检查 / environment-promotion-source (pull_request_target) Successful in 5s
fix(wechat): 协议页切后台暂停超时计时并全量映射技术文案
2026-11-05 11:32:29 +08:00

190 lines
6.3 KiB
JavaScript

const userApi = require('../../api/user')
const auth = require('../../utils/auth')
const profileCache = require('../../utils/profile-cache')
const { showErrorToast, toUserMessage } = require('../../utils/toast')
Page({
data: {
profile: {},
loading: true,
binding: false,
smsForm: { phone: '', code: '' },
codeLoading: false,
countdown: 0,
timer: null,
smsExpanded: false,
unbindMode: false,
closureRequest: null,
closureLoading: true,
closureError: ''
},
onShow() { this.loadProfile(); this.loadClosureRequest() },
async loadClosureRequest() {
this.setData({ closureLoading: true, closureError: '' })
try {
const closureRequest = await userApi.getAccountClosure()
this.setData({ closureRequest: closureRequest || null })
} catch (error) {
this.setData({ closureError: toUserMessage(error, '注销状态加载失败') })
} finally {
this.setData({ closureLoading: false })
}
},
async requestClosure() {
const confirmed = await new Promise(resolve => wx.showModal({
title: '申请注销账号',
content: '申请提交后由管理人员处理。完成后会清理账号身份资料,必要业务记录仍按保存要求保留。你可以在处理前撤回。',
confirmText: '提交申请',
success: result => resolve(result.confirm),
fail: () => resolve(false)
}))
if (!confirmed) return
try {
const closureRequest = await userApi.requestAccountClosure()
this.setData({ closureRequest })
wx.showToast({ title: '注销申请已提交' })
} catch (error) {
showErrorToast(error, '申请提交失败')
}
},
async withdrawClosure() {
const confirmed = await new Promise(resolve => wx.showModal({ title: '撤回注销申请', content: '确定撤回账号注销申请吗?', success: result => resolve(result.confirm), fail: () => resolve(false) }))
if (!confirmed) return
try {
await userApi.withdrawAccountClosure()
await this.loadClosureRequest()
wx.showToast({ title: '申请已撤回' })
} catch (error) {
showErrorToast(error, '撤回失败')
}
},
async loadProfile(options = {}) {
this.setData({ loading: true })
try {
const profile = await profileCache.getProfile(options)
this.setData({ profile, 'smsForm.phone': '' })
const app = getApp()
const session = auth.getSession()
app.setSession({ ...session, userInfo: { ...(session.userInfo || {}), ...profile } })
} catch (error) {
showErrorToast(error, '账号信息加载失败')
} 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, unbindMode: false })
},
beginUnbind() {
this.setData({ smsExpanded: true, unbindMode: true, 'smsForm.code': '' })
},
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) {
showErrorToast(error, '绑定失败')
} finally {
this.setData({ binding: false })
}
},
async sendCode() {
const phone = String(this.data.smsForm.phone || '')
if (!this.data.unbindMode && !/^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 {
if (this.data.unbindMode) await userApi.sendUnbindPhoneCode()
else await userApi.sendBindPhoneCode(phone)
this.startCountdown()
wx.showToast({ title: '验证码已发送' })
} catch (error) {
showErrorToast(error, '验证码发送失败')
} 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) {
showErrorToast(error, '绑定失败')
} finally {
this.setData({ binding: false })
}
},
async unbindSmsPhone() {
const code = String(this.data.smsForm.code || '')
if (!/^\d{6}$/.test(code)) {
wx.showToast({ title: '请输入当前手机号收到的6位验证码', icon: 'none' }); return
}
const confirmed = await new Promise(resolve => wx.showModal({
title: '解绑手机号',
content: '解绑后手机号将从账号资料中删除,短信验证码仅用于验证当前手机号。确定继续吗?',
confirmText: '确认解绑',
success: result => resolve(result.confirm),
fail: () => resolve(false)
}))
if (!confirmed) return
this.setData({ binding: true })
try {
await userApi.unbindPhone(code)
profileCache.invalidate()
this.setData({ unbindMode: false, smsExpanded: false, 'smsForm.code': '' })
await this.loadProfile({ force: true })
wx.showToast({ title: '手机号已解绑' })
} catch (error) {
showErrorToast(error, '解绑失败')
} finally {
this.setData({ binding: false })
}
},
onUnload() { if (this.data.timer) clearInterval(this.data.timer) }
})