PasswordStrengthContent.vue
4.22 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
<!-- PasswordStrengthContent.vue -->
<template>
<div class="password-strength-content">
<div class="strength-label">密码需满足以下要求:</div>
<div class="strength-bar">
<div
class="strength-fill"
:class="levelClass"
:style="{ width: fillWidth }"
></div>
</div>
<div class="strength-text" :class="levelClass">
密码强度:{{ strengthText }}
</div>
<div class="requirements">
<div v-for="(req, key) in requirements" :key="key" class="requirement" :class="getReqClass(key)">
<span class="requirement-icon">{{ getReqIcon(key) }}</span>
<span>{{ req.label }}</span>
</div>
</div>
</div>
</template>
<script setup lang="ts">
import { computed } from 'vue';
import { useStorage } from '@/hooks/useStorage'
const props = defineProps({
password: {
type: String,
default: ''
},
storeKey: {
type: String,
default: 'firstUnmetRequirement'
}
});
const emits = defineEmits(['getCheckResult']);
const { setStorage } = useStorage()
const checkPasswordStrength = (pwd) => {
if (!pwd) return { score: 0, level: 'weak', checks: {} };
const checks = {
length: pwd.length >= 8,
lower: /[a-z]/.test(pwd),
upper: /[A-Z]/.test(pwd),
number: /[0-9]/.test(pwd),
special: /[!@#$%^&*()_+\-=\[\]{};':"\\|,.<>\/?]/.test(pwd)
};
const score = Object.values(checks).filter(Boolean).length;
let level = 'weak';
if (score >= 5) level = 'strong';
else if (score >= 3) level = 'medium';
console.log(checks,'checkPasswordStrength');
findFirstUnmetRequirement(checks)
return { score, level, checks };
};
const result = computed(() => checkPasswordStrength(props.password));
const fillWidth = computed(() => `${(result.value.score / 5) * 100}%`);
const strengthText = computed(() => ({ weak: '弱', medium: '中', strong: '强' })[result.value.level]);
const levelClass = computed(() => result.value.level);
const requirements = {
number: { label: '包含数字 (0-9)' },
length: { label: '至少 8 个字符' },
lower: { label: '包含小写字母 (a-z)' },
upper: { label: '包含大写字母 (A-Z)' },
special: { label: '包含特殊符号' }
};
const getReqClass = (key) => (result.value.checks[key] ? 'met' : props.password.length >= 3 ? 'unmet' : 'pending');
const getReqIcon = (key) => {
if (result.value.checks[key]) return '✓';
if (props.password.length >= 3) return '✗';
return '○';
};
/**
* 匹配未满足的规则
* @param checks
*/
const findFirstUnmetRequirement = (checks) => {
if (!props.password) return null;
console.log(checks,'firstUnmetRequirement');
for (const key in requirements) {
const isMet = checks[key];
if (!isMet) {
let label = requirements[key].label;
setStorage(props.storeKey, label)
return label; // 返回未满足的第一条规则的 label
}
}
setStorage(props.storeKey, '')
return null; // 全部满足
};
// 暴露方法给父组件
defineExpose({
});
</script>
<style scoped lang="scss">
/* 同前,省略样式 */
.password-strength-content {
font-size: 14px;
}
.strength-label {
margin-bottom: 12px;
color: #333;
font-weight: 500;
}
.strength-bar {
height: 6px;
border-radius: 3px;
background: #e9ecef;
overflow: hidden;
margin: 8px 0;
}
.strength-fill {
height: 100%;
border-radius: 3px;
transition: all 0.3s ease-out;
}
.strength-fill.weak {
background-color: #dc3545;
}
.strength-fill.medium {
background-color: #ffc107;
}
.strength-fill.strong {
background-color: #28a745;
}
.strength-text {
font-weight: 600;
font-size: 13px;
margin-bottom: 12px;
}
.strength-text.weak {
color: #dc3545;
}
.strength-text.medium {
color: #e0a800;
}
.strength-text.strong {
color: #28a745;
}
.requirements {
color: #555;
}
.requirement {
display: flex;
align-items: center;
margin: 4px 0;
}
.requirement-icon {
width: 16px;
text-align: center;
margin-right: 6px;
font-weight: bold;
}
.requirement.met .requirement-icon {
color: #28a745;
}
.requirement.unmet .requirement-icon {
color: #dc3545;
}
.requirement.pending .requirement-icon {
color: #adb5bd;
}
.requirement.met {
color: #28a745;
}
.requirement.unmet {
color: #dc3545;
}
.requirement.pending {
color: #6c757d;
}
</style>