ResetPassword.vue
7.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
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
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
<template>
<div class="auth-bg">
<div class="auth-container">
<!-- 返回按钮 -->
<div class="back-button" @click="goBack">
<img src="/zuo.svg" alt="goBack" class="input-icon-svg" />
</div>
<!-- 头部Logo与标题 -->
<div class="auth-header">
<div class="logo">
<img src="/newLogo.png" alt="Logo" class="logo-img" />
</div>
<h1 class="auth-title">{{ t("resetPassword.title") }}</h1>
<p class="auth-subtitle">{{ t("resetPassword.subtitle") }}</p>
</div>
<!-- 重置密码表单(只保留新密码与确认密码) -->
<el-form
:model="form"
:rules="rules"
ref="resetFormRef"
class="reset-form"
label-position="top"
@submit.prevent="onSubmit"
>
<el-form-item prop="newPassword" :label="t('resetPassword.newPasswordLabel')">
<el-input
v-model="form.newPassword"
:placeholder="t('resetPassword.newPasswordPlaceholder')"
show-password
clearable
>
<template #prefix>
<img src="/mima.svg" alt="password" class="input-icon-svg" />
</template>
</el-input>
</el-form-item>
<el-form-item prop="confirmPassword" :label="t('resetPassword.confirmPasswordLabel')">
<el-input
v-model="form.confirmPassword"
:placeholder="t('resetPassword.confirmPasswordPlaceholder')"
show-password
clearable
>
<template #prefix>
<img src="/mima.svg" alt="password" class="input-icon-svg" />
</template>
</el-input>
</el-form-item>
<el-form-item>
<el-button
type="primary"
class="submit-btn"
@click="onSubmit"
:loading="authStore.loading"
round
native-type="button"
>
{{ t("resetPassword.resetBtn") }}
</el-button>
</el-form-item>
</el-form>
<!-- 操作链接:返回登录 -->
<div class="action-links">
<el-link @click="goBack" type="info">
{{ t("resetPassword.backToLogin") }}
</el-link>
</div>
</div>
</div>
</template>
<script setup lang="ts">
import { ref, reactive, computed, onMounted } from "vue";
import { useRouter, useRoute } from "vue-router";
import { useI18n } from "vue-i18n";
import { ElMessage, type FormInstance, type FormRules } from "element-plus";
import { useAuthStore } from "@/stores/auth";
const router = useRouter();
const route = useRoute();
const { t } = useI18n();
const authStore = useAuthStore();
const emit = defineEmits<{
(e: "switch-to-login"): void;
}>();
const resetFormRef = ref<FormInstance>();
const form = reactive({
newPassword: "",
confirmPassword: "",
});
// 从邮件链接带入 ?token=...
const token = computed(() => {
return (route.query.token as string) || "";
});
// 密码确认验证
const validateConfirmPassword = (_rule: any, value: string, callback: any) => {
if (value !== form.newPassword) {
callback(new Error(t("resetPassword.passwordMismatch")));
} else {
callback();
}
};
const rules = computed<FormRules>(() => ({
newPassword: [
{
required: true,
message: t("resetPassword.newPasswordRequired"),
trigger: "blur",
},
{
min: 8,
message: t("resetPassword.passwordMinLength"),
trigger: "blur",
},
],
confirmPassword: [
{
required: true,
message: t("resetPassword.confirmPasswordRequired"),
trigger: "blur",
},
{ validator: validateConfirmPassword, trigger: "blur" },
],
}));
const onSubmit = async () => {
try {
authStore.clearError();
if (!resetFormRef.value) return;
const valid = await resetFormRef.value.validate();
if (!valid) return;
if (!token.value) {
ElMessage.error(t("resetPassword.tokenInvalid"));
goBack();
return;
}
const result = await authStore.resetPassword({
token: token.value,
newPassword: form.newPassword,
});
if (result.success) {
ElMessage.success(t("resetPassword.success"));
setTimeout(() => {
goBack();
}, 1200);
} else {
ElMessage.error(result.message || t("resetPassword.resetFailed"));
}
} catch (e) {
console.error("重置密码错误:", e);
ElMessage.error(t("resetPassword.resetFailed"));
}
};
const goBack = () => {
// 回到登录页;如果在 Auth 容器内可配合切卡
emit("switch-to-login");
if (route?.path !== "/auth") {
router.push("/auth");
}
};
onMounted(() => {
authStore.clearError();
// 没有 token 直接回登录
if (!token.value) {
ElMessage.warning(t("resetPassword.tokenMissing"));
goBack();
}
});
</script>
<style scoped>
.auth-bg {
min-height: 100vh;
background: var(--color-login-bg);
display: flex;
align-items: center;
justify-content: center;
position: relative;
}
.auth-container {
background: var(--color-login-card);
border-radius: 20px;
padding: 40px 32px 32px 32px;
max-width: 420px;
width: 90vw;
box-shadow: var(--box-shadow);
position: relative;
z-index: 1;
}
.back-button {
position: absolute;
top: 20px;
left: 20px;
width: 32px;
height: 32px;
display: flex;
align-items: center;
justify-content: center;
cursor: pointer;
border-radius: 8px;
transition: background-color 0.3s;
z-index: 10;
}
.back-button:hover {
background-color: rgba(255, 255, 255, 0.1);
}
.input-icon-svg {
width: 20px;
height: 20px;
object-fit: contain;
}
.auth-header {
text-align: center;
margin-bottom: 32px;
}
.logo {
width: 64px;
height: 64px;
display: flex;
align-items: center;
justify-content: center;
margin: 0 auto 20px;
background: transparent;
border-radius: 0;
box-shadow: none;
overflow: visible;
}
.logo-img {
width: 100%;
height: 100%;
object-fit: contain;
border-radius: 0;
}
.auth-title {
font-size: 28px;
font-weight: 700;
margin-bottom: 8px;
background: linear-gradient(135deg, #165dff 0%, #4a7cff 100%);
background-clip: text;
-webkit-background-clip: text;
-webkit-text-fill-color: transparent;
line-height: 1.2;
padding-bottom: 2px;
}
.auth-subtitle {
font-size: 16px;
color: #cccccc;
margin-bottom: 10px;
}
.reset-form {
margin-bottom: 24px;
}
.reset-form :deep(.el-form-item__label) {
color: var(--el-text-color-regular);
font-weight: 500;
margin-bottom: 8px;
}
.reset-form :deep(.el-input__wrapper) {
background-color: var(--el-fill-color-light);
box-shadow: none;
border: 1px solid var(--el-border-color);
border-radius: 8px;
padding: 8px 12px;
transition: border-color 0.3s;
}
.reset-form :deep(.el-input__wrapper:hover) {
border-color: var(--el-color-primary);
}
.reset-form :deep(.el-input__wrapper.is-focus) {
border-color: var(--el-color-primary);
box-shadow: 0 0 0 2px rgba(22, 93, 255, 0.1);
}
.reset-form :deep(.el-input__inner) {
color: var(--el-text-color-primary);
}
.reset-form :deep(.el-input__prefix) {
display: flex;
align-items: center;
margin-right: 8px;
}
.submit-btn {
width: 100%;
font-size: 16px;
height: 44px;
border-radius: 12px;
background: linear-gradient(135deg, #165dff 0%, #4a7cff 100%);
border: none;
font-weight: 600;
margin-top: 8px;
}
.submit-btn:hover {
opacity: 0.9;
}
.action-links {
display: flex;
justify-content: center;
gap: 12px;
align-items: center;
margin-top: 16px;
}
.action-links :deep(.el-link) {
font-size: 14px;
}
</style>