198 lines
7.0 KiB
JavaScript
198 lines
7.0 KiB
JavaScript
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,
|
|
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: error.message || '注销状态加载失败' })
|
|
} 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) {
|
|
wx.showToast({ title: error.message || '申请提交失败', icon: 'none' })
|
|
}
|
|
},
|
|
|
|
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) {
|
|
wx.showToast({ title: error.message || '撤回失败', icon: 'none' })
|
|
}
|
|
},
|
|
|
|
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) {
|
|
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, 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) {
|
|
wx.showToast({ title: error.message || '绑定失败', icon: 'none' })
|
|
} 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) {
|
|
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 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) {
|
|
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) }
|
|
})
|