refactor: form validation of the reset password page (#403)

pull/406/head
Ryan Wang 2022-01-07 19:44:44 +08:00 committed by GitHub
parent e1d38482ae
commit 3e8a120b2c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 72 additions and 93 deletions

View File

@ -5,43 +5,44 @@
<span>重置密码</span> <span>重置密码</span>
</div> </div>
<div> <div>
<a-form layout="vertical"> <a-form-model ref="sendCodeForm" :model="form.model" :rules="form.sendCodeRules" layout="vertical">
<a-form-item> <a-form-model-item prop="username">
<a-input v-model="resetParam.username" placeholder="用户名"> <a-input v-model="form.model.username" placeholder="用户名">
<a-icon slot="prefix" style="color: rgba(0, 0, 0, 0.25)" type="user" /> <a-icon slot="prefix" style="color: rgba(0, 0, 0, 0.25)" type="user" />
</a-input> </a-input>
</a-form-item> </a-form-model-item>
<a-form-item> <a-form-model-item prop="email">
<a-input v-model="resetParam.email" placeholder="邮箱"> <a-input v-model="form.model.email" placeholder="邮箱">
<a-icon slot="prefix" style="color: rgba(0, 0, 0, 0.25)" type="mail" /> <a-icon slot="prefix" style="color: rgba(0, 0, 0, 0.25)" type="mail" />
</a-input> </a-input>
</a-form-item> </a-form-model-item>
<a-form-item> </a-form-model>
<a-input v-model="resetParam.code" placeholder="验证码" type="password"> <a-form-model ref="passwordForm" :model="form.model" :rules="form.rules" layout="vertical">
<a-form-model-item prop="code">
<a-input v-model="form.model.code" placeholder="验证码" type="password">
<a-icon slot="prefix" style="color: rgba(0, 0, 0, 0.25)" type="safety-certificate" /> <a-icon slot="prefix" style="color: rgba(0, 0, 0, 0.25)" type="safety-certificate" />
<a slot="addonAfter" href="javascript:void(0);" @click="handleSendCode"> </a> <a slot="addonAfter" href="javascript:void(0);" @click="handleSendCode"> </a>
</a-input> </a-input>
</a-form-item> </a-form-model-item>
<a-form-model-item prop="password">
<a-form-item> <a-input v-model="form.model.password" autocomplete="new-password" placeholder="新密码" type="password">
<a-input v-model="resetParam.password" autocomplete="new-password" placeholder="新密码" type="password">
<a-icon slot="prefix" style="color: rgba(0, 0, 0, 0.25)" type="lock" /> <a-icon slot="prefix" style="color: rgba(0, 0, 0, 0.25)" type="lock" />
</a-input> </a-input>
</a-form-item> </a-form-model-item>
<a-form-item> <a-form-model-item prop="confirmPassword">
<a-input <a-input
v-model="resetParam.confirmPassword" v-model="form.model.confirmPassword"
autocomplete="new-password" autocomplete="new-password"
placeholder="确认密码" placeholder="确认密码"
type="password" type="password"
> >
<a-icon slot="prefix" style="color: rgba(0, 0, 0, 0.25)" type="lock" /> <a-icon slot="prefix" style="color: rgba(0, 0, 0, 0.25)" type="lock" />
</a-input> </a-input>
</a-form-item> </a-form-model-item>
<a-form-item> <a-form-model-item>
<a-button :block="true" type="primary" @click="handleResetPassword"></a-button> <a-button :block="true" type="primary" @click="handleResetPassword"></a-button>
</a-form-item> </a-form-model-item>
</a-form> </a-form-model>
<router-link :to="{ name: 'Login' }" class="tip"> <router-link :to="{ name: 'Login' }" class="tip">
返回登录 返回登录
</router-link> </router-link>
@ -54,88 +55,66 @@ import apiClient from '@/utils/api-client'
export default { export default {
data() { data() {
const validateConfirmPassword = (rule, value, callback) => {
if (value && this.form.model.password !== value) {
callback(new Error('确认密码与新密码不一致'))
} else {
callback()
}
}
return { return {
resetParam: { form: {
username: '', model: {},
email: '', sendCodeRules: {
code: '', username: [{ required: true, message: '* 用户名不能为空', trigger: ['change'] }],
password: '', email: [{ required: true, message: '* 电子邮箱地址不能为空', trigger: ['change'] }]
confirmPassword: '' },
rules: {
code: [{ required: true, message: '* 验证码不能为空', trigger: ['change'] }],
password: [
{ required: true, message: '* 新密码不能为空', trigger: ['change'] },
{ max: 100, min: 8, message: '* 密码的字符长度必须在 8 - 100 之间', trigger: ['change'] }
],
confirmPassword: [
{ required: true, message: '* 确认密码不能为空', trigger: ['change'] },
{ validator: validateConfirmPassword, trigger: ['change'] }
]
}
} }
} }
}, },
methods: { methods: {
handleSendCode() { handleSendCode() {
if (!this.resetParam.username) { this.$refs.sendCodeForm.validate(async valid => {
this.$notification['error']({ if (valid) {
message: '提示', const hideLoading = this.$message.loading('发送中...', 0)
description: '用户名不能为空!' try {
}) await apiClient.sendResetPasswordCode(this.form.model)
return
}
if (!this.resetParam.email) {
this.$notification['error']({
message: '提示',
description: '邮箱不能为空!'
})
return
}
const hide = this.$message.loading('发送中...', 0)
apiClient
.sendResetPasswordCode(this.resetParam)
.then(() => {
this.$message.success('邮件发送成功,五分钟内有效') this.$message.success('邮件发送成功,五分钟内有效')
}) } catch (e) {
.finally(() => { this.$log.error('Failed send code: ', e)
hide() } finally {
hideLoading()
}
}
}) })
}, },
handleResetPassword() { handleResetPassword() {
if (!this.resetParam.username) { this.$refs.sendCodeForm.validate(sendCodeValid => {
this.$notification['error']({ if (!sendCodeValid) {
message: '提示',
description: '用户名不能为空!'
})
return return
} }
if (!this.resetParam.email) { this.$refs.passwordForm.validate(async valid => {
this.$notification['error']({ if (valid) {
message: '提示', try {
description: '邮箱不能为空!' await apiClient.resetPassword(this.form.model)
}) await this.$router.push({ name: 'Login' })
return
}
if (!this.resetParam.code) {
this.$notification['error']({
message: '提示',
description: '验证码不能为空!'
})
return
}
if (!this.resetParam.password) {
this.$notification['error']({
message: '提示',
description: '新密码不能为空!'
})
return
}
if (!this.resetParam.confirmPassword) {
this.$notification['error']({
message: '提示',
description: '确认密码不能为空!'
})
return
}
if (this.resetParam.confirmPassword !== this.resetParam.password) {
this.$notification['error']({
message: '提示',
description: '确认密码和新密码不匹配!'
})
return
}
apiClient.resetPassword(this.resetParam).then(() => {
this.$message.success('密码重置成功!') this.$message.success('密码重置成功!')
this.$router.push({ name: 'Login' }) } catch (e) {
this.$log.error('Failed reset password: ', e)
}
}
})
}) })
} }
} }