PasswordModify.vue
5.78 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
<template>
<div>
<el-dialog
top="100px"
width="600px"
:title="$t('Navigator.ChangePassword')"
append-to-body
:visible="passwordModifyVisible"
:close-on-click-modal="false"
@close="closeDialog"
>
<el-form
ref="pwdForm"
:model="pwdForm"
:rules="rules"
label-width="120px"
>
<el-form-item :label="$t('Navigator.OldPassword')" prop="old">
<el-input
v-model="pwdForm.old"
clearable
type="password"
:placeholder="$t('Navigator.PleaseEnterYourOldPassword')"
></el-input>
</el-form-item>
<el-form-item :label="$t('Navigator.NewPassword')" prop="new">
<el-input
v-model="pwdForm.new"
clearable
type="password"
:placeholder="$t('Navigator.PleaseEnterANewPassword')"
></el-input>
</el-form-item>
<el-form-item
:label="$t('Navigator.RepeatNewPassword')"
prop="newRepeat"
>
<el-input
v-model="pwdForm.newRepeat"
clearable
type="password"
:placeholder="$t('Navigator.PleaseEnterTheNewPasswordAgain')"
></el-input>
</el-form-item>
</el-form>
<div slot="footer">
<el-button @click="closeDialog">
{{ $t('Common.Cancel') }}
</el-button>
<el-button type="primary" @click="submitForm">
{{ $t('Common.Confirm') }}
</el-button>
</div>
</el-dialog>
</div>
</template>
<script>
import { encryptWithRsa } from '@/utils'
import { updatePassword } from '@/api/user'
import { validateOldPassword } from '@/api/uc.js'
export default {
name: 'PasswordModify',
props: {
accountInfo: {
type: Object,
default: null,
},
passwordModifyVisible: {
type: Boolean,
default: false,
},
},
data() {
const checkOld = (rule, value, cb) => {
if (value === '') {
cb(new Error('请输入旧密码'))
} else {
cb()
}
}
const checkNewRepeat = (rule, value, cb) => {
if (value === '') {
cb(new Error('请再次输入新密码'))
} else if (value !== this.pwdForm.new) {
cb(new Error('两次输入密码不一致!'))
} else {
cb()
}
}
const checkNew = (rule, value, cb) => {
if (value === '') {
cb(new Error('请输入新密码'))
}
const { pwdStrategy: config } = this.userInfo
if (config && config.enable == 1) {
const { pwdRule, pwdLength } = config
if (pwdRule) {
if (value.length < pwdLength) {
cb(new Error('新密码长度至少为' + pwdLength))
} else if (pwdRule == 2) {
var re = new RegExp('(?=.*[0-9])(?=.*[a-zA-Z])')
if (!re.test(value)) {
cb(new Error('新密码必须包含数字、字母'))
} else {
cb()
}
} else if (pwdRule == 3) {
var re = new RegExp('(?=.*[0-9])(?=.*[a-zA-Z])(?=.*[^a-zA-Z0-9])')
if (!re.test(value)) {
cb(new Error('新密码必须包含数字、字母、特殊字符'))
} else {
cb()
}
} else if (pwdRule == 4) {
var re = new RegExp(
'(?=.*[0-9])(?=.*[A-Z])(?=.*[a-z])(?=.*[^a-zA-Z0-9])'
)
if (!re.test(value)) {
cb(new Error('新密码必须包含数字、大小字母、特殊字符'))
} else {
cb()
}
} else {
cb()
}
}
} else {
if (value.length < 6) {
cb(new Error('新密码长度至少为6'))
} else if (value === this.pwdForm.old) {
cb(new Error('新密码不能与旧密码相同'))
} else {
cb()
}
}
}
const validateOldPwd = (rule, value, callback) => {
const params = {
oldPwd: encryptWithRsa(this.pwdForm.old),
}
validateOldPassword(params).then((res) => {
if (res.value) {
callback()
} else {
callback(new Error('请输入正确的密码'))
}
})
}
return {
userInfo: {},
pwdForm: {
old: '',
new: '',
newRepeat: '',
},
rules: {
old: [
{ validator: checkOld, trigger: 'blur' },
{ validator: validateOldPwd, trigger: 'blur' },
],
new: [{ validator: checkNew, trigger: 'blur' }],
newRepeat: [{ validator: checkNewRepeat, trigger: 'blur' }],
},
}
},
watch: {
passwordModifyVisible(val) {
if (val && this.accountInfo) {
this.userInfo = this.accountInfo.user
}
},
},
methods: {
closeDialog() {
this.$emit('update:passwordModifyVisible', false)
},
submitForm() {
this.$refs['pwdForm'].validate((valid) => {
if (valid) {
const jsonData = {
oldPwd: encryptWithRsa(this.pwdForm.old),
newPwd: encryptWithRsa(this.pwdForm.new),
}
updatePassword(jsonData).then((res) => {
if (res.state) {
this.closeDialog()
this.$message.success(res.message)
setTimeout(() => {
this.$emit('logout')
}, 300)
} else {
this.$message.error(res.message)
}
})
}
})
},
},
}
</script>
<style lang="scss" scoped></style>