ResetPassword.vue
6.48 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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
<template>
<el-dialog
title="重置密码"
:visible.sync="dialogVisible"
width="30%"
append-to-body
:before-close="handleClose"
>
<el-form
ref="resetPasswordForm"
:model="resetPasswordForm"
:rules="passwordRules"
label-width="120px"
status-icon
class="resetPasswordForm"
>
<el-form-item label="旧密码:" prop="old">
<el-input
v-model="resetPasswordForm.old"
type="password"
placeholder="请输入旧密码"
></el-input>
</el-form-item>
<el-form-item label="新密码:" prop="new">
<el-input
v-model="resetPasswordForm.new"
type="password"
placeholder="请输入新密码"
></el-input>
</el-form-item>
<el-form-item label="密码确认:" prop="newRepeat">
<el-input
v-model="resetPasswordForm.newRepeat"
type="password"
placeholder="请再次输入新密码"
></el-input>
</el-form-item>
</el-form>
<div slot="footer" class="dialog-footer__wrap">
<el-button size="small" @click="handleClose">取消</el-button>
<el-button
size="small"
type="primary"
:disabled="isDisabled"
@click="handleConfirm"
>
确定
</el-button>
</div>
</el-dialog>
</template>
<script>
import {
getDefaultPwdStrategy,
resetPassword,
validateOldPassword,
} from '@/api/uc.js'
import { recordRoute } from '@/config'
import { encryptWithRsa } from '@/utils'
export default {
name: 'ResetPassword',
props: {
account: {
type: String,
default: '',
},
},
data() {
const validateNew = (rule, value, callback) => {
if (value === '') {
callback(new Error('请输入新密码'))
}
const { pwdRule, pwdLength, enable } = this.pwdStrategy
if (enable === 1) {
if (pwdRule) {
if (value.length < pwdLength) {
callback(new Error('新密码长度至少为' + pwdLength))
}
if ([2, 3, 4].includes(pwdRule)) {
this.validatePassword(pwdRule, value, callback)
} else {
callback()
}
}
} else {
if (value.length < 6) {
callback(new Error('新密码长度至少为6'))
} else {
callback()
}
}
}
const validateOld = (rule, value, callback) => {
if (value === '') {
callback(new Error('请输入旧密码'))
} else {
callback()
}
}
const validateNewRepeat = (rule, value, callback) => {
if (value === '') {
callback(new Error('请再次输入新密码'))
} else if (value !== this.resetPasswordForm.new) {
callback(new Error('两次输入的密码不一致!'))
} else {
callback()
}
}
const validateOldPwd = (rule, value, callback) => {
const params = {
oldPwd: encryptWithRsa(this.resetPasswordForm.old),
}
validateOldPassword(params).then((res) => {
if (res.value) {
callback()
} else {
callback(new Error('请输入正确的密码'))
}
})
}
return {
dialogVisible: false,
resetPasswordForm: {
old: '',
new: '',
newRepeat: '',
},
passwordRules: {
old: [
{ validator: validateOld, trigger: 'blur' },
{ validator: validateOldPwd, trigger: 'blur' },
{ required: true, trigger: 'blur' },
],
new: [
{ validator: validateNew, trigger: 'blur' },
{ required: true, trigger: 'blur' },
],
newRepeat: [
{ validator: validateNewRepeat, trigger: 'blur' },
{ required: true, trigger: 'blur' },
],
},
pwdStrategy: {},
isDisabled: false,
}
},
methods: {
getDefaultPasswordStrategy() {
getDefaultPwdStrategy().then((res) => {
this.pwdStrategy = res
})
},
handleClose() {
this.$refs.resetPasswordForm.clearValidate()
this.dialogVisible = false
this.resetPasswordForm = {
old: '',
new: '',
newRepeat: '',
}
this.logout()
},
handleConfirm() {
this.$refs['resetPasswordForm'].validate((valid) => {
if (valid) {
if (!this.account) {
return this.$message.warning('请返回重新登录')
}
this.isDisabled = true
const params = {
account: this.account,
oldPwd: this.resetPasswordForm.old,
newPwd: this.resetPasswordForm.new,
}
resetPassword(params).then((res) => {
if (res.state) {
this.$message.success(res.message)
this.isDisabled = false
this.$emit('reset-success')
this.dialogVisible = false
} else {
this.$message.warning(res.message)
this.isDisabled = false
}
})
} else {
this.$message.warning('密码校验失败,请检查密码')
}
})
},
openResetPasswordDialog() {
this.dialogVisible = true
this.getDefaultPasswordStrategy()
},
validatePassword(pwdRule, value, callback) {
const passwordRuleMap = {
2: new RegExp('(?=.*[0-9])(?=.*[a-zA-Z])'),
3: new RegExp('(?=.*[0-9])(?=.*[a-zA-Z])(?=.*[^a-zA-Z0-9])'),
4: new RegExp('(?=.*[0-9])(?=.*[A-Z])(?=.*[a-z])(?=.*[^a-zA-Z0-9])'),
}
const passwordTipMap = {
2: '新密码必须包含数字、字母',
3: '新密码必须包含数字、字母、特殊字符',
4: '新密码必须包含数字、大小写字母、特殊字符',
}
if (!passwordRuleMap[pwdRule].test(value)) {
callback(new Error(passwordTipMap[pwdRule]))
} else {
callback()
}
},
async logout() {
await this.$store.dispatch('user/logout')
if (recordRoute) {
const fullPath = this.$route.fullPath
this.$router.push(`/login?redirect=${fullPath}`)
} else {
this.$router.push('/login')
}
},
},
}
</script>
<style lang="scss" scoped></style>