Files
JayuCloud/JY-Salon-WeChat/pages/password/index.js
T

49 lines
1.4 KiB
JavaScript

const userApi = require('../../api/user')
function clearSessionAndGoLogin() {
const app = getApp()
if (app && typeof app.clearSession === 'function') {
app.clearSession()
}
wx.reLaunch({ url: '/pages/login/index' })
}
Page({
data: {
form: { oldPassword: '', newPassword: '', confirmPassword: '' },
saving: false
},
onInput(event) { this.setData({ [`form.${event.currentTarget.dataset.field}`]: event.detail.value }) },
async save() {
if (this.data.saving) {
return
}
if (!this.data.form.oldPassword) {
wx.showToast({ title: '请填写原密码', icon: 'none' })
return
}
if (!this.data.form.newPassword || !this.data.form.confirmPassword) {
wx.showToast({ title: '请填写新密码', icon: 'none' })
return
}
if (this.data.form.newPassword !== this.data.form.confirmPassword) {
wx.showToast({ title: '两次密码不一致', icon: 'none' })
return
}
this.setData({ saving: true })
try {
await userApi.updatePassword({
oldPassword: this.data.form.oldPassword,
password: this.data.form.newPassword
})
wx.showToast({ title: '密码已修改', icon: 'success' })
setTimeout(clearSessionAndGoLogin, 600)
} catch (error) {
wx.showToast({ title: error.message || '密码修改失败', icon: 'none' })
} finally {
this.setData({ saving: false })
}
},
back() { wx.navigateBack() }
})