@pisell/pisellos 0.0.194 → 0.0.195

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.
@@ -2,6 +2,13 @@ import { Module, PisellCore, ModuleOptions } from '../../types';
2
2
  import { BaseModule } from '../BaseModule';
3
3
  import { PaymentModuleAPI, PaymentMethod, PaymentOrder, PaymentItem, PaymentItemInput, PaymentUpdateFields, PushOrderParams, CashPayment, EftposPayment, WalletPayment } from './types';
4
4
  export * from './types';
5
+ export { generateRequestUniqueId };
6
+ /**
7
+ * 生成请求唯一ID
8
+ * 格式: 年月日时分秒毫秒+4位随机数
9
+ * 示例: 20241201143025123456
10
+ */
11
+ declare function generateRequestUniqueId(): string;
5
12
  /**
6
13
  * 支付模块实现
7
14
  *
@@ -28,6 +28,7 @@ import { EftposPaymentImpl } from "./eftpos";
28
28
  import { WalletPaymentImpl } from "./wallet";
29
29
  import { Decimal } from 'decimal.js';
30
30
  export * from "./types";
31
+ export { generateRequestUniqueId };
31
32
 
32
33
  /**
33
34
  * 格式化金额为保留两位小数的字符串
@@ -42,6 +43,25 @@ function formatAmount(amount) {
42
43
  }
43
44
  }
44
45
 
46
+ /**
47
+ * 生成请求唯一ID
48
+ * 格式: 年月日时分秒毫秒+4位随机数
49
+ * 示例: 20241201143025123456
50
+ */
51
+ function generateRequestUniqueId() {
52
+ var now = new Date();
53
+ var year = now.getFullYear();
54
+ var month = String(now.getMonth() + 1).padStart(2, '0');
55
+ var day = String(now.getDate()).padStart(2, '0');
56
+ var hour = String(now.getHours()).padStart(2, '0');
57
+ var minute = String(now.getMinutes()).padStart(2, '0');
58
+ var second = String(now.getSeconds()).padStart(2, '0');
59
+ var millisecond = String(now.getMilliseconds()).padStart(3, '0');
60
+ var randomDigits = Math.floor(Math.random() * 9000) + 1000; // 1000-9999的4位随机数
61
+
62
+ return "".concat(year).concat(month).concat(day).concat(hour).concat(minute).concat(second).concat(millisecond).concat(randomDigits);
63
+ }
64
+
45
65
  /**
46
66
  * 支付模块实现
47
67
  *
@@ -219,7 +239,7 @@ export var PaymentModule = /*#__PURE__*/function (_BaseModule) {
219
239
  key: "registerNetworkHandlers",
220
240
  value: function registerNetworkHandlers() {
221
241
  var _this2 = this;
222
- var network = this.app.usePlugin('network');
242
+ var network = this.app.plugins.get('network');
223
243
  this.logInfo('Registering network status listener');
224
244
  network === null || network === void 0 || network.addListener('networkStatusChange', function (status) {
225
245
  console.log('网络状态:', status.connected);
@@ -1721,6 +1741,7 @@ export var PaymentModule = /*#__PURE__*/function (_BaseModule) {
1721
1741
  value: (function () {
1722
1742
  var _runPaymentSyncQueue = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee25() {
1723
1743
  var _this3 = this;
1744
+ var _queue$tasks, queue;
1724
1745
  return _regeneratorRuntime().wrap(function _callee25$(_context25) {
1725
1746
  while (1) switch (_context25.prev = _context25.next) {
1726
1747
  case 0:
@@ -1734,8 +1755,20 @@ export var PaymentModule = /*#__PURE__*/function (_BaseModule) {
1734
1755
  this.logWarning('TasksManager not initialized, cannot run sync queue');
1735
1756
  return _context25.abrupt("return");
1736
1757
  case 6:
1758
+ // 检查队列里有没有要跑的东西,没有就不 run 了
1759
+ queue = this.app.tasksManager.getTaskQueue({
1760
+ module: 'payment',
1761
+ queueId: 'payment_sync'
1762
+ });
1763
+ if (queue !== null && queue !== void 0 && (_queue$tasks = queue.tasks) !== null && _queue$tasks !== void 0 && _queue$tasks.length) {
1764
+ _context25.next = 10;
1765
+ break;
1766
+ }
1767
+ console.log('[PaymentModule] 支付同步队列无内容,不执行');
1768
+ return _context25.abrupt("return");
1769
+ case 10:
1737
1770
  console.log('[PaymentModule] 开始执行支付同步队列');
1738
- _context25.next = 9;
1771
+ _context25.next = 13;
1739
1772
  return this.app.tasksManager.run({
1740
1773
  module: 'payment',
1741
1774
  queueId: 'payment_sync',
@@ -1750,21 +1783,21 @@ export var PaymentModule = /*#__PURE__*/function (_BaseModule) {
1750
1783
  }
1751
1784
  }
1752
1785
  });
1753
- case 9:
1786
+ case 13:
1754
1787
  this.logInfo('runPaymentSyncQueue completed successfully');
1755
- _context25.next = 16;
1788
+ _context25.next = 20;
1756
1789
  break;
1757
- case 12:
1758
- _context25.prev = 12;
1790
+ case 16:
1791
+ _context25.prev = 16;
1759
1792
  _context25.t0 = _context25["catch"](1);
1760
1793
  console.error('[PaymentModule] 执行支付同步队列失败', _context25.t0);
1761
1794
  this.logError('runPaymentSyncQueue failed', _context25.t0);
1762
1795
  // 不抛出错误,避免影响主流程
1763
- case 16:
1796
+ case 20:
1764
1797
  case "end":
1765
1798
  return _context25.stop();
1766
1799
  }
1767
- }, _callee25, this, [[1, 12]]);
1800
+ }, _callee25, this, [[1, 16]]);
1768
1801
  }));
1769
1802
  function runPaymentSyncQueue() {
1770
1803
  return _runPaymentSyncQueue.apply(this, arguments);
@@ -1782,6 +1815,9 @@ export var PaymentModule = /*#__PURE__*/function (_BaseModule) {
1782
1815
  _payload$task$payload = payload.task.payload, orderUuid = _payload$task$payload.orderUuid, orderId = _payload$task$payload.orderId, paymentData = _payload$task$payload.paymentData, core = _payload$task$payload.core;
1783
1816
  console.log("[PaymentModule] \u5F00\u59CB\u540E\u53F0\u540C\u6B65\u652F\u4ED8\uFF0C\u8BA2\u5355ID: ".concat(orderId));
1784
1817
 
1818
+ // 生成request_unique: 年月日时分秒毫秒+4位随机数
1819
+ paymentData.request_unique = generateRequestUniqueId();
1820
+
1785
1821
  // 从全局获取必要的实例
1786
1822
  app = window.app;
1787
1823
  logger = app === null || app === void 0 ? void 0 : app.logger; // 记录开始日志
@@ -1797,42 +1833,42 @@ export var PaymentModule = /*#__PURE__*/function (_BaseModule) {
1797
1833
  });
1798
1834
  }
1799
1835
  if (app) {
1800
- _context26.next = 7;
1836
+ _context26.next = 8;
1801
1837
  break;
1802
1838
  }
1803
1839
  throw new Error('App 实例未找到');
1804
- case 7:
1840
+ case 8:
1805
1841
  request = app.request;
1806
1842
  if (request) {
1807
- _context26.next = 10;
1843
+ _context26.next = 11;
1808
1844
  break;
1809
1845
  }
1810
1846
  throw new Error('Request 插件未找到');
1811
- case 10:
1847
+ case 11:
1812
1848
  dbManager = app.dbManager;
1813
1849
  if (dbManager) {
1814
- _context26.next = 13;
1850
+ _context26.next = 14;
1815
1851
  break;
1816
1852
  }
1817
1853
  throw new Error('DBManager 未找到');
1818
- case 13:
1819
- _context26.prev = 13;
1820
- _context26.next = 16;
1854
+ case 14:
1855
+ _context26.prev = 14;
1856
+ _context26.next = 17;
1821
1857
  return request.post("/shop/order/".concat(orderId, "/order-payment"), paymentData, {
1822
1858
  noToast: true
1823
1859
  });
1824
- case 16:
1860
+ case 17:
1825
1861
  requestRes = _context26.sent;
1826
1862
  if (!(requestRes.status === true)) {
1827
- _context26.next = 34;
1863
+ _context26.next = 35;
1828
1864
  break;
1829
1865
  }
1830
- _context26.next = 20;
1866
+ _context26.next = 21;
1831
1867
  return dbManager.get('order', orderUuid);
1832
- case 20:
1868
+ case 21:
1833
1869
  order = _context26.sent;
1834
1870
  if (!order) {
1835
- _context26.next = 26;
1871
+ _context26.next = 27;
1836
1872
  break;
1837
1873
  }
1838
1874
  order.payment.forEach(function (payment) {
@@ -1840,19 +1876,19 @@ export var PaymentModule = /*#__PURE__*/function (_BaseModule) {
1840
1876
  // 清除之前的错误状态
1841
1877
  delete payment.syncError;
1842
1878
  });
1843
- _context26.next = 25;
1879
+ _context26.next = 26;
1844
1880
  return dbManager.update('order', order);
1845
- case 25:
1846
- console.log("[PaymentModule] \u8BA2\u5355 ".concat(orderUuid, " \u540C\u6B65\u72B6\u6001\u5DF2\u66F4\u65B0"));
1847
1881
  case 26:
1882
+ console.log("[PaymentModule] \u8BA2\u5355 ".concat(orderUuid, " \u540C\u6B65\u72B6\u6001\u5DF2\u66F4\u65B0"));
1883
+ case 27:
1848
1884
  console.log("[PaymentModule] \u8BA2\u5355 ".concat(orderUuid, " \u540E\u53F0\u540C\u6B65\u6210\u529F"));
1849
- _context26.next = 29;
1885
+ _context26.next = 30;
1850
1886
  return core.effects.emit('payment:onPaymentSyncSuccess', {
1851
1887
  orderUuid: orderUuid,
1852
1888
  orderId: orderId,
1853
1889
  timestamp: Date.now()
1854
1890
  });
1855
- case 29:
1891
+ case 30:
1856
1892
  console.log("[PaymentModule] \u5DF2\u901A\u8FC7 effects \u901A\u77E5\u524D\u7AEF\u8BA2\u5355 ".concat(orderUuid, " \u540C\u6B65\u6210\u529F"));
1857
1893
 
1858
1894
  // 记录成功日志
@@ -1871,7 +1907,7 @@ export var PaymentModule = /*#__PURE__*/function (_BaseModule) {
1871
1907
  orderUuid: orderUuid,
1872
1908
  orderId: orderId
1873
1909
  });
1874
- case 34:
1910
+ case 35:
1875
1911
  // 接口返回失败,视为业务错误处理
1876
1912
  console.log("[PaymentModule] \u63A5\u53E3\u8FD4\u56DE\u5931\u8D25\uFF0C\u8BA2\u5355 ".concat(orderUuid, " \u540C\u6B65\u5931\u8D25"));
1877
1913
 
@@ -1883,18 +1919,18 @@ export var PaymentModule = /*#__PURE__*/function (_BaseModule) {
1883
1919
  businessError.isBusinessError = true;
1884
1920
  businessError.responseData = requestRes;
1885
1921
  throw businessError;
1886
- case 40:
1887
- _context26.next = 79;
1922
+ case 41:
1923
+ _context26.next = 80;
1888
1924
  break;
1889
- case 42:
1890
- _context26.prev = 42;
1891
- _context26.t0 = _context26["catch"](13);
1925
+ case 43:
1926
+ _context26.prev = 43;
1927
+ _context26.t0 = _context26["catch"](14);
1892
1928
  console.error('[PaymentModule] 后台同步失败:', _context26.t0);
1893
1929
 
1894
1930
  // 检查是否是网络错误(没有响应码)还是业务错误(有响应码)
1895
1931
  isNetworkError = !_context26.t0.response && !_context26.t0.status && (_context26.t0.code === 'NETWORK_ERROR' || _context26.t0.code === 'ERR_NETWORK' || _context26.t0.code === 'ECONNREFUSED' || _context26.t0.code === 'ENOTFOUND' || _context26.t0.code === 'ETIMEDOUT' || ((_error$message = _context26.t0.message) === null || _error$message === void 0 ? void 0 : _error$message.includes('网络')) || ((_error$message2 = _context26.t0.message) === null || _error$message2 === void 0 ? void 0 : _error$message2.includes('network')) || ((_error$message3 = _context26.t0.message) === null || _error$message3 === void 0 ? void 0 : _error$message3.includes('fetch')));
1896
1932
  if (!isNetworkError) {
1897
- _context26.next = 52;
1933
+ _context26.next = 53;
1898
1934
  break;
1899
1935
  }
1900
1936
  // 网络错误:重试
@@ -1914,12 +1950,12 @@ export var PaymentModule = /*#__PURE__*/function (_BaseModule) {
1914
1950
  });
1915
1951
  }
1916
1952
  throw _context26.t0;
1917
- case 52:
1953
+ case 53:
1918
1954
  // 业务错误(有响应码):不标记为已同步,通过 effects 通知前端,但不重试
1919
1955
  console.log("[PaymentModule] \u4E1A\u52A1\u9519\u8BEF\uFF0C\u8BA2\u5355 ".concat(orderUuid, " \u540C\u6B65\u5931\u8D25\uFF0C\u901A\u77E5\u524D\u7AEF"));
1920
- _context26.prev = 53;
1956
+ _context26.prev = 54;
1921
1957
  if (!(core && core.effects)) {
1922
- _context26.next = 62;
1958
+ _context26.next = 63;
1923
1959
  break;
1924
1960
  }
1925
1961
  // 通过 effects 通知前端同步错误
@@ -1936,15 +1972,15 @@ export var PaymentModule = /*#__PURE__*/function (_BaseModule) {
1936
1972
  // 可以添加更多响应数据
1937
1973
  errorData.responseData = _context26.t0.responseData;
1938
1974
  }
1939
- _context26.next = 59;
1975
+ _context26.next = 60;
1940
1976
  return core.effects.emit('payment:onPaymentSyncError', errorData);
1941
- case 59:
1977
+ case 60:
1942
1978
  console.log("[PaymentModule] \u5DF2\u901A\u8FC7 effects \u901A\u77E5\u524D\u7AEF\u8BA2\u5355 ".concat(orderUuid, " \u540C\u6B65\u9519\u8BEF"));
1943
- _context26.next = 63;
1979
+ _context26.next = 64;
1944
1980
  break;
1945
- case 62:
1946
- console.warn("[PaymentModule] \u65E0\u6CD5\u83B7\u53D6 core \u5B9E\u4F8B\uFF0C\u65E0\u6CD5\u901A\u77E5\u524D\u7AEF\u9519\u8BEF");
1947
1981
  case 63:
1982
+ console.warn("[PaymentModule] \u65E0\u6CD5\u83B7\u53D6 core \u5B9E\u4F8B\uFF0C\u65E0\u6CD5\u901A\u77E5\u524D\u7AEF\u9519\u8BEF");
1983
+ case 64:
1948
1984
  // 记录业务错误日志
1949
1985
  if (logger) {
1950
1986
  logger.addLog({
@@ -1960,11 +1996,11 @@ export var PaymentModule = /*#__PURE__*/function (_BaseModule) {
1960
1996
  }
1961
1997
  });
1962
1998
  }
1963
- _context26.next = 70;
1999
+ _context26.next = 71;
1964
2000
  break;
1965
- case 66:
1966
- _context26.prev = 66;
1967
- _context26.t1 = _context26["catch"](53);
2001
+ case 67:
2002
+ _context26.prev = 67;
2003
+ _context26.t1 = _context26["catch"](54);
1968
2004
  console.error("[PaymentModule] \u901A\u77E5\u524D\u7AEF\u540C\u6B65\u9519\u8BEF\u5931\u8D25:", _context26.t1);
1969
2005
 
1970
2006
  // 记录通知错误日志
@@ -1979,23 +2015,23 @@ export var PaymentModule = /*#__PURE__*/function (_BaseModule) {
1979
2015
  }
1980
2016
  });
