@pisell/pisellos 0.0.237 → 0.0.238

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.
@@ -30,3 +30,4 @@ export declare class DiscountModule extends BaseModule implements Module, Discou
30
30
  clear(): Promise<void>;
31
31
  storeChange(): void;
32
32
  }
33
+ export type { Discount } from './types';
@@ -26,6 +26,7 @@ function _toPrimitive(t, r) { if ("object" != _typeof(t) || !t) return t; var e
26
26
  import { BaseModule } from "../BaseModule";
27
27
  import { generateDuration, mergeRelationForms } from "./utils";
28
28
  import { isNormalProduct } from "../Product/utils";
29
+ import dayjs from 'dayjs';
29
30
  export var OrderModule = /*#__PURE__*/function (_BaseModule) {
30
31
  _inherits(OrderModule, _BaseModule);
31
32
  var _super = _createSuper(OrderModule);
@@ -205,6 +206,11 @@ export var OrderModule = /*#__PURE__*/function (_BaseModule) {
205
206
  orderData.bookings = [];
206
207
  orderData.relation_products = [];
207
208
  }
209
+
210
+ // 兜底策略,如果是第一次下单,没有 order_id,且没有 schedule_date,则设置为当前时间
211
+ if (!params.order_id && !orderData.schedule_date) {
212
+ orderData.schedule_date = dayjs().format('YYYY-MM-DD HH:mm:ss');
213
+ }
208
214
  console.log('[Order] 调用后端接口创建订单:', {
209
215
  url: '/order/checkout',
210
216
  orderType: orderData.type,
@@ -220,25 +226,25 @@ export var OrderModule = /*#__PURE__*/function (_BaseModule) {
220
226
  });
221
227
 
222
228
  // 调用后端接口
223
- _context3.next = 8;
229
+ _context3.next = 9;
224
230
  return this.request.post('/order/checkout', orderData);
225
- case 8:
231
+ case 9:
226
232
  response = _context3.sent;
227
233
  console.log('[Order] 订单创建成功,后端响应:', {
228
234
  success: !!response,
229
235
  hasOrderId: !!(response !== null && response !== void 0 && (_response$data = response.data) !== null && _response$data !== void 0 && _response$data.order_id || response !== null && response !== void 0 && response.order_id)
230
236
  });
231
237
  return _context3.abrupt("return", response);
232
- case 13:
233
- _context3.prev = 13;
238
+ case 14:
239
+ _context3.prev = 14;
234
240
  _context3.t0 = _context3["catch"](1);
235
241
  console.error('[Order] createOrderByCheckout 创建订单失败:', _context3.t0);
236
242
  throw _context3.t0;
237
- case 17:
243
+ case 18:
238
244
  case "end":
239
245
  return _context3.stop();
240
246
  }
241
- }, _callee3, this, [[1, 13]]);
247
+ }, _callee3, this, [[1, 14]]);
242
248
  }));
