@pisell/pisellos 0.0.337 → 0.0.338

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.
@@ -17,6 +17,7 @@ export declare class RegisterAndLoginImpl extends BaseModule implements Module {
17
17
  private apiCaller;
18
18
  private otherParams;
19
19
  private channel;
20
+ private countriesCache;
20
21
  constructor(name?: string, version?: string);
21
22
  /**
22
23
  * 重新发送邮箱注册链接
@@ -179,5 +180,9 @@ export declare class RegisterAndLoginImpl extends BaseModule implements Module {
179
180
  * 获取国家区号列表
180
181
  */
181
182
  getCountries(): Promise<CountryInfo[]>;
183
+ /**
184
+ * 异步更新国家缓存数据
185
+ */
186
+ private updateCountriesCache;
182
187
  destroy(): Promise<void>;
183
188
  }
@@ -43,6 +43,8 @@ export var RegisterAndLoginImpl = /*#__PURE__*/function (_BaseModule) {
43
43
  _defineProperty(_assertThisInitialized(_this), "apiCaller", void 0);
44
44
  _defineProperty(_assertThisInitialized(_this), "otherParams", {});
45
45
  _defineProperty(_assertThisInitialized(_this), "channel", 'default');
46
+ // 内存缓存
47
+ _defineProperty(_assertThisInitialized(_this), "countriesCache", null);
46
48
  return _this;
47
49
  }
48
50
 
@@ -983,11 +985,11 @@ export var RegisterAndLoginImpl = /*#__PURE__*/function (_BaseModule) {
983
985
  this.store.isLoggedIn = true;
984
986
 
985
987
  // 保存到本地存储
986
- this.window.localStorage.setItem('auth_token', token);
987
- this.window.localStorage.setItem('user_info', JSON.stringify(user));
988
- if (refreshToken) {
989
- this.window.localStorage.setItem('refresh_token', refreshToken);
990
- }
988
+ // this.window.localStorage.setItem('auth_token', token);
989
+ // this.window.localStorage.setItem('user_info', JSON.stringify(user));
990
+ // if (refreshToken) {
991
+ // this.window.localStorage.setItem('refresh_token', refreshToken);
992
+ // }
991
993
  }
992
994
  _context14.next = 13;
993
995
  return this.core.effects.emit(RegisterAndLoginHooks.onRegisterSuccess, response.data);
@@ -1052,7 +1054,7 @@ export var RegisterAndLoginImpl = /*#__PURE__*/function (_BaseModule) {
1052
1054
  case 7:
1053
1055
  response = _context15.sent;
1054
1056
  if (!(response.status && response.data)) {
1055
- _context15.next = 19;
1057
+ _context15.next = 16;
1056
1058
  break;
1057
1059
  }
1058
1060
  _response$data5 = response.data, user = _response$data5.user, token = _response$data5.token, refreshToken = _response$data5.refreshToken;
@@ -1060,45 +1062,45 @@ export var RegisterAndLoginImpl = /*#__PURE__*/function (_BaseModule) {
1060
1062
  this.store.isLoggedIn = true;
1061
1063
 
1062
1064
  // 保存到本地存储
1063
- this.window.localStorage.setItem('auth_token', token);
1064
- this.window.localStorage.setItem('user_info', JSON.stringify(user));
1065
- if (refreshToken) {
1066
- this.window.localStorage.setItem('refresh_token', refreshToken);
1067
- }
1068
- _context15.next = 17;
1065
+ // this.window.localStorage.setItem('auth_token', token);
1066
+ // this.window.localStorage.setItem('user_info', JSON.stringify(user));
1067
+ // if (refreshToken) {
1068
+ // this.window.localStorage.setItem('refresh_token', refreshToken);
1069
+ // }
1070
+ _context15.next = 14;
1069
1071
  return this.core.effects.emit(RegisterAndLoginHooks.onLoginSuccess, response.data);
1070
- case 17:
1071
- _context15.next = 22;
1072
+ case 14:
1073
+ _context15.next = 19;
1072
1074
  break;
1073
- case 19:
1075
+ case 16:
1074
1076
  this.store.error = response.message || '登录失败';
1075
- _context15.next = 22;
1077
+ _context15.next = 19;
1076
1078
  return this.core.effects.emit(RegisterAndLoginHooks.onLoginFailed, {
1077
1079
  error: this.store.error,
1078
1080
  params: params
1079
1081
  });
1080
- case 22:
1082
+ case 19:
1081
1083
  return _context15.abrupt("return", response);
1082
- case 25:
1083
- _context15.prev = 25;
1084
+ case 22:
1085
+ _context15.prev = 22;
1084
1086
  _context15.t0 = _context15["catch"](0);
1085
1087
  this.store.error = '登录失败';
1086
- _context15.next = 30;
1088
+ _context15.next = 27;
1087
1089
  return this.core.effects.emit(RegisterAndLoginHooks.onLoginFailed, {
1088
1090
  error: this.store.error,
1089
1091
  params: params
1090
1092
  });
1091
- case 30:
1093
+ case 27:
1092
1094
  throw _context15.t0;
1093
- case 31:
1094
- _context15.prev = 31;
1095
+ case 28:
1096
+ _context15.prev = 28;
1095
1097
  this.store.isLoading = false;
1096
- return _context15.finish(31);
1097
- case 34:
1098
+ return _context15.finish(28);
1099
+ case 31:
1098
1100
  case "end":
1099
1101
  return _context15.stop();
1100
1102
  }
1101
- }, _callee15, this, [[0, 25, 31, 34]]);
1103
+ }, _callee15, this, [[0, 22, 28, 31]]);
1102
1104
  }));
