@pisell/pisellos 3.0.69 → 3.0.71

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