243
249
  function createOrderByCheckout(_x4) {
244
250
  return _createOrderByCheckout.apply(this, arguments);
@@ -1641,7 +1641,7 @@ export var CheckoutImpl = /*#__PURE__*/function (_BaseModule) {
1641
1641
  key: "addPaymentItemAsync",
1642
1642
  value: (function () {
1643
1643
  var _addPaymentItemAsync = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee25(paymentItem) {
1644
- var currentOrderId, isCurrentOrderReal;
1644
+ var _paymentItem$type, _paymentItem$code, isEftposPayment;
1645
1645
  return _regeneratorRuntime().wrap(function _callee25$(_context25) {
1646
1646
  while (1) switch (_context25.prev = _context25.next) {
1647
1647
  case 0:
@@ -1669,89 +1669,63 @@ export var CheckoutImpl = /*#__PURE__*/function (_BaseModule) {
1669
1669
  case 6:
1670
1670
  console.log('[Checkout] 支付项添加成功');
1671
1671
 
1672
- // 重新从Payment模块获取最新的订单数据,确保支付项同步
1673
- currentOrderId = this.store.currentOrder.order_id; // 保存当前的订单ID
1674
- isCurrentOrderReal = currentOrderId && !this.isVirtualOrderId(currentOrderId);
1675
- console.log('[Checkout] 准备同步订单数据:', {
1676
- 当前订单ID: currentOrderId,
1677
- 是否为真实ID: isCurrentOrderReal
1672
+ // 检查是否是 EFTPOS 支付,如果是则立即同步订单
1673
+ isEftposPayment = ((_paymentItem$type = paymentItem.type) === null || _paymentItem$type === void 0 ? void 0 : _paymentItem$type.toLowerCase()) === 'eftpos' || ((_paymentItem$code = paymentItem.code) === null || _paymentItem$code === void 0 ? void 0 : _paymentItem$code.toUpperCase().includes('EFTPOS'));
1674
+ console.log('[Checkout] EFTPOS 支付检查:', {
1675
+ paymentCode: paymentItem.code,
1676
+ paymentType: paymentItem.type,
1677
+ isEftposPayment: isEftposPayment,
1678
+ currentOrderSynced: this.store.isOrderSynced
1678
1679
  });
1679
-
1680
- // const updatedOrder = await this.payment.getPaymentOrderByUuidAsync(this.store.currentOrder.uuid);
1681
- // if (updatedOrder) {
1682
- // // 如果当前订单ID是真实ID,但获取到的订单ID是虚拟ID,需要保护真实ID
1683
- // if (isCurrentOrderReal && this.isVirtualOrderId(updatedOrder.order_id)) {
1684
- // console.warn('[Checkout] 检测到订单ID回退,保护真实订单ID:', {
1685
- // 当前真实ID: currentOrderId,
1686
- // 获取到的虚拟ID: updatedOrder.order_id
1687
- // });
1688
- // updatedOrder.order_id = currentOrderId; // 恢复真实的订单ID
1689
- // }
1690
-
1691
- // this.store.currentOrder = updatedOrder;
1692
- // console.log('[Checkout] 订单数据已同步:', {
1693
- // 支付项数量: updatedOrder.payment.length,
1694
- // 最终订单ID: updatedOrder.order_id,
1695
- // 是否为虚拟ID: this.isVirtualOrderId(updatedOrder.order_id)
1696
- // });
1697
- // }
1698
-
1699
- // // 检查是否需要同步订单到后端
1700
- // const needsOrderSync = this.shouldSyncOrderForPayment(paymentItem.code, paymentItem.type);
1701
-
1702
- // console.log('[Checkout] 支付项同步检查:', {
1703
- // paymentCode: paymentItem.code,
1704
- // paymentType: paymentItem.type,
1705
- // paymentAmount: paymentItem.amount,
1706
- // expectedCashCode: PaymentMethodType.Cash,
1707
- // needsOrderSync,
1708
- // isOrderSynced: this.store.isOrderSynced
1709
- // });
1710
-
1711
- // if (needsOrderSync) {
1712
- // console.log('[Checkout] 检测到非现金/自定义支付,开始同步订单到后端...', {
1713
- // 同步前订单ID: this.store.currentOrder.order_id,
1714
- // 是否为虚拟ID: this.isVirtualOrderId(this.store.currentOrder.order_id),
1715
- // 订单同步状态: this.store.isOrderSynced,
1716
- // 订单UUID: this.store.currentOrder.uuid
1717
- // });
1718
-
1719
- // await this.syncOrderToBackend();
1720
-
1721
- // console.log('[Checkout] addPaymentItemAsync 中的同步完成:', {
1722
- // 同步后订单ID: this.store.currentOrder.order_id,
1723
- // 是否为虚拟ID: this.isVirtualOrderId(this.store.currentOrder.order_id),
1724
- // 订单同步状态: this.store.isOrderSynced
1725
- // });
1726
- // }
1727
-
1728
- // 支付项添加后,更新 stateAmount 为剩余未支付金额
1729
- _context25.next = 12;
1730
- return this.updateStateAmountToRemaining();
1731
- case 12:
1680
+ if (!isEftposPayment) {
1681
+ _context25.next = 22;
1682
+ break;
1683
+ }
1684
+ console.log('[Checkout] 检测到 EFTPOS 支付,立即同步订单到后端...');
1685
+ _context25.prev = 11;
1732
1686
  _context25.next = 14;
1687
+ return this.syncOrderToBackendWithReturn(true);
1688
+ case 14:
1689
+ console.log('[Checkout] EFTPOS 支付后订单同步完成 (已标记为手动同步):', {
1690
+ 订单ID: this.store.currentOrder.order_id,
1691
+ 是否已同步: this.store.isOrderSynced
1692
+ });
1693
+ _context25.next = 22;
1694
+ break;
1695
+ case 17:
1696
+ _context25.prev = 17;
1697
+ _context25.t0 = _context25["catch"](11);
1698
+ console.error('[Checkout] EFTPOS 支付后订单同步失败:', _context25.t0);
1699
+ // 不抛出错误,避免影响支付流程,但记录错误
1700
+ _context25.next = 22;
1701
+ return this.handleError(new Error("EFTPOS \u652F\u4ED8\u540E\u8BA2\u5355\u540C\u6B65\u5931\u8D25: ".concat(_context25.t0 instanceof Error ? _context25.t0.message : String(_context25.t0))), CheckoutErrorType.OrderCreationFailed);
1702
+ case 22:
1703
+ _context25.next = 24;
1704
+ return this.updateStateAmountToRemaining();
1705
+ case 24:
1706
+ _context25.next = 26;
1733
1707
  return this.core.effects.emit(CheckoutHooks.OnPaymentStarted, {
1734
1708
  orderUuid: this.store.currentOrder.uuid,
1735
1709
  paymentMethodCode: paymentItem.code,
1736
1710
  amount: String(paymentItem.amount),
1737
1711
  timestamp: Date.now()
1738
1712
  });
1739
- case 14:
1740
- _context25.next = 22;
1713
+ case 26:
1714
+ _context25.next = 34;
1741
1715
  break;
1742
- case 16:
1743
- _context25.prev = 16;
1744
- _context25.t0 = _context25["catch"](0);
1745
- console.error('[Checkout] 添加支付项失败:', _context25.t0);
1746
- _context25.next = 21;
1747
- return this.handleError(_context25.t0, CheckoutErrorType.PaymentFailed);
1748
- case 21:
1749
- throw _context25.t0;
1750
- case 22:
1716
+ case 28:
1717
+ _context25.prev = 28;
1718
+ _context25.t1 = _context25["catch"](0);
1719
+ console.error('[Checkout] 添加支付项失败:', _context25.t1);
1720
+ _context25.next = 33;
1721
+ return this.handleError(_context25.t1, CheckoutErrorType.PaymentFailed);
1722
+ case 33:
1723
+ throw _context25.t1;
1724
+ case 34:
1751
1725
  case "end":
1752
1726
  return _context25.stop();
1753
1727
  }
1754
- }, _callee25, this, [[0, 16]]);
1728
+ }, _callee25, this, [[0, 28], [11, 17]]);
1755
1729
  }));
1756
1730
  function addPaymentItemAsync(_x16) {
1757
1731
  return _addPaymentItemAsync.apply(this, arguments);
@@ -2569,10 +2543,10 @@ export var CheckoutImpl = /*#__PURE__*/function (_BaseModule) {
2569
2543
  allPaymentItems = _context33.sent;
2570
2544
  // 过滤掉带有 voucher_id 的支付项(代金券类支付项)
2571
2545
  filteredPaymentItems = allPaymentItems.filter(function (payment) {
2572
- return !payment.voucher_id || payment.voucher_id.trim() === '';
2546
+ return !payment.voucher_id;
2573
2547
  }); // 记录过滤结果
2574
2548
  voucherItems = allPaymentItems.filter(function (payment) {
2575
- return payment.voucher_id && payment.voucher_id.trim() !== '';
2549
+ return payment.voucher_id;
2576
2550
  });
2577
2551
  console.log('[Checkout] 支付项过滤结果:', {
2578
2552
  总支付项: allPaymentItems.length,
@@ -3436,7 +3410,7 @@ export var CheckoutImpl = /*#__PURE__*/function (_BaseModule) {
3436
3410
  key: "checkOrderPaymentCompletion",
3437
3411
  value: (function () {
3438
3412
  var _checkOrderPaymentCompletion = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee47() {
3439
- var remainingAmount, remainingValue, totalAmount, paidAmount, currentPayments, allPaymentsHaveVoucherId;
3413
+ var remainingAmount, remainingValue, totalAmount, paidAmount, currentPayments, hasPaymentItems, allPaymentsHaveVoucherId, shouldAutoSync;
3440
3414
  return _regeneratorRuntime().wrap(function _callee47$(_context47) {
3441
3415
  while (1) switch (_context47.prev = _context47.next) {
3442
3416
  case 0:
@@ -3453,7 +3427,7 @@ export var CheckoutImpl = /*#__PURE__*/function (_BaseModule) {
3453
3427
  remainingAmount = _context47.sent;
3454
3428
  remainingValue = parseFloat(remainingAmount); // 当剩余金额小于等于0时,认为订单已完成支付
3455
3429
  if (!(remainingValue <= 0)) {
3456
- _context47.next = 27;
3430
+ _context47.next = 29;
3457
3431
  break;
3458
3432
  }
3459
3433
  totalAmount = this.store.currentOrder.total_amount;
@@ -3470,18 +3444,32 @@ export var CheckoutImpl = /*#__PURE__*/function (_BaseModule) {
3470
3444
  isOrderSynced: this.store.isOrderSynced
3471
3445
  });
3472
3446
 
3473
- // 从 Payment 模块获取最新支付项,检查是否所有支付项都带有 voucher_id
3447
+ // 从 Payment 模块获取最新支付项,检查自动同步条件
3474
3448
  _context47.next = 15;
3475
3449
  return this.payment.getPaymentItemsAsync(this.store.currentOrder.uuid, false // 不包括已撤销的支付项
3476
3450
  );
3477
3451
  case 15:
3478
3452
  currentPayments = _context47.sent;
3479
- allPaymentsHaveVoucherId = currentPayments.length > 0 && currentPayments.every(function (item) {
3453
+ // 检查自动同步条件:
3454
+ // 1. 待付金额为 0 ✓ (已在上面检查)
3455
+ // 2. 需要有支付项
3456
+ // 3. 支付项不能全是带 voucher_id 的数据
3457
+ hasPaymentItems = currentPayments.length > 0;
3458
+ allPaymentsHaveVoucherId = hasPaymentItems && currentPayments.every(function (item) {
3480
3459
  return item.status !== 'voided' && item.voucher_id;
3481
- });
3482
- console.log('[Checkout] 支付项voucher_id检查:', {
3460
+ }); // 判断是否需要自动同步订单
3461
+ // 条件:
3462
+ // 1. 有支付项
3463
+ // 2. 不全是代金券支付
3464
+ // 3. 订单未同步 OR (订单已同步但支付刚完成,需要同步最终支付状态)
3465
+ shouldAutoSync = hasPaymentItems && !allPaymentsHaveVoucherId;
3466
+ console.log('[Checkout] 自动同步订单条件检查:', {
3483
3467
  paymentCount: currentPayments.length,
3468
+ hasPaymentItems: hasPaymentItems,
3484
3469
  allHaveVoucherId: allPaymentsHaveVoucherId,
3470
+ isOrderSynced: this.store.isOrderSynced,
3471
+ shouldAutoSync: shouldAutoSync,
3472
+ reason: shouldAutoSync ? '支付完成,需要同步最终支付状态' : '跳过同步',
3485
3473
  paymentItems: currentPayments.map(function (p) {
3486
3474
  return {
3487
3475
  uuid: p.uuid,
@@ -3492,29 +3480,24 @@ export var CheckoutImpl = /*#__PURE__*/function (_BaseModule) {
3492
3480
  };
3493
3481
  })
3494
3482
  });
3495
-
3496
- // // 如果支付完成但订单未同步,需要判断是否同步
3497
- // if (!this.store.isOrderSynced) {
3498
- // if (allPaymentsHaveVoucherId) {
3499
- // console.log('[Checkout] 所有支付项均为代金券类型,跳过订单同步');
3500
- // } else {
3501
- // console.log('[Checkout] 支付完成但订单未同步,开始同步订单到后端...');
3502
- // await this.syncOrderToBackend();
3503
- // }
3504
- // }
3505
- if (!allPaymentsHaveVoucherId) {
3506
- _context47.next = 22;
3483
+ if (!shouldAutoSync) {
3484
+ _context47.next = 26;
3507
3485
  break;
3508
3486
  }
3509
- console.log('[Checkout] 所有支付项均为代金券类型,跳过订单同步');
3510
- _context47.next = 25;
3511
- break;
3512
- case 22:
3513
- console.log('[Checkout] 支付完成但订单未同步,开始同步订单到后端...');
3514
- _context47.next = 25;
3487
+ console.log('[Checkout] 满足自动同步条件,开始同步订单到后端...');
3488
+ _context47.next = 24;
3515
3489
  return this.syncOrderToBackend();
3516
- case 25:
3490
+ case 24:
3517
3491
  _context47.next = 27;
3492
+ break;
3493
+ case 26:
3494
+ if (!hasPaymentItems) {
3495
+ console.log('[Checkout] 没有支付项,跳过订单同步');
3496
+ } else if (allPaymentsHaveVoucherId) {
3497
+ console.log('[Checkout] 所有支付项均为代金券类型,跳过订单同步');
3498
+ }
3499
+ case 27:
3500
+ _context47.next = 29;
3518
3501
  return this.core.effects.emit(CheckoutHooks.OnOrderPaymentCompleted, {
3519
3502
  orderUuid: this.store.currentOrder.uuid,
3520
3503
  orderId: this.store.currentOrder.order_id,
@@ -3523,18 +3506,18 @@ export var CheckoutImpl = /*#__PURE__*/function (_BaseModule) {
3523
3506
  remainingAmount: remainingAmount,
3524
3507
  timestamp: Date.now()
3525
3508
  });
3526
- case 27:
3527
- _context47.next = 32;
3528
- break;
3529
3509
  case 29:
3530
- _context47.prev = 29;
3510
+ _context47.next = 34;
3511
+ break;
3512
+ case 31:
3513
+ _context47.prev = 31;
3531
3514
  _context47.t0 = _context47["catch"](0);
3532
3515
  console.error('[Checkout] 检查订单支付完成状态失败:', _context47.t0);
3533
- case 32:
3516
+ case 34:
3534
3517
  case "end":
3535
3518
  return _context47.stop();
3536
3519
  }
3537
- }, _callee47, this, [[0, 29]]);
3520
+ }, _callee47, this, [[0, 31]]);
3538
3521
  }));
3539
3522
  function checkOrderPaymentCompletion() {
3540
3523
  return _checkOrderPaymentCompletion.apply(this, arguments);
@@ -3607,7 +3590,7 @@ export var CheckoutImpl = /*#__PURE__*/function (_BaseModule) {
3607
3590
  key: "syncOrderToBackendWithReturn",
3608
3591
  value: (function () {
3609
3592
  var _syncOrderToBackendWithReturn = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee48() {
3610
- var _this$store$currentCu2, _this$store$currentOr18, _this$store$currentOr19;
3593
+ var _this$store$currentCu2, _this$store$currentOr18, _this$store$currentOr19, _checkoutResponse3, _checkoutResponse4, _checkoutResponse5, _checkoutResponse6;
3611
3594
  var isManual,
3612
3595
  customPaymentItems,
3613
3596
  syncType,
@@ -3618,10 +3601,16 @@ export var CheckoutImpl = /*#__PURE__*/function (_BaseModule) {
3618
3601
  operation,
3619
3602
  paymentItems,
3620
3603
  orderParams,
3604
+ startTime,
3621
3605
  checkoutResponse,
3606
+ submitSuccess,
3607
+ submitError,
3608
+ _checkoutResponse,
3609
+ _checkoutResponse2,
3622
3610
  realOrderId,
3623
- _data3,
3611
+ _checkoutResponse7,
3624
3612
  extractedOrderId,
3613
+ _checkoutResponse8,
3625
3614
  updatedOrder,
3626
3615
  beforeManualUpdate,
3627
3616
  finalOrderId,
@@ -3755,31 +3744,70 @@ export var CheckoutImpl = /*#__PURE__*/function (_BaseModule) {
3755
3744
  console.log('[Checkout] 创建新订单操作');
3756
3745
  }
3757
3746
 
3758
- // 调用 Order 模块的专用 createOrderByCheckout 方法
3759
- _context48.next = 24;
3747
+ // 发送下单接口请求开始事件
3748
+ startTime = Date.now();
3749
+ _context48.next = 25;
3750
+ return this.core.effects.emit(CheckoutHooks.OnOrderSubmitStart, {
3751
+ orderUuid: this.store.currentOrder.uuid,
3752
+ operation: isUpdateOperation ? 'update' : 'create',
3753
+ isManual: isManual,
3754
+ paymentItemCount: paymentItems.length,
3755
+ timestamp: startTime
3756
+ });
3757
+ case 25:
3758
+ submitSuccess = false;
3759
+ _context48.prev = 26;
3760
+ _context48.next = 29;
3760
3761
  return this.order.createOrderByCheckout(orderParams);
3761
- case 24:
3762
+ case 29:
3762
3763
  checkoutResponse = _context48.sent;
3764
+ submitSuccess = true;
3765
+ console.log('[Checkout] 下单接口调用成功');
3766
+ _context48.next = 40;
3767
+ break;
3768
+ case 34:
3769
+ _context48.prev = 34;
3770
+ _context48.t1 = _context48["catch"](26);
3771
+ submitSuccess = false;
3772
+ submitError = _context48.t1 instanceof Error ? _context48.t1.message : String(_context48.t1);
3773
+ console.error('[Checkout] 下单接口调用失败:', submitError);
3774
+ throw _context48.t1;
3775
+ case 40:
3776
+ _context48.prev = 40;
3777
+ _context48.next = 43;
3778
+ return this.core.effects.emit(CheckoutHooks.OnOrderSubmitEnd, {
3779
+ success: submitSuccess,
3780
+ orderUuid: this.store.currentOrder.uuid,
3781
+ operation: isUpdateOperation ? 'update' : 'create',
3782
+ isManual: isManual,
3783
+ orderId: submitSuccess ? ((_checkoutResponse = checkoutResponse) === null || _checkoutResponse === void 0 || (_checkoutResponse = _checkoutResponse.data) === null || _checkoutResponse === void 0 ? void 0 : _checkoutResponse.order_id) || ((_checkoutResponse2 = checkoutResponse) === null || _checkoutResponse2 === void 0 ? void 0 : _checkoutResponse2.order_id) : undefined,
3784
+ error: submitError,
3785
+ duration: Date.now() - startTime,
3786
+ timestamp: Date.now()
3787
+ });
3788
+ case 43:
3789
+ return _context48.finish(40);
3790
+ case 44:
3763
3791
  console.log('[Checkout] 后端返回的响应数据:', {
3764
- status: checkoutResponse === null || checkoutResponse === void 0 ? void 0 : checkoutResponse.status,
3765
- code: checkoutResponse === null || checkoutResponse === void 0 ? void 0 : checkoutResponse.code,
3766
- message: checkoutResponse === null || checkoutResponse === void 0 ? void 0 : checkoutResponse.message,
3767
- data: checkoutResponse === null || checkoutResponse === void 0 ? void 0 : checkoutResponse.data
3792
+ status: (_checkoutResponse3 = checkoutResponse) === null || _checkoutResponse3 === void 0 ? void 0 : _checkoutResponse3.status,
3793
+ code: (_checkoutResponse4 = checkoutResponse) === null || _checkoutResponse4 === void 0 ? void 0 : _checkoutResponse4.code,
3794
+ message: (_checkoutResponse5 = checkoutResponse) === null || _checkoutResponse5 === void 0 ? void 0 : _checkoutResponse5.message,
3795
+ data: (_checkoutResponse6 = checkoutResponse) === null || _checkoutResponse6 === void 0 ? void 0 : _checkoutResponse6.data
3768
3796
  });
3769
3797
  if (!isUpdateOperation) {
3770
- _context48.next = 31;
3798
+ _context48.next = 50;
3771
3799
  break;
3772
3800
  }
3773
3801
  // 更新操作:使用现有的订单ID
3774
3802
  realOrderId = currentOrderId;
3775
3803
  console.log("[Checkout] \u8BA2\u5355\u66F4\u65B0\u6210\u529F\uFF0C\u8BA2\u5355ID: ".concat(realOrderId));
3776
- _context48.next = 52;
3804
+ _context48.next = 71;
3777
3805
  break;
3778
- case 31:
3806
+ case 50:
3779
3807
  // 创建操作:从响应中提取新的订单ID
3780
- extractedOrderId = checkoutResponse === null || checkoutResponse === void 0 || (_data3 = checkoutResponse.data) === null || _data3 === void 0 ? void 0 : _data3.order_id; // 如果data.order_id不存在,尝试直接从根级获取
3808
+ extractedOrderId = (_checkoutResponse7 = checkoutResponse) === null || _checkoutResponse7 === void 0 || (_checkoutResponse7 = _checkoutResponse7.data) === null || _checkoutResponse7 === void 0 ? void 0 : _checkoutResponse7.order_id; // 如果data.order_id不存在,尝试直接从根级获取
3781
3809
  if (!extractedOrderId) {
3782
- extractedOrderId = checkoutResponse === null || checkoutResponse === void 0 ? void 0 : checkoutResponse.order_id;
3810
+ extractedOrderId = (_checkoutResponse8 = checkoutResponse) === null || _checkoutResponse8 === void 0 ? void 0 : _checkoutResponse8.order_id;
3783
3811
  }
3784
3812
 
3785
3813
  // 确保转换为字符串类型
@@ -3787,11 +3815,11 @@ export var CheckoutImpl = /*#__PURE__*/function (_BaseModule) {
3787
3815
  extractedOrderId = String(extractedOrderId);
3788
3816
  }
3789
3817
  if (extractedOrderId) {
3790
- _context48.next = 36;
3818
+ _context48.next = 55;
3791
3819
  break;
3792
3820
  }
3793
3821
  throw new Error('后端返回的订单信息中未包含订单ID');
3794
- case 36:
3822
+ case 55:
3795
3823
  realOrderId = extractedOrderId;
3796
3824
  console.log('[Checkout] 订单创建成功,真实订单ID:', realOrderId);
3797
3825
 
@@ -3801,10 +3829,10 @@ export var CheckoutImpl = /*#__PURE__*/function (_BaseModule) {
3801
3829
  oldOrderId: this.store.currentOrder.order_id,
3802
3830
  newOrderId: realOrderId
3803
3831
  });
3804
- _context48.prev = 39;
3805
- _context48.next = 42;
3832
+ _context48.prev = 58;
3833
+ _context48.next = 61;
3806
3834
  return this.payment.replaceOrderIdByUuidAsync(this.store.currentOrder.uuid, realOrderId);
3807
- case 42:
3835
+ case 61:
3808
3836
  updatedOrder = _context48.sent;
3809
3837
  console.log('[Checkout] Payment模块替换订单ID结果:', {
3810
3838
  wasSuccessful: !!updatedOrder,
@@ -3831,22 +3859,22 @@ export var CheckoutImpl = /*#__PURE__*/function (_BaseModule) {
3831
3859
  目标ID: realOrderId
3832
3860
  });
3833
3861
  }
3834
- _context48.next = 52;
3862
+ _context48.next = 71;
3835
3863
  break;
3836
- case 47:
3837
- _context48.prev = 47;
3838
- _context48.t1 = _context48["catch"](39);
3839
- console.error('[Checkout] 调用Payment模块替换订单ID时发生错误:', _context48.t1);
3864
+ case 66:
3865
+ _context48.prev = 66;
3866
+ _context48.t2 = _context48["catch"](58);
3867
+ console.error('[Checkout] 调用Payment模块替换订单ID时发生错误:', _context48.t2);
3840
3868
 
3841
3869
  // 发生错误时也进行手动替换
3842
3870
  this.store.currentOrder.order_id = realOrderId;
3843
3871
  console.log('[Checkout] 错误恢复:手动设置订单ID:', realOrderId);
3844
- case 52:
3872
+ case 71:
3845
3873
  // 标记订单已同步
3846
3874
  this.store.isOrderSynced = true;
3847
3875
 
3848
3876
  // 触发订单同步完成事件
3849
- _context48.next = 55;
3877
+ _context48.next = 74;
3850
3878
  return this.core.effects.emit(CheckoutHooks.OnOrderSynced, {
3851
3879
  orderUuid: this.store.currentOrder.uuid,
3852
3880
  realOrderId: realOrderId,
@@ -3855,7 +3883,7 @@ export var CheckoutImpl = /*#__PURE__*/function (_BaseModule) {
3855
3883
  isManual: isManual,
3856
3884
  response: checkoutResponse
3857
3885
  });
3858
- case 55:
3886
+ case 74:
3859
3887
  // 验证最终状态
3860
3888
  finalOrderId = this.store.currentOrder.order_id;
3861
3889
  finalIsVirtual = this.isVirtualOrderId(finalOrderId || '');
@@ -3873,11 +3901,11 @@ export var CheckoutImpl = /*#__PURE__*/function (_BaseModule) {
3873
3901
 
3874
3902
  // 返回真实订单ID
3875
3903
  return _context48.abrupt("return", realOrderId);
3876
- case 60:
3904
+ case 79:
3877
3905
  case "end":
3878
3906
  return _context48.stop();
3879
3907
  }
3880
- }, _callee48, this, [[39, 47]]);
3908
+ }, _callee48, this, [[26, 34, 40, 44], [58, 66]]);
3881
3909
  }));
3882
3910
  function syncOrderToBackendWithReturn() {
3883
3911
  return _syncOrderToBackendWithReturn.apply(this, arguments);
@@ -393,7 +393,11 @@ export declare enum CheckoutHooks {
393
393
  /** 订单取消 */
394
394
  OnOrderCancelled = "checkout:onOrderCancelled",
395
395
  /** 订单状态已清理 */
396
- OnOrderCleared = "checkout:onOrderCleared"
396
+ OnOrderCleared = "checkout:onOrderCleared",
397
+ /** 下单接口请求开始 */
398
+ OnOrderSubmitStart = "checkout:onOrderSubmitStart",
399
+ /** 下单接口请求完成 */
400
+ OnOrderSubmitEnd = "checkout:onOrderSubmitEnd"
397
401
  }
398
402
  /**
399
403
  * 结账状态数据
@@ -824,4 +828,34 @@ export interface CheckoutEventData {
824
828
  } | null;
825
829
  timestamp: number;
826
830
  };
831
+ /** 下单接口请求开始事件 */
832
+ orderSubmitStart: {
833
+ /** 订单UUID */
834
+ orderUuid: string;
835
+ /** 操作类型 */
836
+ operation: 'create' | 'update';
837
+ /** 是否手动同步 */
838
+ isManual: boolean;
839
+ /** 支付项数量 */
840
+ paymentItemCount: number;
841
+ timestamp: number;
842
+ };
843
+ /** 下单接口请求完成事件 */
844
+ orderSubmitEnd: {
845
+ /** 是否成功 */
846
+ success: boolean;
847
+ /** 订单UUID */
848
+ orderUuid: string;
849
+ /** 操作类型 */
850
+ operation: 'create' | 'update';
851
+ /** 是否手动同步 */
852
+ isManual: boolean;
853
+ /** 返回的订单ID(成功时) */
854
+ orderId?: string;
855
+ /** 错误信息(失败时) */
856
+ error?: string;
857
+ /** 耗时(毫秒) */
858
+ duration?: number;
859
+ timestamp: number;
860
+ };
827
861
  }
@@ -119,6 +119,8 @@ export var CheckoutHooks = /*#__PURE__*/function (CheckoutHooks) {
119
119
  CheckoutHooks["OnShopDiscountChanged"] = "checkout:onShopDiscountChanged";
120
120
  CheckoutHooks["OnOrderCancelled"] = "checkout:onOrderCancelled";
121
121
  CheckoutHooks["OnOrderCleared"] = "checkout:onOrderCleared";
122
+ CheckoutHooks["OnOrderSubmitStart"] = "checkout:onOrderSubmitStart";
123
+ CheckoutHooks["OnOrderSubmitEnd"] = "checkout:onOrderSubmitEnd";
122
124
  return CheckoutHooks;
123
125
  }({});
124
126
 
@@ -47,6 +47,7 @@ export declare class ShopDiscountImpl extends BaseModule implements Module {
47
47
  private setDiscountList;
48
48
  private getDiscountList;
49
49
  private getCustomerWallet;
50
+ private bestDiscount;
50
51
  private loadPrepareConfig;
51
52
  }
52
53
  export default ShopDiscountImpl;
@@ -50,7 +50,8 @@ export var ShopDiscountImpl = /*#__PURE__*/function (_BaseModule) {
50
50
  customer: null,
51
51
  productList: [],
52
52
  discount: null,
53
- rules: null
53
+ rules: null,
54
+ originalDiscountList: []
54
55
  };
55
56
  return _this;
56
57
  }
@@ -360,26 +361,27 @@ export var ShopDiscountImpl = /*#__PURE__*/function (_BaseModule) {
360
361
  discountList: this.getDiscountList()
361
362
  }, newProductList = _ref3.productList, newDiscountList = _ref3.discountList, isAvailable = _ref3.isAvailable;
362
363
  if (!isAvailable) {
363
- _context5.next = 20;
364
+ _context5.next = 21;
364
365
  break;
365
366
  }
366
367
  this.setDiscountList(newDiscountList || []);
368
+ this.store.originalDiscountList = newDiscountList || [];
367
369
  this.setProductList(newProductList || []);
368
370
  if (!(this.isWalkIn() && resultDiscountList.length && ((_this$options$otherPa6 = this.options.otherParams) === null || _this$options$otherPa6 === void 0 ? void 0 : _this$options$otherPa6.platform) === 'shop')) {
369
- _context5.next = 20;
371
+ _context5.next = 21;
370
372
  break;
371
373
  }
372
- _context5.next = 20;
374
+ _context5.next = 21;
373
375
  return this.getCustomerWallet(resultDiscountList[0].customer_id);
374
- case 20:
376
+ case 21:
375
377
  return _context5.abrupt("return", {
376
378
  type: "clientCalc",
377
379
  isAvailable: isAvailable || false,
378
380
  productList: newProductList || this.store.productList || [],
379
381
  discountList: newDiscountList || this.getDiscountList()
380
382
  });
381
- case 23:
382
- _context5.prev = 23;
383
+ case 24:
384
+ _context5.prev = 24;
383
385
  _context5.t1 = _context5["catch"](0);
384
386
  console.error('[ShopDiscount] 扫码出错:', _context5.t1);
385
387
  return _context5.abrupt("return", {
@@ -388,11 +390,11 @@ export var ShopDiscountImpl = /*#__PURE__*/function (_BaseModule) {
388
390
  productList: this.store.productList || [],
389
391
  discountList: this.getDiscountList()
390
392
  });
391
- case 27:
393
+ case 28:
392
394
  case "end":
393
395
  return _context5.stop();
394
396
  }
395
- }, _callee5, this, [[0, 23]]);
397
+ }, _callee5, this, [[0, 24]]);
396
398
  }));
397
399
  function scanCode(_x4, _x5) {
398
400
  return _scanCode.apply(this, arguments);
@@ -580,20 +582,52 @@ export var ShopDiscountImpl = /*#__PURE__*/function (_BaseModule) {
580
582
  return _getCustomerWallet.apply(this, arguments);
581
583
  }
582
584
  return getCustomerWallet;
583
- }() // 加载准备配置
585
+ }()
584
586
  }, {
585
- key: "loadPrepareConfig",
587
+ key: "bestDiscount",
586
588
  value: function () {
587
- var _loadPrepareConfig = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee8(params) {
588
- var _this$getCustomer2, _this$store$discount7, _this$getDiscountList, _this$store$productLi, customerId, goodPassList, scanDiscount, scanDiscountIds, newGoodPassList, newDiscountList, result;
589
+ var _bestDiscount = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee8() {
590
+ var _this$store$productLi;
591
+ var newDiscountList, result;
589
592
  return _regeneratorRuntime().wrap(function _callee8$(_context8) {
590
593
  while (1) switch (_context8.prev = _context8.next) {
591
594
  case 0:
592
- _context8.prev = 0;
595
+ newDiscountList = this.store.originalDiscountList;
596
+ this.setDiscountList(newDiscountList);
597
+ if (!((_this$store$productLi = this.store.productList) !== null && _this$store$productLi !== void 0 && _this$store$productLi.length)) {
598
+ _context8.next = 6;
599
+ break;
600
+ }
601
+ result = this.calcDiscount(this.store.productList);
602
+ _context8.next = 6;
603
+ return this.core.effects.emit(ShopDiscountHooks.onLoadPrepareCalcResult, result);
604
+ case 6:
605
+ _context8.next = 8;
606
+ return this.core.effects.emit(ShopDiscountHooks.onLoadDiscountList, newDiscountList);
607
+ case 8:
608
+ case "end":
609
+ return _context8.stop();
610
+ }
611
+ }, _callee8, this);
612
+ }));
613
+ function bestDiscount() {
614
+ return _bestDiscount.apply(this, arguments);
615
+ }
616
+ return bestDiscount;
617
+ }() // 加载准备配置
618
+ }, {
619
+ key: "loadPrepareConfig",
620
+ value: function () {
621
+ var _loadPrepareConfig = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee9(params) {
622
+ var _this$getCustomer2, _this$store$discount7, _this$getDiscountList, _this$store$productLi2, customerId, goodPassList, scanDiscount, scanDiscountIds, newGoodPassList, newDiscountList, result;
623
+ return _regeneratorRuntime().wrap(function _callee9$(_context9) {
624
+ while (1) switch (_context9.prev = _context9.next) {
625
+ case 0:
626
+ _context9.prev = 0;
593
627
  customerId = params.customerId || ((_this$getCustomer2 = this.getCustomer()) === null || _this$getCustomer2 === void 0 ? void 0 : _this$getCustomer2.id); // if (customerId === 1 || !customerId) {
594
628
  // return
595
629
  // }
596
- _context8.next = 4;
630
+ _context9.next = 4;
597
631
  return (_this$store$discount7 = this.store.discount) === null || _this$store$discount7 === void 0 ? void 0 : _this$store$discount7.loadPrepareConfig({
598
632
  customer_id: customerId,
599
633
  action: 'create',
@@ -601,7 +635,7 @@ export var ShopDiscountImpl = /*#__PURE__*/function (_BaseModule) {
601
635
  with_discount_card: 1
602
636
  });
603
637
  case 4:
604
- goodPassList = _context8.sent;
638
+ goodPassList = _context9.sent;
605
639
  scanDiscount = (_this$getDiscountList = this.getDiscountList()) === null || _this$getDiscountList === void 0 ? void 0 : _this$getDiscountList.filter(function (item) {
606
640
  return item.isScan;
607
641
  }); // goodPassList 里可能有 scanDiscount 重合的部分,goodPassList 需要剔除
@@ -612,29 +646,30 @@ export var ShopDiscountImpl = /*#__PURE__*/function (_BaseModule) {
612
646
  return !scanDiscountIds.includes(n.id);
613
647
  });
614
648
  newDiscountList = [].concat(_toConsumableArray(scanDiscount), _toConsumableArray(newGoodPassList || []));
649
+ this.store.originalDiscountList = newDiscountList;
615
650
  this.setDiscountList(newDiscountList || []);
616
- if (!((_this$store$productLi = this.store.productList) !== null && _this$store$productLi !== void 0 && _this$store$productLi.length)) {
617
- _context8.next = 14;
651
+ if (!((_this$store$productLi2 = this.store.productList) !== null && _this$store$productLi2 !== void 0 && _this$store$productLi2.length)) {
652
+ _context9.next = 15;
618
653
  break;
619
654
  }
620
655
  result = this.calcDiscount(this.store.productList);
621
- _context8.next = 14;
656
+ _context9.next = 15;
622
657
  return this.core.effects.emit(ShopDiscountHooks.onLoadPrepareCalcResult, result);
623
- case 14:
624
- _context8.next = 16;
658
+ case 15:
659
+ _context9.next = 17;
625
660
  return this.core.effects.emit(ShopDiscountHooks.onLoadDiscountList, newDiscountList);
626
- case 16:
627
- _context8.next = 21;
661
+ case 17:
662
+ _context9.next = 22;
628
663
  break;
629
- case 18:
630
- _context8.prev = 18;
631
- _context8.t0 = _context8["catch"](0);
632
- console.error('[ShopDiscount] 加载准备配置出错:', _context8.t0);
633
- case 21:
664
+ case 19:
665
+ _context9.prev = 19;
666
+ _context9.t0 = _context9["catch"](0);
667
+ console.error('[ShopDiscount] 加载准备配置出错:', _context9.t0);
668
+ case 22:
634
669
  case "end":
635
- return _context8.stop();
670
+ return _context9.stop();
636
671
  }
637
- }, _callee8, this, [[0, 18]]);
672
+ }, _callee9, this, [[0, 19]]);
638
673
  }));
