@pisell/pisellos 0.0.480 → 0.0.481

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.
Files changed (147) hide show
  1. package/dist/core/index.d.ts +3 -2
  2. package/dist/core/index.js +7 -0
  3. package/dist/effects/index.d.ts +2 -2
  4. package/dist/effects/index.js +34 -81
  5. package/dist/model/strategy/adapter/promotion/evaluator.js +99 -26
  6. package/dist/model/strategy/adapter/walletPass/type.d.ts +9 -2
  7. package/dist/model/strategy/adapter/walletPass/utils.d.ts +6 -6
  8. package/dist/model/strategy/adapter/walletPass/utils.js +104 -77
  9. package/dist/modules/Customer/index.js +1 -1
  10. package/dist/modules/Discount/index.d.ts +6 -2
  11. package/dist/modules/Discount/index.js +14 -8
  12. package/dist/modules/Order/index.d.ts +1 -1
  13. package/dist/modules/Order/index.js +18 -13
  14. package/dist/modules/Payment/index.d.ts +4 -0
  15. package/dist/modules/Payment/index.js +774 -649
  16. package/dist/modules/Payment/walletpass.js +44 -17
  17. package/dist/modules/Product/index.d.ts +1 -1
  18. package/dist/modules/Product/types.d.ts +2 -0
  19. package/dist/modules/ProductList/index.d.ts +3 -0
  20. package/dist/modules/ProductList/index.js +9 -7
  21. package/dist/modules/Rules/index.d.ts +2 -2
  22. package/dist/modules/Rules/index.js +37 -31
  23. package/dist/modules/Rules/types.d.ts +2 -2
  24. package/dist/modules/Schedule/index.d.ts +9 -0
  25. package/dist/modules/Schedule/index.js +15 -2
  26. package/dist/plugins/app-types/app/app.d.ts +1 -0
  27. package/dist/plugins/request.d.ts +2 -0
  28. package/dist/server/index.d.ts +107 -2
  29. package/dist/server/index.js +1507 -279
  30. package/dist/server/modules/index.d.ts +6 -0
  31. package/dist/server/modules/index.js +7 -0
  32. package/dist/server/modules/menu/index.d.ts +19 -0
  33. package/dist/server/modules/menu/index.js +221 -71
  34. package/dist/server/modules/order/index.d.ts +87 -0
  35. package/dist/server/modules/order/index.js +916 -0
  36. package/dist/server/modules/order/types.d.ts +530 -0
  37. package/dist/server/modules/order/types.js +141 -0
  38. package/dist/server/modules/order/utils/filterBookings.d.ts +6 -0
  39. package/dist/server/modules/order/utils/filterBookings.js +350 -0
  40. package/dist/server/modules/order/utils/filterOrders.d.ts +15 -0
  41. package/dist/server/modules/order/utils/filterOrders.js +226 -0
  42. package/dist/server/modules/products/index.d.ts +117 -5
  43. package/dist/server/modules/products/index.js +1450 -240
  44. package/dist/server/modules/products/types.d.ts +25 -1
  45. package/dist/server/modules/products/types.js +3 -0
  46. package/dist/server/modules/resource/index.d.ts +86 -0
  47. package/dist/server/modules/resource/index.js +1128 -0
  48. package/dist/server/modules/resource/types.d.ts +121 -0
  49. package/dist/server/modules/resource/types.js +47 -0
  50. package/dist/server/modules/schedule/index.d.ts +19 -0
  51. package/dist/server/modules/schedule/index.js +229 -68
  52. package/dist/server/utils/product.d.ts +5 -0
  53. package/dist/server/utils/product.js +71 -31
  54. package/dist/solution/BookingTicket/index.d.ts +10 -2
  55. package/dist/solution/BookingTicket/index.js +41 -28
  56. package/dist/solution/BookingTicket/utils/scan/index.js +1 -1
  57. package/dist/solution/Checkout/index.d.ts +1 -0
  58. package/dist/solution/Checkout/index.js +286 -188
  59. package/dist/solution/Checkout/utils/index.d.ts +2 -1
  60. package/dist/solution/Checkout/utils/index.js +6 -4
  61. package/dist/solution/RegisterAndLogin/config.js +340 -1
  62. package/dist/solution/Sales/index.d.ts +96 -0
  63. package/dist/solution/Sales/index.js +566 -0
  64. package/dist/solution/Sales/types.d.ts +67 -0
  65. package/dist/solution/Sales/types.js +26 -0
  66. package/dist/solution/ShopDiscount/index.d.ts +1 -0
  67. package/dist/solution/ShopDiscount/index.js +35 -22
  68. package/dist/solution/ShopDiscount/types.d.ts +6 -0
  69. package/dist/solution/ShopDiscount/utils.d.ts +9 -0
  70. package/dist/solution/ShopDiscount/utils.js +21 -27
  71. package/dist/solution/index.d.ts +2 -1
  72. package/dist/solution/index.js +2 -1
  73. package/dist/types/index.d.ts +5 -0
  74. package/lib/core/index.d.ts +3 -2
  75. package/lib/core/index.js +4 -0
  76. package/lib/effects/index.d.ts +2 -2
  77. package/lib/effects/index.js +22 -31
  78. package/lib/model/strategy/adapter/promotion/evaluator.js +57 -8
  79. package/lib/model/strategy/adapter/walletPass/type.d.ts +9 -2
  80. package/lib/model/strategy/adapter/walletPass/utils.d.ts +6 -6
  81. package/lib/model/strategy/adapter/walletPass/utils.js +113 -49
  82. package/lib/modules/Customer/index.js +1 -1
  83. package/lib/modules/Discount/index.d.ts +6 -2
  84. package/lib/modules/Discount/index.js +3 -1
  85. package/lib/modules/Order/index.d.ts +1 -1
  86. package/lib/modules/Order/index.js +20 -18
  87. package/lib/modules/Payment/index.d.ts +4 -0
  88. package/lib/modules/Payment/index.js +134 -66
  89. package/lib/modules/Payment/walletpass.js +23 -4
  90. package/lib/modules/Product/index.d.ts +1 -1
  91. package/lib/modules/Product/types.d.ts +2 -0
  92. package/lib/modules/ProductList/index.d.ts +3 -0
  93. package/lib/modules/ProductList/index.js +2 -2
  94. package/lib/modules/Rules/index.d.ts +2 -2
  95. package/lib/modules/Rules/index.js +69 -73
  96. package/lib/modules/Rules/types.d.ts +2 -2
  97. package/lib/modules/Schedule/index.d.ts +9 -0
  98. package/lib/modules/Schedule/index.js +11 -0
  99. package/lib/plugins/app-types/app/app.d.ts +1 -0
  100. package/lib/plugins/request.d.ts +2 -0
  101. package/lib/server/index.d.ts +107 -2
  102. package/lib/server/index.js +773 -51
  103. package/lib/server/modules/index.d.ts +6 -0
  104. package/lib/server/modules/index.js +16 -2
  105. package/lib/server/modules/menu/index.d.ts +19 -0
  106. package/lib/server/modules/menu/index.js +121 -2
  107. package/lib/server/modules/order/index.d.ts +87 -0
  108. package/lib/server/modules/order/index.js +543 -0
  109. package/lib/server/modules/order/types.d.ts +530 -0
  110. package/lib/server/modules/order/types.js +34 -0
  111. package/lib/server/modules/order/utils/filterBookings.d.ts +6 -0
  112. package/lib/server/modules/order/utils/filterBookings.js +320 -0
  113. package/lib/server/modules/order/utils/filterOrders.d.ts +15 -0
  114. package/lib/server/modules/order/utils/filterOrders.js +197 -0
  115. package/lib/server/modules/products/index.d.ts +117 -5
  116. package/lib/server/modules/products/index.js +799 -62
  117. package/lib/server/modules/products/types.d.ts +25 -1
  118. package/lib/server/modules/products/types.js +1 -0
  119. package/lib/server/modules/resource/index.d.ts +86 -0
  120. package/lib/server/modules/resource/index.js +557 -0
  121. package/lib/server/modules/resource/types.d.ts +121 -0
  122. package/lib/server/modules/resource/types.js +35 -0
  123. package/lib/server/modules/schedule/index.d.ts +19 -0
  124. package/lib/server/modules/schedule/index.js +141 -12
  125. package/lib/server/utils/product.d.ts +5 -0
  126. package/lib/server/utils/product.js +56 -27
  127. package/lib/solution/BookingTicket/index.d.ts +10 -2
  128. package/lib/solution/BookingTicket/index.js +10 -2
  129. package/lib/solution/BookingTicket/utils/scan/index.js +0 -1
  130. package/lib/solution/Checkout/index.d.ts +1 -0
  131. package/lib/solution/Checkout/index.js +399 -331
  132. package/lib/solution/Checkout/utils/index.d.ts +2 -1
  133. package/lib/solution/Checkout/utils/index.js +6 -4
  134. package/lib/solution/RegisterAndLogin/config.js +266 -1
  135. package/lib/solution/Sales/index.d.ts +96 -0
  136. package/lib/solution/Sales/index.js +416 -0
  137. package/lib/solution/Sales/types.d.ts +67 -0
  138. package/lib/solution/Sales/types.js +35 -0
  139. package/lib/solution/ShopDiscount/index.d.ts +1 -0
  140. package/lib/solution/ShopDiscount/index.js +14 -6
  141. package/lib/solution/ShopDiscount/types.d.ts +6 -0
  142. package/lib/solution/ShopDiscount/utils.d.ts +9 -0
  143. package/lib/solution/ShopDiscount/utils.js +6 -10
  144. package/lib/solution/index.d.ts +2 -1
  145. package/lib/solution/index.js +4 -2
  146. package/lib/types/index.d.ts +5 -0
  147. package/package.json +1 -1