1103
1105
  function login(_x14) {
1104
1106
  return _login.apply(this, arguments);
@@ -1128,7 +1130,7 @@ export var RegisterAndLoginImpl = /*#__PURE__*/function (_BaseModule) {
1128
1130
  case 7:
1129
1131
  response = _context16.sent;
1130
1132
  if (!(response.status && response.data)) {
1131
- _context16.next = 19;
1133
+ _context16.next = 16;
1132
1134
  break;
1133
1135
  }
1134
1136
  _response$data6 = response.data, user = _response$data6.user, token = _response$data6.token, refreshToken = _response$data6.refreshToken;
@@ -1136,45 +1138,45 @@ export var RegisterAndLoginImpl = /*#__PURE__*/function (_BaseModule) {
1136
1138
  this.store.isLoggedIn = true;
1137
1139
 
1138
1140
  // 保存到本地存储
1139
- this.window.localStorage.setItem('auth_token', token);
1140
- this.window.localStorage.setItem('user_info', JSON.stringify(user));
1141
- if (refreshToken) {
1142
- this.window.localStorage.setItem('refresh_token', refreshToken);
1143
- }
1144
- _context16.next = 17;
1141
+ // this.window.localStorage.setItem('auth_token', token);
1142
+ // this.window.localStorage.setItem('user_info', JSON.stringify(user));
1143
+ // if (refreshToken) {
1144
+ // this.window.localStorage.setItem('refresh_token', refreshToken);
1145
+ // }
1146
+ _context16.next = 14;
1145
1147
  return this.core.effects.emit(RegisterAndLoginHooks.onOAuthLoginSuccess, response.data);
1146
- case 17:
1147
- _context16.next = 22;
1148
+ case 14:
1149
+ _context16.next = 19;
1148
1150
  break;
1149
- case 19:
1151
+ case 16:
1150
1152
  this.store.error = response.message || 'OAuth 登录失败';
1151
- _context16.next = 22;
1153
+ _context16.next = 19;
1152
1154
  return this.core.effects.emit(RegisterAndLoginHooks.onOAuthLoginFailed, {
1153
1155
  error: this.store.error,
1154
1156
  params: params
1155
1157
  });
1156
- case 22:
1158
+ case 19:
1157
1159
  return _context16.abrupt("return", response);
1158
- case 25:
1159
- _context16.prev = 25;
1160
+ case 22:
1161
+ _context16.prev = 22;
1160
1162
  _context16.t0 = _context16["catch"](0);
1161
1163
  this.store.error = 'OAuth 登录失败';
1162
- _context16.next = 30;
1164
+ _context16.next = 27;
1163
1165
  return this.core.effects.emit(RegisterAndLoginHooks.onOAuthLoginFailed, {
1164
1166
  error: this.store.error,
1165
1167
  params: params
1166
1168
  });
1167
- case 30:
1169
+ case 27:
1168
1170
  throw _context16.t0;
1169
- case 31:
1170
- _context16.prev = 31;
1171
+ case 28:
1172
+ _context16.prev = 28;
1171
1173
  this.store.isLoading = false;
1172
- return _context16.finish(31);
1173
- case 34:
1174
+ return _context16.finish(28);
1175
+ case 31:
1174
1176
  case "end":
1175
1177
  return _context16.stop();
1176
1178
  }
1177
- }, _callee16, this, [[0, 25, 31, 34]]);
1179
+ }, _callee16, this, [[0, 22, 28, 31]]);
1178
1180
  }));
