LoginForm.vue
9.28 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
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
<template>
<el-form
:model="form"
:rules="rules"
ref="loginFormRef"
class="login-form"
label-position="top"
@submit.prevent="onSubmit"
>
<el-form-item prop="email" :label="$t('login.emailLabel')">
<el-input
v-model="form.email"
:placeholder="$t('login.emailPlaceholder')"
clearable
>
<template #prefix>
<img src="/youxiang.svg" alt="email" class="input-icon-svg" />
</template>
</el-input>
</el-form-item>
<el-form-item prop="password" :label="$t('login.passwordLabel')">
<el-input
v-model="form.password"
:placeholder="$t('login.passwordPlaceholder')"
show-password
clearable
>
<template #prefix>
<img src="/mima.svg" alt="password" class="input-icon-svg" />
</template>
</el-input>
</el-form-item>
<el-form-item>
<div class="remember-container">
<el-checkbox v-model="form.remember"
>{{ $t("login.remember") }}
</el-checkbox>
<div class="auth-links">
<el-link
@click="goToForgotPassword"
type="primary"
style="cursor: pointer; color: #f56c6c"
>
{{ $t("login.forgot") }}
</el-link>
<el-link
@click="goToGoActivate"
type="warning"
style="margin-left: 16px; cursor: pointer; color: #1e6fff"
>
{{ $t("login.notActivated") }}
</el-link>
</div>
</div>
</el-form-item>
<el-form-item>
<el-button
type="primary"
class="login-btn"
@click="onSubmit"
:loading="authStore.loading"
round
native-type="button"
>
{{ $t("login.loginBtn") }}
</el-button>
</el-form-item>
</el-form>
</template>
<script setup lang="ts">
import { ref, reactive, computed } from "vue";
import { useRouter } from "vue-router";
import { useI18n } from "vue-i18n";
import { ElMessage, type FormInstance, type FormRules } from "element-plus";
import { useAuthStore } from "@/stores/auth";
import { bindWechat } from "@/api/wechat";
const router = useRouter();
const { t } = useI18n();
const authStore = useAuthStore();
const loginFormRef = ref<FormInstance>();
const form = reactive({
email: "",
password: "",
remember: false,
});
const rules = computed<FormRules>(() => ({
email: [
{
required: true,
message: t("login.emailRequired"),
trigger: "blur",
},
{
validator: (_rule: any, value: string, callback: any) => {
const emailRegex = /^[^\s@]+@[^\s@]+\.[^\s@]+$/;
if (value && !emailRegex.test(value)) {
callback(new Error(t("login.emailInvalid")));
} else {
callback();
}
},
trigger: "blur",
},
],
password: [
{
required: true,
message: t("login.passwordRequired"),
trigger: "blur",
},
],
}));
const onSubmit = async () => {
try {
// 清除之前的错误
authStore.clearError();
// 验证表单
if (!loginFormRef.value) return;
const valid = await loginFormRef.value.validate();
if (!valid) return;
// 调用登录action
const result = await authStore.login({
email: form.email,
password: form.password,
remember: form.remember,
});
if (result.success) {
// 登录成功:若存在待绑定的微信state,自动绑定
let bindToken: string | null = null;
try {
bindToken = sessionStorage.getItem("wechatBindToken");
} catch (_) {}
if (bindToken) {
try {
await bindWechat({ state: bindToken });
ElMessage.success(t("login.wechatBindSuccess") || "微信已绑定");
} catch (e: any) {
const code = e?.response?.data?.code;
const msg =
e?.response?.data?.message ||
t("login.wechatBindFailed") ||
"微信绑定失败";
if (code === "WECHAT_ALREADY_BOUND") {
// 已被其他账号绑定或冲突
ElMessage.warning(msg);
} else {
ElMessage.error(msg);
}
} finally {
try {
sessionStorage.removeItem("wechatBindToken");
} catch (_) {}
}
}
// 登录成功,跳转到欢迎页面并传递登录成功标识
ElMessage.success(t("login.success"));
// 等待一个 tick 确保响应式状态更新完成
await new Promise(resolve => setTimeout(resolve, 50));
// 使用 replace 而不是 push,避免返回时回到登录页
try {
await router.replace({
path: "/app/welcome",
query: {
loginSuccess: "true",
registerSuccess: "true",
showReward: "false",
},
});
} catch (err) {
// 如果跳转失败,尝试直接使用 push
router.push({
path: "/app/welcome",
query: {
loginSuccess: "true",
registerSuccess: "true",
showReward: "false",
},
});
}
}
} catch (error) {
// 网络错误等异常情况,显示错误消息
ElMessage.error(t("login.loginFailed"));
}
};
const goToForgotPassword = () => {
// 如果用户已输入邮箱,通过 query 参数传递
const query = form.email ? { email: form.email } : {};
router.push({
path: "/forgot-password",
query,
});
};
const goToGoActivate = () => {
// 如果用户已输入邮箱,通过 query 参数传递
const query = form.email ? { email: form.email } : {};
router.push({
path: "/go-activate",
query,
});
};
</script>
<style scoped lang="scss">
.remember-container {
width: 100%;
display: flex;
justify-content: space-between;
align-items: center;
}
.login-form {
margin-top: 16px;
// 表单标签样式
:deep(.el-form-item__label) {
color: #606266;
font-weight: 500;
font-size: 14px;
margin-bottom: 8px;
padding: 0;
line-height: 1.5;
&::before {
color: #f56c6c;
}
}
// 复选框样式
:deep(.el-checkbox__label) {
color: var(--color-text);
font-size: 14px;
}
:deep(.el-checkbox__input.is-checked .el-checkbox__inner) {
background-color: #409eff;
border-color: #409eff;
}
:deep(.el-checkbox__inner) {
background-color: transparent;
border-color: var(--checkbox__inner);
}
// 成功状态样式
:deep(.el-form-item.is-success) {
.el-input__inner,
.el-input__inner:focus,
.el-textarea__inner,
.el-textarea__inner:focus {
border-color: #409eff;
}
}
// 输入框前缀图标
:deep(.el-input__prefix) {
display: flex;
align-items: center;
justify-content: center;
height: 100%;
.input-icon-svg {
margin-left: 10px;
margin-right: 6px;
display: block;
}
}
// 聚焦时图标不透明度
:deep(.el-input.is-focus .el-input__prefix .input-icon-svg) {
opacity: 1;
}
// 输入框样式
:deep(.el-input__inner) {
height: 40px;
font-size: 16px;
border-radius: 12px;
background: #ffffff !important;
color: #606266 !important;
transition: all 0.3s ease;
&::placeholder {
color: #c0c4cc;
font-size: 16px;
}
}
}
// 登录按钮
.login-btn {
width: 100%;
height: 44px;
font-size: 16px;
font-weight: 500;
border-radius: 12px;
background: linear-gradient(135deg, #3399ff 0%, #00c9ff 100%);
border: none;
margin-top: 8px;
box-shadow: 0 2px 8px rgba(51, 153, 255, 0.3);
&:hover {
box-shadow: 0 4px 12px rgba(51, 153, 255, 0.4);
}
&:active {
box-shadow: 0 1px 4px rgba(51, 153, 255, 0.3);
}
}
// 链接区域
.auth-links {
display: flex;
align-items: center;
justify-content: flex-end;
font-size: 14px;
:deep(.el-link) {
font-size: 14px;
font-weight: 400;
}
}
@media (max-width: 540px) {
.login-form {
:deep(.el-form-item) {
margin-bottom: 24px !important;
}
:deep(.el-form-item__label) {
font-size: 17px !important;
margin-bottom: 10px !important;
}
:deep(.el-input__inner) {
height: 52px !important;
font-size: 17px !important;
&::placeholder {
font-size: 16px !important;
}
}
:deep(.el-input__prefix .input-icon-svg) {
width: 20px !important;
height: 20px !important;
}
}
.login-btn {
height: 54px !important;
font-size: 19px !important;
margin-top: 16px !important;
border-radius: 14px !important;
}
.remember-container {
margin-bottom: 8px !important;
:deep(.el-checkbox__label) {
font-size: 16px !important;
}
:deep(.el-checkbox__inner) {
width: 18px !important;
height: 18px !important;
}
}
.auth-links {
font-size: 16px !important;
:deep(.el-link) {
font-size: 16px !important;
}
}
}
@media (max-width: 400px) {
.remember-container {
flex-direction: column;
align-items: flex-start;
gap: 8px;
}
.auth-links {
width: 100%;
justify-content: space-between;
margin-top: 4px;
:deep(.el-link) {
margin-left: 0 !important;
}
}
}
// 图标样式
.input-icon-svg {
width: 18px;
height: 18px;
object-fit: contain;
flex-shrink: 0;
opacity: 0.6;
transition: opacity 0.3s ease;
vertical-align: middle;
}
</style>