@pisell/pisellos 2.3.109 → 2.3.111

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 (30) hide show
  1. package/dist/modules/OpenData/index.d.ts +1 -1
  2. package/dist/modules/OpenData/index.js +19 -11
  3. package/dist/modules/Order/index.d.ts +1 -0
  4. package/dist/modules/Order/index.js +22 -21
  5. package/dist/modules/Payment/cashRecommendationAlgorithm.d.ts +4 -5
  6. package/dist/modules/Payment/cashRecommendationAlgorithm.js +17 -367
  7. package/dist/server/index.js +4 -5
  8. package/dist/server/utils/small-ticket.d.ts +8 -5
  9. package/dist/server/utils/small-ticket.js +678 -140
  10. package/dist/solution/BookingByStep/index.d.ts +1 -1
  11. package/dist/solution/BookingTicket/index.d.ts +6 -0
  12. package/dist/solution/BookingTicket/index.js +548 -485
  13. package/dist/utils/os-private-payload.d.ts +5 -0
  14. package/dist/utils/os-private-payload.js +17 -0
  15. package/lib/model/strategy/adapter/promotion/index.js +1 -46
  16. package/lib/modules/OpenData/index.d.ts +1 -1
  17. package/lib/modules/OpenData/index.js +9 -1
  18. package/lib/modules/Order/index.d.ts +1 -0
  19. package/lib/modules/Order/index.js +15 -18
  20. package/lib/modules/Payment/cashRecommendationAlgorithm.d.ts +4 -5
  21. package/lib/modules/Payment/cashRecommendationAlgorithm.js +9 -354
  22. package/lib/server/index.js +4 -5
  23. package/lib/server/utils/small-ticket.d.ts +8 -5
  24. package/lib/server/utils/small-ticket.js +524 -65
  25. package/lib/solution/BookingByStep/index.d.ts +1 -1
  26. package/lib/solution/BookingTicket/index.d.ts +6 -0
  27. package/lib/solution/BookingTicket/index.js +32 -3
  28. package/lib/utils/os-private-payload.d.ts +5 -0
  29. package/lib/utils/os-private-payload.js +22 -0
  30. package/package.json +1 -1
@@ -33,7 +33,7 @@ export declare class OpenDataModule extends BaseModule implements Module {
33
33
  getOpenData(): OpenDataConfig | null;
34
34
  getInvoiceLayoutConfig(): InvoiceLayoutConfig;
35
35
  getInvoiceTemplateSettings(): InvoiceTemplateSettings;
36
- ensureInvoiceTemplateSettings(target: string): Promise<InvoiceTemplateSettings>;
36
+ ensureInvoiceTemplateSettings(target: string, openDataConfig?: OpenDataConfig): Promise<InvoiceTemplateSettings>;
37
37
  private commitInvoiceTemplateSettings;
38
38
  private resolveInvoiceTemplateSettings;
39
39
  private buildInvoiceTemplateCacheKey;
@@ -233,39 +233,47 @@ export var OpenDataModule = /*#__PURE__*/function (_BaseModule) {
233
233
  }, {
234
234
  key: "ensureInvoiceTemplateSettings",
235
235
  value: function () {
236
- var _ensureInvoiceTemplateSettings = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee3(target) {
236
+ var _ensureInvoiceTemplateSettings = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee3(target, openDataConfig) {
237
237
  var normalizedTarget, config, fetchRevision, resolved;
238
238
  return _regeneratorRuntime().wrap(function _callee3$(_context3) {
239
239
  while (1) switch (_context3.prev = _context3.next) {
240
240
  case 0:
241
241
  normalizedTarget = String(target || '').trim();
242
+ if (openDataConfig && openDataConfig !== this.store.data) {
243
+ // 部分解决方案会在初始化参数中直接收到已经拉取完成的 OpenData。
244
+ // 将它纳入模块状态后再解析 Document,避免为了 Invoice 重复请求 setting,
245
+ // 同时让后续同步 getter 能校验同一份 document id 与 target。
246
+ this.fetchRevision += 1;
247
+ this.store.data = openDataConfig;
248
+ this.store.lastFetchedAt = Date.now();
249
+ }
242
250
  config = this.store.data;
243
251
  if (!(!normalizedTarget || !config)) {
244
- _context3.next = 4;
252
+ _context3.next = 5;
245
253
  break;
246
254
  }
247
255
  return _context3.abrupt("return", getDefaultInvoiceTemplateSettings());
248
- case 4:
256
+ case 5:
249
257
  fetchRevision = this.fetchRevision;
250
- _context3.next = 7;
258
+ _context3.next = 8;
251
259
  return this.resolveInvoiceTemplateSettings(config, normalizedTarget);
252
- case 7:
260
+ case 8:
253
261
  resolved = _context3.sent;
254
262
  if (!(fetchRevision !== this.fetchRevision || this.store.data !== config)) {
255
- _context3.next = 10;
263
+ _context3.next = 11;
256
264
  break;
257
265
  }
258
266
  return _context3.abrupt("return", getDefaultInvoiceTemplateSettings());
259
- case 10:
267
+ case 11:
260
268
  this.commitInvoiceTemplateSettings(normalizedTarget, resolved);
261
269
  return _context3.abrupt("return", normalizeRawInvoiceTemplateSettings(resolved.settings));
262
- case 12:
270
+ case 13:
263
271
  case "end":
264
272
  return _context3.stop();
265
273
  }
266
274
  }, _callee3, this);
267
275
  }));
268
- function ensureInvoiceTemplateSettings(_x4) {
276
+ function ensureInvoiceTemplateSettings(_x4, _x5) {
269
277
  return _ensureInvoiceTemplateSettings.apply(this, arguments);
270
278
  }
271
279
  return ensureInvoiceTemplateSettings;
@@ -367,7 +375,7 @@ export var OpenDataModule = /*#__PURE__*/function (_BaseModule) {
367
375
  }
368
376
  }, _callee4, this);
369
377
  }));