639
674
  function loadPrepareConfig(_x8) {
640
675
  return _loadPrepareConfig.apply(this, arguments);
@@ -1,4 +1,4 @@
1
- import { DiscountModule } from '../../modules/Discount';
1
+ import { DiscountModule, Discount } from '../../modules/Discount';
2
2
  import { RulesModule } from '../../modules/Rules';
3
3
  export declare enum ShopDiscountHooks {
4
4
  onInited = "shopDiscount:onInited",
@@ -22,4 +22,5 @@ export interface ShopDiscountState {
22
22
  discount: DiscountModule | null;
23
23
  rules: RulesModule | null;
24
24
  productList: Record<string, any>[];
25
+ originalDiscountList: Discount[];
25
26
  }
@@ -30,3 +30,4 @@ export declare class DiscountModule extends BaseModule implements Module, Discou
30
30
  clear(): Promise<void>;
31
31
  storeChange(): void;
32
32
  }
33
+ export type { Discount } from './types';
@@ -1,6 +1,8 @@
1
+ var __create = Object.create;
1
2
  var __defProp = Object.defineProperty;
2
3
  var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
3
4
  var __getOwnPropNames = Object.getOwnPropertyNames;
5
+ var __getProtoOf = Object.getPrototypeOf;
4
6
  var __hasOwnProp = Object.prototype.hasOwnProperty;
5
7
  var __export = (target, all) => {
6
8
  for (var name in all)
@@ -14,6 +16,14 @@ var __copyProps = (to, from, except, desc) => {
14
16
  }
15
17
  return to;
16
18
  };
19
+ var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
20
+ // If the importer is in node compatibility mode or this is not an ESM
21
+ // file that has been converted to a CommonJS file using a Babel-
22
+ // compatible transform (i.e. "__esModule" has not been set), then set
23
+ // "default" to the CommonJS "module.exports" for node compatibility.
24
+ isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
25
+ mod
26
+ ));
17
27
  var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
