@pisell/pisellos 0.0.335 → 0.0.337

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,1488 @@
1
+ var __defProp = Object.defineProperty;
2
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
3
+ var __getOwnPropNames = Object.getOwnPropertyNames;
4
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
5
+ var __export = (target, all) => {
6
+ for (var name in all)
7
+ __defProp(target, name, { get: all[name], enumerable: true });
8
+ };
9
+ var __copyProps = (to, from, except, desc) => {
10
+ if (from && typeof from === "object" || typeof from === "function") {
11
+ for (let key of __getOwnPropNames(from))
12
+ if (!__hasOwnProp.call(to, key) && key !== except)
13
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
14
+ }
15
+ return to;
16
+ };
17
+ var __reExport = (target, mod, secondTarget) => (__copyProps(target, mod, "default"), secondTarget && __copyProps(secondTarget, mod, "default"));
18
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
19
+
20
+ // src/solution/RegisterAndLogin/index.ts
21
+ var RegisterAndLogin_exports = {};
22
+ __export(RegisterAndLogin_exports, {
23
+ RegisterAndLoginImpl: () => RegisterAndLoginImpl
24
+ });
25
+ module.exports = __toCommonJS(RegisterAndLogin_exports);
26
+ var import_BaseModule = require("../../modules/BaseModule");
27
+ var import_types = require("./types");
28
+ var import_config = require("./config");
29
+ __reExport(RegisterAndLogin_exports, require("./types"), module.exports);
30
+ __reExport(RegisterAndLogin_exports, require("./config"), module.exports);
31
+ var RegisterAndLoginImpl = class extends import_BaseModule.BaseModule {
32
+ constructor(name, version) {
33
+ super(name, version);
34
+ // 提供给 baseModule 用的默认名称和默认版本
35
+ this.defaultName = "registerAndLogin";
36
+ this.defaultVersion = "1.0.0";
37
+ this.isSolution = true;
38
+ this.otherParams = {};
39
+ this.channel = "default";
40
+ }
41
+ /**
42
+ * 重新发送邮箱注册链接
43
+ */
44
+ async resendEmailRegisterLink() {
45
+ try {
46
+ if (!this.store.emailRegisterLinkCode) {
47
+ const error = "没有找到邮箱注册链接的 code,请先调用 sendEmailRegisterLink";
48
+ this.store.error = error;
49
+ return {
50
+ status: false,
51
+ message: error
52
+ };
53
+ }
54
+ this.store.isLoading = true;
55
+ this.store.error = null;
56
+ const response = await this.apiCaller.call(
57
+ "resendEmailRegisterLink",
58
+ { code: this.store.emailRegisterLinkCode }
59
+ );
60
+ if (response.status) {
61
+ await this.core.effects.emit(
62
+ import_types.RegisterAndLoginHooks.onEmailVerificationSent,
63
+ {
64
+ target: "resend",
65
+ purpose: import_types.VerificationPurpose.REGISTER
66
+ }
67
+ );
68
+ } else {
69
+ this.store.error = response.message || "重新发送注册邀请链接失败";
70
+ await this.core.effects.emit(
71
+ import_types.RegisterAndLoginHooks.onEmailVerificationFailed,
72
+ {
73
+ target: "resend",
74
+ error: this.store.error
75
+ }
76
+ );
77
+ }
78
+ return response;
79
+ } catch (error) {
80
+ this.store.error = "重新发送注册邀请链接失败";
81
+ await this.core.effects.emit(
82
+ import_types.RegisterAndLoginHooks.onEmailVerificationFailed,
83
+ {
84
+ target: "resend",
85
+ error: this.store.error
86
+ }
87
+ );
88
+ return error;
89
+ } finally {
90
+ this.store.isLoading = false;
91
+ }
92
+ }
93
+ /**
94
+ * 邮箱验证码注册
95
+ * @param params 注册参数
96
+ * @returns 注册结果
97
+ */
98
+ async emailCodeRegister(params) {
99
+ try {
100
+ this.store.isLoading = true;
101
+ this.store.error = null;
102
+ await this.core.effects.emit(
103
+ import_types.RegisterAndLoginHooks.onRegisterStarted,
104
+ params
105
+ );
106
+ const response = await this.apiCaller.call(
107
+ "emailCodeRegister",
108
+ params
109
+ );
110
+ if (response.status && response.data) {
111
+ const { user, token, refreshToken, expiresAt } = response.data;
112
+ if (token) {
113
+ this.store.currentUser = user;
114
+ this.store.isLoggedIn = true;
115
+ }
116
+ await this.core.effects.emit(
117
+ import_types.RegisterAndLoginHooks.onRegisterSuccess,
118
+ response.data
119
+ );
120
+ } else {
121
+ this.store.error = response.message || "邮箱验证码注册失败";
122
+ await this.core.effects.emit(import_types.RegisterAndLoginHooks.onRegisterFailed, {
123
+ error: this.store.error,
124
+ params
125
+ });
126
+ }
127
+ return response;
128
+ } catch (error) {
129
+ this.store.error = "邮箱验证码注册失败";
130
+ await this.core.effects.emit(import_types.RegisterAndLoginHooks.onRegisterFailed, {
131
+ error: this.store.error,
132
+ params
133
+ });
134
+ throw error;
135
+ } finally {
136
+ this.store.isLoading = false;
137
+ }
138
+ }
139
+ /**
140
+ * 手机验证码注册
141
+ */
142
+ async phoneCodeRegister(params) {
143
+ try {
144
+ this.store.isLoading = true;
145
+ this.store.error = null;
146
+ await this.core.effects.emit(
147
+ import_types.RegisterAndLoginHooks.onRegisterStarted,
148
+ params
149
+ );
150
+ const response = await this.apiCaller.call(
151
+ "phoneCodeRegister",
152
+ params
153
+ );
154
+ if (response.status && response.data) {
155
+ const { user, token, refreshToken, expiresAt } = response.data;
156
+ if (token) {
157
+ this.store.currentUser = user;
158
+ this.store.isLoggedIn = true;
159
+ }
160
+ await this.core.effects.emit(
161
+ import_types.RegisterAndLoginHooks.onRegisterSuccess,
162
+ response.data
163
+ );
164
+ } else {
165
+ this.store.error = response.message || "注册失败";
166
+ await this.core.effects.emit(import_types.RegisterAndLoginHooks.onRegisterFailed, {
167
+ error: this.store.error,
168
+ params
169
+ });
170
+ }
171
+ return response;
172
+ } catch (error) {
173
+ this.store.error = error.message || "注册失败";
174
+ await this.core.effects.emit(import_types.RegisterAndLoginHooks.onRegisterFailed, {
175
+ error: this.store.error,
176
+ params
177
+ });
178
+ throw error;
179
+ } finally {
180
+ this.store.isLoading = false;
181
+ }
182
+ }
183
+ async initialize(core, options) {
184
+ this.core = core;
185
+ this.store = {
186
+ currentUser: null,
187
+ isLoggedIn: false,
188
+ isLoading: false,
189
+ verificationCodeSent: {},
190
+ oauthConfig: options.oauthConfig || {},
191
+ error: null,
192
+ ...options.store
193
+ };
194
+ this.otherParams = options.otherParams || {};
195
+ this.channel = this.otherParams.channel || "default";
196
+ this.request = core.getPlugin("request");
197
+ this.window = core.getPlugin("window");
198
+ if (!this.request) {
199
+ throw new Error("RegisterAndLogin 模块需要 request 插件支持");
200
+ }
201
+ if (!this.window) {
202
+ throw new Error("RegisterAndLogin 模块需要 window 插件支持");
203
+ }
204
+ this.apiCaller = (0, import_config.createApiCaller)(this.request, this.channel);
205
+ await this.restoreLoginState();
206
+ console.log("[RegisterAndLogin] 初始化完成");
207
+ await this.core.effects.emit(import_types.RegisterAndLoginHooks.onInited, {});
208
+ }
209
+ /**
210
+ * 从本地存储恢复登录状态
211
+ */
212
+ async restoreLoginState() {
213
+ try {
214
+ const token = this.window.localStorage.getItem("auth_token");
215
+ const userInfo = this.window.localStorage.getItem("user_info");
216
+ if (token && userInfo) {
217
+ const user = JSON.parse(userInfo);
218
+ this.store.currentUser = user;
219
+ this.store.isLoggedIn = true;
220
+ await this.validateToken(token);
221
+ }
222
+ } catch (error) {
223
+ console.warn("[RegisterAndLogin] 恢复登录状态失败:", error);
224
+ this.clearLoginState();
225
+ }
226
+ }
227
+ /**
228
+ * 验证 token 有效性
229
+ */
230
+ async validateToken(token) {
231
+ try {
232
+ const response = await this.apiCaller.call(
233
+ "validateToken",
234
+ token
235
+ );
236
+ if (response.status && response.data) {
237
+ this.store.currentUser = response.data;
238
+ return true;
239
+ } else {
240
+ this.clearLoginState();
241
+ return false;
242
+ }
243
+ } catch (error) {
244
+ console.warn("[RegisterAndLogin] Token 验证失败:", error);
245
+ this.clearLoginState();
246
+ return false;
247
+ }
248
+ }
249
+ /**
250
+ * 清除登录状态
251
+ */
252
+ clearLoginState() {
253
+ this.store.currentUser = null;
254
+ this.store.isLoggedIn = false;
255
+ this.window.localStorage.removeItem("auth_token");
256
+ this.window.localStorage.removeItem("user_info");
257
+ this.window.localStorage.removeItem("refresh_token");
258
+ }
259
+ /**
260
+ * 发送邮箱验证码
261
+ */
262
+ async sendEmailVerificationCode(params) {
263
+ try {
264
+ this.store.isLoading = true;
265
+ this.store.error = null;
266
+ const response = await this.apiCaller.call(
267
+ "sendEmailVerificationCode",
268
+ params
269
+ );
270
+ if (response.status) {
271
+ const now = Date.now();
272
+ this.store.verificationCodeSent[params.target] = {
273
+ sent: true,
274
+ sentAt: now,
275
+ expiresAt: now + 5 * 60 * 1e3
276
+ // 5分钟过期
277
+ };
278
+ await this.core.effects.emit(
279
+ import_types.RegisterAndLoginHooks.onEmailVerificationSent,
280
+ {
281
+ target: params.target,
282
+ purpose: params.purpose
283
+ }
284
+ );
285
+ } else {
286
+ this.store.error = response.message || "发送验证码失败";
287
+ await this.core.effects.emit(
288
+ import_types.RegisterAndLoginHooks.onEmailVerificationFailed,
289
+ {
290
+ target: params.target,
291
+ error: this.store.error
292
+ }
293
+ );
294
+ }
295
+ return response;
296
+ } catch (error) {
297
+ this.store.error = "发送验证码失败";
298
+ await this.core.effects.emit(
299
+ import_types.RegisterAndLoginHooks.onEmailVerificationFailed,
300
+ {
301
+ target: params.target,
302
+ error: this.store.error
303
+ }
304
+ );
305
+ return error;
306
+ } finally {
307
+ this.store.isLoading = false;
308
+ }
309
+ }
310
+ /**
311
+ * 发送邮箱登录验证码
312
+ */
313
+ async sendEmailLoginCode(params) {
314
+ try {
315
+ this.store.isLoading = true;
316
+ this.store.error = null;
317
+ const response = await this.apiCaller.call(
318
+ "sendEmailLoginCode",
319
+ params
320
+ );
321
+ if (response.status) {
322
+ const now = Date.now();
323
+ this.store.verificationCodeSent[params.target] = {
324
+ sent: true,
325
+ sentAt: now,
326
+ expiresAt: now + 5 * 60 * 1e3
327
+ // 5分钟过期
328
+ };
329
+ await this.core.effects.emit(
330
+ import_types.RegisterAndLoginHooks.onEmailVerificationSent,
331
+ {
332
+ target: params.target,
333
+ purpose: params.purpose
334
+ }
335
+ );
336
+ } else {
337
+ this.store.error = response.message || "发送验证码失败";
338
+ await this.core.effects.emit(
339
+ import_types.RegisterAndLoginHooks.onEmailVerificationFailed,
340
+ {
341
+ target: params.target,
342
+ error: this.store.error
343
+ }
344
+ );
345
+ }
346
+ return response;
347
+ } catch (error) {
348
+ this.store.error = "发送验证码失败";
349
+ await this.core.effects.emit(
350
+ import_types.RegisterAndLoginHooks.onEmailVerificationFailed,
351
+ {
352
+ target: params.target,
353
+ error: this.store.error
354
+ }
355
+ );
356
+ return error;
357
+ } finally {
358
+ this.store.isLoading = false;
359
+ }
360
+ }
361
+ /**
362
+ * 发送手机号注册验证码
363
+ */
364
+ async sendSmsRegisterCode(params) {
365
+ try {
366
+ this.store.isLoading = true;
367
+ this.store.error = null;
368
+ const response = await this.apiCaller.call(
369
+ "sendSmsRegisterCode",
370
+ params
371
+ );
372
+ if (response.status) {
373
+ const now = Date.now();
374
+ this.store.verificationCodeSent[params.phone] = {
375
+ sent: true,
376
+ sentAt: now,
377
+ expiresAt: now + 5 * 60 * 1e3
378
+ // 5分钟过期
379
+ };
380
+ await this.core.effects.emit(
381
+ import_types.RegisterAndLoginHooks.onSmsVerificationSent,
382
+ {
383
+ target: params.phone,
384
+ purpose: import_types.VerificationPurpose.REGISTER
385
+ }
386
+ );
387
+ } else {
388
+ this.store.error = response.message || "发送注册验证码失败";
389
+ await this.core.effects.emit(
390
+ import_types.RegisterAndLoginHooks.onSmsVerificationFailed,
391
+ {
392
+ target: params.phone,
393
+ error: this.store.error
394
+ }
395
+ );
396
+ }
397
+ return response;
398
+ } catch (error) {
399
+ this.store.error = "发送注册验证码失败";
400
+ await this.core.effects.emit(
401
+ import_types.RegisterAndLoginHooks.onSmsVerificationFailed,
402
+ {
403
+ target: params.phone,
404
+ error: this.store.error
405
+ }
406
+ );
407
+ return error;
408
+ } finally {
409
+ this.store.isLoading = false;
410
+ }
411
+ }
412
+ /**
413
+ * 发送邮箱注册邀请链接
414
+ */
415
+ async sendEmailRegisterLink(email) {
416
+ var _a;
417
+ try {
418
+ this.store.isLoading = true;
419
+ this.store.error = null;
420
+ const response = await this.apiCaller.call(
421
+ "sendEmailRegisterLink",
422
+ email
423
+ );
424
+ if (response.status) {
425
+ if ((_a = response.data) == null ? void 0 : _a.code) {
426
+ this.store.emailRegisterLinkCode = response.data.code;
427
+ }
428
+ const now = Date.now();
429
+ this.store.verificationCodeSent[email] = {
430
+ sent: true,
431
+ sentAt: now,
432
+ expiresAt: now + 24 * 60 * 60 * 1e3
433
+ // 24小时过期
434
+ };
435
+ await this.core.effects.emit(
436
+ import_types.RegisterAndLoginHooks.onEmailVerificationSent,
437
+ {
438
+ target: email,
439
+ purpose: import_types.VerificationPurpose.REGISTER
440
+ }
441
+ );
442
+ } else {
443
+ this.store.error = response.message || "发送注册邀请链接失败";
444
+ await this.core.effects.emit(
445
+ import_types.RegisterAndLoginHooks.onEmailVerificationFailed,
446
+ {
447
+ target: email,
448
+ error: this.store.error
449
+ }
450
+ );
451
+ }
452
+ return response;
453
+ } catch (error) {
454
+ this.store.error = "发送注册邀请链接失败";
455
+ await this.core.effects.emit(
456
+ import_types.RegisterAndLoginHooks.onEmailVerificationFailed,
457
+ {
458
+ target: email,
459
+ error: this.store.error
460
+ }
461
+ );
462
+ return error;
463
+ } finally {
464
+ this.store.isLoading = false;
465
+ }
466
+ }
467
+ /**
468
+ * 验证邮箱注册链接
469
+ */
470
+ async verifyEmailRegistrationLink(params) {
471
+ try {
472
+ this.store.isLoading = true;
473
+ this.store.error = null;
474
+ const response = await this.apiCaller.call(
475
+ "verifyEmailRegistrationLink",
476
+ params
477
+ );
478
+ if (response.status) {
479
+ await this.core.effects.emit(
480
+ import_types.RegisterAndLoginHooks.onEmailVerificationVerified,
481
+ {
482
+ target: "email-registration-link",
483
+ purpose: import_types.VerificationPurpose.REGISTER
484
+ }
485
+ );
486
+ } else {
487
+ this.store.error = response.message || "邮箱注册链接验证失败";
488
+ await this.core.effects.emit(
489
+ import_types.RegisterAndLoginHooks.onEmailVerificationFailed,
490
+ {
491
+ target: "email-registration-link",
492
+ error: this.store.error
493
+ }
494
+ );
495
+ }
496
+ return response;
497
+ } catch (error) {
498
+ this.store.error = "邮箱注册链接验证失败";
499
+ await this.core.effects.emit(
500
+ import_types.RegisterAndLoginHooks.onEmailVerificationFailed,
501
+ {
502
+ target: "email-registration-link",
503
+ error: this.store.error
504
+ }
505
+ );
506
+ return error;
507
+ } finally {
508
+ this.store.isLoading = false;
509
+ }
510
+ }
511
+ /**
512
+ * 邮箱密码登录
513
+ */
514
+ async emailPasswordLogin(params) {
515
+ try {
516
+ this.store.isLoading = true;
517
+ this.store.error = null;
518
+ await this.core.effects.emit(import_types.RegisterAndLoginHooks.onLoginStarted, {
519
+ type: "email-password",
520
+ email: params.email
521
+ });
522
+ const response = await this.apiCaller.call(
523
+ "emailPasswordLogin",
524
+ params
525
+ );
526
+ if (response.status && response.data) {
527
+ this.store.isLoggedIn = true;
528
+ this.store.userInfo = response.data.user;
529
+ this.store.token = response.data.token;
530
+ if (response.data.refreshToken) {
531
+ this.store.refreshToken = response.data.refreshToken;
532
+ }
533
+ await this.core.effects.emit(import_types.RegisterAndLoginHooks.onLoginSuccess, {
534
+ user: response.data.user,
535
+ token: response.data.token,
536
+ loginType: "email-password"
537
+ });
538
+ } else {
539
+ this.store.error = response.message || "邮箱密码登录失败";
540
+ await this.core.effects.emit(import_types.RegisterAndLoginHooks.onLoginFailed, {
541
+ error: this.store.error,
542
+ loginType: "email-password"
543
+ });
544
+ }
545
+ return response;
546
+ } catch (error) {
547
+ this.store.error = "邮箱密码登录失败";
548
+ await this.core.effects.emit(import_types.RegisterAndLoginHooks.onLoginFailed, {
549
+ error: this.store.error,
550
+ loginType: "email-password"
551
+ });
552
+ return error;
553
+ } finally {
554
+ this.store.isLoading = false;
555
+ }
556
+ }
557
+ /**
558
+ * 验证验证码
559
+ */
560
+ async verifyCode(params) {
561
+ try {
562
+ this.store.isLoading = true;
563
+ this.store.error = null;
564
+ const response = await this.apiCaller.call(
565
+ "verifyCode",
566
+ params
567
+ );
568
+ if (response.status) {
569
+ const hookName = params.type === import_types.VerificationCodeType.EMAIL ? import_types.RegisterAndLoginHooks.onEmailVerificationVerified : import_types.RegisterAndLoginHooks.onSmsVerificationVerified;
570
+ await this.core.effects.emit(hookName, {
571
+ target: params.target,
572
+ purpose: params.purpose
573
+ });
574
+ } else {
575
+ this.store.error = response.message || "验证码验证失败";
576
+ const hookName = params.type === import_types.VerificationCodeType.EMAIL ? import_types.RegisterAndLoginHooks.onEmailVerificationFailed : import_types.RegisterAndLoginHooks.onSmsVerificationFailed;
577
+ await this.core.effects.emit(hookName, {
578
+ target: params.target,
579
+ error: this.store.error
580
+ });
581
+ }
582
+ return response;
583
+ } catch (error) {
584
+ this.store.error = "验证码验证失败";
585
+ return error;
586
+ } finally {
587
+ this.store.isLoading = false;
588
+ }
589
+ }
590
+ /**
591
+ * 用户注册
592
+ */
593
+ async register(params) {
594
+ try {
595
+ this.store.isLoading = true;
596
+ this.store.error = null;
597
+ await this.core.effects.emit(
598
+ import_types.RegisterAndLoginHooks.onRegisterStarted,
599
+ params
600
+ );
601
+ const response = await this.apiCaller.call(
602
+ "register",
603
+ params
604
+ );
605
+ if (response.status && response.data) {
606
+ const { user, token, refreshToken, expiresAt } = response.data;
607
+ if (token) {
608
+ this.store.currentUser = user;
609
+ this.store.isLoggedIn = true;
610
+ this.window.localStorage.setItem("auth_token", token);
611
+ this.window.localStorage.setItem("user_info", JSON.stringify(user));
612
+ if (refreshToken) {
613
+ this.window.localStorage.setItem("refresh_token", refreshToken);
614
+ }
615
+ }
616
+ await this.core.effects.emit(
617
+ import_types.RegisterAndLoginHooks.onRegisterSuccess,
618
+ response.data
619
+ );
620
+ } else {
621
+ this.store.error = response.message || "注册失败";
622
+ await this.core.effects.emit(import_types.RegisterAndLoginHooks.onRegisterFailed, {
623
+ error: this.store.error,
624
+ params
625
+ });
626
+ }
627
+ return response;
628
+ } catch (error) {
629
+ this.store.error = "注册失败";
630
+ await this.core.effects.emit(import_types.RegisterAndLoginHooks.onRegisterFailed, {
631
+ error: this.store.error,
632
+ params
633
+ });
634
+ throw error;
635
+ } finally {
636
+ this.store.isLoading = false;
637
+ }
638
+ }
639
+ /**
640
+ * 用户登录
641
+ */
642
+ async login(params) {
643
+ try {
644
+ this.store.isLoading = true;
645
+ this.store.error = null;
646
+ await this.core.effects.emit(
647
+ import_types.RegisterAndLoginHooks.onLoginStarted,
648
+ params
649
+ );
650
+ const response = await this.apiCaller.call(
651
+ "login",
652
+ params
653
+ );
654
+ if (response.status && response.data) {
655
+ const { user, token, refreshToken } = response.data;
656
+ this.store.currentUser = user;
657
+ this.store.isLoggedIn = true;
658
+ this.window.localStorage.setItem("auth_token", token);
659
+ this.window.localStorage.setItem("user_info", JSON.stringify(user));
660
+ if (refreshToken) {
661
+ this.window.localStorage.setItem("refresh_token", refreshToken);
662
+ }
663
+ await this.core.effects.emit(
664
+ import_types.RegisterAndLoginHooks.onLoginSuccess,
665
+ response.data
666
+ );
667
+ } else {
668
+ this.store.error = response.message || "登录失败";
669
+ await this.core.effects.emit(import_types.RegisterAndLoginHooks.onLoginFailed, {
670
+ error: this.store.error,
671
+ params
672
+ });
673
+ }
674
+ return response;
675
+ } catch (error) {
676
+ this.store.error = "登录失败";
677
+ await this.core.effects.emit(import_types.RegisterAndLoginHooks.onLoginFailed, {
678
+ error: this.store.error,
679
+ params
680
+ });
681
+ throw error;
682
+ } finally {
683
+ this.store.isLoading = false;
684
+ }
685
+ }
686
+ /**
687
+ * OAuth 登录
688
+ */
689
+ async oauthLogin(params) {
690
+ try {
691
+ this.store.isLoading = true;
692
+ this.store.error = null;
693
+ await this.core.effects.emit(
694
+ import_types.RegisterAndLoginHooks.onOAuthLoginStarted,
695
+ params
696
+ );
697
+ const response = await this.apiCaller.call("oauthLogin", params);
698
+ if (response.status && response.data) {
699
+ const { user, token, refreshToken } = response.data;
700
+ this.store.currentUser = user;
701
+ this.store.isLoggedIn = true;
702
+ this.window.localStorage.setItem("auth_token", token);
703
+ this.window.localStorage.setItem("user_info", JSON.stringify(user));
704
+ if (refreshToken) {
705
+ this.window.localStorage.setItem("refresh_token", refreshToken);
706
+ }
707
+ await this.core.effects.emit(
708
+ import_types.RegisterAndLoginHooks.onOAuthLoginSuccess,
709
+ response.data
710
+ );
711
+ } else {
712
+ this.store.error = response.message || "OAuth 登录失败";
713
+ await this.core.effects.emit(import_types.RegisterAndLoginHooks.onOAuthLoginFailed, {
714
+ error: this.store.error,
715
+ params
716
+ });
717
+ }
718
+ return response;
719
+ } catch (error) {
720
+ this.store.error = "OAuth 登录失败";
721
+ await this.core.effects.emit(import_types.RegisterAndLoginHooks.onOAuthLoginFailed, {
722
+ error: this.store.error,
723
+ params
724
+ });
725
+ throw error;
726
+ } finally {
727
+ this.store.isLoading = false;
728
+ }
729
+ }
730
+ /**
731
+ * 用户登出
732
+ */
733
+ async logout() {
734
+ try {
735
+ await this.apiCaller.call("logout");
736
+ } catch (error) {
737
+ console.warn("[RegisterAndLogin] 登出接口调用失败:", error);
738
+ } finally {
739
+ this.clearLoginState();
740
+ }
741
+ }
742
+ /** 发送重置密码邮箱链接 */
743
+ async sendResetPasswordLink(params) {
744
+ try {
745
+ this.store.isLoading = true;
746
+ this.store.error = null;
747
+ const response = await this.apiCaller.call(
748
+ "sendResetPasswordLink",
749
+ params
750
+ );
751
+ if (response.status) {
752
+ this.store.verificationCodeSent[params.email] = {
753
+ sent: true,
754
+ sentAt: Date.now(),
755
+ expiresAt: Date.now() + 5 * 60 * 1e3
756
+ // 5分钟后过期
757
+ };
758
+ this.core.effects.emit(import_types.RegisterAndLoginHooks.onEmailVerificationSent, {
759
+ email: params.email,
760
+ purpose: "reset-password-link"
761
+ });
762
+ } else {
763
+ this.store.error = response.message || "发送重置密码链接失败";
764
+ }
765
+ return response;
766
+ } catch (error) {
767
+ this.store.error = "发送重置密码链接失败";
768
+ return error;
769
+ } finally {
770
+ this.store.isLoading = false;
771
+ }
772
+ }
773
+ /** 检测重置密码链接code有效性 */
774
+ async checkResetPasswordCode(params) {
775
+ try {
776
+ this.store.isLoading = true;
777
+ this.store.error = null;
778
+ const response = await this.apiCaller.call(
779
+ "checkResetPasswordCode",
780
+ params
781
+ );
782
+ if (!response.status) {
783
+ this.store.error = response.message || "验证码无效";
784
+ }
785
+ return response;
786
+ } catch (error) {
787
+ this.store.error = "验证码验证失败";
788
+ return error;
789
+ } finally {
790
+ this.store.isLoading = false;
791
+ }
792
+ }
793
+ /** 校验邮件链接 code 有效性 */
794
+ async checkEmailLinkCode(params) {
795
+ try {
796
+ this.store.isLoading = true;
797
+ this.store.error = null;
798
+ const response = await this.apiCaller.call(
799
+ "checkEmailLinkCode",
800
+ params
801
+ );
802
+ if (!response.status) {
803
+ this.store.error = response.message || "邮件链接验证码无效";
804
+ }
805
+ return response;
806
+ } catch (error) {
807
+ this.store.error = "邮件链接验证码验证失败";
808
+ return error;
809
+ } finally {
810
+ this.store.isLoading = false;
811
+ }
812
+ }
813
+ /** 通过code重置密码 */
814
+ async resetPasswordByCode(params) {
815
+ try {
816
+ this.store.isLoading = true;
817
+ this.store.error = null;
818
+ this.core.effects.emit(import_types.RegisterAndLoginHooks.onPasswordResetStarted, {
819
+ code: params.code
820
+ });
821
+ const response = await this.apiCaller.call(
822
+ "resetPasswordByCode",
823
+ params
824
+ );
825
+ if (response.status) {
826
+ this.core.effects.emit(import_types.RegisterAndLoginHooks.onPasswordResetSuccess, {
827
+ code: params.code
828
+ });
829
+ } else {
830
+ this.store.error = response.message || "密码重置失败";
831
+ this.core.effects.emit(import_types.RegisterAndLoginHooks.onPasswordResetFailed, {
832
+ code: params.code,
833
+ error: this.store.error
834
+ });
835
+ }
836
+ return response;
837
+ } catch (error) {
838
+ this.store.error = "密码重置失败";
839
+ this.core.effects.emit(import_types.RegisterAndLoginHooks.onPasswordResetFailed, {
840
+ code: params.code,
841
+ error: this.store.error
842
+ });
843
+ return error;
844
+ } finally {
845
+ this.store.isLoading = false;
846
+ }
847
+ }
848
+ /**
849
+ * 重置密码
850
+ */
851
+ async resetPassword(params) {
852
+ try {
853
+ this.store.isLoading = true;
854
+ this.store.error = null;
855
+ if (params.type === "email") {
856
+ this.core.effects.emit(import_types.RegisterAndLoginHooks.onPasswordResetStarted, {
857
+ email: params.target.email
858
+ });
859
+ } else {
860
+ this.core.effects.emit(import_types.RegisterAndLoginHooks.onPasswordResetStarted, {
861
+ phone: params.target.phone
862
+ });
863
+ }
864
+ let response;
865
+ if (params.type === "email") {
866
+ if (!params.target.email) {
867
+ throw new Error("邮箱地址不能为空");
868
+ }
869
+ response = await this.apiCaller.call(
870
+ "resetPasswordByEmail",
871
+ {
872
+ email: params.target.email,
873
+ password: params.password,
874
+ code: params.code
875
+ }
876
+ );
877
+ } else {
878
+ if (!params.target.phone || !params.target.country_calling_code) {
879
+ throw new Error("手机号和国家区号不能为空");
880
+ }
881
+ response = await this.apiCaller.call(
882
+ "resetPasswordByPhone",
883
+ {
884
+ phone: params.target.phone,
885
+ password: params.password,
886
+ code: params.code,
887
+ country_calling_code: params.target.country_calling_code
888
+ }
889
+ );
890
+ }
891
+ if (response.status) {
892
+ const key = params.type === "email" ? params.target.email : params.target.phone;
893
+ if (this.store.verificationCodeSent[key]) {
894
+ delete this.store.verificationCodeSent[key];
895
+ }
896
+ if (params.type === "email") {
897
+ this.core.effects.emit(import_types.RegisterAndLoginHooks.onPasswordResetSuccess, {
898
+ email: params.target.email
899
+ });
900
+ } else {
901
+ this.core.effects.emit(import_types.RegisterAndLoginHooks.onPasswordResetSuccess, {
902
+ phone: params.target.phone
903
+ });
904
+ }
905
+ } else {
906
+ this.store.error = response.message || "密码重置失败";
907
+ if (params.type === "email") {
908
+ this.core.effects.emit(import_types.RegisterAndLoginHooks.onPasswordResetFailed, {
909
+ email: params.target.email,
910
+ error: this.store.error
911
+ });
912
+ } else {
913
+ this.core.effects.emit(import_types.RegisterAndLoginHooks.onPasswordResetFailed, {
914
+ phone: params.target.phone,
915
+ error: this.store.error
916
+ });
917
+ }
918
+ }
919
+ return response;
920
+ } catch (error) {
921
+ this.store.error = "密码重置失败";
922
+ if (params.type === "email") {
923
+ this.core.effects.emit(import_types.RegisterAndLoginHooks.onPasswordResetFailed, {
924
+ email: params.target.email,
925
+ error: this.store.error
926
+ });
927
+ } else {
928
+ this.core.effects.emit(import_types.RegisterAndLoginHooks.onPasswordResetFailed, {
929
+ phone: params.target.phone,
930
+ error: this.store.error
931
+ });
932
+ }
933
+ return error;
934
+ } finally {
935
+ this.store.isLoading = false;
936
+ }
937
+ }
938
+ /** 发送手机登录验证码 */
939
+ async sendSmsLoginCode(params) {
940
+ try {
941
+ this.store.isLoading = true;
942
+ this.store.error = null;
943
+ const response = await this.apiCaller.call(
944
+ "sendSmsLoginCode",
945
+ params
946
+ );
947
+ if (response.status) {
948
+ const now = Date.now();
949
+ this.store.verificationCodeSent[params.phone] = {
950
+ sent: true,
951
+ sentAt: now,
952
+ expiresAt: now + 5 * 60 * 1e3
953
+ // 5分钟过期
954
+ };
955
+ await this.core.effects.emit(
956
+ import_types.RegisterAndLoginHooks.onSmsVerificationSent,
957
+ {
958
+ target: params.phone,
959
+ purpose: import_types.VerificationPurpose.LOGIN
960
+ }
961
+ );
962
+ } else {
963
+ this.store.error = response.message || "发送登录验证码失败";
964
+ await this.core.effects.emit(
965
+ import_types.RegisterAndLoginHooks.onSmsVerificationFailed,
966
+ {
967
+ target: params.phone,
968
+ error: this.store.error
969
+ }
970
+ );
971
+ }
972
+ return response;
973
+ } catch (error) {
974
+ this.store.error = "发送登录验证码失败";
975
+ await this.core.effects.emit(
976
+ import_types.RegisterAndLoginHooks.onSmsVerificationFailed,
977
+ {
978
+ target: params.phone,
979
+ error: this.store.error
980
+ }
981
+ );
982
+ return error;
983
+ } finally {
984
+ this.store.isLoading = false;
985
+ }
986
+ }
987
+ /** 手机号+短信验证码登录 */
988
+ async phoneCodeLogin(params) {
989
+ try {
990
+ this.store.isLoading = true;
991
+ this.store.error = null;
992
+ await this.core.effects.emit(import_types.RegisterAndLoginHooks.onLoginStarted, {
993
+ type: "phone-code",
994
+ phone: params.phone
995
+ });
996
+ const response = await this.apiCaller.call(
997
+ "phoneCodeLogin",
998
+ params
999
+ );
1000
+ if (response.status && response.data) {
1001
+ this.store.isLoggedIn = true;
1002
+ this.store.userInfo = response.data.user;
1003
+ this.store.token = response.data.token;
1004
+ if (response.data.refreshToken) {
1005
+ this.store.refreshToken = response.data.refreshToken;
1006
+ }
1007
+ await this.core.effects.emit(import_types.RegisterAndLoginHooks.onLoginSuccess, {
1008
+ user: response.data.user,
1009
+ token: response.data.token,
1010
+ loginType: "phone-code"
1011
+ });
1012
+ } else {
1013
+ this.store.error = response.message || "手机号验证码登录失败";
1014
+ await this.core.effects.emit(import_types.RegisterAndLoginHooks.onLoginFailed, {
1015
+ error: this.store.error,
1016
+ loginType: "phone-code"
1017
+ });
1018
+ }
1019
+ return response;
1020
+ } catch (error) {
1021
+ this.store.error = "手机号验证码登录失败";
1022
+ await this.core.effects.emit(import_types.RegisterAndLoginHooks.onLoginFailed, {
1023
+ error: this.store.error,
1024
+ loginType: "phone-code"
1025
+ });
1026
+ return error;
1027
+ } finally {
1028
+ this.store.isLoading = false;
1029
+ }
1030
+ }
1031
+ /** Guest 登录 */
1032
+ async guestLogin() {
1033
+ try {
1034
+ this.store.isLoading = true;
1035
+ this.store.error = null;
1036
+ this.core.effects.emit(import_types.RegisterAndLoginHooks.onLoginStarted, {
1037
+ type: "guest"
1038
+ });
1039
+ const response = await this.apiCaller.call("guestLogin", this.channel);
1040
+ if (response.status && response.data) {
1041
+ this.store.isLoggedIn = true;
1042
+ this.store.userInfo = response.data.user;
1043
+ this.store.token = response.data.token;
1044
+ if (response.data.refreshToken) {
1045
+ this.store.refreshToken = response.data.refreshToken;
1046
+ }
1047
+ this.core.effects.emit(import_types.RegisterAndLoginHooks.onLoginSuccess, {
1048
+ user: response.data.user,
1049
+ token: response.data.token,
1050
+ loginType: "guest"
1051
+ });
1052
+ } else {
1053
+ this.store.error = response.message || "Guest 登录失败";
1054
+ this.core.effects.emit(import_types.RegisterAndLoginHooks.onLoginFailed, {
1055
+ error: this.store.error,
1056
+ loginType: "guest"
1057
+ });
1058
+ }
1059
+ return response;
1060
+ } catch (error) {
1061
+ this.store.error = "Guest 登录失败";
1062
+ this.core.effects.emit(import_types.RegisterAndLoginHooks.onLoginFailed, {
1063
+ error: this.store.error,
1064
+ loginType: "guest"
1065
+ });
1066
+ return error;
1067
+ } finally {
1068
+ this.store.isLoading = false;
1069
+ }
1070
+ }
1071
+ /** 检查邮箱是否已注册 */
1072
+ async checkEmailExists(params) {
1073
+ try {
1074
+ this.store.isLoading = true;
1075
+ this.store.error = null;
1076
+ const response = await this.apiCaller.call("checkEmailExists", params);
1077
+ if (!response.status) {
1078
+ this.store.error = response.message || "检查邮箱失败";
1079
+ }
1080
+ return response;
1081
+ } catch (error) {
1082
+ this.store.error = "检查邮箱失败";
1083
+ return error;
1084
+ } finally {
1085
+ this.store.isLoading = false;
1086
+ }
1087
+ }
1088
+ /** 检查邮箱验证码是否有效 */
1089
+ async checkEmailCode(params) {
1090
+ try {
1091
+ this.store.isLoading = true;
1092
+ this.store.error = null;
1093
+ const response = await this.apiCaller.call("checkEmailCode", params);
1094
+ if (!response.status) {
1095
+ this.store.error = response.message || "检查邮箱验证码失败";
1096
+ }
1097
+ return response;
1098
+ } catch (error) {
1099
+ this.store.error = "检查邮箱验证码失败";
1100
+ return error;
1101
+ } finally {
1102
+ this.store.isLoading = false;
1103
+ }
1104
+ }
1105
+ /** 检查手机验证码是否有效 */
1106
+ async checkMobileCode(params) {
1107
+ try {
1108
+ this.store.isLoading = true;
1109
+ this.store.error = null;
1110
+ const response = await this.apiCaller.call("checkMobileCode", params);
1111
+ if (!response.status) {
1112
+ this.store.error = response.message || "检查手机验证码失败";
1113
+ }
1114
+ return response;
1115
+ } catch (error) {
1116
+ this.store.error = "检查手机验证码失败";
1117
+ return error;
1118
+ } finally {
1119
+ this.store.isLoading = false;
1120
+ }
1121
+ }
1122
+ /** 手机号密码登录 */
1123
+ async phonePasswordLogin(params) {
1124
+ try {
1125
+ this.store.isLoading = true;
1126
+ this.store.error = null;
1127
+ this.core.effects.emit(import_types.RegisterAndLoginHooks.onLoginStarted, {
1128
+ type: "phone-password",
1129
+ phone: params.phone
1130
+ });
1131
+ const response = await this.apiCaller.call(
1132
+ "phonePasswordLogin",
1133
+ params
1134
+ );
1135
+ if (response.status && response.data) {
1136
+ this.store.isLoggedIn = true;
1137
+ this.store.userInfo = response.data.user;
1138
+ this.store.token = response.data.token;
1139
+ if (response.data.refreshToken) {
1140
+ this.store.refreshToken = response.data.refreshToken;
1141
+ }
1142
+ this.core.effects.emit(import_types.RegisterAndLoginHooks.onLoginSuccess, {
1143
+ user: response.data.user,
1144
+ token: response.data.token,
1145
+ loginType: "phone-password"
1146
+ });
1147
+ } else {
1148
+ this.store.error = response.message || "手机号密码登录失败";
1149
+ this.core.effects.emit(import_types.RegisterAndLoginHooks.onLoginFailed, {
1150
+ error: this.store.error,
1151
+ loginType: "phone-password"
1152
+ });
1153
+ }
1154
+ return response;
1155
+ } catch (error) {
1156
+ this.store.error = "手机号密码登录失败";
1157
+ this.core.effects.emit(import_types.RegisterAndLoginHooks.onLoginFailed, {
1158
+ error: this.store.error,
1159
+ loginType: "phone-password"
1160
+ });
1161
+ return error;
1162
+ } finally {
1163
+ this.store.isLoading = false;
1164
+ }
1165
+ }
1166
+ /** 发送密码重置邮箱验证码 */
1167
+ async sendPasswordResetEmail(params) {
1168
+ try {
1169
+ this.store.isLoading = true;
1170
+ this.store.error = null;
1171
+ const response = await this.apiCaller.call(
1172
+ "sendPasswordResetEmail",
1173
+ params
1174
+ );
1175
+ if (response.status) {
1176
+ const now = Date.now();
1177
+ this.store.verificationCodeSent[params.email] = {
1178
+ sent: true,
1179
+ sentAt: now,
1180
+ expiresAt: now + 10 * 60 * 1e3
1181
+ // 10分钟过期
1182
+ };
1183
+ this.core.effects.emit(import_types.RegisterAndLoginHooks.onEmailVerificationSent, {
1184
+ target: params.email,
1185
+ purpose: import_types.VerificationPurpose.PASSWORD_RESET
1186
+ });
1187
+ } else {
1188
+ this.store.error = response.message || "发送密码重置验证码失败";
1189
+ this.core.effects.emit(
1190
+ import_types.RegisterAndLoginHooks.onEmailVerificationFailed,
1191
+ {
1192
+ target: params.email,
1193
+ error: this.store.error
1194
+ }
1195
+ );
1196
+ }
1197
+ return response;
1198
+ } catch (error) {
1199
+ this.store.error = "发送密码重置验证码失败";
1200
+ this.core.effects.emit(import_types.RegisterAndLoginHooks.onEmailVerificationFailed, {
1201
+ target: params.email,
1202
+ error: this.store.error
1203
+ });
1204
+ return error;
1205
+ } finally {
1206
+ this.store.isLoading = false;
1207
+ }
1208
+ }
1209
+ /** 发送手机号验证码重置密码 */
1210
+ async sendPasswordResetSms(params) {
1211
+ try {
1212
+ this.store.isLoading = true;
1213
+ this.store.error = null;
1214
+ const response = await this.apiCaller.call(
1215
+ "sendPasswordResetSms",
1216
+ params
1217
+ );
1218
+ if (response.status) {
1219
+ return response;
1220
+ } else {
1221
+ this.store.error = response.message || "发送手机号验证码重置密码失败";
1222
+ }
1223
+ return response;
1224
+ } catch (error) {
1225
+ this.store.error = "发送手机号验证码重置密码失败";
1226
+ return error;
1227
+ } finally {
1228
+ this.store.isLoading = false;
1229
+ }
1230
+ }
1231
+ /**
1232
+ * 获取当前用户信息
1233
+ */
1234
+ getCurrentUser() {
1235
+ return this.store.currentUser;
1236
+ }
1237
+ /**
1238
+ * 检查是否已登录
1239
+ */
1240
+ isLoggedIn() {
1241
+ return this.store.isLoggedIn;
1242
+ }
1243
+ /**
1244
+ * 获取加载状态
1245
+ */
1246
+ isLoading() {
1247
+ return this.store.isLoading;
1248
+ }
1249
+ /**
1250
+ * 获取错误信息
1251
+ */
1252
+ getError() {
1253
+ return this.store.error;
1254
+ }
1255
+ /**
1256
+ * 清除错误信息
1257
+ */
1258
+ clearError() {
1259
+ this.store.error = null;
1260
+ }
1261
+ /**
1262
+ * 检查验证码是否已发送且未过期
1263
+ */
1264
+ isVerificationCodeSent(target) {
1265
+ const codeInfo = this.store.verificationCodeSent[target];
1266
+ if (!codeInfo || !codeInfo.sent) {
1267
+ return false;
1268
+ }
1269
+ return Date.now() < codeInfo.expiresAt;
1270
+ }
1271
+ /**
1272
+ * 获取验证码剩余时间(秒)
1273
+ */
1274
+ getVerificationCodeRemainingTime(target) {
1275
+ const codeInfo = this.store.verificationCodeSent[target];
1276
+ if (!codeInfo || !codeInfo.sent) {
1277
+ return 0;
1278
+ }
1279
+ const remaining = Math.max(0, codeInfo.expiresAt - Date.now());
1280
+ return Math.ceil(remaining / 1e3);
1281
+ }
1282
+ /**
1283
+ * 初始化 Facebook SDK
1284
+ */
1285
+ async initFacebookSDK(token) {
1286
+ return new Promise((resolve, reject) => {
1287
+ if (typeof window === "undefined") {
1288
+ reject(new Error("Facebook SDK 只能在浏览器环境中使用"));
1289
+ return;
1290
+ }
1291
+ if (window.FB) {
1292
+ resolve();
1293
+ return;
1294
+ }
1295
+ const script = document.createElement("script");
1296
+ script.src = "https://connect.facebook.net/en_US/sdk.js";
1297
+ script.async = true;
1298
+ script.defer = true;
1299
+ script.crossOrigin = "anonymous";
1300
+ script.onload = () => {
1301
+ window.FB.init({
1302
+ appId: token,
1303
+ cookie: true,
1304
+ xfbml: true,
1305
+ version: "v18.0"
1306
+ });
1307
+ resolve();
1308
+ };
1309
+ script.onerror = () => {
1310
+ reject(new Error("Facebook SDK 加载失败"));
1311
+ };
1312
+ document.head.appendChild(script);
1313
+ });
1314
+ }
1315
+ /**
1316
+ * 初始化 Apple SDK
1317
+ */
1318
+ async initAppleSDK() {
1319
+ return new Promise((resolve, reject) => {
1320
+ if (typeof window === "undefined") {
1321
+ reject(new Error("Apple SDK 只能在浏览器环境中使用"));
1322
+ return;
1323
+ }
1324
+ if (window.AppleID) {
1325
+ resolve();
1326
+ return;
1327
+ }
1328
+ const script = document.createElement("script");
1329
+ script.src = "https://file.mypisell-dev.com/static/apple.js";
1330
+ script.async = true;
1331
+ script.defer = true;
1332
+ script.crossOrigin = "anonymous";
1333
+ script.onload = () => {
1334
+ setTimeout(() => {
1335
+ if (window.AppleID) {
1336
+ resolve();
1337
+ } else {
1338
+ reject(new Error("Apple SDK 加载失败:AppleID 对象不可用"));
1339
+ }
1340
+ }, 100);
1341
+ };
1342
+ script.onerror = () => {
1343
+ reject(new Error("Apple SDK 加载失败"));
1344
+ };
1345
+ document.head.appendChild(script);
1346
+ });
1347
+ }
1348
+ /**
1349
+ * Facebook 登录
1350
+ */
1351
+ async loginWithFacebook(token) {
1352
+ await this.initFacebookSDK(token);
1353
+ return new Promise((resolve, reject) => {
1354
+ window.FB.login(
1355
+ async (response) => {
1356
+ if (response.authResponse) {
1357
+ try {
1358
+ const result = await this.apiCaller.call("facebookLogin", {
1359
+ token: response.authResponse.accessToken
1360
+ });
1361
+ resolve(result);
1362
+ } catch (error) {
1363
+ reject(error);
1364
+ }
1365
+ } else {
1366
+ reject(new Error("facebook_login_cancel"));
1367
+ }
1368
+ },
1369
+ { scope: "email,public_profile" }
1370
+ );
1371
+ });
1372
+ }
1373
+ /**
1374
+ * Apple 登录(需要在支持的环境中使用)
1375
+ */
1376
+ async loginWithApple(token) {
1377
+ await this.initAppleSDK();
1378
+ return new Promise((resolve, reject) => {
1379
+ var _a, _b, _c, _d;
1380
+ try {
1381
+ const appleSignInDiv = document.createElement("div");
1382
+ appleSignInDiv.id = "appleid-signin-temp";
1383
+ appleSignInDiv.setAttribute("data-color", "black");
1384
+ appleSignInDiv.setAttribute("data-border", "false");
1385
+ appleSignInDiv.setAttribute("data-type", "sign-in");
1386
+ appleSignInDiv.setAttribute("data-mode", "logo-only");
1387
+ appleSignInDiv.setAttribute("data-logo-size", "small");
1388
+ appleSignInDiv.style.opacity = "0";
1389
+ appleSignInDiv.style.position = "absolute";
1390
+ appleSignInDiv.style.left = "-9999px";
1391
+ appleSignInDiv.style.top = "-9999px";
1392
+ appleSignInDiv.style.width = "1px";
1393
+ appleSignInDiv.style.height = "1px";
1394
+ document.body.appendChild(appleSignInDiv);
1395
+ if ((_b = (_a = window.AppleID) == null ? void 0 : _a.auth) == null ? void 0 : _b.init) {
1396
+ window.AppleID.auth.init({
1397
+ clientId: token,
1398
+ // 使用传入的 token 作为 clientId
1399
+ scope: "name email",
1400
+ redirectURI: window.location.origin,
1401
+ state: "signin",
1402
+ usePopup: true
1403
+ });
1404
+ }
1405
+ if ((_d = (_c = window.AppleID) == null ? void 0 : _c.auth) == null ? void 0 : _d.signIn) {
1406
+ window.AppleID.auth.signIn().then(async (authResult) => {
1407
+ var _a2, _b2;
1408
+ try {
1409
+ if (document.body.contains(appleSignInDiv)) {
1410
+ document.body.removeChild(appleSignInDiv);
1411
+ }
1412
+ const authorizationCode = (_a2 = authResult.authorization) == null ? void 0 : _a2.code;
1413
+ const identityToken = (_b2 = authResult.authorization) == null ? void 0 : _b2.id_token;
1414
+ const result = await this.apiCaller.call("appleLogin", {
1415
+ token: authorizationCode || identityToken
1416
+ });
1417
+ resolve(result);
1418
+ } catch (error) {
1419
+ reject(error);
1420
+ }
1421
+ }).catch((error) => {
1422
+ if (document.body.contains(appleSignInDiv)) {
1423
+ document.body.removeChild(appleSignInDiv);
1424
+ }
1425
+ reject(new Error("Apple 登录失败: " + ((error == null ? void 0 : error.error) || (error == null ? void 0 : error.message) || "未知错误")));
1426
+ });
1427
+ } else {
1428
+ reject(new Error("Apple SDK 未正确加载"));
1429
+ }
1430
+ } catch (error) {
1431
+ reject(error);
1432
+ }
1433
+ });
1434
+ }
1435
+ /**
1436
+ * 更新 OAuth 配置
1437
+ */
1438
+ updateOAuthConfig(config) {
1439
+ this.store.oauthConfig = {
1440
+ ...this.store.oauthConfig,
1441
+ ...config
1442
+ };
1443
+ }
1444
+ /**
1445
+ * 获取当前渠道
1446
+ */
1447
+ getCurrentChannel() {
1448
+ return this.channel;
1449
+ }
1450
+ /**
1451
+ * 获取 API 调用器(用于高级用法)
1452
+ */
1453
+ getApiCaller() {
1454
+ return this.apiCaller;
1455
+ }
1456
+ /**
1457
+ * 获取国家区号列表
1458
+ */
1459
+ async getCountries() {
1460
+ try {
1461
+ this.store.isLoading = true;
1462
+ this.store.error = null;
1463
+ const response = await this.apiCaller.call("getCountries");
1464
+ if (response.status && response.data) {
1465
+ return response.data;
1466
+ } else {
1467
+ this.store.error = response.message || "获取国家区号失败";
1468
+ throw new Error(this.store.error);
1469
+ }
1470
+ } catch (error) {
1471
+ this.store.error = "获取国家区号失败";
1472
+ console.error("[RegisterAndLogin] 获取国家区号失败:", error);
1473
+ throw error;
1474
+ } finally {
1475
+ this.store.isLoading = false;
1476
+ }
1477
+ }
1478
+ async destroy() {
1479
+ await this.core.effects.emit(import_types.RegisterAndLoginHooks.onDestroy, {});
1480
+ console.log("[RegisterAndLogin] 已销毁");
1481
+ }
1482
+ };
1483
+ // Annotate the CommonJS export names for ESM import in node:
1484
+ 0 && (module.exports = {
1485
+ RegisterAndLoginImpl,
1486
+ ...require("./types"),
1487
+ ...require("./config")
1488
+ });