Files
kh-device-admin/jero-web/src/components/tools/UserPassword.vue
T

193 lines
6.4 KiB
Java
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
<template>
<a-modal
:title="title"
:width="modalWidth"
:visible="visible"
:confirmLoading="confirmLoading"
@ok="handleOk"
@cancel="handleCancel"
cancelText="关闭"
>
<a-spin :spinning="confirmLoading">
<a-form :form="form">
<a-form-item
:labelCol="labelCol"
:wrapperCol="wrapperCol"
label="旧密码">
<a-input :type="passwordType1" placeholder="请输入旧密码" v-decorator="[ 'oldpassword', validatorRules.oldpassword]" >
<a-icon title="显示密码" @click="passwordType1='text'" slot="suffix" v-if="passwordType1==='password'" type="eye" :style="{ color: '#B3B7C2',fontSize:'20px' }"/>
<a-icon title="隐藏密码" @click="passwordType1='password'" slot="suffix" v-else type="eye-invisible" :style="{ color: '#B3B7C2',fontSize:'20px' }"/>
</a-input>
</a-form-item>
<a-form-item
:labelCol="labelCol"
:wrapperCol="wrapperCol"
label="新密码">
<a-input :type="passwordType2" placeholder="请输入新密码" :maxLength="20" v-decorator="[ 'password', validatorRules.password]" >
<a-icon title="显示密码" @click="passwordType2='text'" slot="suffix" v-if="passwordType2==='password'" type="eye" :style="{ color: '#B3B7C2',fontSize:'20px' }"/>
<a-icon title="隐藏密码" @click="passwordType2='password'" slot="suffix" v-else type="eye-invisible" :style="{ color: '#B3B7C2',fontSize:'20px' }"/>
</a-input>
</a-form-item>
<a-form-item
:labelCol="labelCol"
:wrapperCol="wrapperCol"
label="确认新密码">
<a-input :type="passwordType3" @blur="handleConfirmBlur" placeholder="请确认新密码" :maxLength="20" v-decorator="[ 'confirmpassword', validatorRules.confirmpassword]">
<a-icon title="显示密码" @click="passwordType3='text'" slot="suffix" v-if="passwordType3==='password'" type="eye" :style="{ color: '#B3B7C2',fontSize:'20px' }"/>
<a-icon title="隐藏密码" @click="passwordType3='password'" slot="suffix" v-else type="eye-invisible" :style="{ color: '#B3B7C2',fontSize:'20px' }"/>
</a-input>
</a-form-item>
</a-form>
</a-spin>
</a-modal>
</template>
<script>
import { postAction } from '@/api/manage'
import { mapActions } from 'vuex'
import { JSEncrypt } from 'jsencrypt'
import { getRSAPublicKey } from '@/utils/encryption.js'
export default {
name: 'UserPassword',
data () {
return {
rsaPublicKey: '',
passwordType1: 'password',
passwordType2: 'password',
passwordType3: 'password',
title: '修改密码',
modalWidth: 800,
visible: false,
confirmLoading: false,
validatorRules: {
oldpassword: {
rules: [{
required: true, message: '请输入旧密码!'
}]
},
password: {
rules: [
{ required: true, message: '请输入新密码!' },
{ pattern: /^(?=.*[a-z])(?=.*[A-Z])(?=.*\d)(?=.*[~!@#$%^&*()_+`\-={}:";'<>?,./]).{8,}$/, message: '密码由8-20位数字、大小写字母和特殊符号组成!' },
{ validator: this.validateToNextPassword }
]
},
confirmpassword: {
rules: [
{ required: true, message: '请确认新密码!' },
{ pattern: /^(?=.*[a-z])(?=.*[A-Z])(?=.*\d)(?=.*[~!@#$%^&*()_+`\-={}:";'<>?,./]).{8,}$/, message: '密码由8-20位数字大小写字母和特殊符号组成!' },
{ validator: this.validateToNextPassword }
]
}
},
confirmDirty: false,
labelCol: {
xs: { span: 24 },
sm: { span: 5 }
},
wrapperCol: {
xs: { span: 24 },
sm: { span: 16 }
},
form: this.$form.createForm(this),
url: 'sys/user/updatePassword',
username: ''
}
},
methods: {
...mapActions(['Logout']),
show (uname) {
if (!uname) {
this.$message.warning('当前系统无登录用户!')
} else {
this.username = uname
this.form.resetFields()
this.visible = true
}
},
handleCancel () {
this.close()
},
close () {
this.$emit('close')
this.visible = false
this.disableSubmit = false
this.selectedRole = []
},
// 获取RSA公钥
getPublicKey () {
// 获取公钥
getRSAPublicKey().then(res => {
this.rsaPublicKey = res.result
})
},
handleOk () {
const that = this
// 触发表单验证
getRSAPublicKey().then(res => {
this.rsaPublicKey = res.result
this.form.validateFields((err, values) => {
if (!err) {
that.confirmLoading = true
const params = Object.assign({ username: this.username, rsaPublicKey:this.rsaPublicKey}, values)
console.log('修改密码提交数据', params)
const encrypt = new JSEncrypt()
encrypt.setPublicKey(this.rsaPublicKey)
// 公钥加密
params.confirmpassword = encrypt.encrypt(values.confirmpassword)
params.password = encrypt.encrypt(values.password)
params.oldpassword = encrypt.encrypt(values.oldpassword)
postAction(this.url, params).then((res) => {
if (res.success) {
console.log(res)
that.$message.success(res.message)
that.close()
that.Logout().then(()=>{
window.location.replace('/user/login')
})
} else {
that.$message.warning(res.message)
}
}).finally(() => {
that.confirmLoading = false
})
}
})
})
},
validateToNextPassword (rule, value, callback) {
const form = this.form
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('两次输入的密码不一样'))
} else {
callback()
}
},
handleConfirmBlur (e) {
const value = e.target.value
this.confirmDirty = this.confirmDirty || !!value
}
}
}
</script>
<style scoped>
</style>