1179
1181
  function oauthLogin(_x15) {
1180
1182
  return _oauthLogin.apply(this, arguments);
@@ -2418,65 +2420,158 @@ export var RegisterAndLoginImpl = /*#__PURE__*/function (_BaseModule) {
2418
2420
  key: "getCountries",
2419
2421
  value: (function () {
2420
2422
  var _getCountries = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee38() {
2421
- var response;
2423
+ var CACHE_KEY, cachedData, cached, response, countries;
2422
2424
  return _regeneratorRuntime().wrap(function _callee38$(_context38) {
2423
2425
  while (1) switch (_context38.prev = _context38.next) {
2424
2426
  case 0:
2425
- _context38.prev = 0;
2427
+ CACHE_KEY = 'pisell_countries_cache';
2428
+ _context38.prev = 1;
2426
2429
  this.store.isLoading = true;
2427
2430
  this.store.error = null;
2428
- _context38.next = 5;
2431
+
2432
+ // 1. 检查内存缓存
2433
+ if (!this.countriesCache) {
2434
+ _context38.next = 7;
2435
+ break;
2436
+ }
2437
+ this.store.isLoading = false;
2438
+ return _context38.abrupt("return", this.countriesCache);
2439
+ case 7:
2440
+ // 2. 检查 localStorage 缓存
2441
+ cachedData = null;
2442
+ try {
2443
+ cached = this.window.localStorage.getItem(CACHE_KEY);
2444
+ if (cached) {
2445
+ cachedData = JSON.parse(cached);
2446
+ }
2447
+ } catch (error) {
2448
+ console.warn('[RegisterAndLogin] localStorage 缓存解析失败:', error);
2449
+ }
2450
+
2451
+ // 3. 如果有 localStorage 缓存,先返回缓存数据
2452
+ if (!(cachedData && Array.isArray(cachedData) && cachedData.length > 0)) {
2453
+ _context38.next = 14;
2454
+ break;
2455
+ }
2456
+ // 设置内存缓存
2457
+ this.countriesCache = cachedData;
2458
+
2459
+ // 异步请求后端数据进行对比和更新
2460
+ this.updateCountriesCache(CACHE_KEY, cachedData);
2461
+ this.store.isLoading = false;
2462
+ return _context38.abrupt("return", cachedData);
2463
+ case 14:
2464
+ _context38.next = 16;
2429
2465
  return this.apiCaller.call('getCountries');
2430
- case 5:
2466
+ case 16:
2431
2467
  response = _context38.sent;
2432
2468
  if (!(response.status && response.data)) {
2433
- _context38.next = 10;
2469
+ _context38.next = 24;
2434
2470
  break;
2435
2471
  }
2436
- return _context38.abrupt("return", response.data);
2437
- case 10:
2472
+ countries = response.data; // 更新内存缓存
2473
+ this.countriesCache = countries;
2474
+
2475
+ // 更新 localStorage 缓存
2476
+ try {
2477
+ localStorage.setItem(CACHE_KEY, JSON.stringify(countries));
2478
+ } catch (error) {
2479
+ console.warn('[RegisterAndLogin] localStorage 存储失败:', error);
2480
+ }
2481
+ return _context38.abrupt("return", countries);
2482
+ case 24:
2438
2483
  this.store.error = response.message || '获取国家区号失败';
2439
2484
  throw new Error(this.store.error);
2440
- case 12:
2441
- _context38.next = 19;
2485
+ case 26:
2486
+ _context38.next = 33;
2442
2487
  break;
2443
- case 14:
2444
- _context38.prev = 14;
2445
- _context38.t0 = _context38["catch"](0);
2488
+ case 28:
2489
+ _context38.prev = 28;
2490
+ _context38.t0 = _context38["catch"](1);
2446
2491
  this.store.error = '获取国家区号失败';
2447
2492
  console.error('[RegisterAndLogin] 获取国家区号失败:', _context38.t0);
2448
2493
  throw _context38.t0;
2449
- case 19:
2450
- _context38.prev = 19;
2494
+ case 33:
2495
+ _context38.prev = 33;
2451
2496
  this.store.isLoading = false;
2452
- return _context38.finish(19);
2453
- case 22:
2497
+ return _context38.finish(33);
2498
+ case 36:
2454
2499
  case "end":
2455
2500
  return _context38.stop();
2456
2501
  }
2457
- }, _callee38, this, [[0, 14, 19, 22]]);
2502
+ }, _callee38, this, [[1, 28, 33, 36]]);
2458
2503
  }));
2459
2504
  function getCountries() {
2460
2505
  return _getCountries.apply(this, arguments);
2461
2506
  }
2462
2507
  return getCountries;
