SettingButton.vue
5.01 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
<template>
<div>
<el-popover
placement="bottom"
:width="50"
trigger="click"
>
<template #reference>
<i class="el-icon-setting"></i>
</template>
<ul>
<li @click="closePwdSetting(true)">修改密码</li>
<li @click="logout">退出登录</li>
</ul>
</el-popover>
<el-dialog title="设置密码" v-model="dialogFormVisible">
<el-form :model="ruleForm" :rules="rules" ref="ruleForm">
<el-form-item label="原密码" :label-width="formLabelWidth" prop="oldPassword" required>
<el-input v-model="ruleForm.oldPassword" maxLength="20" placeholder="请输入原密码" show-password></el-input>
</el-form-item>
<el-form-item label="新密码" :label-width="formLabelWidth" prop="newPassword" required>
<el-input v-model="ruleForm.newPassword" maxLength="20" placeholder="请输入新密码" show-password></el-input>
</el-form-item>
<el-form-item label="确认密码" :label-width="formLabelWidth" prop="confirmedPwd" required>
<el-input v-model="ruleForm.confirmedPwd" maxLength="20" placeholder="请再次确认新密码" show-password></el-input>
</el-form-item>
</el-form>
<template #footer>
<span class="dialog-footer">
<el-button @click="closePwdSetting(false)">取 消</el-button>
<el-button type="primary" @click="submitPwdBtn('ruleForm')">确 定</el-button>
</span>
</template>
</el-dialog>
</div>
</template>
<script>
// import {
// SettingOutlined,
// } from '@ant-design/icons-vue';
import store from '@/store';
import LoginService from '@/services/Login';
export default {
name: 'SettingButton',
components: {
// SettingOutlined
},
data(){
var validatePass = (rule, value, callback) => {
if (value !== this.ruleForm.newPassword) {
callback(new Error('两次输入密码不一致!'));
} else {
callback();
}
};
return {
dialogFormVisible: false,
rules: {
oldPassword: [
{ required: true, message: '请输入原密码', trigger: 'blur' },
],
newPassword: [
{ required: true, message: '请输入新密码', trigger: 'blur' },
],
confirmedPwd: [
{ required: true, message: '请再次确认新密码', trigger: 'blur' },
{ validator: validatePass, trigger: 'blur' }
],
},
ruleForm: {
oldPassword: '',
newPassword: '',
confirmedPwd: ''
},
formLabelWidth: '80px'
}
},
methods: {
closePwdSetting(status) {
this.dialogFormVisible = status;
},
logout() {
localStorage.removeItem('token');
localStorage.removeItem('userInfo');
localStorage.removeItem('breadcrumb');
store.dispatch('app/setToken', '');
setTimeout(()=>{
window.location.reload();
}, 300)
},
sendData() {
const loginService = new LoginService();
let { oldPassword, newPassword } = this.ruleForm;
let params = { oldPassword, newPassword };
let userInfo = localStorage.getItem('userInfo');
if(userInfo) {
userInfo = JSON.parse(userInfo);
params['userId'] = userInfo.id;
}
loginService.changePassword(params).then((res) => {
let { code, message } = res;
if(code == 200) {
this.$message.success({
message: '修改密码成功',
type: 'success'
});
this.closePwdSetting(true);
} else {
this.$notify.error({
title: '修改密码失败',
message,
duration: 1000
});
}
})
},
submitPwdBtn(form) {
this.$refs[form].validate((valid) => {
if (valid) {
this.sendData();
} else {
return false;
}
});
}
}
}
</script>
<style lang="less" scoped>
@import 'styles/common.less';
@import 'styles/variables.less';
.el-icon-setting {
margin-right: 20px;
font-size: 25px;
}
ul {
margin: 0;
padding: 0;
list-style: none;
li {
text-align: center;
height: 30px;
line-height: 30px;
&:hover {
color: @defaultTextColor;
}
}
}
</style>