password.vue
3.42 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
<template>
<div class="password">
<Form ref="formInline" :label-width="120" :model="formInline" :rules="ruleInline">
<Form-item prop="passwd" label="输入密码">
<Input type="password" size="large" v-model="formInline.passwd" placeholder="输入密码" @on-enter="handleSubmit('formInline')">
</Input>
</Form-item>
<Form-item prop="passwdCheck" label="再次输入密码">
<Input type="password" size="large" v-model="formInline.passwdCheck" placeholder="再次输入密码" @on-enter="handleSubmit('formInline')">
</Input>
</Form-item>
<Form-item class="login-btn">
<Button type="primary" :loading="loading" @click="handleSubmit('formInline')">
确认修改
</Button>
</Form-item>
</Form>
</div>
</template>
<script>
import UserService from 'services/user/user-service';
import Vue from 'vue';
export default {
name: 'password',
data() {
const validatePass = (rule, value, callback) => {
if (value === '') {
return callback(new Error('请输入密码'));
} else if (!/^(?=.*[a-z])(?=.*[A-Z])(?=.*\d)(?=.*[\W_])[a-zA-Z\d\W_]{8,}$/.test(value)) {
return callback(new Error('密码长度必须大于八位,且包含大写字母,小写字母,数字和特殊字符'));
} else {
if (this.formInline.passwdCheck !== '') {
// 对第二个密码框单独验证
this.$refs.formInline.validateField('passwdCheck');
}
return callback();
}
};
const validatePassCheck = (rule, value, callback) => {
if (value === '') {
return callback(new Error('请再次输入密码'));
} else if (value !== this.formInline.passwd) {
return callback(new Error('两次输入密码不一致!'));
} else {
return callback();
}
};
return {
loading: false,
formInline: {
passwd: '',
password: ''
},
ruleInline: {
passwd: [
{ validator: validatePass, trigger: 'change' }
],
passwdCheck: [
{ validator: validatePassCheck, trigger: 'change' }
]
}
};
},
created() {
this.UserService = new UserService();
},
methods: {
handleSubmit(name) {
this.$refs[name].validate((valid) => {
if (valid) {
this.update(this.formInline.passwdCheck);
} else {
this.$Message.error('请按照规则输入密码!');
}
});
},
update(passwdCheck) {
this.loading = true;
this.UserService.update({
password: passwdCheck
}).then(ret => {
this.loading = false;
if (ret.code === 200) {
Vue.$store.remove('needUpdate');
this.$router.push('/');
} else {
this.$Message.error(ret.message);
}
});
}
}
};
</script>
<style lang="scss">
.password {
width: 500px;
}
</style>