2508
+ }()
2509
+ /**
2510
+ * 异步更新国家缓存数据
2511
+ */
2512
+ )
2513
+ }, {
2514
+ key: "updateCountriesCache",
2515
+ value: (function () {
2516
+ var _updateCountriesCache = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee39(cacheKey, cachedData) {
2517
+ var response, newData, hasChanged;
2518
+ return _regeneratorRuntime().wrap(function _callee39$(_context39) {
2519
+ while (1) switch (_context39.prev = _context39.next) {
2520
+ case 0:
2521
+ _context39.prev = 0;
2522
+ _context39.next = 3;
2523
+ return this.apiCaller.call('getCountries');
2524
+ case 3:
2525
+ response = _context39.sent;
2526
+ if (response.status && response.data) {
2527
+ newData = response.data; // 比较数据是否有变化(简单的长度和内容对比)
2528
+ hasChanged = newData.length !== cachedData.length || JSON.stringify(newData) !== JSON.stringify(cachedData);
2529
+ if (hasChanged) {
2530
+ // 更新内存缓存
2531
+ this.countriesCache = newData;
2532
+
2533
+ // 更新 localStorage 缓存
2534
+ try {
2535
+ localStorage.setItem(cacheKey, JSON.stringify(newData));
2536
+ } catch (error) {
2537
+ console.warn('[RegisterAndLogin] localStorage 更新失败:', error);
2538
+ }
2539
+ console.log('[RegisterAndLogin] 国家数据已更新');
2540
+ }
2541
+ }
2542
+ _context39.next = 10;
2543
+ break;
2544
+ case 7:
2545
+ _context39.prev = 7;
2546
+ _context39.t0 = _context39["catch"](0);
2547
+ console.warn('[RegisterAndLogin] 后台更新国家数据失败:', _context39.t0);
2548
+ case 10:
2549
+ case "end":
2550
+ return _context39.stop();
2551
+ }
2552
+ }, _callee39, this, [[0, 7]]);
2553
+ }));
2554
+ function updateCountriesCache(_x34, _x35) {
2555
+ return _updateCountriesCache.apply(this, arguments);
2556
+ }
2557
+ return updateCountriesCache;
2463
2558
  }())