18
28
 
19
29
  // src/modules/Order/index.ts
@@ -25,6 +35,7 @@ module.exports = __toCommonJS(Order_exports);
25
35
  var import_BaseModule = require("../BaseModule");
26
36
  var import_utils = require("./utils");
27
37
  var import_utils2 = require("../Product/utils");
38
+ var import_dayjs = __toESM(require("dayjs"));
28
39
  var OrderModule = class extends import_BaseModule.BaseModule {
29
40
  constructor(name, version) {
30
41
  super(name, version);
@@ -143,6 +154,9 @@ var OrderModule = class extends import_BaseModule.BaseModule {
143
154
  orderData.bookings = [];
144
155
  orderData.relation_products = [];
145
156
  }
157
+ if (!params.order_id && !orderData.schedule_date) {
158
+ orderData.schedule_date = (0, import_dayjs.default)().format("YYYY-MM-DD HH:mm:ss");
159
+ }
146
160
  console.log("[Order] 调用后端接口创建订单:", {
147
161
  url: "/order/checkout",
148
162
  orderType: orderData.type,
@@ -908,6 +908,7 @@ var CheckoutImpl = class extends import_BaseModule.BaseModule {
908
908
  * @throws 当前没有活跃订单时抛出错误
909
909
  */
910
910
  async addPaymentItemAsync(paymentItem) {
911
+ var _a, _b;
911
912
  try {
912
913
  if (!this.store.currentOrder) {
913
914
  throw (0, import_utils.createCheckoutError)(
@@ -930,12 +931,29 @@ var CheckoutImpl = class extends import_BaseModule.BaseModule {
930
931
  paymentItem
931
932
  );
932
933
  console.log("[Checkout] 支付项添加成功");
933
- const currentOrderId = this.store.currentOrder.order_id;
934
- const isCurrentOrderReal = currentOrderId && !this.isVirtualOrderId(currentOrderId);
935
- console.log("[Checkout] 准备同步订单数据:", {
936
- 当前订单ID: currentOrderId,
937
- 是否为真实ID: isCurrentOrderReal
934
+ const isEftposPayment = ((_a = paymentItem.type) == null ? void 0 : _a.toLowerCase()) === "eftpos" || ((_b = paymentItem.code) == null ? void 0 : _b.toUpperCase().includes("EFTPOS"));
935
+ console.log("[Checkout] EFTPOS 支付检查:", {
936
+ paymentCode: paymentItem.code,
937
+ paymentType: paymentItem.type,
938
+ isEftposPayment,
939
+ currentOrderSynced: this.store.isOrderSynced
938
940
  });
941
+ if (isEftposPayment) {
942
+ console.log("[Checkout] 检测到 EFTPOS 支付,立即同步订单到后端...");
943
+ try {
944
+ await this.syncOrderToBackendWithReturn(true);
945
+ console.log("[Checkout] EFTPOS 支付后订单同步完成 (已标记为手动同步):", {
946
+ 订单ID: this.store.currentOrder.order_id,
947
+ 是否已同步: this.store.isOrderSynced
948
+ });
949
+ } catch (error) {
950
+ console.error("[Checkout] EFTPOS 支付后订单同步失败:", error);
951
+ await this.handleError(
952
+ new Error(`EFTPOS 支付后订单同步失败: ${error instanceof Error ? error.message : String(error)}`),
953
+ import_types.CheckoutErrorType.OrderCreationFailed
954
+ );
955
+ }
956
+ }
939
957
  await this.updateStateAmountToRemaining();
940
958
  await this.core.effects.emit(import_types.CheckoutHooks.OnPaymentStarted, {
941
959
  orderUuid: this.store.currentOrder.uuid,
@@ -1492,10 +1510,10 @@ var CheckoutImpl = class extends import_BaseModule.BaseModule {
1492
1510
  this.store.currentOrder.uuid
1493
1511
  );
1494
1512
  const filteredPaymentItems = allPaymentItems.filter(
1495
- (payment) => !payment.voucher_id || payment.voucher_id.trim() === ""
1513
+ (payment) => !payment.voucher_id
1496
1514
  );
1497
1515
  const voucherItems = allPaymentItems.filter(
1498
- (payment) => payment.voucher_id && payment.voucher_id.trim() !== ""
1516
+ (payment) => payment.voucher_id
1499
1517
  );
1500
1518
  console.log("[Checkout] 支付项过滤结果:", {
1501
1519
  总支付项: allPaymentItems.length,
@@ -2023,12 +2041,18 @@ var CheckoutImpl = class extends import_BaseModule.BaseModule {
2023
2041
  false
2024
2042
  // 不包括已撤销的支付项
2025
2043
  );
2026
- const allPaymentsHaveVoucherId = currentPayments.length > 0 && currentPayments.every(
2044
+ const hasPaymentItems = currentPayments.length > 0;
2045
+ const allPaymentsHaveVoucherId = hasPaymentItems && currentPayments.every(
2027
2046
  (item) => item.status !== "voided" && item.voucher_id
2028
2047
  );
2029
- console.log("[Checkout] 支付项voucher_id检查:", {
2048
+ const shouldAutoSync = hasPaymentItems && !allPaymentsHaveVoucherId;
2049
+ console.log("[Checkout] 自动同步订单条件检查:", {
2030
2050
  paymentCount: currentPayments.length,
2051
+ hasPaymentItems,
2031
2052
  allHaveVoucherId: allPaymentsHaveVoucherId,
2053
+ isOrderSynced: this.store.isOrderSynced,
2054
+ shouldAutoSync,
2055
+ reason: shouldAutoSync ? "支付完成,需要同步最终支付状态" : "跳过同步",
2032
2056
  paymentItems: currentPayments.map((p) => ({
2033
2057
  uuid: p.uuid,
2034
2058
  code: p.code,
@@ -2037,11 +2061,15 @@ var CheckoutImpl = class extends import_BaseModule.BaseModule {
2037
2061
  status: p.status
2038
2062
  }))
2039
2063
  });
2040
- if (allPaymentsHaveVoucherId) {
2041
- console.log("[Checkout] 所有支付项均为代金券类型,跳过订单同步");
2042
- } else {
2043
- console.log("[Checkout] 支付完成但订单未同步,开始同步订单到后端...");
2064
+ if (shouldAutoSync) {
2065
+ console.log("[Checkout] 满足自动同步条件,开始同步订单到后端...");
2044
2066
  await this.syncOrderToBackend();
2067
+ } else {
2068
+ if (!hasPaymentItems) {
2069
+ console.log("[Checkout] 没有支付项,跳过订单同步");
2070
+ } else if (allPaymentsHaveVoucherId) {
2071
+ console.log("[Checkout] 所有支付项均为代金券类型,跳过订单同步");
2072
+ }
2045
2073
  }
2046
2074
  await this.core.effects.emit(import_types.CheckoutHooks.OnOrderPaymentCompleted, {
2047
2075
  orderUuid: this.store.currentOrder.uuid,
@@ -2104,7 +2132,7 @@ var CheckoutImpl = class extends import_BaseModule.BaseModule {
2104
2132
  * @returns 后端返回的真实订单ID
2105
2133
  */
2106
2134
  async syncOrderToBackendWithReturn(isManual = false, customPaymentItems) {
2107
- var _a, _b, _c, _d;
2135
+ var _a, _b, _c, _d, _e;
2108
2136
  if (!this.store.localOrderData || !this.store.currentOrder) {
2109
2137
  throw new Error("缺少必要的订单数据,无法同步到后端");
2110
2138
  }
@@ -2204,7 +2232,38 @@ var CheckoutImpl = class extends import_BaseModule.BaseModule {
2204
2232
  } else {
2205
2233
  console.log("[Checkout] 创建新订单操作");
2206
2234
  }
2207
- const checkoutResponse = await this.order.createOrderByCheckout(orderParams);
2235
+ const startTime = Date.now();
2236
+ await this.core.effects.emit(import_types.CheckoutHooks.OnOrderSubmitStart, {
2237
+ orderUuid: this.store.currentOrder.uuid,
2238
+ operation: isUpdateOperation ? "update" : "create",
2239
+ isManual,
2240
+ paymentItemCount: paymentItems.length,
2241
+ timestamp: startTime
2242
+ });
2243
+ let checkoutResponse;
2244
+ let submitSuccess = false;
2245
+ let submitError;
2246
+ try {
2247
+ checkoutResponse = await this.order.createOrderByCheckout(orderParams);
2248
+ submitSuccess = true;
2249
+ console.log("[Checkout] 下单接口调用成功");
2250
+ } catch (error) {
2251
+ submitSuccess = false;
2252
+ submitError = error instanceof Error ? error.message : String(error);
2253
+ console.error("[Checkout] 下单接口调用失败:", submitError);
2254
+ throw error;
2255
+ } finally {
2256
+ await this.core.effects.emit(import_types.CheckoutHooks.OnOrderSubmitEnd, {
2257
+ success: submitSuccess,
2258
+ orderUuid: this.store.currentOrder.uuid,
2259
+ operation: isUpdateOperation ? "update" : "create",
2260
+ isManual,
2261
+ orderId: submitSuccess ? ((_d = checkoutResponse == null ? void 0 : checkoutResponse.data) == null ? void 0 : _d.order_id) || (checkoutResponse == null ? void 0 : checkoutResponse.order_id) : void 0,
2262
+ error: submitError,
2263
+ duration: Date.now() - startTime,
2264
+ timestamp: Date.now()
2265
+ });
2266
+ }
2208
2267
  console.log("[Checkout] 后端返回的响应数据:", {
2209
2268
  status: checkoutResponse == null ? void 0 : checkoutResponse.status,
2210
2269
  code: checkoutResponse == null ? void 0 : checkoutResponse.code,
@@ -2216,7 +2275,7 @@ var CheckoutImpl = class extends import_BaseModule.BaseModule {
2216
2275
  realOrderId = currentOrderId;
2217
2276
  console.log(`[Checkout] 订单更新成功,订单ID: ${realOrderId}`);
2218
2277
  } else {
2219
- let extractedOrderId = (_d = checkoutResponse == null ? void 0 : checkoutResponse.data) == null ? void 0 : _d.order_id;
2278
+ let extractedOrderId = (_e = checkoutResponse == null ? void 0 : checkoutResponse.data) == null ? void 0 : _e.order_id;
2220
2279
  if (!extractedOrderId) {
2221
2280
  extractedOrderId = checkoutResponse == null ? void 0 : checkoutResponse.order_id;
2222
2281
  }
@@ -393,7 +393,11 @@ export declare enum CheckoutHooks {
393
393
  /** 订单取消 */
394
394
  OnOrderCancelled = "checkout:onOrderCancelled",
395
395
  /** 订单状态已清理 */
396
- OnOrderCleared = "checkout:onOrderCleared"
396
+ OnOrderCleared = "checkout:onOrderCleared",
397
+ /** 下单接口请求开始 */
398
+ OnOrderSubmitStart = "checkout:onOrderSubmitStart",
399
+ /** 下单接口请求完成 */
400
+ OnOrderSubmitEnd = "checkout:onOrderSubmitEnd"
397
401
  }
398
402
  /**
399
403
  * 结账状态数据
@@ -824,4 +828,34 @@ export interface CheckoutEventData {
824
828
  } | null;
825
829
  timestamp: number;
826
830
  };
831
+ /** 下单接口请求开始事件 */
832
+ orderSubmitStart: {
833
+ /** 订单UUID */
834
+ orderUuid: string;
835
+ /** 操作类型 */
836
+ operation: 'create' | 'update';
837
+ /** 是否手动同步 */
838
+ isManual: boolean;
839
+ /** 支付项数量 */
840
+ paymentItemCount: number;
841
+ timestamp: number;
842
+ };
843
+ /** 下单接口请求完成事件 */
844
+ orderSubmitEnd: {
845
+ /** 是否成功 */
846
+ success: boolean;
847
+ /** 订单UUID */
848
+ orderUuid: string;
849
+ /** 操作类型 */
850
+ operation: 'create' | 'update';
851
+ /** 是否手动同步 */
852
+ isManual: boolean;
853
+ /** 返回的订单ID(成功时) */
854
+ orderId?: string;
855
+ /** 错误信息(失败时) */
856
+ error?: string;
857
+ /** 耗时(毫秒) */
858
+ duration?: number;
859
+ timestamp: number;
860
+ };
827
861
  }
@@ -71,6 +71,8 @@ var CheckoutHooks = /* @__PURE__ */ ((CheckoutHooks2) => {
71
71
  CheckoutHooks2["OnShopDiscountChanged"] = "checkout:onShopDiscountChanged";
72
72
  CheckoutHooks2["OnOrderCancelled"] = "checkout:onOrderCancelled";
73
73
  CheckoutHooks2["OnOrderCleared"] = "checkout:onOrderCleared";
74
+ CheckoutHooks2["OnOrderSubmitStart"] = "checkout:onOrderSubmitStart";
75
+ CheckoutHooks2["OnOrderSubmitEnd"] = "checkout:onOrderSubmitEnd";
74
76
  return CheckoutHooks2;
75
77
  })(CheckoutHooks || {});
76
78
  // Annotate the CommonJS export names for ESM import in node:
@@ -47,6 +47,7 @@ export declare class ShopDiscountImpl extends BaseModule implements Module {
47
47
  private setDiscountList;
48
48
  private getDiscountList;
49
49
  private getCustomerWallet;
50
+ private bestDiscount;
50
51
  private loadPrepareConfig;
51
52
  }
52
53
  export default ShopDiscountImpl;
@@ -50,7 +50,8 @@ var ShopDiscountImpl = class extends import_BaseModule.BaseModule {
50
50
  customer: null,
51
51
  productList: [],
52
52
  discount: null,
53
- rules: null
53
+ rules: null,
54
+ originalDiscountList: []
54
55
  };
55
56
  }
56
57
  // =========== 生命周期方法 ===========
@@ -241,6 +242,7 @@ var ShopDiscountImpl = class extends import_BaseModule.BaseModule {
241
242
  };
242
243
  if (isAvailable) {
243
244
  this.setDiscountList(newDiscountList || []);
245
+ this.store.originalDiscountList = newDiscountList || [];
244
246
  this.setProductList(newProductList || []);
245
247
  if (this.isWalkIn() && resultDiscountList.length && ((_b = this.options.otherParams) == null ? void 0 : _b.platform) === "shop") {
246
248
  await this.getCustomerWallet(
@@ -388,6 +390,22 @@ var ShopDiscountImpl = class extends import_BaseModule.BaseModule {
388
390
  console.error("[ShopDiscount] 获取客户钱包信息出错:", error);
389
391
  }
390
392
  }
393
+ async bestDiscount() {
394
+ var _a;
395
+ const newDiscountList = this.store.originalDiscountList;
396
+ this.setDiscountList(newDiscountList);
397
+ if ((_a = this.store.productList) == null ? void 0 : _a.length) {
398
+ const result = this.calcDiscount(this.store.productList);
399
+ await this.core.effects.emit(
400
+ import_types.ShopDiscountHooks.onLoadPrepareCalcResult,
401
+ result
402
+ );
403
+ }
404
+ await this.core.effects.emit(
405
+ import_types.ShopDiscountHooks.onLoadDiscountList,
406
+ newDiscountList
407
+ );
408
+ }
391
409
  // 加载准备配置
392
410
  async loadPrepareConfig(params) {
393
411
  var _a, _b, _c, _d;
@@ -405,6 +423,7 @@ var ShopDiscountImpl = class extends import_BaseModule.BaseModule {
405
423
  const scanDiscountIds = scanDiscount.map((n) => n.id);
406
424
  const newGoodPassList = goodPassList == null ? void 0 : goodPassList.filter((n) => !scanDiscountIds.includes(n.id));
407
425
  const newDiscountList = [...scanDiscount, ...newGoodPassList || []];
426
+ this.store.originalDiscountList = newDiscountList;
408
427
  this.setDiscountList(newDiscountList || []);
409
428
  if ((_d = this.store.productList) == null ? void 0 : _d.length) {
410
429
  const result = this.calcDiscount(this.store.productList);
@@ -1,4 +1,4 @@
1
- import { DiscountModule } from '../../modules/Discount';
1
+ import { DiscountModule, Discount } from '../../modules/Discount';
2
2
  import { RulesModule } from '../../modules/Rules';
3
3
  export declare enum ShopDiscountHooks {
4
4
  onInited = "shopDiscount:onInited",
@@ -22,4 +22,5 @@ export interface ShopDiscountState {
22
22
  discount: DiscountModule | null;
23
23
  rules: RulesModule | null;
24
24
  productList: Record<string, any>[];
25
+ originalDiscountList: Discount[];
25
26
  }
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "private": false,
3
3
  "name": "@pisell/pisellos",
4
- "version": "0.0.237",
4
+ "version": "0.0.238",
5
5
  "description": "一个可扩展的前端模块化SDK框架,支持插件系统",
6
6
  "main": "dist/index.js",
7
7
  "types": "dist/index.d.ts",