Files
laws_chery_client/src/views/system/modules/PasswordModal.vue
T

167 lines
4.9 KiB
Vue

<template>
<j-modal
:title="$t('resetPassword.resetPasswordModalTitle')"
:width="800"
:visible="visible"
:confirmLoading="confirmLoading"
switchFullscreen
@ok="handleSubmit"
@cancel="handleCancel"
:cancelText="$t('close')"
style="top:20px;"
>
<a-spin :spinning="confirmLoading">
<a-form :form="form">
<a-form-item :label="$t('user.userAccount')" :labelCol="labelCol" :wrapperCol="wrapperCol">
<a-input :placeholder="$t('pleaseEnter') + $t('user.userAccount')" v-decorator="[ 'username', {}]" :readOnly="true"/>
</a-form-item>
<!-- 登录密码 -->
<a-form-item :label="$t('user.loginPassword')" :labelCol="labelCol" :wrapperCol="wrapperCol" :hasFeedback="true" >
<a-input type="password" :placeholder="$t('pleaseEnter') + $t('user.loginPassword')" v-decorator="[ 'password', validatorRules.password]" />
</a-form-item>
<!-- 确认密码 -->
<a-form-item :label="$t('user.confirmPassword')" :labelCol="labelCol" :wrapperCol="wrapperCol" :hasFeedback="true" >
<a-input type="password" @blur="handleConfirmBlur" :placeholder="$t('user.reenterLoginPassword')" v-decorator="[ 'confirmpassword', validatorRules.confirmpassword]"/>
</a-form-item>
</a-form>
</a-spin>
</j-modal>
</template>
<script>
import { changePassword } from '@/api/api'
import { JSEncrypt } from 'jsencrypt'
import { getRSAPublicKey } from '../../../utils/encryption'
export default {
name: 'PasswordModal',
data () {
return {
visible: false,
confirmLoading: false,
confirmDirty: false,
validatorRules: {
password: {
rules: [{
required: true,
pattern: /^(?=.*[a-zA-Z])(?=.*\d)(?=.*[~!@#$%^&*()_+`\-={}:";'<>?,./]).{8,}$/,
message: this.$t('user.passwordConsists')
}, {
validator: this.validateToNextPassword
}],
validateTrigger: 'blur'
},
confirmpassword: {
rules: [{
required: true, message: this.$t('user.reenterLoginPassword')
}, {
validator: this.compareToFirstPassword
}],
validateTrigger: 'blur'
}
},
model: {},
labelCol: {
xs: { span: 24 },
sm: { span: 5 }
},
wrapperCol: {
xs: { span: 24 },
sm: { span: 16 }
},
form: this.$form.createForm(this),
rsaPublicKey: ''
}
},
created () {
console.log('created')
},
methods: {
show (username) {
this.form.resetFields()
this.visible = true
this.model.username = username
this.$nextTick(() => {
this.form.setFieldsValue({ username: username })
})
},
close () {
this.$emit('close')
this.visible = false
this.disableSubmit = false
this.selectedRole = []
},
handleSubmit () {
// 先拉取秘钥
getRSAPublicKey().then(res => {
this.rsaPublicKey = res.result
// 触发表单验证
this.form.validateFields((err, values) => {
if (!err) {
this.confirmLoading = true
const formData = Object.assign(this.model, values)
// 对密码进行加密
const encrypt = new JSEncrypt()
encrypt.setPublicKey(this.rsaPublicKey)
// 登录密码
if (formData.password) {
formData.password = encrypt.encrypt(formData.password)
}
// 确认密码
if (formData.confirmpassword) {
formData.confirmpassword = encrypt.encrypt(formData.confirmpassword)
}
changePassword(formData).then((res) => {
if (res.success) {
this.$message.success(res.message)
this.$emit('ok')
} else {
this.$message.warning(res.message)
}
}).finally(() => {
this.confirmLoading = false
this.close()
})
}
})
})
},
handleCancel () {
this.close()
},
validateToNextPassword (rule, value, callback) {
const form = this.form
const confirmpassword = form.getFieldValue('confirmpassword')
console.log('confirmpassword==>', confirmpassword)
if (value && confirmpassword && value !== confirmpassword) {
callback(new Error(this.$t('user.passwordsEnteredTwice')))
}
if (value && this.confirmDirty) {
form.validateFields(['confirm'], { force: true })
}
callback()
},
compareToFirstPassword (rule, value, callback) {
const form = this.form
if (value && value !== form.getFieldValue('password')) {
callback(new Error(this.$t('user.passwordsEnteredTwice')))
} else {
callback()
}
},
handleConfirmBlur (e) {
const value = e.target.value
this.confirmDirty = this.confirmDirty || !!value
}
}
}
</script>