2464
2559
  }, {
2465
2560
  key: "destroy",
2466
2561
  value: function () {
2467
- var _destroy = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee39() {
2468
- return _regeneratorRuntime().wrap(function _callee39$(_context39) {
2469
- while (1) switch (_context39.prev = _context39.next) {
2562
+ var _destroy = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee40() {
2563
+ return _regeneratorRuntime().wrap(function _callee40$(_context40) {
2564
+ while (1) switch (_context40.prev = _context40.next) {
2470
2565
  case 0:
2471
- _context39.next = 2;
2566
+ _context40.next = 2;
2472
2567
  return this.core.effects.emit(RegisterAndLoginHooks.onDestroy, {});
2473
2568
  case 2:
2474
2569
  console.log('[RegisterAndLogin] 已销毁');
2475
2570
  case 3:
2476
2571
  case "end":
2477
- return _context39.stop();
2572
+ return _context40.stop();
2478
2573
  }
2479
- }, _callee39, this);
2574
+ }, _callee40, this);
2480
2575
  }));
2481
2576
  function destroy() {
2482
2577
  return _destroy.apply(this, arguments);
@@ -17,6 +17,7 @@ export declare class RegisterAndLoginImpl extends BaseModule implements Module {
17
17
  private apiCaller;
18
18
  private otherParams;
19
19
  private channel;
20
+ private countriesCache;
20
21
  constructor(name?: string, version?: string);
21
22
  /**
22
23
  * 重新发送邮箱注册链接
@@ -179,5 +180,9 @@ export declare class RegisterAndLoginImpl extends BaseModule implements Module {
179
180
  * 获取国家区号列表
180
181
  */
181
182
  getCountries(): Promise<CountryInfo[]>;
183
+ /**
184
+ * 异步更新国家缓存数据
185
+ */
186
+ private updateCountriesCache;
182
187
  destroy(): Promise<void>;
183
188
  }
@@ -37,6 +37,8 @@ var RegisterAndLoginImpl = class extends import_BaseModule.BaseModule {
37
37
  this.isSolution = true;
38
38
  this.otherParams = {};
39
39
  this.channel = "default";
40
+ // 内存缓存
41
+ this.countriesCache = null;
40
42
  }
41
43
  /**
42
44
  * 重新发送邮箱注册链接
@@ -607,11 +609,6 @@ var RegisterAndLoginImpl = class extends import_BaseModule.BaseModule {
607
609
  if (token) {
608
610
  this.store.currentUser = user;
609
611
  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
612
  }
616
613
  await this.core.effects.emit(
617
614
  import_types.RegisterAndLoginHooks.onRegisterSuccess,
@@ -655,11 +652,6 @@ var RegisterAndLoginImpl = class extends import_BaseModule.BaseModule {
655
652
  const { user, token, refreshToken } = response.data;
656
653
  this.store.currentUser = user;
657
654
  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
655
  await this.core.effects.emit(
664
656
  import_types.RegisterAndLoginHooks.onLoginSuccess,
665
657
  response.data
@@ -699,11 +691,6 @@ var RegisterAndLoginImpl = class extends import_BaseModule.BaseModule {
699
691
  const { user, token, refreshToken } = response.data;
700
692
  this.store.currentUser = user;
701
693
  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
694
  await this.core.effects.emit(
708
695
  import_types.RegisterAndLoginHooks.onOAuthLoginSuccess,
709
696
  response.data
@@ -1457,12 +1444,39 @@ var RegisterAndLoginImpl = class extends import_BaseModule.BaseModule {
1457
1444
  * 获取国家区号列表
1458
1445
  */
1459
1446
  async getCountries() {
1447
+ const CACHE_KEY = "pisell_countries_cache";
1460
1448
  try {
1461
1449
  this.store.isLoading = true;
1462
1450
  this.store.error = null;
1451
+ if (this.countriesCache) {
1452
+ this.store.isLoading = false;
1453
+ return this.countriesCache;
1454
+ }
1455
+ let cachedData = null;
1456
+ try {
1457
+ const cached = this.window.localStorage.getItem(CACHE_KEY);
1458
+ if (cached) {
1459
+ cachedData = JSON.parse(cached);
1460
+ }
1461
+ } catch (error) {
1462
+ console.warn("[RegisterAndLogin] localStorage 缓存解析失败:", error);
1463
+ }
1464
+ if (cachedData && Array.isArray(cachedData) && cachedData.length > 0) {
1465
+ this.countriesCache = cachedData;
1466
+ this.updateCountriesCache(CACHE_KEY, cachedData);
1467
+ this.store.isLoading = false;
1468
+ return cachedData;
1469
+ }
1463
1470
  const response = await this.apiCaller.call("getCountries");
1464
1471
  if (response.status && response.data) {
1465
- return response.data;
1472
+ const countries = response.data;
1473
+ this.countriesCache = countries;
1474
+ try {
1475
+ localStorage.setItem(CACHE_KEY, JSON.stringify(countries));
1476
+ } catch (error) {
1477
+ console.warn("[RegisterAndLogin] localStorage 存储失败:", error);
1478
+ }
1479
+ return countries;
1466
1480
  } else {
1467
1481
  this.store.error = response.message || "获取国家区号失败";
1468
1482
  throw new Error(this.store.error);
@@ -1475,6 +1489,29 @@ var RegisterAndLoginImpl = class extends import_BaseModule.BaseModule {
1475
1489
  this.store.isLoading = false;
1476
1490
  }
1477
1491
  }
1492
+ /**
1493
+ * 异步更新国家缓存数据
1494
+ */
1495
+ async updateCountriesCache(cacheKey, cachedData) {
1496
+ try {
1497
+ const response = await this.apiCaller.call("getCountries");
1498
+ if (response.status && response.data) {
1499
+ const newData = response.data;
1500
+ const hasChanged = newData.length !== cachedData.length || JSON.stringify(newData) !== JSON.stringify(cachedData);
1501
+ if (hasChanged) {
1502
+ this.countriesCache = newData;
1503
+ try {
1504
+ localStorage.setItem(cacheKey, JSON.stringify(newData));
1505
+ } catch (error) {
1506
+ console.warn("[RegisterAndLogin] localStorage 更新失败:", error);
1507
+ }
1508
+ console.log("[RegisterAndLogin] 国家数据已更新");
1509
+ }
1510
+ }
1511
+ } catch (error) {
1512
+ console.warn("[RegisterAndLogin] 后台更新国家数据失败:", error);
1513
+ }
1514
+ }
1478
1515
  async destroy() {
1479
1516
  await this.core.effects.emit(import_types.RegisterAndLoginHooks.onDestroy, {});
1480
1517
  console.log("[RegisterAndLogin] 已销毁");
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "private": false,
3
3
  "name": "@pisell/pisellos",
4
- "version": "0.0.337",
4
+ "version": "0.0.338",
5
5
  "description": "一个可扩展的前端模块化SDK框架,支持插件系统",
6
6
  "main": "dist/index.js",
7
7
  "types": "dist/index.d.ts",