370
- function resolveInvoiceTemplateSettings(_x5, _x6) {
378
+ function resolveInvoiceTemplateSettings(_x6, _x7) {
371
379
  return _resolveInvoiceTemplateSettings.apply(this, arguments);
372
380
  }
373
381
  return resolveInvoiceTemplateSettings;
@@ -424,7 +432,7 @@ export var OpenDataModule = /*#__PURE__*/function (_BaseModule) {
424
432
  }
425
433
  }, _callee5, this);
426
434
  }));
427
- function fetchInvoiceTemplateDocument(_x7) {
435
+ function fetchInvoiceTemplateDocument(_x8) {
428
436
  return _fetchInvoiceTemplateDocument.apply(this, arguments);
429
437
  }
430
438
  return fetchInvoiceTemplateDocument;
@@ -588,6 +588,7 @@ export declare class OrderModule extends BaseModule implements Module, OrderModu
588
588
  */
589
589
  createOrderByCheckout(params: CheckoutOrderParams): Promise<any>;
590
590
  submitSalesOrder<T = any>(params: SubmitSalesOrderParams): Promise<T>;
591
+ private isSalesCheckoutHandledByOsServer;
591
592
  /**
592
593
  * 发送支付链接邮件。
593
594
  *
@@ -35,6 +35,7 @@ import { OrderHooks } from "./types";
35
35
  import { composeLinePrice, generateDuration, getAllDiscountList, mergeRelationForms, ensureCartItemOriginHolder, buildSubmitPayload, createDefaultTempOrder, createDefaultOrderRulesHooks, createEmptySummary, createUuidV4, formatDateTime, isTempOrder, normalizeOrderMetaList, normalizeBookingIsAll, normalizeBookingSubType, mapPaymentItemsToOrderPayments, normalizeOrderProductDiscountList, resolveEffectivePerUnitDiscount, ensureProductSku, getProductSkuOptions, normalizeProductSkuOptions, sumOptionUnitPrice, clearTempOrderHolderAssignments, getOrderProductLineUid, indexOrderProductsByUid, calculateOrderProductsDeposit, buildPointCardSnapshotFromWalletPassList, mergeOrderProductDisplayFields, resolveIsFormSubject, isMarkdownBundle } from "./utils";
36
36
  import { isNormalProduct } from "../Product/utils";
37
37
  import dayjs from 'dayjs';
38
+ import { stripOsPrivatePayloadFields } from "../../utils/os-private-payload";
38
39
  import { buildSalesNoteText, decodeSalesNotesSnapshot, getSalesNotePolicy } from "./salesNotes";
39
40
  import { ensureOrderPaymentNumber, mergeOrderPaymentRecord, resolveOrderPaymentIdentity } from "./payment-utils";
40
41
  import { resolvePaymentNumberDevicePrefix, resolvePaymentNumberDevicePrefixFromDeviceId } from "../../utils/payment-number";
@@ -107,13 +108,6 @@ function getVoucherPaymentLifecycleKey(payment) {
107
108
  if (!isVoucherPaymentRecord(payment)) return null;
108
109
  return "".concat(String(payment.voucher_id), "::").concat(String(payment.status || ''));
109
110
  }
110
- function hasPersistedOrderId(orderId) {
111
- if (orderId === undefined || orderId === null || orderId === '') return false;
112
- if (typeof orderId === 'string' && orderId.startsWith('LOCAL_')) return false;
113
- var normalizedOrderId = Number(orderId);
114
- if (Number.isFinite(normalizedOrderId)) return normalizedOrderId > 0;
115
- return String(orderId).trim() !== '';
116
- }
117
111
  export var OrderModule = /*#__PURE__*/function (_BaseModule) {
118
112
  _inherits(OrderModule, _BaseModule);
119
113
  var _super = _createSuper(OrderModule);
@@ -595,7 +589,6 @@ export var OrderModule = /*#__PURE__*/function (_BaseModule) {
595
589
  this.store.virtualCurrencyList = (prepareConfigResult === null || prepareConfigResult === void 0 ? void 0 : prepareConfigResult.virtualCurrencyList) || [];
596
590
  if (prepareConfigResult) {
597
591
  this.writePointCardSnapshotFromPrepareConfig({
598
- orderId: orderId,
599
592
  customerId: params.customerId,
600
593
  availableWalletPassList: prepareConfigResult.availableWalletPassList || []
601
594
  });
@@ -1533,12 +1526,10 @@ export var OrderModule = /*#__PURE__*/function (_BaseModule) {
1533
1526
  }, {
1534
1527
  key: "writePointCardSnapshotFromPrepareConfig",
1535
1528
  value: function writePointCardSnapshotFromPrepareConfig(params) {
1536
- var _tempOrder$customer_i, _params$orderId;
1529
+ var _tempOrder$customer_i;
1537
1530
  var tempOrder = this.store.tempOrder;
1538
1531
  if (!tempOrder) return;
1539
1532
  if (String((_tempOrder$customer_i = tempOrder.customer_id) !== null && _tempOrder$customer_i !== void 0 ? _tempOrder$customer_i : '') !== String(params.customerId)) return;
1540
- var effectiveOrderId = (_params$orderId = params.orderId) !== null && _params$orderId !== void 0 ? _params$orderId : tempOrder.order_id;
1541
- if (hasPersistedOrderId(effectiveOrderId)) return;
1542
1533
  tempOrder.order_meta_list = _objectSpread(_objectSpread({}, normalizeOrderMetaList(tempOrder.order_meta_list) || {}), {}, {
1543
1534
  point_cards_snapshot: buildPointCardSnapshotFromWalletPassList(params.availableWalletPassList)
1544
1535
  });
@@ -3871,7 +3862,7 @@ export var OrderModule = /*#__PURE__*/function (_BaseModule) {
3871
3862
  email: tempOrder.email
3872
3863
  });
3873
3864
  var nextCustomerId = (_tempOrder$customer_i4 = tempOrder.customer_id) !== null && _tempOrder$customer_i4 !== void 0 ? _tempOrder$customer_i4 : null;
3874
- if (String(previousCustomerId !== null && previousCustomerId !== void 0 ? previousCustomerId : '') !== String(nextCustomerId !== null && nextCustomerId !== void 0 ? nextCustomerId : '') && !hasPersistedOrderId(tempOrder.order_id)) {
3865
+ if (String(previousCustomerId !== null && previousCustomerId !== void 0 ? previousCustomerId : '') !== String(nextCustomerId !== null && nextCustomerId !== void 0 ? nextCustomerId : '')) {
3875
3866
  this.store.availableWalletIds = [];
3876
3867
  this.clearTempOrderPointCardSnapshot(tempOrder);
3877
3868
  }
@@ -3914,9 +3905,7 @@ export var OrderModule = /*#__PURE__*/function (_BaseModule) {
3914
3905
  if (String(previousCustomerId !== null && previousCustomerId !== void 0 ? previousCustomerId : '') !== String(nextCustomerId !== null && nextCustomerId !== void 0 ? nextCustomerId : '')) {
3915
3906
  this.store.availableWalletIds = [];
3916
3907
  this.store.virtualCurrencyList = [];
3917
- if (!hasPersistedOrderId(tempOrder.order_id)) {
3918
- this.clearTempOrderPointCardSnapshot(tempOrder);
3919
- }
3908
+ this.clearTempOrderPointCardSnapshot(tempOrder);
3920
3909
  clearTempOrderHolderAssignments(tempOrder);
3921
3910
  this.clearCustomerBoundDiscounts(tempOrder);
3922
3911
  this.applyDiscount();
@@ -3963,9 +3952,7 @@ export var OrderModule = /*#__PURE__*/function (_BaseModule) {
3963
3952
  if (tempOrder._extend) delete tempOrder._extend.customerSnapshot;
3964
3953
  this.store.availableWalletIds = [];
3965
3954
  this.store.virtualCurrencyList = [];
3966
- if (!hasPersistedOrderId(tempOrder.order_id)) {
3967
- this.clearTempOrderPointCardSnapshot(tempOrder);
3968
- }
3955
+ this.clearTempOrderPointCardSnapshot(tempOrder);
3969
3956
  this.clearCustomerBoundDiscounts(tempOrder);
3970
3957
  this.applyDiscount();
3971
3958
  this.persistTempOrder();
@@ -6754,17 +6741,18 @@ export var OrderModule = /*#__PURE__*/function (_BaseModule) {
6754
6741
  key: "submitSalesOrder",
6755
6742
  value: function () {
6756
6743
  var _submitSalesOrder = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee44(params) {
6757
- var url, query, fetchUrl;
6744
+ var url, query, fetchUrl, requestQuery;
6758
6745
  return _regeneratorRuntime().wrap(function _callee44$(_context47) {
6759
6746
  while (1) switch (_context47.prev = _context47.next) {
6760
6747
  case 0:
6761
6748
  url = params.url, query = params.query;
6762
6749
  fetchUrl = url || '/order/sales/checkout';
6763
- return _context47.abrupt("return", this.request.post(fetchUrl, query, {
6750
+ requestQuery = this.isSalesCheckoutHandledByOsServer(fetchUrl) ? query : stripOsPrivatePayloadFields(query);
6751
+ return _context47.abrupt("return", this.request.post(fetchUrl, requestQuery, {
6764
6752
  osServer: true,
6765
6753
  customToast: function customToast() {}
6766
6754
  }));
6767
- case 3:
6755
+ case 4:
6768
6756
  case "end":
6769
6757
  return _context47.stop();
6770
6758
  }
@@ -6775,6 +6763,19 @@ export var OrderModule = /*#__PURE__*/function (_BaseModule) {
6775
6763
  }
6776
6764
  return submitSalesOrder;
6777
6765
  }()
6766
+ }, {
6767
+ key: "isSalesCheckoutHandledByOsServer",
6768
+ value: function isSalesCheckoutHandledByOsServer(fetchUrl) {
6769
+ var _this$core2, _this$request;
6770
+ var server = (_this$core2 = this.core) === null || _this$core2 === void 0 ? void 0 : _this$core2.server;
6771
+ if (!server || typeof server.hasRoute !== 'function') return false;
6772
+ var requestBaseUrl = String(((_this$request = this.request) === null || _this$request === void 0 ? void 0 : _this$request.baseUrl) || '/shop').replace(/\/+$/, '');
6773
+ if (!requestBaseUrl.startsWith('/')) return false;
6774
+ var normalizedFetchUrl = fetchUrl.startsWith('/') ? fetchUrl : "/".concat(fetchUrl);
6775
+ var routePath = normalizedFetchUrl === requestBaseUrl || normalizedFetchUrl.startsWith("".concat(requestBaseUrl, "/")) ? normalizedFetchUrl : "".concat(requestBaseUrl).concat(normalizedFetchUrl);
6776
+ return server.hasRoute('post', routePath) === true;
6777
+ }
6778
+
6778
6779
  /**
6779
6780
  * 发送支付链接邮件。
6780
6781
  *
@@ -2,10 +2,9 @@
2
2
  * 现金支付推荐算法
3
3
  *
4
4
  * 核心原理:
5
- * 1. 每个推荐金额都应该是独立的最优组合
6
- * 2. 不推荐在已有最优解基础上添加额外面额的组合
7
- * 3. 优先推荐使用不同数量币种的组合
8
- * 4. 根据组合判断去重,避免扩展组合
5
+ * 1. 第一项始终是精确应付金额
6
+ * 2. 其余推荐只能是严格大于应付金额的真实币种面额
7
+ * 3. 最多推荐三个更高面额
9
8
  */
10
9
  /**
11
10
  * 常见国家货币面额配置
@@ -13,7 +12,7 @@
13
12
  export declare const CURRENCY_DENOMINATIONS: Record<string, number[]>;
14
13
  /**
15
14
  * 最优支付金额推荐算法
16
- * 推荐通过不同数量面额组合刚好足够支付的最小金额
15
+ * 返回精确应付金额,以及最多三个更高的真实币种面额
17
16
  *
18
17
  * @param targetAmount 目标金额
19
18
  * @param denominations 币种面值数组
@@ -8,10 +8,9 @@ function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len
8
8
  * 现金支付推荐算法
9
9
  *
10
10
  * 核心原理:
11
- * 1. 每个推荐金额都应该是独立的最优组合
12
- * 2. 不推荐在已有最优解基础上添加额外面额的组合
13
- * 3. 优先推荐使用不同数量币种的组合
14
- * 4. 根据组合判断去重,避免扩展组合
11
+ * 1. 第一项始终是精确应付金额
12
+ * 2. 其余推荐只能是严格大于应付金额的真实币种面额
13
+ * 3. 最多推荐三个更高面额
15
14
  */
16
15
 
17
16
  /**
@@ -47,377 +46,28 @@ export var CURRENCY_DENOMINATIONS = {
47
46
  'DEFAULT': [100, 50, 20, 10, 5, 1, 0.5, 0.25, 0.1, 0.05, 0.01]
48
47
  };
49
48
 
50
- /**
51
- * 支付组合类型定义
52
- */
53
-
54
49
  /**
55
50
  * 最优支付金额推荐算法
56
- * 推荐通过不同数量面额组合刚好足够支付的最小金额
51
+ * 返回精确应付金额,以及最多三个更高的真实币种面额
57
52
  *
58
53
  * @param targetAmount 目标金额
59
54
  * @param denominations 币种面值数组
60
55
  * @returns 推荐支付金额数组
61
56
  */
62
57
  export function recommendOptimalPayments(targetAmount, denominations) {
63
- // 参数验证
64
- if (targetAmount <= 0 || !isFinite(targetAmount) || isNaN(targetAmount)) {
58
+ if (targetAmount <= 0 || !Number.isFinite(targetAmount)) {
65
59
  return [];
66
60
  }
67
- if (!denominations || !Array.isArray(denominations) || denominations.length === 0) {
68
- return [Math.ceil(targetAmount)];
69
- }
70
-
71
- // 过滤有效面额并限制搜索空间以提高性能
72
- var maxReasonableDenom = targetAmount * 5; // 适度限制搜索空间
73
- var validDenoms = denominations.filter(function (denom) {
74
- return denom > 0 && isFinite(denom) && !isNaN(denom) && denom <= maxReasonableDenom;
75
- }).slice(0, 8); // 限制面额数量以提高性能
76
-
77
- if (validDenoms.length === 0) {
78
- // 如果没有合适的面额,返回最接近的整数金额
79
- return [Math.ceil(targetAmount)];
80
- }
81
- try {
82
- // 处理精度问题,将金额转换为整数计算
83
- var precision = 100; // 假设最小单位是0.01
84
- var target = Math.round(targetAmount * precision);
85
- var denoms = validDenoms.map(function (d) {
86
- return Math.round(d * precision);
87
- }).sort(function (a, b) {
88
- return b - a;
89
- });
90
-
91
- // 存储不同的支付组合方案
92
- var paymentOptions = [];
93
-
94
- // 首先尝试找到精确匹配的组合
95
- var exactCombination = findExactCombination(target, denoms, precision);
96
- if (exactCombination) {
97
- paymentOptions.push(exactCombination);
98
- }
99
-
100
- // 为了提高效率,我们将按币种数量来生成组合
101
- // 1币种组合:只用一种面额
102
- for (var i = 0; i < Math.min(denoms.length, 6); i++) {
103
- var denom = denoms[i];
104
- var count = Math.ceil(target / denom);
105
- var sum = count * denom;
106
- if (sum >= target && count <= 12) {
107
- // 限制硬币数量以提高性能
108
- paymentOptions.push({
109
- combination: Array(count).fill(denom / precision),
110
- sum: sum / precision,
111
- coinCount: count,
112
- denomTypes: 1
113
- });
114
- }
115
- }
116
-
117
- // 2币种组合:使用两种面额(大幅限制搜索范围以提高性能)
118
- for (var _i = 0; _i < Math.min(denoms.length - 1, 5); _i++) {
119
- for (var j = _i + 1; j < Math.min(denoms.length, 6); j++) {
120
- var denom1 = denoms[_i];
121
- var denom2 = denoms[j];
122
-
123
- // 大幅限制搜索范围,基于目标金额动态调整
124
- var maxCount1 = Math.min(Math.ceil(target / denom1) + 1, 8);
125
- var maxCount2 = Math.min(Math.ceil(target / denom2) + 1, 10);
126
- for (var count1 = 1; count1 <= maxCount1; count1++) {
127
- for (var count2 = 1; count2 <= maxCount2; count2++) {
128
- var _sum = count1 * denom1 + count2 * denom2;
129
- if (_sum >= target) {
130
- var combination = [].concat(_toConsumableArray(Array(count1).fill(denom1 / precision)), _toConsumableArray(Array(count2).fill(denom2 / precision)));
131
- paymentOptions.push({
132
- combination: combination,
133
- sum: _sum / precision,
134
- coinCount: count1 + count2,
135
- denomTypes: 2
136
- });
137
- }
138
- }
139
- }
140
- }
141
- }
142
-
143
- // 3币种组合:使用三种面额(大幅限制搜索以提高效率)
144
- for (var _i2 = 0; _i2 < Math.min(denoms.length - 2, 3); _i2++) {
145
- for (var _j = _i2 + 1; _j < Math.min(denoms.length - 1, 4); _j++) {
146
- for (var k = _j + 1; k < Math.min(denoms.length, 5); k++) {
147
- var _denom = denoms[_i2];
148
- var _denom2 = denoms[_j];
149
- var denom3 = denoms[k];
150
-
151
- // 大幅限制搜索范围
152
- var _maxCount = Math.min(Math.ceil(target / _denom) + 1, 5);
153
- var _maxCount2 = Math.min(Math.ceil(target / _denom2) + 1, 6);
154
- var maxCount3 = Math.min(Math.ceil(target / denom3) + 1, 8);
155
- for (var _count = 1; _count <= _maxCount; _count++) {
156
- for (var _count2 = 1; _count2 <= _maxCount2; _count2++) {
157
- for (var count3 = 1; count3 <= maxCount3; count3++) {
158
- var _sum2 = _count * _denom + _count2 * _denom2 + count3 * denom3;
159
- if (_sum2 >= target) {
160
- var _combination = [].concat(_toConsumableArray(Array(_count).fill(_denom / precision)), _toConsumableArray(Array(_count2).fill(_denom2 / precision)), _toConsumableArray(Array(count3).fill(denom3 / precision)));
161
- paymentOptions.push({
162
- combination: _combination,
163
- sum: _sum2 / precision,
164
- coinCount: _count + _count2 + count3,
165
- denomTypes: 3
166
- });
167
- }
168
- }
169
- }
170
- }
171
- }
172
- }
173
- }
174
-
175
- // 4币种组合:使用四种面额(极度限制搜索以提高性能)
176
- for (var _i3 = 0; _i3 < Math.min(denoms.length - 3, 2); _i3++) {
177
- for (var _j2 = _i3 + 1; _j2 < Math.min(denoms.length - 2, 3); _j2++) {
178
- for (var _k = _j2 + 1; _k < Math.min(denoms.length - 1, 4); _k++) {
179
- for (var l = _k + 1; l < Math.min(denoms.length, 5); l++) {
180
- var _denom3 = denoms[_i3];
181
- var _denom4 = denoms[_j2];
182
- var _denom5 = denoms[_k];
183
- var denom4 = denoms[l];
184
-
185
- // 极度限制搜索范围
186
- var _maxCount3 = Math.min(Math.ceil(target / _denom3) + 1, 3);
187
- var _maxCount4 = Math.min(Math.ceil(target / _denom4) + 1, 4);
188
- var _maxCount5 = Math.min(Math.ceil(target / _denom5) + 1, 5);
189
- var maxCount4 = Math.min(Math.ceil(target / denom4) + 1, 6);
190
- for (var _count3 = 1; _count3 <= _maxCount3; _count3++) {
191
- for (var _count4 = 1; _count4 <= _maxCount4; _count4++) {
192
- for (var _count5 = 1; _count5 <= _maxCount5; _count5++) {
193
- for (var count4 = 1; count4 <= maxCount4; count4++) {
194
- var _sum3 = _count3 * _denom3 + _count4 * _denom4 + _count5 * _denom5 + count4 * denom4;
195
- if (_sum3 >= target) {
196
- var _combination2 = [].concat(_toConsumableArray(Array(_count3).fill(_denom3 / precision)), _toConsumableArray(Array(_count4).fill(_denom4 / precision)), _toConsumableArray(Array(_count5).fill(_denom5 / precision)), _toConsumableArray(Array(count4).fill(denom4 / precision)));
197
- paymentOptions.push({
198
- combination: _combination2,
199
- sum: _sum3 / precision,
200
- coinCount: _count3 + _count4 + _count5 + count4,
201
- denomTypes: 4
202
- });
203
- }
204
- }
205
- }
206
- }
207
- }
208
- }
209
- }
210
- }
211
- }
212
-
213
- // 如果没有找到任何组合,提供默认建议
214
- if (paymentOptions.length === 0) {
215
- // 找到大于等于目标金额的最小面额
216
- var minValidDenom = validDenoms.find(function (denom) {
217
- return denom >= targetAmount;
218
- });
219
- if (minValidDenom) {
220
- return [minValidDenom];
221
- } else {
222
- // 使用最大面额的组合
223
- var maxDenom = Math.max.apply(Math, _toConsumableArray(validDenoms));
224
- var _count6 = Math.ceil(targetAmount / maxDenom);
225
- return [_count6 * maxDenom];
226
- }
227
- }
228
-
229
- // 移除重复和扩展组合
230
- var uniqueCombinations = removeDuplicateAndExtendedCombinations(paymentOptions, targetAmount);
231
-
232
- // 按币种类型数量排序,然后按总硬币数量排序,最后按金额排序
233
- uniqueCombinations.sort(function (a, b) {
234
- if (a.denomTypes !== b.denomTypes) {
235
- return a.denomTypes - b.denomTypes;
236
- }
237
- if (a.coinCount !== b.coinCount) {
238
- return a.coinCount - b.coinCount;
239
- }
240
- return a.sum - b.sum;
241
- });
242
-
243
- // 按金额去重并返回推荐的支付金额
244
- var uniqueAmounts = new Set();
245
- var finalResults = [];
246
- for (var _i4 = 0, _uniqueCombinations = uniqueCombinations; _i4 < _uniqueCombinations.length; _i4++) {
247
- var item = _uniqueCombinations[_i4];
248
- var roundedAmount = Math.round(item.sum * 100) / 100; // 处理浮点数精度
249
- if (!uniqueAmounts.has(roundedAmount) && finalResults.length < 10) {
250
- uniqueAmounts.add(roundedAmount);
251
- finalResults.push(roundedAmount);
252
- }
253
- }
254
- return finalResults.sort(function (a, b) {
255
- return a - b;
256
- });
257
- } catch (error) {
258
- // 发生错误时返回安全的默认值
259
- console.warn('推荐支付金额计算出错:', error);
260
- // 返回最接近的整数金额作为兜底
261
- var safeAmount = Math.ceil(targetAmount);
262
- return [targetAmount, safeAmount];
263
- }
264
- }
265
-
266
- /**
267
- * 尝试找到精确匹配目标金额的组合
268
- */
269
- function findExactCombination(target, denoms, precision) {
270
- // 简单的深度优先搜索,限制搜索深度
271
- function dfs(remaining, denomIndex, currentCombination) {
272
- if (remaining === 0) {
273
- return currentCombination;
274
- }
275
- if (remaining < 0 || denomIndex >= denoms.length || currentCombination.length > 10) {
276
- return null;
277
- }
278
- var denom = denoms[denomIndex];
279
-
280
- // 尝试使用当前面额 0 到 maxCount 次
281
- var maxCount = Math.min(Math.floor(remaining / denom), 8);
282
- for (var count = maxCount; count >= 0; count--) {
283
- var newCombination = [].concat(_toConsumableArray(currentCombination), _toConsumableArray(Array(count).fill(denom)));
284
- var result = dfs(remaining - count * denom, denomIndex + 1, newCombination);
285
- if (result) {
286
- return result;
287
- }
288
- }
289
- return null;
290
- }
291
- var exactMatch = dfs(target, 0, []);
292
- if (exactMatch && exactMatch.length > 0) {
293
- var denomTypes = new Set(exactMatch).size;
294
- return {
295
- combination: exactMatch.map(function (d) {
296
- return d / precision;
297
- }),
298
- sum: target / precision,
299
- coinCount: exactMatch.length,
300
- denomTypes: denomTypes
301
- };
302
- }
303
- return null;
304
- }
305
-
306
- /**
307
- * 移除重复和扩展组合
308
- * 核心原理:如果组合A包含组合B的所有硬币,并且还有额外硬币,则A是B的扩展,应该被移除
309
- */
310
- function removeDuplicateAndExtendedCombinations(combinations, targetAmount) {
311
- var result = [];
312
-
313
- // 按币种类型数量、硬币数量、金额排序,确保较简单的组合在前面
314
- combinations.sort(function (a, b) {
315
- if (a.denomTypes !== b.denomTypes) {
316
- return a.denomTypes - b.denomTypes;
317
- }
318
- if (a.coinCount !== b.coinCount) {
319
- return a.coinCount - b.coinCount;
320
- }
321
- return a.sum - b.sum;
322
- });
323
- for (var i = 0; i < combinations.length; i++) {
324
- var current = combinations[i];
325
- var shouldSkip = false;
326
-
327
- // 检查当前组合是否与已添加的组合重复或者是扩展
328
- for (var j = 0; j < result.length; j++) {
329
- var existing = result[j];
330
-
331
- // 如果完全相同,跳过
332
- if (isSameCombination(current.combination, existing.combination)) {
333
- shouldSkip = true;
334
- break;
335
- }
336
-
337
- // 如果当前组合是已存在组合的扩展,跳过
338
- if (isExtensionOf(current.combination, existing.combination)) {
339
- shouldSkip = true;
340
- break;
341
- }
342
- }
343
-
344
- // 检查是否有其他组合是当前组合的扩展,如果有,移除那些扩展
345
- if (!shouldSkip) {
346
- // 移除所有当前组合的扩展
347
- for (var _j3 = result.length - 1; _j3 >= 0; _j3--) {
348
- if (isExtensionOf(result[_j3].combination, current.combination)) {
349
- result.splice(_j3, 1);
350
- }
351
- }
352
- result.push(current);
353
- }
354
- }
355
- return result;
356
- }
357
-
358
- /**
359
- * 检查组合A是否是组合B的扩展(即A包含B的所有硬币,并且还有额外的硬币)
360
- */
361
- function isExtensionOf(combinationA, combinationB) {
362
- // 如果A的硬币数量少于等于B,A不可能是B的扩展
363
- if (combinationA.length <= combinationB.length) {
364
- return false;
365
- }
366
-
367
- // 统计每个面额的数量
368
- var countA = {};
369
- var countB = {};
370
- combinationA.forEach(function (coin) {
371
- countA[coin] = (countA[coin] || 0) + 1;
372
- });
373
- combinationB.forEach(function (coin) {
374
- countB[coin] = (countB[coin] || 0) + 1;
375
- });
376
-
377
- // 检查B的所有面额在A中是否都有足够的数量
378
- for (var coin in countB) {
379
- if (!countA[coin] || countA[coin] < countB[coin]) {
380
- return false;
381
- }
382
- }
383
-
384
- // 检查A是否有B没有的额外硬币
385
- var hasExtra = false;
386
- for (var _coin in countA) {
387
- if (countA[_coin] > (countB[_coin] || 0)) {
388
- hasExtra = true;
389
- break;
390
- }
391
- }
392
- return hasExtra;
393
- }
394
-
395
- /**
396
- * 检查两个组合是否本质相同(相同的面额组合)
397
- */
398
- function isSameCombination(combinationA, combinationB) {
399
- if (combinationA.length !== combinationB.length) {
400
- return false;
401
- }
402
- var countA = {};
403
- var countB = {};
404
- combinationA.forEach(function (coin) {
405
- countA[coin] = (countA[coin] || 0) + 1;
406
- });
407
- combinationB.forEach(function (coin) {
408
- countB[coin] = (countB[coin] || 0) + 1;
409
- });
410
-
411
- // 检查两个组合是否有相同的面额和数量
412
- for (var coin in countA) {
413
- if (countA[coin] !== (countB[coin] || 0)) {
414
- return false;
415
- }
416
- }
417
- for (var _coin2 in countB) {
418
- if (countB[_coin2] !== (countA[_coin2] || 0)) {
419
- return false;
420
- }
421
- }
422
- return true;
61
+ var precision = 100;
62
+ var normalizedTarget = Math.round(targetAmount * precision) / precision;
63
+ var higherDenominations = _toConsumableArray(new Set((Array.isArray(denominations) ? denominations : []).filter(function (denomination) {
64
+ return denomination > 0 && Number.isFinite(denomination);
65
+ }).map(function (denomination) {
66
+ return Math.round(denomination * precision) / precision;
67
+ }))).filter(function (denomination) {
68
+ return denomination > normalizedTarget;
69
+ }).sort(function (a, b) {
70
+ return a - b;
71
+ }).slice(0, 3);
72
+ return [normalizedTarget].concat(_toConsumableArray(higherDenominations));
423
73
  }