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
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
<template>
<div class="app_content">
<a-spin :spinning="loading" style="width: 100%;height: 100%;">
<a-form-model ref="form" :model="formData" :rules="formRules" class="from-table">
<a-row>
<a-col :span="6" class="bg-gray">
<div class="required">原密码</div>
</a-col>
<a-col :span="18">
<a-form-model-item prop="oldPassword">
<a-input type="password" placeholder="请输入原密码" v-model="formData.oldPassword" :readOnly="readOnly" @click="() => {this.readOnly=false }" @check="() => { this.readOnly=false }" @blur="() => { this.readOnly=false }" style="width: 300px" ></a-input>
</a-form-model-item>
</a-col>
</a-row>
<a-row>
<a-col :span="6" class="bg-gray">
<div class="required">新密码</div>
</a-col>
<a-col :span="18">
<a-form-model-item prop="newPassword">
<a-input type="password" placeholder="请输入新密码" v-model="formData.newPassword" @change="PassworcChanged" style="width: 300px" ></a-input>
</a-form-model-item>
</a-col>
</a-row>
<a-row>
<a-col :span="24">
<table border="1" cellpadding="1" cellspacing="1" style="background-color:#e0f0ff; line-height: 20px; margin-top: 10px;">
<tr>
<td id="strength_L" style="width: 150px; height: 10px;" align="center">弱</td>
<td id="strength_M" style="width: 150px; height: 10px;" align="center">中</td>
<td id="strength_H" style="width: 150px; height: 10px;" align="center">强</td>
</tr>
</table>
</a-col>
</a-row>
<a-row>
<a-col :span="6" class="bg-gray">
<div class="required">确认新密码</div>
</a-col>
<a-col :span="18">
<a-form-model-item prop="checkPwd">
<a-input type="password" placeholder="请确认新密码" v-model="formData.checkPwd" style="width: 300px" ></a-input>
</a-form-model-item>
</a-col>
</a-row>
<a-row>
<a-col :span="24" style="text-align: center;">
<a-button @click="closeWindow">取消</a-button>
<a-button type="primary" style="margin-left: 8px" @click="submit">保存</a-button>
</a-col>
</a-row>
</a-form-model>
</a-spin>
</div>
</template>
<script>
import { removeToken } from '@/views/utils/auth'
import { isEmptyParams } from "@/views/utils/common"
export default {
name: "updatePwd",
data () {
return {
loading: false,
readOnly: true,
// form表单
formData: {
userId: undefined,
oldPassword: undefined,
newPassword: undefined,
checkPwd: undefined,
},
formRules: {
oldPassword: [
{ required: true, message: '请输入原密码' }
],
newPassword: [
{ required: true, message: '请输入新密码' },
{
validator: (rule, value, callback) => {
if (value == '' || value == undefined) {
callback()
}
let ckPwd = /^(?=.*\d)(?=.*[a-zA-Z]).{6,12}$/;
if (ckPwd.test(value)) {
callback()
} else {
callback(new Error('密码只能由数字和字母混合组成,长度为6-12位~'));
}
}
},
],
checkPwd: [
{ required: true, message: '请确认新密码' },
{
validator: (rule, value, callback) => {
if (value == '' || value == undefined) {
callback()
}
if (value == this.formData.newPassword) {
callback()
} else {
callback(new Error('与新密码不一致!'));
}
},
},
],
},
}
},
created () {
},
methods: {
PassworcChanged() {
this.pwStrength();
},
//CharMode函数
//测试某个字符是属于哪一类.
CharMode(iN) {
if (iN >= 48 && iN <= 57) //数字
return 1;
if (iN >= 65 && iN <= 90) //大写字母
return 2;
if (iN >= 97 && iN <= 122) //小写
return 4;
else
return 8; //特殊字符
},
//bitTotal函数
//计算出当前密码当中一共有多少种模式
bitTotal(num) {
let modes = 0;
for (var i = 0; i < 4; i++) {
if (num & 1) modes++;
num >>>= 1;
}
return modes;
},
//checkStrong函数
//返回密码的强度级别
checkStrong(sPW) {
if (sPW.length < 6)
return 0; //密码太短
let Modes = 0;
for (var i = 0; i < sPW.length; i++) {
//测试每一个字符的类别并统计一共有多少种模式.
Modes |= this.CharMode(sPW.charCodeAt(i));
}
return this.bitTotal(Modes);
},
//pwStrength函数
//当用户放开键盘或密码输入框失去焦点时,根据不同的级别显示不同的颜色
pwStrength() {
var pwd = this.formData.newPassword;
let Lcolor = "";
let Mcolor = "";
let Hcolor = "";
if (pwd == "") {
Lcolor = Mcolor = Hcolor = "#e0f0ff";
return;
}
var O_color = "#e0f0ff";
var L_color = "#FF0000";
var M_color = "#FF9900";
var H_color = "#33CC00";
if (pwd == null || pwd == '') {
Lcolor = Mcolor = Hcolor = O_color;
return;
}
var S_level = this.checkStrong(pwd);
switch (S_level) {
case 0:
Lcolor = Mcolor = Hcolor = O_color;
break;
case 1:
Lcolor = L_color;
Mcolor = Hcolor = O_color;
break;
case 2:
Lcolor = Mcolor = M_color;
Hcolor = O_color;
break;
default:
Lcolor = Mcolor = Hcolor = H_color;
break;
}
document.getElementById("strength_L").style.background = Lcolor;
document.getElementById("strength_M").style.background = Mcolor;
document.getElementById("strength_H").style.background = Hcolor;
},
CheckPasswordStrong() {
var pwdOld = this.formData.oldPassword;
var pwd1 = this.formData.newPassword;
var pwd2 = this.formData.checkPwd;
if (pwdOld == "") {
alert("请填写原密码!");
return false;
}
if (pwd1 == "") {
alert("请填写新密码!");
return false;
}
if (pwd2 == "") {
alert("请填写确认密码!");
return false;
}
var S_level = this.checkStrong(pwd1);
if (S_level >= 2)
return true;
else {
alert("密码安全等级不够,请重新填写!");
return false;
}
},
submit () {
let vm = this
this.$refs.form.validate(valid => {
if (valid) {
let pars = isEmptyParams(vm.formData)
let par = { ...pars }
vm.$api.base.updatePwd(par).then(data => {
if (data) {
alert("修改成功,请重新登录!")
window.sessionStorage.clear();
removeToken()
window.location.href = "/"
}
})
}
});
},
closeWindow () {
this.$emit('close', 'close')
}
}
}
</script>
<style scoped lang="less">
.tab_input_r {
.ant-form-item {
margin: 0px 0px !important;
}
.ant-input {
margin-top: 20px;
width: 180px;
}
.ant-form-item-control {
width: 180px !important;
}
.ant-row {
height: 80px;
}
}
</style>