@@ -52,6 +52,8 @@ var CheckoutImpl = class extends import_BaseModule.BaseModule {
52
52
  // LoggerManager 实例
53
53
  // 计算缓存(用于性能优化)
54
54
  this.calculationCache = {};
55
+ // 同步订单到后端锁(按订单维度),防止短时间内重复触发导致同一订单多次同步
56
+ this.syncOrderToBackendInFlightByOrderKey = /* @__PURE__ */ new Map();
55
57
  }
56
58
  async initialize(core, options) {
57
59
  this.core = core;
@@ -289,55 +291,62 @@ var CheckoutImpl = class extends import_BaseModule.BaseModule {
289
291
  this.logInfo("initWalletData called", {
290
292
  params
291
293
  });
292
- const amountInfo = (_b = (_a = this.store.currentOrder) == null ? void 0 : _a.order_info) == null ? void 0 : _b.amount_breakdown;
293
- if (!amountInfo) {
294
- this.logInfo("initWalletData amountInfo not found", {
295
- params
294
+ try {
295
+ const amountInfo = (_b = (_a = this.store.currentOrder) == null ? void 0 : _a.order_info) == null ? void 0 : _b.amount_breakdown;
296
+ if (!amountInfo) {
297
+ this.logInfo("initWalletData amountInfo not found", {
298
+ params
299
+ });
300
+ return;
301
+ }
302
+ const walletBusinessData = {
303
+ customer_id: ((_c = this.store.currentCustomer) == null ? void 0 : _c.customer_id) ? Number(this.store.currentCustomer.customer_id) : void 0,
304
+ holder: (_f = (_e = (_d = this.store.currentOrder) == null ? void 0 : _d.order_info) == null ? void 0 : _e.original_order_data) == null ? void 0 : _f.holder,
305
+ amountInfo: {
306
+ totalAmount: amountInfo.totalAmount,
307
+ subTotal: amountInfo.subTotal,
308
+ depositAmount: (_g = this.store.currentOrder) == null ? void 0 : _g.deposit_amount,
309
+ isDeposit: (_h = this.store.currentOrder) == null ? void 0 : _h.is_deposit
310
+ },
311
+ products: this.getProductListByOrder(),
312
+ is_price_include_tax: this.otherParams.is_price_include_tax,
313
+ // core 有
314
+ ...params
315
+ };
316
+ if (this.store.isOrderSynced) {
317
+ walletBusinessData.payment_order_id = (_i = this.store.currentOrder) == null ? void 0 : _i.order_id;
318
+ }
319
+ this.logInfo("开始拉取:initializeWalletDataFromBusinessAsync", {
320
+ walletBusinessData
296
321
  });
297
- return;
298
- }
299
- const walletBusinessData = {
300
- customer_id: ((_c = this.store.currentCustomer) == null ? void 0 : _c.customer_id) ? Number(this.store.currentCustomer.customer_id) : void 0,
301
- holder: (_f = (_e = (_d = this.store.currentOrder) == null ? void 0 : _d.order_info) == null ? void 0 : _e.original_order_data) == null ? void 0 : _f.holder,
302
- amountInfo: {
303
- totalAmount: amountInfo.totalAmount,
304
- subTotal: amountInfo.subTotal,
305
- depositAmount: (_g = this.store.currentOrder) == null ? void 0 : _g.deposit_amount,
306
- isDeposit: (_h = this.store.currentOrder) == null ? void 0 : _h.is_deposit
307
- },
308
- products: this.getProductListByOrder(),
309
- is_price_include_tax: this.otherParams.is_price_include_tax,
310
- // core
311
- ...params
312
- };
313
- if (this.store.isOrderSynced) {
314
- walletBusinessData.payment_order_id = (_i = this.store.currentOrder) == null ? void 0 : _i.order_id;
315
- }
316
- this.logInfo("开始拉取:initializeWalletDataFromBusinessAsync", {
317
- walletBusinessData
318
- });
319
- await this.payment.wallet.initializeWalletDataFromBusinessAsync(
320
- walletBusinessData
321
- );
322
- this.logInfo("调用结束:initializeWalletDataFromBusinessAsync", {
323
- walletBusinessData
324
- });
325
- if (!this.store.currentOrder) {
326
- return;
322
+ await this.payment.wallet.initializeWalletDataFromBusinessAsync(
323
+ walletBusinessData
324
+ );
325
+ this.logInfo("调用结束:initializeWalletDataFromBusinessAsync", {
326
+ walletBusinessData
327
+ });
328
+ if (!this.store.currentOrder) {
329
+ return;
330
+ }
331
+ this.logInfo("initWalletData currentOrder found", {
332
+ currentOrder: this.store.currentOrder
333
+ });
334
+ await this.core.effects.emit(`${this.name}:onWalletDataInitialized`, {
335
+ orderUuid: (_j = this.store.currentOrder) == null ? void 0 : _j.uuid,
336
+ customerId: walletBusinessData.customer_id,
337
+ walletBusinessData: {
338
+ customer_id: walletBusinessData.customer_id,
339
+ amountInfo: walletBusinessData.amountInfo,
340
+ order_wait_pay_amount: params == null ? void 0 : params.order_wait_pay_amount
341
+ },
342
+ timestamp: Date.now()
343
+ });
344
+ } catch (error) {
345
+ this.logError("initWalletData error", {
346
+ error: error instanceof Error ? error.message : String(error)
347
+ });
348
+ throw error;
327
349
  }
328
- this.logInfo("initWalletData currentOrder found", {
329
- currentOrder: this.store.currentOrder
330
- });
331
- await this.core.effects.emit(`${this.name}:onWalletDataInitialized`, {
332
- orderUuid: (_j = this.store.currentOrder) == null ? void 0 : _j.uuid,
333
- customerId: walletBusinessData.customer_id,
334
- walletBusinessData: {
335
- customer_id: walletBusinessData.customer_id,
336
- amountInfo: walletBusinessData.amountInfo,
337
- order_wait_pay_amount: params == null ? void 0 : params.order_wait_pay_amount
338
- },
339
- timestamp: Date.now()
340
- });
341
350
  }
342
351
  checkIsNeedDepositAsync(bookings, relationProducts) {
343
352
  const summaryDeposit = {
@@ -504,7 +513,8 @@ var CheckoutImpl = class extends import_BaseModule.BaseModule {
504
513
  * 会覆盖 order_info 与金额等字段,并重新计算待付金额,最后设置为 currentOrder。
505
514
  */
506
515
  async updateLocalOrderAsync(params) {
507
- var _a, _b, _c, _d, _e, _f, _g, _h, _i, _j, _k, _l, _m, _n, _o;
516
+ var _a, _b, _c, _d, _e, _f, _g, _h, _i, _j, _k, _l, _m, _n, _o, _p, _q;
517
+ this.payment.wallet.clearAllCache();
508
518
  this.logInfo("updateLocalOrderAsync called", {
509
519
  orderId: params.orderId,
510
520
  orderDataType: (_a = params.orderData) == null ? void 0 : _a.type,
@@ -603,7 +613,7 @@ var CheckoutImpl = class extends import_BaseModule.BaseModule {
603
613
  });
604
614
  const syncedPayments = updated2.payment.filter((p) => p.isSynced && p.status !== "voided");
605
615
  const syncedAmount2 = syncedPayments.reduce((sum, p) => {
606
- const amt = new import_decimal.default(p.amount || "0");
616
+ let amt = new import_decimal.default(p.amount || "0");
607
617
  const rounding = new import_decimal.default(Number(p.rounding_amount) > 0 ? 0 : p.rounding_amount || "0").abs();
608
618
  return sum.plus(amt).plus(rounding);
609
619
  }, new import_decimal.default(0));
@@ -620,7 +630,7 @@ var CheckoutImpl = class extends import_BaseModule.BaseModule {
620
630
  }
621
631
  const totalAmount = new import_decimal.default(amountInfo.totalAmount || "0");
622
632
  const activePayments = (existingOrder.payment || []).filter(
623
- (p) => p.status !== "voided"
633
+ (p) => p.status !== "voided" && (!p.voucher_id || p.voucher_id && p.isSynced)
624
634
  );
625
635
  const paidAmount = activePayments.reduce((sum, p) => {
626
636
  const amt = new import_decimal.default(p.amount || "0");
@@ -711,7 +721,7 @@ var CheckoutImpl = class extends import_BaseModule.BaseModule {
711
721
  }
712
722
  });
713
723
  this.store.currentOrder = created;
714
- if (params.existPayment && params.existPayment.length > 0) {
724
+ if (params.existPayment && params.existPayment.length > 0 || ((_p = this.store.currentOrder) == null ? void 0 : _p.order_id) && !(0, import_utils.isVirtualOrderId)((_q = this.store.currentOrder) == null ? void 0 : _q.order_id)) {
715
725
  this.store.isOrderSynced = true;
716
726
  const syncedPayments = created.payment.filter((p) => p.isSynced && p.status !== "voided");
717
727
  const syncedAmount = syncedPayments.reduce((sum, p) => {
@@ -1064,7 +1074,7 @@ var CheckoutImpl = class extends import_BaseModule.BaseModule {
1064
1074
  if (Number(remainingAmount) > 0) {
1065
1075
  this.logInfo("订单金额还有待付的,同步 EFTPOS 支付");
1066
1076
  const isEftposPayment2 = ((_d = paymentItem.type) == null ? void 0 : _d.toLowerCase()) === "eftpos" || ((_e = paymentItem.code) == null ? void 0 : _e.toUpperCase().includes("EFTPOS"));
1067
- const isCashPayment2 = paymentItem.code === "CASHMANUAL";
1077
+ const isCash = (0, import_utils.isCashPayment)(paymentItem);
1068
1078
  const isCustomePayment = paymentItem.type === "custom";
1069
1079
  this.logInfo("EFTPOS 支付检查:", {
1070
1080
  paymentCode: paymentItem.code,
@@ -1072,7 +1082,7 @@ var CheckoutImpl = class extends import_BaseModule.BaseModule {
1072
1082
  isEftposPayment: isEftposPayment2,
1073
1083
  currentOrderSynced: this.store.isOrderSynced
1074
1084
  });
1075
- if (isEftposPayment2 || isCashPayment2 || isCustomePayment) {
1085
+ if (isEftposPayment2 || isCash || isCustomePayment) {
1076
1086
  this.logInfo("检测到 EFTPOS 支付,立即同步订单到后端...");
1077
1087
  try {
1078
1088
  const syncResult = await this.syncOrderToBackendWithReturn(true);
@@ -1194,6 +1204,9 @@ var CheckoutImpl = class extends import_BaseModule.BaseModule {
1194
1204
  */
1195
1205
  async updateVoucherPaymentItemsAsync(voucherPaymentItems) {
1196
1206
  try {
1207
+ this.logInfo("updateVoucherPaymentItemsAsync called:", {
1208
+ voucherPaymentItems
1209
+ });
1197
1210
  if (!this.store.currentOrder) {
1198
1211
  throw (0, import_utils.createCheckoutError)(
1199
1212
  import_types.CheckoutErrorType.ValidationFailed,
@@ -1203,6 +1216,17 @@ var CheckoutImpl = class extends import_BaseModule.BaseModule {
1203
1216
  const paymentItems = await this.payment.getPaymentItemsAsync(
1204
1217
  this.store.currentOrder.uuid
1205
1218
  );
1219
+ if (!voucherPaymentItems || (voucherPaymentItems == null ? void 0 : voucherPaymentItems.length) === 0) {
1220
+ const savedVoucherPaymentItems = (paymentItems == null ? void 0 : paymentItems.filter((item) => item.voucher_id && !item.isSynced)) || [];
1221
+ this.logInfo("未传入 voucher 且本地订单无 voucher 记录,阻断updateVoucherPaymentItemsAsync", {
1222
+ voucherPaymentItems,
1223
+ savedVoucherPaymentItems
1224
+ });
1225
+ if ((savedVoucherPaymentItems == null ? void 0 : savedVoucherPaymentItems.length) === 0) {
1226
+ this.updateStateAmountToRemaining(false);
1227
+ return;
1228
+ }
1229
+ }
1206
1230
  const allPaymentItemsSynced = paymentItems.every((item) => item.isSynced);
1207
1231
  const remainingAmount = await this.calculateRemainingAmountAsync();
1208
1232
  const remainingValue = new import_decimal.default(remainingAmount);
@@ -1272,7 +1296,7 @@ var CheckoutImpl = class extends import_BaseModule.BaseModule {
1272
1296
  });
1273
1297
  this.logInfo("代金券支付项批量更新成功");
1274
1298
  } catch (error) {
1275
- this.logError("[Checkout] 批量更新代金券支付项失败:", error);
1299
+ this.logInfo("[Checkout] 批量更新代金券支付项失败:", { error });
1276
1300
  await this.handleError(error, import_types.CheckoutErrorType.PaymentFailed);
1277
1301
  throw error;
1278
1302
  }
@@ -1805,7 +1829,7 @@ var CheckoutImpl = class extends import_BaseModule.BaseModule {
1805
1829
  * @returns 处理后的支付项(包含找零信息)
1806
1830
  */
1807
1831
  async processCashPaymentItem(paymentItem) {
1808
- const cashPayment = (0, import_utils.isCashPayment)(paymentItem.code, paymentItem.type);
1832
+ const cashPayment = (0, import_utils.isCashPayment)(paymentItem);
1809
1833
  if (!cashPayment) {
1810
1834
  return paymentItem;
1811
1835
  }
@@ -2012,9 +2036,9 @@ var CheckoutImpl = class extends import_BaseModule.BaseModule {
2012
2036
  */
2013
2037
  calculatePaidAmountFromItems(payments) {
2014
2038
  const paidAmount = payments.filter((payment) => payment.status !== "voided").reduce((sum, payment) => {
2015
- const amount = new import_decimal.default(payment.amount || "0");
2016
- const roundingAmount = new import_decimal.default(payment.rounding_amount || "0");
2017
- const effectiveAmount = amount.add(roundingAmount.isNegative() ? roundingAmount.abs() : 0);
2039
+ const paymentAmount = new import_decimal.default(payment.amount || 0);
2040
+ const roundingAmount = new import_decimal.default(payment.rounding_amount || 0);
2041
+ const effectiveAmount = paymentAmount.minus(roundingAmount);
2018
2042
  return sum.add(effectiveAmount);
2019
2043
  }, new import_decimal.default(0));
2020
2044
  return paidAmount.toFixed(2);
@@ -2641,311 +2665,355 @@ var CheckoutImpl = class extends import_BaseModule.BaseModule {
2641
2665
  * @returns 包含订单ID、UUID和完整后端响应的对象
2642
2666
  */
2643
2667
  async syncOrderToBackendWithReturn(isManual = false, customPaymentItems, paymentStatus) {
2644
- var _a, _b, _c, _d, _e, _f, _g, _h;
2668
+ var _a, _b, _c, _d, _e, _f, _g, _h, _i;
2645
2669
  if (!this.store.localOrderData || !this.store.currentOrder) {
2646
2670
  throw new Error("缺少必要的订单数据,无法同步到后端");
2647
2671
  }
2648
- this.logInfo("syncOrderToBackendWithReturn called", { isManual });
2649
- const currentOrderId = this.store.currentOrder.order_id;
2650
- const isUpdateOperation = this.store.isOrderSynced || !(0, import_utils.isVirtualOrderId)(currentOrderId);
2651
- this.logInfo(`开始同步订单到后端...`, {
2652
- currentOrderId,
2653
- isVirtualId: (0, import_utils.isVirtualOrderId)(currentOrderId || ""),
2654
- orderUuid: this.store.currentOrder.uuid,
2655
- isOrderSynced: this.store.isOrderSynced,
2656
- isManual
2657
- });
2658
- const paymentItems = customPaymentItems || await this.payment.getPaymentItemsAsync(this.store.currentOrder.uuid);
2659
- const processedPaymentItems = paymentItems.map((item) => {
2660
- var _a2, _b2;
2661
- return {
2662
- ...item,
2663
- metadata: {
2664
- ...item.metadata,
2665
- rounding_rule: ((_a2 = item.metadata) == null ? void 0 : _a2.rounding_rule) || this.otherParams.order_rounding_setting,
2666
- shop_wallet_pass_id: ((_b2 = item.metadata) == null ? void 0 : _b2.shop_wallet_pass_id) || this.otherParams.shop_wallet_pass_id
2667
- }
2668
- };
2669
- });
2670
- let finalDepositAmount;
2671
- const manualDepositAmount = ((_a = this.store.currentOrder) == null ? void 0 : _a.deposit_amount) || "0.00";
2672
- const manualDepositValue = new import_decimal.default(manualDepositAmount);
2673
- if (manualDepositValue.gt(0)) {
2674
- finalDepositAmount = manualDepositAmount;
2675
- this.logInfo("使用手动设置的定金金额", {
2676
- manualDepositAmount,
2677
- reason: "用户通过setDepositAmountAsync手动设置了定金金额"
2678
- });
2679
- } else {
2680
- finalDepositAmount = "0.00";
2681
- this.logInfo("定金金额为0", {
2682
- manualDepositAmount,
2683
- reason: "手动设置和计算值均为0"
2684
- });
2685
- }
2686
- if (this.store.currentOrder.is_deposit === 0 && processedPaymentItems.every((item) => item.order_payment_type !== "deposit")) {
2687
- finalDepositAmount = "0.00";
2688
- this.store.currentOrder.is_deposit = 0;
2689
- }
2690
- if (this.store.currentOrder.is_deposit === 0 && processedPaymentItems.some((item) => item.order_payment_type === "deposit")) {
2691
- finalDepositAmount = processedPaymentItems.filter((item) => item.status !== "voided" && item.order_payment_type === "deposit").reduce((sum, item) => {
2692
- const amt = new import_decimal.default(item.amount || "0");
2693
- const rounding = new import_decimal.default(item.rounding_amount || "0");
2694
- return sum.plus(amt).sub(rounding);
2695
- }, new import_decimal.default(0)).toFixed(2);
2672
+ const orderLockKey = this.store.currentOrder.uuid || this.store.currentOrder.order_id;
2673
+ if (!orderLockKey) {
2674
+ throw new Error("缺少订单标识,无法同步到后端");
2696
2675
  }
2697
- if (this.store.currentOrder.is_deposit === 1 && new import_decimal.default(finalDepositAmount).gte(new import_decimal.default(this.store.currentOrder.total_amount))) {
2698
- finalDepositAmount = processedPaymentItems.filter((item) => item.status !== "voided" && item.order_payment_type === "deposit").reduce((sum, item) => {
2699
- const amt = new import_decimal.default(item.amount || "0");
2700
- const rounding = new import_decimal.default(item.rounding_amount || "0");
2701
- return sum.plus(amt).sub(rounding);
2702
- }, new import_decimal.default(0)).toFixed(2);
2676
+ const existingInFlight = this.syncOrderToBackendInFlightByOrderKey.get(orderLockKey);
2677
+ if (existingInFlight) {
2678
+ this.logInfo(
2679
+ "syncOrderToBackendWithReturn 触发锁:当前订单正在同步中,将等待当前同步结束",
2680
+ {
2681
+ orderLockKey,
2682
+ orderUuid: this.store.currentOrder.uuid,
2683
+ orderId: this.store.currentOrder.order_id,
2684
+ isManual,
2685
+ hasCustomPaymentItems: !!customPaymentItems
2686
+ }
2687
+ );
2688
+ return await existingInFlight;
2703
2689
  }
2704
- this.logInfo("定金金额确定结果", {
2705
- depositPaymentItemsCount: processedPaymentItems.length,
2706
- depositPaymentItems: processedPaymentItems.map((item) => ({
2707
- uuid: item.uuid,
2708
- code: item.code,
2709
- amount: item.amount,
2710
- rounding_amount: item.rounding_amount,
2711
- order_payment_type: item.order_payment_type,
2712
- status: item.status
2713
- })),
2714
- manualDepositAmount,
2715
- finalDepositAmount,
2716
- isDeposit: ((_b = this.store.currentOrder) == null ? void 0 : _b.is_deposit) || 0
2690
+ let resolveInFlight;
2691
+ let rejectInFlight;
2692
+ const inFlightPromise = new Promise((resolve, reject) => {
2693
+ resolveInFlight = resolve;
2694
+ rejectInFlight = reject;
2717
2695
  });
2718
- let orderPaymentStatus = paymentStatus;
2719
- if (!orderPaymentStatus) {
2720
- const totalPaidAmount = processedPaymentItems.filter((item) => item.status !== "voided").reduce((sum, item) => {
2721
- const amt = new import_decimal.default(item.amount || "0");
2722
- const rounding = new import_decimal.default(item.rounding_amount || "0");
2723
- return sum.plus(amt).sub(rounding);
2724
- }, new import_decimal.default(0));
2725
- if (totalPaidAmount.gte(new import_decimal.default(this.store.currentOrder.total_amount))) {
2726
- orderPaymentStatus = "paid";
2727
- } else if (totalPaidAmount.gt(0)) {
2728
- orderPaymentStatus = "partially_paid";
2729
- } else {
2730
- orderPaymentStatus = "unpaid";
2731
- }
2732
- }
2733
- const orderParams = {
2734
- ...this.store.localOrderData,
2735
- payment_status: orderPaymentStatus,
2736
- type: this.store.localOrderData.type,
2737
- platform: this.store.localOrderData.platform,
2738
- payments: processedPaymentItems,
2739
- // 使用处理过的支付项数据
2740
- customer_id: (_c = this.store.currentCustomer) == null ? void 0 : _c.customer_id,
2741
- // 添加客户ID
2742
- is_price_include_tax: this.otherParams.is_price_include_tax,
2743
- // core 有
2744
- tax_title: this.otherParams.tax_title,
2745
- // core 没有
2746
- tax_rate: this.otherParams.tax_rate,
2747
- // core 有
2748
- tax_country_code: this.otherParams.tax_country_code,
2749
- // core 没有
2750
- currency_code: this.otherParams.currency_code,
2751
- currency_symbol: this.otherParams.currency_symbol,
2752
- currency_format: this.otherParams.currency_format,
2753
- is_deposit: (((_d = this.store.currentOrder) == null ? void 0 : _d.is_deposit) || Number(finalDepositAmount) > 0 ? 1 : 0) || 0,
2754
- deposit_amount: finalDepositAmount,
2755
- // 使用最终确定的定金金额(手动设置优先)
2756
- product_tax_fee: this.store.localOrderData.tax_fee,
2757
- note: this.store.localOrderData.shop_note,
2758
- bookings: (_e = this.store.localOrderData.bookings) == null ? void 0 : _e.map((item) => {
2696
+ this.syncOrderToBackendInFlightByOrderKey.set(orderLockKey, inFlightPromise);
2697
+ try {
2698
+ this.logInfo("syncOrderToBackendWithReturn called", { isManual });
2699
+ const currentOrderId = this.store.currentOrder.order_id;
2700
+ const isUpdateOperation = this.store.isOrderSynced || !(0, import_utils.isVirtualOrderId)(currentOrderId);
2701
+ this.logInfo(`开始同步订单到后端...`, {
2702
+ currentOrderId,
2703
+ isVirtualId: (0, import_utils.isVirtualOrderId)(currentOrderId || ""),
2704
+ orderUuid: this.store.currentOrder.uuid,
2705
+ isOrderSynced: this.store.isOrderSynced,
2706
+ isManual
2707
+ });
2708
+ const paymentItems = customPaymentItems || await this.payment.getPaymentItemsAsync(this.store.currentOrder.uuid);
2709
+ const processedPaymentItems = paymentItems.map((item) => {
2710
+ var _a2, _b2;
2759
2711
  return {
2760
2712
  ...item,
2761
- product: {
2762
- ...item.product,
2763
- custom_deposit_data: void 0
2764
- },
2765
- deposit: void 0
2713
+ metadata: {
2714
+ ...item.metadata,
2715
+ rounding_rule: ((_a2 = item.metadata) == null ? void 0 : _a2.rounding_rule) || this.otherParams.order_rounding_setting,
2716
+ shop_wallet_pass_id: ((_b2 = item.metadata) == null ? void 0 : _b2.shop_wallet_pass_id) || this.otherParams.shop_wallet_pass_id
2717
+ }
2766
2718
  };
2767
- }),
2768
- relation_products: (_f = this.store.localOrderData.relation_products) == null ? void 0 : _f.map(
2769
- (item) => {
2719
+ });
2720
+ let finalDepositAmount;
2721
+ const manualDepositAmount = ((_a = this.store.currentOrder) == null ? void 0 : _a.deposit_amount) || "0.00";
2722
+ const manualDepositValue = new import_decimal.default(manualDepositAmount);
2723
+ if (manualDepositValue.gt(0)) {
2724
+ finalDepositAmount = manualDepositAmount;
2725
+ this.logInfo("使用手动设置的定金金额", {
2726
+ manualDepositAmount,
2727
+ reason: "用户通过setDepositAmountAsync手动设置了定金金额"
2728
+ });
2729
+ } else {
2730
+ finalDepositAmount = "0.00";
2731
+ this.logInfo("定金金额为0", {
2732
+ manualDepositAmount,
2733
+ reason: "手动设置和计算值均为0"
2734
+ });
2735
+ }
2736
+ if (this.store.currentOrder.is_deposit === 0 && processedPaymentItems.every((item) => item.order_payment_type !== "deposit")) {
2737
+ finalDepositAmount = "0.00";
2738
+ this.store.currentOrder.is_deposit = 0;
2739
+ }
2740
+ if (this.store.currentOrder.is_deposit === 0 && processedPaymentItems.some((item) => item.order_payment_type === "deposit")) {
2741
+ finalDepositAmount = processedPaymentItems.filter((item) => item.status !== "voided" && item.order_payment_type === "deposit").reduce((sum, item) => {
2742
+ const amt = new import_decimal.default(item.amount || "0");
2743
+ const rounding = new import_decimal.default(item.rounding_amount || "0");
2744
+ return sum.plus(amt).sub(rounding);
2745
+ }, new import_decimal.default(0)).toFixed(2);
2746
+ }
2747
+ if (this.store.currentOrder.is_deposit === 1 && new import_decimal.default(finalDepositAmount).gte(new import_decimal.default(this.store.currentOrder.total_amount))) {
2748
+ finalDepositAmount = processedPaymentItems.filter((item) => item.status !== "voided" && item.order_payment_type === "deposit").reduce((sum, item) => {
2749
+ const amt = new import_decimal.default(item.amount || "0");
2750
+ const rounding = new import_decimal.default(item.rounding_amount || "0");
2751
+ return sum.plus(amt).sub(rounding);
2752
+ }, new import_decimal.default(0)).toFixed(2);
2753
+ }
2754
+ this.logInfo("定金金额确定结果", {
2755
+ depositPaymentItemsCount: processedPaymentItems.length,
2756
+ depositPaymentItems: processedPaymentItems.map((item) => ({
2757
+ uuid: item.uuid,
2758
+ code: item.code,
2759
+ amount: item.amount,
2760
+ rounding_amount: item.rounding_amount,
2761
+ order_payment_type: item.order_payment_type,
2762
+ status: item.status
2763
+ })),
2764
+ manualDepositAmount,
2765
+ finalDepositAmount,
2766
+ isDeposit: ((_b = this.store.currentOrder) == null ? void 0 : _b.is_deposit) || 0
2767
+ });
2768
+ let orderPaymentStatus = paymentStatus;
2769
+ if (!orderPaymentStatus) {
2770
+ const totalPaidAmount = processedPaymentItems.filter((item) => item.status !== "voided").reduce((sum, item) => {
2771
+ const amt = new import_decimal.default(item.amount || "0");
2772
+ const rounding = new import_decimal.default(item.rounding_amount || "0");
2773
+ return sum.plus(amt).sub(rounding);
2774
+ }, new import_decimal.default(0));
2775
+ if (totalPaidAmount.gte(new import_decimal.default(this.store.currentOrder.total_amount))) {
2776
+ orderPaymentStatus = "paid";
2777
+ } else if (totalPaidAmount.gt(0)) {
2778
+ orderPaymentStatus = "partially_paid";
2779
+ } else {
2780
+ orderPaymentStatus = "unpaid";
2781
+ }
2782
+ }
2783
+ const orderParams = {
2784
+ ...this.store.localOrderData,
2785
+ payment_status: orderPaymentStatus,
2786
+ type: this.store.localOrderData.type,
2787
+ platform: this.store.localOrderData.platform,
2788
+ payments: processedPaymentItems,
2789
+ // 使用处理过的支付项数据
2790
+ customer_id: (_c = this.store.currentCustomer) == null ? void 0 : _c.customer_id,
2791
+ // 添加客户ID
2792
+ is_price_include_tax: this.otherParams.is_price_include_tax,
2793
+ // core 有
2794
+ tax_title: this.otherParams.tax_title,
2795
+ // core 没有
2796
+ tax_rate: this.otherParams.tax_rate,
2797
+ // core 有
2798
+ tax_country_code: this.otherParams.tax_country_code,
2799
+ // core 没有
2800
+ currency_code: this.otherParams.currency_code,
2801
+ currency_symbol: this.otherParams.currency_symbol,
2802
+ currency_format: this.otherParams.currency_format,
2803
+ business_code: ((_d = this.otherParams) == null ? void 0 : _d.business_code) || void 0,
2804
+ is_deposit: (((_e = this.store.currentOrder) == null ? void 0 : _e.is_deposit) || Number(finalDepositAmount) > 0 ? 1 : 0) || 0,
2805
+ deposit_amount: finalDepositAmount,
2806
+ // 使用最终确定的定金金额(手动设置优先)
2807
+ product_tax_fee: this.store.localOrderData.tax_fee,
2808
+ note: this.store.localOrderData.shop_note,
2809
+ bookings: (_f = this.store.localOrderData.bookings) == null ? void 0 : _f.map((item) => {
2770
2810
  return {
2771
2811
  ...item,
2772
- custom_deposit_data: void 0,
2812
+ product: {
2813
+ ...item.product,
2814
+ custom_deposit_data: void 0
2815
+ },
2773
2816
  deposit: void 0
2774
2817
  };
2818
+ }),
2819
+ relation_products: (_g = this.store.localOrderData.relation_products) == null ? void 0 : _g.map(
2820
+ (item) => {
2821
+ return {
2822
+ ...item,
2823
+ custom_deposit_data: void 0,
2824
+ deposit: void 0
2825
+ };
2826
+ }
2827
+ )
2828
+ };
2829
+ if (isUpdateOperation) {
2830
+ if ((0, import_utils.isVirtualOrderId)(currentOrderId)) {
2831
+ this.logWarning(
2832
+ "尝试数据修复:暂时不包含order_id,注意:这次调用将作为创建操作处理,但后续会修复数据一致性"
2833
+ );
2834
+ } else {
2835
+ orderParams.order_id = currentOrderId;
2775
2836
  }
2776
- )
2777
- };
2778
- if (isUpdateOperation) {
2779
- if ((0, import_utils.isVirtualOrderId)(currentOrderId)) {
2780
- this.logWarning(
2781
- "尝试数据修复:暂时不包含order_id,注意:这次调用将作为创建操作处理,但后续会修复数据一致性"
2782
- );
2783
- } else {
2784
- orderParams.order_id = currentOrderId;
2785
2837
  }
2786
- }
2787
- const startTime = Date.now();
2788
- await this.core.effects.emit(`${this.name}:onOrderSubmitStart`, {
2789
- orderUuid: this.store.currentOrder.uuid,
2790
- operation: isUpdateOperation ? "update" : "create",
2791
- isManual,
2792
- paymentItemCount: paymentItems.length,
2793
- timestamp: startTime
2794
- });
2795
- let checkoutResponse;
2796
- let submitSuccess = false;
2797
- let submitError;
2798
- try {
2799
- this.logInfo("Calling backend checkout API", {
2800
- url: "/order/checkout",
2801
- isManual,
2802
- ...orderParams
2803
- });
2804
- checkoutResponse = await this.order.createOrderByCheckout(orderParams);
2805
- submitSuccess = true;
2806
- this.store.currentOrder.payment_status = checkoutResponse.data.payment_status;
2807
- this.logInfo("下单接口调用成功", checkoutResponse);
2808
- } catch (error) {
2809
- submitSuccess = false;
2810
- submitError = error instanceof Error ? error.message : String(error);
2811
- this.logError("下单接口调用失败:", submitError);
2812
- await this.core.effects.emit(`${this.name}:onOrderSyncFailed`, {
2813
- orderUuid: this.store.currentOrder.uuid,
2814
- operation: isUpdateOperation ? "update" : "create",
2815
- isManual,
2816
- error: submitError,
2817
- errorType: "network_error",
2818
- duration: Date.now() - startTime,
2819
- timestamp: Date.now()
2820
- });
2821
- throw error;
2822
- } finally {
2823
- await this.core.effects.emit(`${this.name}:onOrderSubmitEnd`, {
2824
- success: submitSuccess,
2838
+ const startTime = Date.now();
2839
+ await this.core.effects.emit(`${this.name}:onOrderSubmitStart`, {
2825
2840
  orderUuid: this.store.currentOrder.uuid,
2826
2841
  operation: isUpdateOperation ? "update" : "create",
2827
2842
  isManual,
2828
- orderId: submitSuccess ? ((_g = checkoutResponse == null ? void 0 : checkoutResponse.data) == null ? void 0 : _g.order_id) || (checkoutResponse == null ? void 0 : checkoutResponse.order_id) : void 0,
2829
- error: submitError,
2830
- duration: Date.now() - startTime,
2831
- timestamp: Date.now()
2843
+ paymentItemCount: paymentItems.length,
2844
+ timestamp: startTime
2832
2845
  });
2833
- }
2834
- const responseStatus = checkoutResponse == null ? void 0 : checkoutResponse.status;
2835
- const isSuccessResponse = responseStatus === true || responseStatus === 200 || responseStatus === "success" || responseStatus === 1 && (checkoutResponse == null ? void 0 : checkoutResponse.code) === 200;
2836
- if (!isSuccessResponse) {
2837
- const errorMessage = (checkoutResponse == null ? void 0 : checkoutResponse.message) || "订单同步失败,后端返回非成功状态";
2838
- await this.core.effects.emit(`${this.name}:onOrderSyncFailed`, {
2839
- orderUuid: this.store.currentOrder.uuid,
2840
- operation: isUpdateOperation ? "update" : "create",
2841
- isManual,
2842
- error: errorMessage,
2843
- errorType: "api_error",
2844
- response: checkoutResponse,
2845
- duration: Date.now() - startTime,
2846
- timestamp: Date.now()
2847
- });
2848
- throw new Error(errorMessage);
2849
- }
2850
- let realOrderId;
2851
- if (isUpdateOperation) {
2852
- realOrderId = currentOrderId;
2853
- } else {
2854
- let extractedOrderId = (_h = checkoutResponse == null ? void 0 : checkoutResponse.data) == null ? void 0 : _h.order_id;
2855
- if (!extractedOrderId) {
2856
- extractedOrderId = checkoutResponse == null ? void 0 : checkoutResponse.order_id;
2857
- }
2858
- if (extractedOrderId !== void 0 && extractedOrderId !== null) {
2859
- extractedOrderId = String(extractedOrderId);
2846
+ let checkoutResponse;
2847
+ let submitSuccess = false;
2848
+ let submitError;
2849
+ try {
2850
+ this.logInfo("Calling backend checkout API", {
2851
+ url: "/order/checkout",
2852
+ isManual,
2853
+ ...orderParams
2854
+ });
2855
+ checkoutResponse = await this.order.createOrderByCheckout(orderParams);
2856
+ submitSuccess = true;
2857
+ this.store.currentOrder.payment_status = checkoutResponse.data.payment_status;
2858
+ this.logInfo("下单接口调用成功", checkoutResponse);
2859
+ } catch (error) {
2860
+ submitSuccess = false;
2861
+ submitError = error instanceof Error ? error.message : String(error);
2862
+ this.logError("下单接口调用失败:", submitError);
2863
+ await this.core.effects.emit(`${this.name}:onOrderSyncFailed`, {
2864
+ orderUuid: this.store.currentOrder.uuid,
2865
+ operation: isUpdateOperation ? "update" : "create",
2866
+ isManual,
2867
+ error: submitError,
2868
+ errorType: "network_error",
2869
+ duration: Date.now() - startTime,
2870
+ timestamp: Date.now()
2871
+ });
2872
+ throw error;
2873
+ } finally {
2874
+ await this.core.effects.emit(`${this.name}:onOrderSubmitEnd`, {
2875
+ success: submitSuccess,
2876
+ orderUuid: this.store.currentOrder.uuid,
2877
+ operation: isUpdateOperation ? "update" : "create",
2878
+ isManual,
2879
+ orderId: submitSuccess ? ((_h = checkoutResponse == null ? void 0 : checkoutResponse.data) == null ? void 0 : _h.order_id) || (checkoutResponse == null ? void 0 : checkoutResponse.order_id) : void 0,
2880
+ error: submitError,
2881
+ duration: Date.now() - startTime,
2882
+ timestamp: Date.now()
2883
+ });
2860
2884
  }
2861
- if (!extractedOrderId) {
2862
- this.logError("后端返回的订单信息中未包含订单ID");
2885
+ const responseStatus = checkoutResponse == null ? void 0 : checkoutResponse.status;
2886
+ const isSuccessResponse = responseStatus === true || responseStatus === 200 || responseStatus === "success" || responseStatus === 1 && (checkoutResponse == null ? void 0 : checkoutResponse.code) === 200;
2887
+ if (!isSuccessResponse) {
2888
+ const errorMessage = (checkoutResponse == null ? void 0 : checkoutResponse.message) || "订单同步失败,后端返回非成功状态";
2889
+ await this.core.effects.emit(`${this.name}:onOrderSyncFailed`, {
2890
+ orderUuid: this.store.currentOrder.uuid,
2891
+ operation: isUpdateOperation ? "update" : "create",
2892
+ isManual,
2893
+ error: errorMessage,
2894
+ errorType: "api_error",
2895
+ response: checkoutResponse,
2896
+ duration: Date.now() - startTime,
2897
+ timestamp: Date.now()
2898
+ });
2899
+ throw new Error(errorMessage);
2863
2900
  }
2864
- realOrderId = extractedOrderId;
2865
- this.logInfo("准备替换订单ID:", {
2866
- orderUuid: this.store.currentOrder.uuid,
2867
- oldOrderId: this.store.currentOrder.order_id,
2868
- newOrderId: realOrderId
2869
- });
2870
- try {
2871
- const latestPaymentStatus = this.store.currentOrder.payment_status;
2872
- const previousOrder = this.store.currentOrder;
2873
- const updatedOrder = await this.payment.replaceOrderIdByUuidAsync(
2874
- previousOrder.uuid,
2875
- realOrderId
2876
- );
2877
- this.logInfo("Payment模块替换订单ID结果:", {
2878
- wasSuccessful: !!updatedOrder,
2879
- returnedOrderId: updatedOrder == null ? void 0 : updatedOrder.order_id,
2880
- expectedOrderId: realOrderId
2901
+ let realOrderId;
2902
+ if (isUpdateOperation) {
2903
+ realOrderId = currentOrderId;
2904
+ } else {
2905
+ let extractedOrderId = (_i = checkoutResponse == null ? void 0 : checkoutResponse.data) == null ? void 0 : _i.order_id;
2906
+ if (!extractedOrderId) {
2907
+ extractedOrderId = checkoutResponse == null ? void 0 : checkoutResponse.order_id;
2908
+ }
2909
+ if (extractedOrderId !== void 0 && extractedOrderId !== null) {
2910
+ extractedOrderId = String(extractedOrderId);
2911
+ }
2912
+ if (!extractedOrderId) {
2913
+ this.logError("后端返回的订单信息中未包含订单ID");
2914
+ }
2915
+ realOrderId = extractedOrderId;
2916
+ this.logInfo("准备替换订单ID:", {
2917
+ orderUuid: this.store.currentOrder.uuid,
2918
+ oldOrderId: this.store.currentOrder.order_id,
2919
+ newOrderId: realOrderId
2881
2920
  });
2882
- if (updatedOrder) {
2883
- this.logInfo("Payment模块返回的更新后订单:", {
2884
- uuid: updatedOrder.uuid,
2885
- orderId: updatedOrder.order_id,
2886
- totalAmount: updatedOrder.total_amount
2921
+ try {
2922
+ const latestPaymentStatus = this.store.currentOrder.payment_status;
2923
+ const previousOrder = this.store.currentOrder;
2924
+ const updatedOrder = await this.payment.replaceOrderIdByUuidAsync(
2925
+ previousOrder.uuid,
2926
+ realOrderId
2927
+ );
2928
+ this.logInfo("Payment模块替换订单ID结果:", {
2929
+ wasSuccessful: !!updatedOrder,
2930
+ returnedOrderId: updatedOrder == null ? void 0 : updatedOrder.order_id,
2931
+ expectedOrderId: realOrderId
2887
2932
  });
2888
- this.store.currentOrder = {
2889
- ...previousOrder,
2890
- ...updatedOrder,
2891
- payment_status: latestPaymentStatus
2892
- };
2893
- if (latestPaymentStatus)
2894
- await this.payment.updateOrderAsync(previousOrder.uuid, {
2933
+ if (updatedOrder) {
2934
+ this.logInfo("Payment模块返回的更新后订单:", {
2935
+ uuid: updatedOrder.uuid,
2936
+ orderId: updatedOrder.order_id,
2937
+ totalAmount: updatedOrder.total_amount
2938
+ });
2939
+ this.store.currentOrder = {
2940
+ ...previousOrder,
2941
+ ...updatedOrder,
2895
2942
  payment_status: latestPaymentStatus
2943
+ };
2944
+ if (latestPaymentStatus)
2945
+ await this.payment.updateOrderAsync(previousOrder.uuid, {
2946
+ payment_status: latestPaymentStatus
2947
+ });
2948
+ this.logInfo(
2949
+ "[Checkout] 订单ID替换成功,当前订单ID:",
2950
+ this.store.currentOrder.order_id
2951
+ );
2952
+ } else {
2953
+ this.logError(
2954
+ "[Checkout] Payment模块返回空订单,订单ID替换失败,开始手动替换"
2955
+ );
2956
+ const beforeManualUpdate = this.store.currentOrder.order_id;
2957
+ this.store.currentOrder.order_id = realOrderId;
2958
+ this.logInfo("手动设置订单ID:", {
2959
+ beforeReplacement: beforeManualUpdate,
2960
+ afterReplacement: this.store.currentOrder.order_id,
2961
+ 目标ID: realOrderId
2896
2962
  });
2897
- this.logInfo(
2898
- "[Checkout] 订单ID替换成功,当前订单ID:",
2899
- this.store.currentOrder.order_id
2900
- );
2901
- } else {
2902
- this.logError(
2903
- "[Checkout] Payment模块返回空订单,订单ID替换失败,开始手动替换"
2904
- );
2905
- const beforeManualUpdate = this.store.currentOrder.order_id;
2963
+ }
2964
+ } catch (error) {
2965
+ this.logError("调用Payment模块替换订单ID时发生错误:", error);
2906
2966
  this.store.currentOrder.order_id = realOrderId;
2907
- this.logInfo("手动设置订单ID:", {
2908
- beforeReplacement: beforeManualUpdate,
2909
- afterReplacement: this.store.currentOrder.order_id,
2910
- 目标ID: realOrderId
2911
- });
2967
+ this.logInfo("错误恢复:手动设置订单ID:", realOrderId);
2912
2968
  }
2913
- } catch (error) {
2914
- this.logError("调用Payment模块替换订单ID时发生错误:", error);
2915
- this.store.currentOrder.order_id = realOrderId;
2916
- this.logInfo("错误恢复:手动设置订单ID:", realOrderId);
2917
2969
  }
2918
- }
2919
- try {
2920
- const syncedPaymentUuids = processedPaymentItems.filter((item) => item.status !== "voided").map((item) => item.uuid).filter(Boolean);
2921
- for (const paymentUuid of syncedPaymentUuids) {
2922
- await this.payment.updatePaymentAsync(
2923
- this.store.currentOrder.uuid,
2924
- paymentUuid,
2925
- { isSynced: true, syncError: void 0 }
2926
- );
2970
+ try {
2971
+ const syncedPaymentUuids = processedPaymentItems.filter((item) => item.status !== "voided").map((item) => item.uuid).filter(Boolean);
2972
+ for (const paymentUuid of syncedPaymentUuids) {
2973
+ await this.payment.updatePaymentAsync(
2974
+ this.store.currentOrder.uuid,
2975
+ paymentUuid,
2976
+ { isSynced: true, syncError: void 0 }
2977
+ );
2978
+ }
2979
+ } catch (e) {
2980
+ this.logWarning("标记支付项已同步失败(不阻塞主流程)", { error: e });
2981
+ }
2982
+ this.store.isOrderSynced = true;
2983
+ this.clearCalculationCache();
2984
+ await this.core.effects.emit(`${this.name}:onOrderSynced`, {
2985
+ orderUuid: this.store.currentOrder.uuid,
2986
+ realOrderId,
2987
+ virtualOrderId: this.store.currentOrder.order_id,
2988
+ timestamp: Date.now(),
2989
+ isManual,
2990
+ response: checkoutResponse
2991
+ });
2992
+ const result = {
2993
+ success: true,
2994
+ orderId: realOrderId,
2995
+ orderUuid: this.store.currentOrder.uuid,
2996
+ response: checkoutResponse
2997
+ };
2998
+ resolveInFlight(result);
2999
+ return result;
3000
+ } catch (error) {
3001
+ rejectInFlight(error);
3002
+ throw error;
3003
+ } finally {
3004
+ const currentInFlight = this.syncOrderToBackendInFlightByOrderKey.get(orderLockKey);
3005
+ if (currentInFlight === inFlightPromise) {
3006
+ this.syncOrderToBackendInFlightByOrderKey.delete(orderLockKey);
2927
3007
  }
2928
- } catch (e) {
2929
- this.logWarning("标记支付项已同步失败(不阻塞主流程)", { error: e });
2930
3008
  }
2931
- this.store.isOrderSynced = true;
2932
- this.clearCalculationCache();
2933
- await this.core.effects.emit(`${this.name}:onOrderSynced`, {
2934
- orderUuid: this.store.currentOrder.uuid,
2935
- realOrderId,
2936
- virtualOrderId: this.store.currentOrder.order_id,
2937
- timestamp: Date.now(),
2938
- isManual,
2939
- response: checkoutResponse
2940
- });
2941
- return {
2942
- success: true,
2943
- orderId: realOrderId,
2944
- orderUuid: this.store.currentOrder.uuid,
2945
- response: checkoutResponse
2946
- };
2947
3009
  }
2948
3010
  async setOtherParams(params, { cover = false } = {}) {
3011
+ this.logInfo("setOtherParams called", {
3012
+ metadata: {
3013
+ params,
3014
+ cover
3015
+ }
3016
+ });
2949
3017
  if (cover) {
2950
3018
  this.otherParams = params;
2951
3019
  } else {