PhoneRegisterForm.vue
20.6 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
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
<template>
<div class="register-form">
<el-form
:model="form"
:rules="rules"
ref="regFormRef"
label-width="0"
class="register-form-content"
>
<el-form-item prop="realName" required>
<el-input
v-model="form.realName"
:placeholder="$t('register.namePlaceholder')"
clearable
size="large"
>
<template #prefix>
<span class="required-asterisk">*</span>
<img src="/yonghuming.svg" alt="realName" class="input-icon-svg" />
</template>
</el-input>
</el-form-item>
<el-form-item prop="phoneNumber" required>
<el-input
v-model="form.phoneNumber"
:placeholder="$t('register.phonePlaceholder')"
clearable
size="large"
maxlength="11"
>
<template #prefix>
<span class="required-asterisk">*</span>
<img src="/shoujihao.svg" alt="phone" class="input-icon-svg" />
</template>
</el-input>
</el-form-item>
<el-form-item prop="password" required>
<el-input
v-model="form.password"
:placeholder="$t('register.passwordPlaceholder')"
show-password
clearable
size="large"
>
<template #prefix>
<span class="required-asterisk">*</span>
<img src="/mima.svg" alt="password" class="input-icon-svg" />
</template>
</el-input>
</el-form-item>
<el-form-item prop="smsCode" required>
<el-input
v-model="form.smsCode"
:placeholder="$t('register.smsCodePlaceholder')"
clearable
size="large"
maxlength="6"
>
<template #prefix>
<span class="required-asterisk">*</span>
<img src="/anquan.svg" alt="smsCode" class="input-icon-svg" />
</template>
<template #suffix>
<el-button
:disabled="countdown > 0"
@click="sendSmsCode"
type="text"
class="sms-btn"
>
{{
countdown > 0
? $t("register.smsCodeRetry", { countdown })
: $t("register.sendSmsCode")
}}
</el-button>
</template>
</el-input>
</el-form-item>
<el-form-item prop="inviteCode">
<el-input
v-model="form.inviteCode"
:placeholder="$t('register.invitePlaceholder')"
:disabled="isInviteCodeFromRoute"
clearable
size="large"
>
<template #prefix>
<img
src="/yaoqingma.svg"
alt="inviteCode"
class="input-icon-svg invite-icon-aligned"
/>
</template>
</el-input>
</el-form-item>
<div class="agreement-section">
<el-checkbox v-model="form.agreeTerms" />
<div class="agreement-text">
<span>{{ $t("register.agreeTerms") }} </span>
<el-link
type="primary"
class="agreement-link"
@click="showAgreementDialog = true"
>{{ $t("register.userAgreement") }}</el-link
> <span>{{ $t("register.and") }}</span
>
<el-link
type="primary"
class="agreement-link"
@click="showPrivacyDialog = true"
>{{ $t("register.privacyPolicy") }}</el-link
>
</div>
</div>
<el-form-item class="submit-section">
<el-button
type="primary"
@click="onSubmit"
:loading="authStore.loading"
size="medium"
class="submit-btn"
>
{{ $t("register.createBtn") }}
</el-button>
</el-form-item>
<el-divider>
{{ $t("register.haveAccount")
}}<el-link
type="primary"
@click="emit('switchToLogin')"
style="color: #1e6fff"
>
{{ $t("register.signIn") }}
</el-link>
</el-divider>
</el-form>
<!-- 成功弹窗 -->
<el-dialog
v-model="showSuccess"
:title="$t('register.successTitle')"
width="400px"
:show-close="false"
class="success-dialog"
append-to-body
>
<div class="success-content">
<el-icon class="success-icon" :size="64" color="#67c23a">
<SuccessFilled />
</el-icon>
<p class="success-message">
注册成功!您的账号 <b>{{ form.phoneNumber }}</b> 已创建完成。
</p>
</div>
<template #footer>
<el-button type="primary" @click="handleSuccessConfirm">
确定
</el-button>
</template>
</el-dialog>
<!-- 用户协议弹窗 -->
<el-dialog
v-model="showAgreementDialog"
:title="$t('register.userServiceAgreement')"
width="600px"
class="agreement-dialog"
append-to-body
:close-on-click-modal="false"
>
<div class="agreement-dialog-content">
<div
class="agreement-text-content"
v-html="formattedAgreementText"
></div>
</div>
<template #footer>
<div class="agreement-dialog-footer">
<el-checkbox v-model="agreementChecked">
{{ $t("register.readAndAgree")
}}<el-link type="primary" @click.prevent style="margin: 0 4px"
>【{{ $t("register.userAgreement") }}】</el-link
>
</el-checkbox>
<el-button type="primary" @click="handleAgreementConfirm">
{{ $t("register.confirm") }}
</el-button>
</div>
</template>
</el-dialog>
<!-- 隐私政策弹窗 -->
<el-dialog
v-model="showPrivacyDialog"
:title="$t('register.privacyPolicy')"
width="600px"
class="agreement-dialog"
append-to-body
:close-on-click-modal="false"
>
<div class="agreement-dialog-content">
<div class="agreement-text-content" v-html="formattedPrivacyText"></div>
</div>
<template #footer>
<div class="agreement-dialog-footer">
<el-checkbox v-model="privacyChecked">
{{ $t("register.readAndAgree")
}}<el-link type="primary" @click.prevent style="margin: 0 4px"
>【{{ $t("register.privacyPolicy") }}】</el-link
>
</el-checkbox>
<el-button type="primary" @click="handlePrivacyConfirm">
{{ $t("register.confirm") }}
</el-button>
</div>
</template>
</el-dialog>
</div>
</template>
<script setup lang="ts">
import { ref, reactive, computed, onMounted, watch } from "vue";
import { useI18n } from "vue-i18n";
import { ElMessage, type FormInstance, type FormRules } from "element-plus";
import { SuccessFilled } from "@element-plus/icons-vue";
import { useAuthStore } from "@/stores/auth";
import { useRouter, useRoute } from "vue-router";
import {
apiSendRegisterSms,
apiRegisterByPhone,
apiLoginByPhonePassword,
} from "@/api/user";
const { t } = useI18n();
const authStore = useAuthStore();
const router = useRouter();
const route = useRoute();
const emit = defineEmits<{
(e: "switchToLogin"): void;
}>();
const regFormRef = ref<FormInstance>();
const showSuccess = ref(false);
const showAgreementDialog = ref(false);
const agreementChecked = ref(false);
const showPrivacyDialog = ref(false);
const privacyChecked = ref(false);
const countdown = ref(0);
const isInviteCodeFromRoute = ref(false);
let timer: NodeJS.Timeout | null = null;
// 协议内容(与邮箱注册相同)
const agreementContent = `
更新日期:2025年11月28日
1. 导言
欢迎您使用北京连心医疗科技有限公司(以下简称"连心医疗"或"我们")提供的"领医智能体"服务(以下简称"本服务")。
本服务是一款基于人工智能技术的医疗健康信息辅助平台,旨在通过互动问答、报告解读等形式,为您提供信息参考和辅助工具。本《用户服务协议》(以下简称"本协议")是您与连心医疗之间就注册、登录、使用本服务所订立的具有法律效力的协议。
在您开始使用本服务之前,请您(以下简称"用户"或"您")务必审慎阅读、充分理解本协议各条款内容,特别是以加粗、下划线等形式显示的关于免除或限制责任、医疗风险、法律适用和争议解决的条款。如果您不同意本协议的任何内容,或者无法准确理解该等条款的含义,请不要进行后续操作,并应立即停止访问或使用本服务。您的注册、登录、使用等行为即视为您已阅读、理解并完全接受本协议的全部内容,并同意接受其约束。
`;
const privacyContent = `
更新日期:2025年11月28日
北京连心医疗科技有限公司(以下简称"我们""连心医疗"或"LinkMed")作为https://linkmed.cc/ 网站(以下简称"本网站")的运营者,深知医疗健康领域个人信息及敏感数据对您的重要性。本隐私政策专为本网站定制,将详细说明我们在您使用本网站账号注册、登录及关联医疗服务过程中,如何收集、使用、存储、保护您的个人信息,以及您享有的信息管理权利。请您在访问、使用本网站前仔细阅读并理解本政策,您使用本网站即视为同意我们按照本政策处理您的个人信息。
`;
// 格式化协议文本
const formattedAgreementText = computed(() => {
return agreementContent
.split("\n")
.map((line) => {
if (!line.trim()) return "<br>";
if (/^\d+\./.test(line.trim())) {
return `<p style="font-weight: bold; margin: 12px 0 8px 0;">${line}</p>`;
}
return `<p style="margin: 6px 0; line-height: 1.6;">${line}</p>`;
})
.join("");
});
const formattedPrivacyText = computed(() => {
return privacyContent
.split("\n")
.map((line) => {
if (!line.trim()) return "<br>";
return `<p style="margin: 6px 0; line-height: 1.6;">${line}</p>`;
})
.join("");
});
const form = reactive({
realName: "",
phoneNumber: "",
password: "",
smsCode: "",
inviteCode: "",
agreeTerms: false,
});
// 监听 form.agreeTerms 的变化
watch(
() => form.agreeTerms,
(newValue) => {
agreementChecked.value = newValue;
privacyChecked.value = newValue;
},
);
const rules = computed<FormRules>(() => ({
realName: [
{ required: true, message: "请输入真实姓名", trigger: "blur" },
{ min: 2, message: "姓名长度至少2位", trigger: "blur" },
],
phoneNumber: [
{ required: true, message: "请输入手机号", trigger: "blur" },
{
validator: (_rule: any, value: string, callback: any) => {
const phoneRegex = /^1[3-9]\d{9}$/;
if (value && !phoneRegex.test(value)) {
callback(new Error("手机号格式不正确"));
} else {
callback();
}
},
trigger: ["blur", "change"],
},
],
password: [
{ required: true, message: "请输入密码", trigger: "blur" },
{ min: 6, max: 20, message: "密码长度至少6位,最多20位", trigger: "blur" },
],
smsCode: [
{ required: true, message: "请输入验证码", trigger: "blur" },
{ len: 6, message: "验证码为6位数字", trigger: "blur" },
],
}));
// 发送短信验证码
const sendSmsCode = async () => {
// 验证手机号
if (!form.phoneNumber) {
ElMessage.warning("请先输入手机号");
return;
}
const phoneRegex = /^1[3-9]\d{9}$/;
if (!phoneRegex.test(form.phoneNumber)) {
ElMessage.warning("手机号格式不正确");
return;
}
try {
await apiSendRegisterSms(form.phoneNumber);
ElMessage.success("验证码已发送,请查收短信");
// 开始倒计时
countdown.value = 60;
timer = setInterval(() => {
countdown.value--;
if (countdown.value <= 0) {
if (timer) clearInterval(timer);
}
}, 1000);
} catch (error: any) {
ElMessage.error(error?.response?.data?.message || "发送验证码失败");
}
};
// 处理协议确认
const handleAgreementConfirm = () => {
if (agreementChecked.value) {
form.agreeTerms = true;
showAgreementDialog.value = false;
} else {
ElMessage.warning(t("register.pleaseReadAndAgreeAgreement"));
}
};
const handlePrivacyConfirm = () => {
if (privacyChecked.value) {
form.agreeTerms = true;
showPrivacyDialog.value = false;
} else {
ElMessage.warning(t("register.pleaseReadAndAgreePrivacy"));
}
};
const handleSuccessConfirm = () => {
showSuccess.value = false;
emit("switchToLogin");
};
// 提交注册
const onSubmit = async () => {
// 表单校验:校验不通过则不发送接口请求
if (!regFormRef.value) {
return;
}
try {
// 执行表单校验
await regFormRef.value.validate();
} catch (error: any) {
// 校验失败,提取第一个错误信息并提示用户
let errorMessage = "请检查表单输入";
if (error && typeof error === "object" && error.fields) {
// Element Plus 校验失败时,error.fields 包含所有字段的错误信息
const fields = error.fields;
const fieldKeys = Object.keys(fields);
// 获取第一个有错误的字段
for (const fieldKey of fieldKeys) {
const fieldErrors = fields[fieldKey];
if (Array.isArray(fieldErrors) && fieldErrors.length > 0) {
const firstError = fieldErrors[0];
if (firstError && firstError.message) {
errorMessage = firstError.message;
break;
}
}
}
}
ElMessage.error(errorMessage);
// 校验失败,直接返回,不发送请求
return;
}
// 检查是否同意协议
if (!form.agreeTerms) {
ElMessage.error("请阅读并同意用户协议和隐私政策");
return;
}
// 所有校验通过后,才发送注册请求
try {
// 1. 先注册
const registerResult = await apiRegisterByPhone({
realName: form.realName,
phoneNumber: form.phoneNumber,
password: form.password,
smsCode: form.smsCode,
inviteCode: form.inviteCode || undefined,
});
if (!registerResult.data) {
ElMessage.error("注册失败");
return;
}
// 2. 注册成功后自动登录
try {
const loginResult = await apiLoginByPhonePassword({
phoneNumber: form.phoneNumber,
password: form.password,
remember: false,
});
const authToken = loginResult.data?.token;
if (!authToken) {
throw new Error("登录失败:未获取到 token");
}
// 使用 loginWithToken 方法处理登录
const loginSuccess = await authStore.loginWithToken(authToken);
if (loginSuccess.success) {
ElMessage.success(t("register.successTitle") || "注册成功");
// 跳转到欢迎页,并触发烟花特效
router.push("/app/welcome?registerSuccess=true&showReward=true");
} else {
ElMessage.warning("注册成功,但自动登录失败,请手动登录");
emit("switchToLogin");
}
} catch (loginError: any) {
console.error("自动登录处理错误:", loginError);
ElMessage.warning(
loginError?.response?.data?.message ||
"注册成功,但自动登录失败,请手动登录",
);
// 注册成功但登录失败,显示成功弹窗让用户手动登录
showSuccess.value = true;
}
} catch (error: any) {
console.error("注册处理错误:", error);
ElMessage.error(error?.response?.data?.message || "注册失败");
}
};
// 清理定时器
onMounted(() => {
// 从路由查询参数中获取邀请码
const refCode = route.query.ref as string;
if (refCode) {
form.inviteCode = refCode;
isInviteCodeFromRoute.value = true;
}
// 组件卸载时清理定时器
return () => {
if (timer) clearInterval(timer);
};
});
</script>
<style scoped lang="scss">
.register-form {
width: 100%;
max-width: 400px;
margin: 0 auto;
padding: 0;
:deep(.el-input__prefix) {
display: flex;
align-items: center;
justify-content: center;
height: 100%;
.required-asterisk {
color: #f56c6c;
font-size: 14px;
font-weight: bold;
margin-left: 10px;
margin-right: 2px;
line-height: 1;
}
.input-icon-svg {
margin-right: 6px;
display: block;
}
.invite-icon-aligned {
margin-left: 18px;
}
}
:deep(.el-input--large .el-input__wrapper) {
padding: 2px 15px 2px 2px !important;
}
:deep(.el-input.is-focus .el-input__prefix .input-icon-svg) {
opacity: 1;
}
}
.register-form-content {
background: transparent;
border-radius: 16px;
padding: 0;
}
.el-form-item {
margin-bottom: 20px;
}
.el-input {
border-radius: 12px;
:deep(.el-input__inner) {
height: 38px;
font-size: 16px;
border-radius: 12px;
background: #ffffff !important;
color: #606266 !important;
transition: all 0.3s ease;
&::placeholder {
color: #c0c4cc;
font-size: 16px;
}
}
:deep(.el-input__suffix) {
color: #909399;
}
}
.input-icon-svg {
width: 18px;
height: 18px;
object-fit: contain;
flex-shrink: 0;
opacity: 0.6;
transition: opacity 0.3s ease;
vertical-align: middle;
}
.sms-btn {
font-size: 13px;
color: #1e6fff;
padding: 0 8px;
white-space: nowrap;
&:disabled {
color: #c0c4cc;
}
}
.agreement-section {
margin: 8px 0;
display: flex;
justify-content: flex-start;
align-items: flex-start;
gap: 8px;
margin-bottom: 16px;
}
.agreement-section :deep(.el-checkbox) {
display: flex;
align-items: flex-start;
line-height: 1.5;
margin-top: 0;
}
.agreement-section :deep(.el-checkbox__input) {
margin-top: 2px;
flex-shrink: 0;
}
.agreement-section :deep(.el-checkbox__label) {
display: none;
}
.agreement-text {
font-size: 14px;
color: #606266;
display: flex;
flex-wrap: wrap;
align-items: center;
line-height: 1.5;
flex: 1;
margin-top: 0;
}
.agreement-link {
font-size: 14px;
text-decoration: none;
}
.submit-section {
margin-top: 10px;
margin-bottom: 24px;
}
.submit-btn {
width: 100%;
height: 44px;
font-size: 16px;
font-weight: 500;
border-radius: 12px;
background: linear-gradient(135deg, #3399ff 0%, #00c9ff 100%);
border: none;
transition: all 0.3s ease;
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);
}
}
.success-dialog {
:deep(.el-dialog) {
border-radius: 16px;
background: var(--color-card-bg);
color: var(--color-text);
}
}
.success-content {
text-align: center;
padding: 20px 0;
}
.success-icon {
font-size: 64px;
color: #67c23a;
margin-bottom: 16px;
}
.success-message {
font-size: 16px;
color: var(--color-text);
line-height: 1.6;
margin: 0;
}
/* 用户协议弹窗样式 */
.agreement-dialog {
:deep(.el-dialog) {
border-radius: 16px;
background: #ffffff;
color: #606266;
display: flex;
flex-direction: column;
height: 600px;
max-height: 80vh;
margin: 0 auto;
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
}
:deep(.el-dialog__wrapper) {
display: flex;
align-items: center;
justify-content: center;
}
:deep(.el-dialog__header) {
padding: 20px 24px;
border-bottom: 1px solid #ebeef5;
flex-shrink: 0;
}
:deep(.el-dialog__title) {
font-size: 18px;
font-weight: 600;
color: #303133;
}
:deep(.el-dialog__body) {
padding: 20px 24px;
overflow-y: auto;
flex: 1;
min-height: 0;
}
:deep(.el-dialog__footer) {
padding: 16px 24px;
border-top: 1px solid #ebeef5;
flex-shrink: 0;
}
}
.agreement-dialog-content {
max-height: 500px;
overflow-y: auto;
padding-right: 8px;
&::-webkit-scrollbar {
width: 6px;
}
&::-webkit-scrollbar-track {
background: #f5f5f5;
border-radius: 3px;
}
&::-webkit-scrollbar-thumb {
background: #c0c4cc;
border-radius: 3px;
&:hover {
background: #a0a4aa;
}
}
}
.agreement-text-content {
font-size: 14px;
line-height: 1.8;
color: #606266;
:deep(p) {
margin: 6px 0;
line-height: 1.8;
}
:deep(p:first-child) {
margin-top: 0;
}
}
.agreement-dialog-footer {
display: flex;
justify-content: space-between;
align-items: center;
width: 100%;
:deep(.el-checkbox) {
flex: 1;
}
:deep(.el-button) {
margin-left: 16px;
}
}
/* 响应式设计 */
@media (max-width: 320px) {
.register-form {
padding: 16px;
}
.register-form-content {
padding: 24px;
}
.el-input {
:deep(.el-input__inner) {
height: 44px;
font-size: 15px;
padding-left: 50px;
}
}
.submit-btn {
height: 44px;
font-size: 15px;
}
}
</style>