1981
2017
  }
1982
- case 70:
1983
- _context26.next = 72;
2018
+ case 71:
2019
+ _context26.next = 73;
1984
2020
  return dbManager.get('order', orderUuid);
1985
- case 72:
2021
+ case 73:
1986
2022
  _order2 = _context26.sent;
1987
2023
  if (!_order2) {
1988
- _context26.next = 78;
2024
+ _context26.next = 79;
1989
2025
  break;
1990
2026
  }
1991
2027
  _order2.payment.forEach(function (payment) {
1992
2028
  payment.isSynced = true;
1993
2029
  });
1994
- _context26.next = 77;
2030
+ _context26.next = 78;
1995
2031
  return dbManager.update('order', _order2);
1996
- case 77:
1997
- console.log("[PaymentModule] \u8BA2\u5355 ".concat(orderUuid, " \u4E1A\u52A1\u9519\u8BEF\uFF0C\u5DF2\u6807\u8BB0\u4E3A\u5DF2\u540C\u6B65"));
1998
2032
  case 78:
2033
+ console.log("[PaymentModule] \u8BA2\u5355 ".concat(orderUuid, " \u4E1A\u52A1\u9519\u8BEF\uFF0C\u5DF2\u6807\u8BB0\u4E3A\u5DF2\u540C\u6B65"));
2034
+ case 79:
1999
2035
  return _context26.abrupt("return", {
2000
2036
  status: 'completed_with_error',
2001
2037
  orderUuid: orderUuid,
@@ -2003,11 +2039,11 @@ export var PaymentModule = /*#__PURE__*/function (_BaseModule) {
2003
2039
  error: _context26.t0.message || '业务错误',
2004
2040
  statusCode: ((_error$response3 = _context26.t0.response) === null || _error$response3 === void 0 ? void 0 : _error$response3.status) || _context26.t0.status
2005
2041
  });
2006
- case 79:
2042
+ case 80:
2007
2043
  case "end":
2008
2044
  return _context26.stop();
2009
2045
  }
