<template>
|
<div>
|
<el-dialog :visible.sync="dialogVisible" title="修改密码" :close-on-click-modal="false"
|
:modal-append-to-body="false" v-if="dialogVisible" width="40%">
|
<el-form size="mini" :model="form" label-width="120px" ref="form" :rules="formRules">
|
<el-form-item label="账号:" prop="userName">
|
<el-input v-model="form.userName" placeholder="请输入账号" autocomplete="off"
|
@blur="blurUserName" @input="inputUserName">
|
</el-input>
|
</el-form-item>
|
<el-form-item label="手机号码:" prop="phone" ref="phone">
|
<el-input v-model="form.phone" placeholder="请输入手机号码" disabled autocomplete="off">
|
</el-input>
|
</el-form-item>
|
<el-form-item label="验证码:" prop="verificationCode" ref="verificationCode">
|
<el-input placeholder="请输入验证码" v-model="form.verificationCode" autocomplete="off">
|
<template slot="append">
|
<el-button type="text" :style="{color: btnColor,width:'140px'}" @click="getCode">
|
{{btnTxt}}
|
</el-button>
|
</template>
|
</el-input>
|
</el-form-item>
|
<el-form-item label="新密码:" prop="newPassword">
|
<el-input v-model="form.newPassword" type="password" placeholder="请输入新密码" clearable
|
autocomplete="new-password">
|
</el-input>
|
</el-form-item>
|
<el-form-item label="确认密码:" prop="checkPassword">
|
<el-input v-model="form.checkPassword" type="password" placeholder="请输入确认密码" clearable
|
autocomplete="new-password">
|
</el-input>
|
</el-form-item>
|
</el-form>
|
<div slot="footer" class="dialog-footer">
|
<el-button size="mini" @click="submit" type="primary" :loading="loading">保存</el-button>
|
<el-button size="mini" @click="dialogVisible = false">取消</el-button>
|
</div>
|
</el-dialog>
|
</div>
|
</template>
|
|
<script>
|
import { mapState, mapMutations } from 'vuex'
|
import { validateUsername, validatePwd } from '@/utils/validator'
|
import ChangePwdApi from '@api/oauth/byPhoneUpdateInfo'
|
const sm2 = require('sm-crypto').sm2
|
export default {
|
name: "changePasswordByPhone",
|
props: {
|
show: {
|
type: Boolean,
|
default: false
|
},
|
},
|
data() {
|
// 手机号码效验
|
const validatePhone = (rule, value, callback) => {
|
const reg = /^[1][3,4,5,6,7,8,9][0-9]{9}$/
|
if (value) {
|
if (!reg.test(value)) {
|
callback(new Error('请输入正确的手机号码'))
|
} else {
|
callback()
|
}
|
} else {
|
callback(new Error('请输入手机号码'))
|
}
|
}
|
return {
|
//验证码部分所需的数据
|
btnColor: '#409eff',
|
btnTxt: "获取验证码",
|
time: 0,
|
form: {
|
userName: null,
|
phone: null,
|
verificationCode: null,
|
newPassword: null,
|
checkPassword: null
|
},
|
loading: false,
|
formRules: {
|
userName: [{ required: true, validator: validateUsername, trigger: 'change' }],
|
phone: [{ required: true, validator: validatePhone, trigger: 'change' }],
|
verificationCode: [
|
{ required: true, message: '请输入验证码', trigger: 'change' },
|
],
|
newPassword: [{ required: true, validator: validatePwd, trigger: 'change' }],
|
checkPassword: [{
|
required: true,
|
validator: (rule, value, callback) => {
|
if (!value) {
|
callback(new Error('请再次输入密码'))
|
} else if (value !== this.form.newPassword) {
|
callback(new Error('两次输入密码不一致'))
|
} else {
|
callback()
|
}
|
},
|
trigger: 'change'
|
}]
|
},
|
dialogVisible: false
|
}
|
},
|
watch: {
|
show(newValue, oldValue) {
|
this.dialogVisible = newValue
|
},
|
dialogVisible(newValue, oldValue) {
|
this.$emit('update:show', newValue)
|
if (!newValue) {
|
this.initData()
|
}
|
}
|
},
|
methods: {
|
...mapMutations(['logout']),
|
doSm3AndSm2Encrypt(sourceStr) {
|
return '04' + sm2.doEncrypt(sourceStr, process.env.VUE_APP_PUBLICKEY, 1)
|
},
|
//根据账号获取手机号码
|
blurUserName(event) {
|
let _value = event.target.value;
|
if (!_value || !/^[0-9a-zA-Z_]{2,32}$/.test(_value)) {
|
return;
|
}
|
ChangePwdApi.getInfo({ userAccount: _value }).then(res => {
|
if (res.code === '0' && res.data) {
|
this.form.phone = res.data.phone;
|
}
|
}).catch((error) => {
|
if (error.data.code === '500') {
|
this.$message({
|
message: '当前账户未关联手机号,请联系管理员',
|
type: 'warning',
|
});
|
}
|
})
|
},
|
//账号变更清空手机号码
|
inputUserName() {
|
this.$refs['phone'].resetField();
|
this.$refs['verificationCode'].resetField();
|
this.btnColor = '#409eff';
|
this.btnTxt = "获取验证码";
|
this.time = 0;
|
},
|
//操作token
|
handleToken(token) {
|
if (token) {
|
window.localStorage.setItem('mall-operate-access_token', token)
|
} else {
|
window.localStorage.removeItem('mall-operate-access_token')
|
}
|
},
|
//获取验证码
|
getCode() {
|
if (!this.form.phone) {
|
//手机号码校验不通过
|
this.$message({
|
message: '手机号码不能为空!',
|
type: 'warning',
|
});
|
} else if (!/^[1][3,4,5,6,7,8,9][0-9]{9}$/.test(this.form.phone)) {
|
this.$message({
|
message: '请输入正确的手机号码',
|
type: 'warning',
|
});
|
} else {
|
this.sendVerificationCode();
|
}
|
},
|
//发送验证码
|
async sendVerificationCode() {
|
try {
|
let params = {
|
phone: this.form.phone
|
}
|
const res = await ChangePwdApi.sendVerificationCode(params)
|
if (res.code === '0') {
|
//【手机号码校验通过】发送axios请求 获取验证码
|
this.time = 60; //设置倒计时的时间(单位:秒)
|
this.disabled = true;
|
this.timer(); //调用倒计时方法
|
}
|
} catch (error) { }
|
},
|
//倒计时方法
|
timer() {
|
if (this.time > 0) {
|
this.time--;
|
this.btnTxt = this.time + "s后重新获取验证码";
|
this.btnColor = '#c0c4cc';
|
setTimeout(this.timer, 1000);
|
} else {
|
this.time = 0;
|
this.btnTxt = "获取验证码";
|
this.btnColor = '#409eff';
|
this.disabled = false;
|
}
|
},
|
//提交密码
|
submit() {
|
this.$refs.form.validate(valid => {
|
if (valid) {
|
this.byCodeGetToken();
|
} else {
|
console.log('error submit!!')
|
return false
|
}
|
})
|
},
|
//核销验证码并生成token
|
async byCodeGetToken() {
|
try {
|
let params = {
|
phone: this.form.phone,
|
userAccount: this.form.userName,
|
verificationCode: this.form.verificationCode
|
}
|
const res = await ChangePwdApi.checkVerificationCodeAndGetToken(params)
|
if (res.code === '0' && res.data) {
|
this.handleToken(res.data);
|
this.handleUpdatePwd();
|
}
|
} catch (error) {
|
if (error.data.code === '500') {
|
this.$message({
|
message: '验证码错误,请重新输入',
|
type: 'warning',
|
});
|
}
|
}
|
},
|
//修改密码
|
handleUpdatePwd() {
|
let params = {
|
password: this.doSm3AndSm2Encrypt(this.form.newPassword),
|
confirmPassword: this.doSm3AndSm2Encrypt(this.form.checkPassword),
|
token: window.localStorage.getItem('mall-operate-access_token'),
|
userAccount: this.form.userName,
|
}
|
if (this.loading) {
|
return
|
}
|
this.loading = true
|
ChangePwdApi.changePsCode(params).then(res => {
|
if (res.code === '0') {
|
this.$message({
|
message: '密码修改成功,请重新登录',
|
type: 'success',
|
});
|
this.logout()
|
this.dialogVisible = false;
|
} else {
|
this.handleToken();
|
}
|
this.loading = false
|
}).catch(() => {
|
this.handleToken();
|
this.loading = false
|
})
|
},
|
//初始化
|
initData() {
|
for (const key in this.form) {
|
this.form[key] = null
|
}
|
this.btnColor = '#409eff';
|
this.btnTxt = "获取验证码";
|
this.time = 0;
|
},
|
}
|
}
|
</script>
|
|
<style>
|
</style>
|