2010
- }, _callee26, null, [[13, 42], [53, 66]]);
2046
+ }, _callee26, null, [[14, 43], [54, 67]]);
2011
2047
  }));
2012
2048
  function syncPaymentTask(_x24) {
2013
2049
  return _syncPaymentTask.apply(this, arguments);
@@ -2,6 +2,13 @@ import { Module, PisellCore, ModuleOptions } from '../../types';
2
2
  import { BaseModule } from '../BaseModule';
3
3
  import { PaymentModuleAPI, PaymentMethod, PaymentOrder, PaymentItem, PaymentItemInput, PaymentUpdateFields, PushOrderParams, CashPayment, EftposPayment, WalletPayment } from './types';
4
4
  export * from './types';
5
+ export { generateRequestUniqueId };
6
+ /**
7
+ * 生成请求唯一ID
8
+ * 格式: 年月日时分秒毫秒+4位随机数
9
+ * 示例: 20241201143025123456
10
+ */
11
+ declare function generateRequestUniqueId(): string;
5
12
  /**
6
13
  * 支付模块实现
7
14
  *
@@ -20,7 +20,8 @@ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: tru
20
20
  // src/modules/Payment/index.ts
21
21
  var Payment_exports = {};
22
22
  __export(Payment_exports, {
23
- PaymentModule: () => PaymentModule
23
+ PaymentModule: () => PaymentModule,
24
+ generateRequestUniqueId: () => generateRequestUniqueId
24
25
  });
25
26
  module.exports = __toCommonJS(Payment_exports);
26
27
  var import_BaseModule = require("../BaseModule");
@@ -40,6 +41,18 @@ function formatAmount(amount) {
40
41
  return "0.00";
41
42
  }
42
43
  }
44
+ function generateRequestUniqueId() {
45
+ const now = /* @__PURE__ */ new Date();
46
+ const year = now.getFullYear();
47
+ const month = String(now.getMonth() + 1).padStart(2, "0");
48
+ const day = String(now.getDate()).padStart(2, "0");
49
+ const hour = String(now.getHours()).padStart(2, "0");
50
+ const minute = String(now.getMinutes()).padStart(2, "0");
51
+ const second = String(now.getSeconds()).padStart(2, "0");
52
+ const millisecond = String(now.getMilliseconds()).padStart(3, "0");
53
+ const randomDigits = Math.floor(Math.random() * 9e3) + 1e3;
54
+ return `${year}${month}${day}${hour}${minute}${second}${millisecond}${randomDigits}`;
55
+ }
43
56
  var PaymentModule = class extends import_BaseModule.BaseModule {
44
57
  constructor(name, version) {
45
58
  super(name, version);
@@ -130,7 +143,7 @@ var PaymentModule = class extends import_BaseModule.BaseModule {
130
143
  *
131
144
  */
132
145
  registerNetworkHandlers() {
133
- const network = this.app.usePlugin("network");
146
+ const network = this.app.plugins.get("network");
134
147
  this.logInfo("Registering network status listener");
135
148
  network == null ? void 0 : network.addListener("networkStatusChange", (status) => {
136
149
  console.log("网络状态:", status.connected);
@@ -738,13 +751,18 @@ var PaymentModule = class extends import_BaseModule.BaseModule {
738
751
  var _a, _b, _c, _d, _e, _f;
739
752
  const { orderUuid, orderId, paymentData, core } = payload.task.payload;
740
753
  console.log(`[PaymentModule] 开始后台同步支付,订单ID: ${orderId}`);
754
+ paymentData.request_unique = generateRequestUniqueId();
741
755
  const app = window.app;
742
756
  const logger = app == null ? void 0 : app.logger;
743
757
  if (logger) {
744
758
  logger.addLog({
745
759
  type: "info",
746
760
  title: "[PaymentModule] Starting syncPaymentTask",
747
- metadata: { orderUuid, orderId, paymentDataKeys: Object.keys(paymentData) }
761
+ metadata: {
762
+ orderUuid,
763
+ orderId,
764
+ paymentDataKeys: Object.keys(paymentData)
765
+ }
748
766
  });
749
767
  }
750
768
  if (!app) {
@@ -878,7 +896,9 @@ var PaymentModule = class extends import_BaseModule.BaseModule {
878
896
  payment.isSynced = true;
879
897
  });
880
898
  await dbManager.update("order", order);
881
- console.log(`[PaymentModule] 订单 ${orderUuid} 业务错误,已标记为已同步`);
899
+ console.log(
900
+ `[PaymentModule] 订单 ${orderUuid} 业务错误,已标记为已同步`
901
+ );
882
902
  }
883
903
  return {
884
904
  status: "completed_with_error",
@@ -964,6 +984,7 @@ var PaymentModule = class extends import_BaseModule.BaseModule {
964
984
  * 外部可以调用此方法来触发支付同步任务的执行
965
985
  */
966
986
  async runPaymentSyncQueue() {
987
+ var _a;
967
988
  this.logInfo("Starting runPaymentSyncQueue");
968
989
  try {
969
990
  if (!this.app.tasksManager) {
@@ -971,6 +992,14 @@ var PaymentModule = class extends import_BaseModule.BaseModule {
971
992
  this.logWarning("TasksManager not initialized, cannot run sync queue");
972
993
  return;
973
994
  }
995
+ const queue = this.app.tasksManager.getTaskQueue({
996
+ module: "payment",
997
+ queueId: "payment_sync"
998
+ });
999
+ if (!((_a = queue == null ? void 0 : queue.tasks) == null ? void 0 : _a.length)) {
1000
+ console.log("[PaymentModule] 支付同步队列无内容,不执行");
1001
+ return;
1002
+ }
974
1003
  console.log("[PaymentModule] 开始执行支付同步队列");
975
1004
  await this.app.tasksManager.run({
976
1005
  module: "payment",
@@ -996,5 +1025,6 @@ var PaymentModule = class extends import_BaseModule.BaseModule {
996
1025
  // Annotate the CommonJS export names for ESM import in node:
997
1026
  0 && (module.exports = {
998
1027
  PaymentModule,
1028
+ generateRequestUniqueId,
999
1029
  ...require("./types")
1000
1030
  });
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "private": false,
3
3
  "name": "@pisell/pisellos",
4
- "version": "0.0.194",
4
+ "version": "0.0.195",
5
5
  "description": "一个可扩展的前端模块化SDK框架,支持插件系统",
6
6
  "main": "dist/index.js",
7
7
  "types": "dist/index.d.ts",