@pisell/pisellos 2.2.274 → 2.2.276

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 (117) hide show
  1. package/dist/core/index.d.ts +2 -0
  2. package/dist/core/index.js +7 -0
  3. package/dist/model/strategy/adapter/promotion/adapter.d.ts +4 -0
  4. package/dist/model/strategy/adapter/promotion/adapter.js +23 -0
  5. package/dist/model/strategy/adapter/promotion/evaluator.d.ts +17 -0
  6. package/dist/model/strategy/adapter/promotion/evaluator.js +279 -6
  7. package/dist/model/strategy/adapter/promotion/examples.d.ts +86 -0
  8. package/dist/model/strategy/adapter/promotion/examples.js +99 -0
  9. package/dist/model/strategy/adapter/promotion/index.d.ts +1 -1
  10. package/dist/model/strategy/adapter/promotion/index.js +1 -1
  11. package/dist/model/strategy/adapter/promotion/type.d.ts +90 -2
  12. package/dist/model/strategy/adapter/promotion/type.js +45 -0
  13. package/dist/modules/OpenData/index.d.ts +2 -0
  14. package/dist/modules/OpenData/index.js +8 -0
  15. package/dist/modules/Order/index.d.ts +28 -8
  16. package/dist/modules/Order/index.js +907 -588
  17. package/dist/modules/Order/payment-utils.d.ts +26 -0
  18. package/dist/modules/Order/payment-utils.js +225 -0
  19. package/dist/modules/Order/types.d.ts +35 -1
  20. package/dist/modules/Order/utils/orderCollectionIdentity.js +7 -2
  21. package/dist/modules/Order/utils.js +10 -6
  22. package/dist/modules/Payment/index.d.ts +2 -0
  23. package/dist/modules/Payment/index.js +78 -49
  24. package/dist/modules/Payment/types.d.ts +104 -25
  25. package/dist/modules/Payment/types.js +17 -0
  26. package/dist/modules/Payment/walletpass.d.ts +38 -1
  27. package/dist/modules/Payment/walletpass.js +917 -114
  28. package/dist/modules/Rules/index.d.ts +3 -0
  29. package/dist/modules/Rules/index.js +100 -70
  30. package/dist/server/index.d.ts +25 -0
  31. package/dist/server/index.js +1993 -1061
  32. package/dist/server/modules/index.d.ts +1 -1
  33. package/dist/server/modules/order/index.d.ts +70 -4
  34. package/dist/server/modules/order/index.js +1816 -728
  35. package/dist/server/modules/order/types.d.ts +25 -3
  36. package/dist/server/modules/order/types.js +7 -1
  37. package/dist/solution/BaseSales/index.d.ts +104 -5
  38. package/dist/solution/BaseSales/index.js +1767 -436
  39. package/dist/solution/BaseSales/types.d.ts +55 -1
  40. package/dist/solution/BaseSales/types.js +2 -1
  41. package/dist/solution/BaseSales/utils/cartPromotion.d.ts +3 -1
  42. package/dist/solution/BaseSales/utils/cartPromotion.js +94 -32
  43. package/dist/solution/BaseSales/utils/parseSalesResponse.d.ts +6 -1
  44. package/dist/solution/BaseSales/utils/transformBaseProductToOrderProduct.js +7 -4
  45. package/dist/solution/BookingByStep/index.d.ts +1 -1
  46. package/dist/solution/BookingTicket/index.d.ts +21 -5
  47. package/dist/solution/BookingTicket/index.js +139 -56
  48. package/dist/solution/BookingTicket/types.d.ts +1 -0
  49. package/dist/solution/BookingTicket/utils/scan/applyGlobalScan.d.ts +8 -0
  50. package/dist/solution/BookingTicket/utils/scan/applyGlobalScan.js +173 -83
  51. package/dist/solution/Sales/index.d.ts +2 -0
  52. package/dist/solution/Sales/index.js +26 -4
  53. package/dist/solution/ScanOrder/index.js +31 -20
  54. package/dist/solution/VenueBooking/index.d.ts +1 -0
  55. package/dist/solution/VenueBooking/index.js +30 -19
  56. package/dist/types/index.d.ts +1 -0
  57. package/dist/utils/payment-number.d.ts +9 -0
  58. package/dist/utils/payment-number.js +69 -0
  59. package/lib/core/index.d.ts +2 -0
  60. package/lib/core/index.js +4 -0
  61. package/lib/model/strategy/adapter/promotion/adapter.d.ts +4 -0
  62. package/lib/model/strategy/adapter/promotion/adapter.js +20 -0
  63. package/lib/model/strategy/adapter/promotion/evaluator.d.ts +17 -0
  64. package/lib/model/strategy/adapter/promotion/evaluator.js +235 -0
  65. package/lib/model/strategy/adapter/promotion/examples.d.ts +86 -0
  66. package/lib/model/strategy/adapter/promotion/examples.js +91 -0
  67. package/lib/model/strategy/adapter/promotion/index.d.ts +1 -1
  68. package/lib/model/strategy/adapter/promotion/index.js +2 -0
  69. package/lib/model/strategy/adapter/promotion/type.d.ts +90 -2
  70. package/lib/model/strategy/adapter/promotion/type.js +2 -0
  71. package/lib/modules/OpenData/index.d.ts +2 -0
  72. package/lib/modules/OpenData/index.js +5 -0
  73. package/lib/modules/Order/index.d.ts +28 -8
  74. package/lib/modules/Order/index.js +252 -84
  75. package/lib/modules/Order/payment-utils.d.ts +26 -0
  76. package/lib/modules/Order/payment-utils.js +224 -0
  77. package/lib/modules/Order/types.d.ts +35 -1
  78. package/lib/modules/Order/utils/orderCollectionIdentity.js +3 -0
  79. package/lib/modules/Order/utils.js +11 -4
  80. package/lib/modules/Payment/index.d.ts +2 -0
  81. package/lib/modules/Payment/index.js +33 -12
  82. package/lib/modules/Payment/types.d.ts +104 -25
  83. package/lib/modules/Payment/types.js +1 -0
  84. package/lib/modules/Payment/walletpass.d.ts +38 -1
  85. package/lib/modules/Payment/walletpass.js +730 -21
  86. package/lib/modules/Rules/index.d.ts +3 -0
  87. package/lib/modules/Rules/index.js +38 -9
  88. package/lib/server/index.d.ts +25 -0
  89. package/lib/server/index.js +726 -31
  90. package/lib/server/modules/index.d.ts +1 -1
  91. package/lib/server/modules/order/index.d.ts +70 -4
  92. package/lib/server/modules/order/index.js +818 -69
  93. package/lib/server/modules/order/types.d.ts +25 -3
  94. package/lib/server/modules/order/types.js +1 -0
  95. package/lib/solution/BaseSales/index.d.ts +104 -5
  96. package/lib/solution/BaseSales/index.js +858 -27
  97. package/lib/solution/BaseSales/types.d.ts +55 -1
  98. package/lib/solution/BaseSales/types.js +2 -1
  99. package/lib/solution/BaseSales/utils/cartPromotion.d.ts +3 -1
  100. package/lib/solution/BaseSales/utils/cartPromotion.js +66 -12
  101. package/lib/solution/BaseSales/utils/parseSalesResponse.d.ts +6 -1
  102. package/lib/solution/BaseSales/utils/transformBaseProductToOrderProduct.js +4 -3
  103. package/lib/solution/BookingByStep/index.d.ts +1 -1
  104. package/lib/solution/BookingTicket/index.d.ts +21 -5
  105. package/lib/solution/BookingTicket/index.js +64 -13
  106. package/lib/solution/BookingTicket/types.d.ts +1 -0
  107. package/lib/solution/BookingTicket/utils/scan/applyGlobalScan.d.ts +8 -0
  108. package/lib/solution/BookingTicket/utils/scan/applyGlobalScan.js +37 -5
  109. package/lib/solution/Sales/index.d.ts +2 -0
  110. package/lib/solution/Sales/index.js +20 -6
  111. package/lib/solution/ScanOrder/index.js +8 -0
  112. package/lib/solution/VenueBooking/index.d.ts +1 -0
  113. package/lib/solution/VenueBooking/index.js +8 -0
  114. package/lib/types/index.d.ts +1 -0
  115. package/lib/utils/payment-number.d.ts +9 -0
  116. package/lib/utils/payment-number.js +64 -0
  117. package/package.json +1 -1
@@ -24,6 +24,85 @@ __export(walletpass_exports, {
24
24
  module.exports = __toCommonJS(walletpass_exports);
25
25
  var import_types = require("./types");
26
26
  var import_utils = require("./utils");
27
+ var WALLET_PAYMENT_ASSET_TAGS = /* @__PURE__ */ new Set([
28
+ "product_voucher",
29
+ "gift_card",
30
+ "point_card"
31
+ ]);
32
+ function isWalletPaymentAsset(asset) {
33
+ return WALLET_PAYMENT_ASSET_TAGS.has(
34
+ String((asset == null ? void 0 : asset.tag) || "").toLowerCase()
35
+ );
36
+ }
37
+ function getWalletAssetId(asset) {
38
+ return (asset == null ? void 0 : asset.id) ?? (asset == null ? void 0 : asset.voucher_id) ?? (asset == null ? void 0 : asset.code);
39
+ }
40
+ function getWalletAssetKey(assetOrId) {
41
+ const id = assetOrId && typeof assetOrId === "object" ? getWalletAssetId(assetOrId) : assetOrId;
42
+ return id === void 0 || id === null ? "" : String(id);
43
+ }
44
+ function matchesWalletAssetCode(asset, normalizedCode) {
45
+ return [asset == null ? void 0 : asset.code, asset == null ? void 0 : asset.encoded].some(
46
+ (identifier) => String(identifier ?? "").trim() === normalizedCode
47
+ );
48
+ }
49
+ function normalizeWalletAssetCode(code) {
50
+ return String(code ?? "").trim().toUpperCase();
51
+ }
52
+ function getWalletAssetCodeKeys(asset) {
53
+ return [asset == null ? void 0 : asset.code, asset == null ? void 0 : asset.encoded].map(normalizeWalletAssetCode).filter(Boolean);
54
+ }
55
+ function isWalletAssetAvailable(asset) {
56
+ if (!asset)
57
+ return false;
58
+ if (asset.unified_available_status !== void 0 && Number(asset.unified_available_status) !== 1) {
59
+ return false;
60
+ }
61
+ if (asset._unified_available_status !== void 0 && Number(asset._unified_available_status) !== 1) {
62
+ return false;
63
+ }
64
+ if (asset.available === false || asset.isDisabled === true)
65
+ return false;
66
+ return true;
67
+ }
68
+ function getWalletAssetDeductionAmount(asset) {
69
+ const value = (asset == null ? void 0 : asset.actualDeduction) ?? (asset == null ? void 0 : asset._available_max_amount) ?? (asset == null ? void 0 : asset.edit_current_amount) ?? (asset == null ? void 0 : asset.recommended_usage_amount) ?? (asset == null ? void 0 : asset.available_max_amount) ?? (asset == null ? void 0 : asset.amount) ?? 0;
70
+ const amount = Number(value);
71
+ return Number.isFinite(amount) && amount > 0 ? amount : 0;
72
+ }
73
+ function mergeWalletAssets(...groups) {
74
+ const merged = [];
75
+ const indexByKey = /* @__PURE__ */ new Map();
76
+ groups.flat().forEach((asset) => {
77
+ const key = getWalletAssetKey(asset);
78
+ if (!key)
79
+ return;
80
+ const existedIndex = indexByKey.get(key);
81
+ if (existedIndex === void 0) {
82
+ indexByKey.set(key, merged.length);
83
+ merged.push(asset);
84
+ return;
85
+ }
86
+ merged[existedIndex] = {
87
+ ...merged[existedIndex],
88
+ ...asset
89
+ };
90
+ });
91
+ return merged;
92
+ }
93
+ function createInitialWalletAssetsState() {
94
+ return {
95
+ status: "idle",
96
+ items: [],
97
+ selectedIds: [],
98
+ selectedItems: [],
99
+ committedIds: [],
100
+ selectionSources: {},
101
+ totalSelectedAmount: 0,
102
+ error: null,
103
+ revision: 0
104
+ };
105
+ }
27
106
  var WalletPassPaymentImpl = class {
28
107
  constructor(paymentModule) {
29
108
  this.paymentModule = paymentModule;
@@ -32,6 +111,24 @@ var WalletPassPaymentImpl = class {
32
111
  this.searchResults = [];
33
112
  this.walletParams = null;
34
113
  this.walletInitData = null;
114
+ this.walletAssetsState = createInitialWalletAssetsState();
115
+ this.walletAssetsRequestSequence = 0;
116
+ this.walletAssetsBehavior = {
117
+ autoSelectAfterSearch: true
118
+ };
119
+ this.pendingAutoSelectSearchCodes = /* @__PURE__ */ new Set();
120
+ this.walletAssetsCalculationContext = {
121
+ orderTotalAmount: 0,
122
+ products: []
123
+ };
124
+ }
125
+ configureWalletAssetsBehavior(options) {
126
+ if (options.autoSelectAfterSearch === void 0)
127
+ return;
128
+ this.walletAssetsBehavior.autoSelectAfterSearch = options.autoSelectAfterSearch;
129
+ if (!options.autoSelectAfterSearch) {
130
+ this.pendingAutoSelectSearchCodes.clear();
131
+ }
35
132
  }
36
133
  /**
37
134
  * 发送事件的辅助方法
@@ -48,6 +145,148 @@ var WalletPassPaymentImpl = class {
48
145
  });
49
146
  }
50
147
  }
148
+ publishWalletAssetsState(patch) {
149
+ this.walletAssetsState = {
150
+ ...this.walletAssetsState,
151
+ ...patch,
152
+ items: [...patch.items ?? this.walletAssetsState.items],
153
+ selectedIds: [
154
+ ...patch.selectedIds ?? this.walletAssetsState.selectedIds
155
+ ],
156
+ selectedItems: [
157
+ ...patch.selectedItems ?? this.walletAssetsState.selectedItems
158
+ ],
159
+ committedIds: [
160
+ ...patch.committedIds ?? this.walletAssetsState.committedIds
161
+ ],
162
+ selectionSources: {
163
+ ...patch.selectionSources ?? this.walletAssetsState.selectionSources
164
+ },
165
+ revision: this.walletAssetsState.revision + 1
166
+ };
167
+ const state = this.getWalletAssetsState();
168
+ this.emitEvent(import_types.WalletPassHooks.OnWalletAssetsStateChanged, { state });
169
+ return state;
170
+ }
171
+ recalculateWalletAssets(items, requestedIds, selectionSources) {
172
+ var _a, _b;
173
+ const committedKeys = new Set(
174
+ this.walletAssetsState.committedIds.map((id) => String(id))
175
+ );
176
+ const itemsByKey = new Map(
177
+ items.map((item) => [getWalletAssetKey(item), item])
178
+ );
179
+ const requested = requestedIds.map((id) => String(id)).filter((id) => !committedKeys.has(id)).map((id) => itemsByKey.get(id)).filter((item) => item && isWalletAssetAvailable(item));
180
+ let nextItems = items;
181
+ let selectedItems = requested;
182
+ const evaluator = (_b = (_a = this.paymentModule.window) == null ? void 0 : _a.getWalletPassEvaluator) == null ? void 0 : _b.call(_a);
183
+ if (evaluator == null ? void 0 : evaluator.recalculateVouchers) {
184
+ try {
185
+ const result = evaluator.recalculateVouchers(
186
+ items,
187
+ requested,
188
+ this.walletAssetsCalculationContext.orderTotalAmount,
189
+ this.walletAssetsCalculationContext.products
190
+ );
191
+ nextItems = mergeWalletAssets(items, (result == null ? void 0 : result.allWithUpdatedStatus) || []);
192
+ selectedItems = ((result == null ? void 0 : result.selectedWithDetails) || []).filter(
193
+ (item) => isWalletAssetAvailable(item) && getWalletAssetDeductionAmount(item) > 0
194
+ );
195
+ } catch (error) {
196
+ this.paymentModule.logWarning(
197
+ "[WalletPass] 共享钱包资产重新计算失败,回退接口可用状态",
198
+ {
199
+ error: error instanceof Error ? error.message : String(error)
200
+ }
201
+ );
202
+ }
203
+ }
204
+ const selectedIds = selectedItems.map((item) => getWalletAssetId(item)).filter((id) => id !== void 0 && id !== null);
205
+ const selectedKeys = new Set(selectedIds.map((id) => String(id)));
206
+ const nextSources = Object.entries(selectionSources).reduce((result, [key, source]) => {
207
+ if (selectedKeys.has(key))
208
+ result[key] = source;
209
+ return result;
210
+ }, {});
211
+ const totalSelectedAmount = selectedItems.reduce(
212
+ (total, item) => total + getWalletAssetDeductionAmount(item),
213
+ 0
214
+ );
215
+ return this.publishWalletAssetsState({
216
+ status: "ready",
217
+ items: nextItems,
218
+ selectedIds,
219
+ selectedItems,
220
+ selectionSources: nextSources,
221
+ totalSelectedAmount,
222
+ error: null
223
+ });
224
+ }
225
+ /** 获取当前缓存中所有支付类 Wallet 资产的扫码 code。 */
226
+ getSearchedWalletAssetCodes() {
227
+ const cachedAssets = this.searchResults.filter(isWalletPaymentAsset);
228
+ const seenCodes = /* @__PURE__ */ new Set();
229
+ return cachedAssets.map((item) => String((item == null ? void 0 : item.code) || "").trim()).filter((code) => {
230
+ const normalizedCode = code.toUpperCase();
231
+ if (!normalizedCode || seenCodes.has(normalizedCode))
232
+ return false;
233
+ seenCodes.add(normalizedCode);
234
+ return true;
235
+ });
236
+ }
237
+ /**
238
+ * 用订单重算接口返回的数据更新扫码 Wallet 缓存。
239
+ * 接口暂时缺失某个扫码 code 时继续保留旧值,避免列表闪空。
240
+ */
241
+ syncSearchedWalletAssets(refreshedCandidates) {
242
+ const cachedAssets = this.searchResults.filter(isWalletPaymentAsset);
243
+ const searchedCodeKeys = new Set(
244
+ this.getSearchedWalletAssetCodes().map((code) => code.toUpperCase())
245
+ );
246
+ if (searchedCodeKeys.size === 0)
247
+ return cachedAssets;
248
+ const refreshedAssets = refreshedCandidates.filter(
249
+ (item) => isWalletPaymentAsset(item) && searchedCodeKeys.has(
250
+ String((item == null ? void 0 : item.code) || "").trim().toUpperCase()
251
+ )
252
+ );
253
+ if (refreshedAssets.length > 0) {
254
+ const replacedCodes = new Set(
255
+ refreshedAssets.map(
256
+ (item) => String((item == null ? void 0 : item.code) || "").trim().toUpperCase()
257
+ )
258
+ );
259
+ this.searchResults = [
260
+ ...this.searchResults.filter(
261
+ (item) => !replacedCodes.has(String((item == null ? void 0 : item.code) || "").trim().toUpperCase())
262
+ ),
263
+ ...refreshedAssets
264
+ ];
265
+ }
266
+ return mergeWalletAssets(
267
+ cachedAssets,
268
+ refreshedAssets
269
+ );
270
+ }
271
+ isPendingAutoSelectAsset(asset) {
272
+ return getWalletAssetCodeKeys(asset).some(
273
+ (code) => this.pendingAutoSelectSearchCodes.has(code)
274
+ );
275
+ }
276
+ clearPendingAutoSelectForAsset(asset) {
277
+ getWalletAssetCodeKeys(asset).forEach((code) => {
278
+ this.pendingAutoSelectSearchCodes.delete(code);
279
+ });
280
+ }
281
+ queuePendingAutoSelectForAssets(assets) {
282
+ if (!this.walletAssetsBehavior.autoSelectAfterSearch)
283
+ return;
284
+ assets.forEach((asset) => {
285
+ getWalletAssetCodeKeys(asset).forEach((code) => {
286
+ this.pendingAutoSelectSearchCodes.add(code);
287
+ });
288
+ });
289
+ }
51
290
  /**
52
291
  * 生成钱包API默认参数
53
292
  * 根据业务数据生成标准的钱包API参数,并存储在模块中
@@ -75,7 +314,7 @@ var WalletPassPaymentImpl = class {
75
314
  // 订单小计金额
76
315
  order_product_amount: subTotal,
77
316
  // 订单待支付金额
78
- order_wait_pay_amount: order_wait_pay_amount || ((amountInfo == null ? void 0 : amountInfo.isDeposit) ? Number(amountInfo == null ? void 0 : amountInfo.depositAmount) : totalAmount),
317
+ order_wait_pay_amount: order_wait_pay_amount ?? ((amountInfo == null ? void 0 : amountInfo.isDeposit) ? Number(amountInfo == null ? void 0 : amountInfo.depositAmount) : totalAmount),
79
318
  // order_behavior_count_customer_id: 1,
80
319
  products,
81
320
  prepare_payments: [],
@@ -255,6 +494,96 @@ var WalletPassPaymentImpl = class {
255
494
  throw error;
256
495
  }
257
496
  }
497
+ /**
498
+ * 商品或订单金额变化后,使用聚合接口一次重算已选与剩余 WalletPass。
499
+ * Walk-In 不发送 customer_id;会员才携带真实 customer_id。
500
+ */
501
+ async aggregateRecalculateWalletAssetsAsync(businessData, selectedIds) {
502
+ var _a, _b;
503
+ const walletParams = this.generateWalletParams(businessData);
504
+ const customerId = Number(walletParams.customer_id || 0);
505
+ const requestParams = {
506
+ ids: selectedIds.map((id) => String(id)),
507
+ exact_codes: this.getSearchedWalletAssetCodes(),
508
+ order_product_amount: walletParams.order_product_amount,
509
+ order_expect_amount: walletParams.order_expect_amount,
510
+ order_wait_pay_amount: walletParams.order_wait_pay_amount,
511
+ products: walletParams.products,
512
+ available: 2,
513
+ filter_prepare_wallet_pass: 1,
514
+ sale_channel: walletParams.sale_channel || "pos"
515
+ };
516
+ if (customerId > 1) {
517
+ requestParams.customer_id = customerId;
518
+ }
519
+ const response = await this.paymentModule.request.post(
520
+ "/machinecode/prepare/deduction/aggregate-recalculate",
521
+ requestParams
522
+ );
523
+ const responseData = response == null ? void 0 : response.data;
524
+ const hasAggregateLists = responseData && (Object.prototype.hasOwnProperty.call(
525
+ responseData,
526
+ "recalculate_list"
527
+ ) || Object.prototype.hasOwnProperty.call(
528
+ responseData,
529
+ "customer_wallet_pass_list"
530
+ ));
531
+ if (!hasAggregateLists) {
532
+ throw new Error("WalletPass aggregate response is missing list data");
533
+ }
534
+ const recalculateList = Array.isArray(responseData == null ? void 0 : responseData.recalculate_list) ? responseData.recalculate_list : [];
535
+ const customerWalletPassList = Array.isArray(
536
+ responseData == null ? void 0 : responseData.customer_wallet_pass_list
537
+ ) ? responseData.customer_wallet_pass_list : [];
538
+ const allList = mergeWalletAssets(
539
+ recalculateList,
540
+ customerWalletPassList
541
+ );
542
+ const walletPassEvaluator = (_b = (_a = this.paymentModule.window) == null ? void 0 : _a.getWalletPassEvaluator) == null ? void 0 : _b.call(_a);
543
+ const evaluated = (walletPassEvaluator == null ? void 0 : walletPassEvaluator.getRecommendedVouchers) ? walletPassEvaluator.getRecommendedVouchers({
544
+ orderTotalAmount: walletParams.order_wait_pay_amount,
545
+ products: walletParams.products,
546
+ vouchers: allList
547
+ }) : {
548
+ recommended: [],
549
+ transformList: allList.filter(isWalletAssetAvailable),
550
+ noApplicableVoucher: allList.filter(
551
+ (item) => !isWalletAssetAvailable(item)
552
+ )
553
+ };
554
+ const returnedSelectedKeys = new Set(
555
+ recalculateList.filter(isWalletAssetAvailable).map((item) => getWalletAssetKey(item))
556
+ );
557
+ const authoritativeSelectedIds = selectedIds.filter(
558
+ (id) => returnedSelectedKeys.has(String(id))
559
+ );
560
+ this.walletRecommendList = evaluated.recommended || [];
561
+ this.userIdentificationCodes = allList;
562
+ this.emitEvent(import_types.WalletPassHooks.OnUserIdentificationCodesUpdated, {
563
+ userIdentificationCodes: this.userIdentificationCodes
564
+ });
565
+ this.walletInitData = {
566
+ transformList: evaluated.transformList || [],
567
+ noApplicableVoucher: evaluated.noApplicableVoucher || [],
568
+ products: walletParams.products || []
569
+ };
570
+ this.paymentModule.logInfo(
571
+ "[WalletPass] 聚合重算 WalletPass 完成",
572
+ {
573
+ selected_count: recalculateList.length,
574
+ remaining_count: customerWalletPassList.length,
575
+ searched_codes_count: requestParams.exact_codes.length
576
+ }
577
+ );
578
+ return {
579
+ walletRecommendList: this.walletRecommendList,
580
+ userIdentificationCodes: this.userIdentificationCodes,
581
+ transformList: evaluated.transformList || [],
582
+ noApplicableVoucher: evaluated.noApplicableVoucher || [],
583
+ products: walletParams.products || [],
584
+ authoritativeSelectedIds
585
+ };
586
+ }
258
587
  async getWalletPassRecommendListAsync(params) {
259
588
  var _a;
260
589
  try {
@@ -356,7 +685,6 @@ var WalletPassPaymentImpl = class {
356
685
  * 特殊逻辑:当识别码长度为9位且前3位为"000"时,调用 /wallet/detail/search 接口
357
686
  */
358
687
  async searchIdentificationCodeAsync(params, config = {}) {
359
- var _a;
360
688
  try {
361
689
  const { code } = params;
362
690
  this.paymentModule.logInfo("[WalletPass] 开始搜索识别码", {
@@ -394,14 +722,14 @@ var WalletPassPaymentImpl = class {
394
722
  const baseWalletParams = this.walletParams;
395
723
  const searchParams = {
396
724
  // 基础钱包参数
397
- sale_channel: params.sale_channel || (baseWalletParams == null ? void 0 : baseWalletParams.sale_channel) || "pos",
398
- customer_id: params.customer_id || (baseWalletParams == null ? void 0 : baseWalletParams.customer_id),
399
- order_expect_amount: params.order_expect_amount || (baseWalletParams == null ? void 0 : baseWalletParams.order_expect_amount),
400
- order_product_amount: params.order_product_amount || (baseWalletParams == null ? void 0 : baseWalletParams.order_product_amount),
401
- order_wait_pay_amount: params.order_wait_pay_amount || (baseWalletParams == null ? void 0 : baseWalletParams.order_wait_pay_amount),
402
- order_behavior_count_customer_id: params.order_behavior_count_customer_id || (baseWalletParams == null ? void 0 : baseWalletParams.order_behavior_count_customer_id) || params.customer_id || (baseWalletParams == null ? void 0 : baseWalletParams.customer_id),
403
- products: params.products || (baseWalletParams == null ? void 0 : baseWalletParams.products),
404
- prepare_payments: params.prepare_payments || (baseWalletParams == null ? void 0 : baseWalletParams.prepare_payments),
725
+ sale_channel: params.sale_channel ?? (baseWalletParams == null ? void 0 : baseWalletParams.sale_channel) ?? "pos",
726
+ customer_id: params.customer_id ?? (baseWalletParams == null ? void 0 : baseWalletParams.customer_id),
727
+ order_expect_amount: params.order_expect_amount ?? (baseWalletParams == null ? void 0 : baseWalletParams.order_expect_amount),
728
+ order_product_amount: params.order_product_amount ?? (baseWalletParams == null ? void 0 : baseWalletParams.order_product_amount),
729
+ order_wait_pay_amount: params.order_wait_pay_amount ?? (baseWalletParams == null ? void 0 : baseWalletParams.order_wait_pay_amount),
730
+ order_behavior_count_customer_id: params.order_behavior_count_customer_id ?? (baseWalletParams == null ? void 0 : baseWalletParams.order_behavior_count_customer_id) ?? params.customer_id ?? (baseWalletParams == null ? void 0 : baseWalletParams.customer_id),
731
+ products: params.products ?? (baseWalletParams == null ? void 0 : baseWalletParams.products) ?? [],
732
+ prepare_payments: params.prepare_payments ?? (baseWalletParams == null ? void 0 : baseWalletParams.prepare_payments) ?? [],
405
733
  multiple: 1,
406
734
  // 搜索特有参数
407
735
  code: params.code,
@@ -417,17 +745,10 @@ var WalletPassPaymentImpl = class {
417
745
  if (typeof searchParams.payment_order_id === "string" && searchParams.payment_order_id.startsWith("LOCAL_")) {
418
746
  searchParams.payment_order_id = void 0;
419
747
  }
420
- let response;
421
- if (!searchParams.products || ((_a = searchParams.products || []) == null ? void 0 : _a.length) === 0) {
422
- response = {
423
- data: []
424
- };
425
- } else {
426
- response = await this.paymentModule.request.post(
427
- "/machinecode/prepare/deduction/search",
428
- searchParams
429
- );
430
- }
748
+ const response = await this.paymentModule.request.post(
749
+ "/machinecode/prepare/deduction/search",
750
+ searchParams
751
+ );
431
752
  const searchResults = (response == null ? void 0 : response.data) || [];
432
753
  if (config.noCache) {
433
754
  return {
@@ -469,6 +790,385 @@ var WalletPassPaymentImpl = class {
469
790
  throw error;
470
791
  }
471
792
  }
793
+ /**
794
+ * 刷新购物车与结账共享的钱包资产。
795
+ *
796
+ * 与 legacy initializeWalletDataFromBusinessAsync 不同,这里维护选择状态;
797
+ * 推荐结果只作为可用性/金额计算输入,不会自动成为 selectedIds。
798
+ */
799
+ async refreshWalletAssetsFromBusinessAsync(businessData, options = {}) {
800
+ var _a;
801
+ const requestSequence = ++this.walletAssetsRequestSequence;
802
+ const nextCustomerId = businessData.customer_id;
803
+ const previousCustomerId = this.walletAssetsState.customerId;
804
+ const customerChanged = String(previousCustomerId ?? "") !== String(nextCustomerId ?? "");
805
+ const preserveSelection = options.preserveSelection !== false && !customerChanged;
806
+ if (customerChanged) {
807
+ this.clearSearchResults();
808
+ }
809
+ const committedKeys = new Set(
810
+ this.walletAssetsState.committedIds.map((id) => String(id))
811
+ );
812
+ const committedItems = this.walletAssetsState.items.filter(
813
+ (item) => committedKeys.has(getWalletAssetKey(item))
814
+ );
815
+ const selectedIds = preserveSelection ? [...this.walletAssetsState.selectedIds] : [];
816
+ const selectionSources = preserveSelection ? { ...this.walletAssetsState.selectionSources } : {};
817
+ const scanSelectedAssets = preserveSelection ? this.walletAssetsState.selectedItems.filter((asset) => {
818
+ const assetId = getWalletAssetId(asset);
819
+ return assetId !== void 0 && assetId !== null && selectionSources[String(assetId)] === "scan";
820
+ }) : [];
821
+ if (!((_a = businessData.products) == null ? void 0 : _a.length)) {
822
+ this.queuePendingAutoSelectForAssets(scanSelectedAssets);
823
+ this.generateWalletParams(businessData);
824
+ const restoredSearchedAssets = this.searchResults.filter(isWalletPaymentAsset).map((item) => ({
825
+ ...item,
826
+ actualDeduction: 0,
827
+ deductionDetails: [],
828
+ _available_max_amount: 0,
829
+ _unified_available_status: 0,
830
+ _wallet_asset_recommended: false
831
+ }));
832
+ this.walletRecommendList = [];
833
+ this.userIdentificationCodes = [];
834
+ this.walletInitData = {
835
+ transformList: [],
836
+ noApplicableVoucher: restoredSearchedAssets,
837
+ products: []
838
+ };
839
+ this.walletAssetsCalculationContext = {
840
+ orderTotalAmount: 0,
841
+ products: []
842
+ };
843
+ this.emitEvent(import_types.WalletPassHooks.OnUserIdentificationCodesUpdated, {
844
+ userIdentificationCodes: []
845
+ });
846
+ return this.publishWalletAssetsState({
847
+ status: "ready",
848
+ customerId: nextCustomerId,
849
+ items: mergeWalletAssets(
850
+ restoredSearchedAssets,
851
+ committedItems
852
+ ),
853
+ selectedIds: [],
854
+ selectedItems: [],
855
+ selectionSources: {},
856
+ totalSelectedAmount: 0,
857
+ error: null
858
+ });
859
+ }
860
+ this.publishWalletAssetsState({
861
+ status: "loading",
862
+ customerId: nextCustomerId,
863
+ items: preserveSelection ? this.walletAssetsState.items : committedItems,
864
+ selectedIds,
865
+ selectedItems: preserveSelection ? this.walletAssetsState.selectedItems : [],
866
+ selectionSources,
867
+ totalSelectedAmount: preserveSelection ? this.walletAssetsState.totalSelectedAmount : 0,
868
+ error: null
869
+ });
870
+ try {
871
+ const searchedWalletAssetCodes = preserveSelection ? this.getSearchedWalletAssetCodes() : [];
872
+ const useAggregateRecalculate = preserveSelection && (selectedIds.length > 0 || searchedWalletAssetCodes.length > 0);
873
+ const aggregateResult = useAggregateRecalculate ? await this.aggregateRecalculateWalletAssetsAsync(
874
+ businessData,
875
+ selectedIds
876
+ ) : null;
877
+ const result = aggregateResult || await this.initializeWalletDataFromBusinessAsync(businessData);
878
+ if (requestSequence !== this.walletAssetsRequestSequence) {
879
+ return this.getWalletAssetsState();
880
+ }
881
+ this.walletAssetsCalculationContext = {
882
+ orderTotalAmount: Number(businessData.order_wait_pay_amount || 0),
883
+ products: result.products || businessData.products || []
884
+ };
885
+ const authoritativeSelectedIds = (aggregateResult == null ? void 0 : aggregateResult.authoritativeSelectedIds) || selectedIds;
886
+ if (aggregateResult) {
887
+ const returnedAggregateKeys = new Set(
888
+ [
889
+ ...result.transformList || [],
890
+ ...result.noApplicableVoucher || []
891
+ ].map((item) => getWalletAssetKey(item))
892
+ );
893
+ const missingSelectedKeys = new Set(
894
+ selectedIds.map((id) => String(id)).filter((id) => !returnedAggregateKeys.has(id))
895
+ );
896
+ if (missingSelectedKeys.size > 0) {
897
+ this.searchResults = this.searchResults.filter(
898
+ (item) => !missingSelectedKeys.has(getWalletAssetKey(item))
899
+ );
900
+ }
901
+ }
902
+ const searchedWalletAssets = preserveSelection ? this.syncSearchedWalletAssets([
903
+ ...result.transformList || [],
904
+ ...result.noApplicableVoucher || []
905
+ ]) : [];
906
+ if (requestSequence !== this.walletAssetsRequestSequence) {
907
+ return this.getWalletAssetsState();
908
+ }
909
+ const recommendedKeys = new Set(
910
+ (result.walletRecommendList || []).map(
911
+ (asset) => getWalletAssetKey(asset)
912
+ )
913
+ );
914
+ const items = mergeWalletAssets(
915
+ preserveSelection && !aggregateResult ? this.walletAssetsState.selectedItems : [],
916
+ searchedWalletAssets,
917
+ result.transformList || [],
918
+ result.noApplicableVoucher || []
919
+ ).map((asset) => ({
920
+ ...asset,
921
+ _wallet_asset_recommended: recommendedKeys.has(
922
+ getWalletAssetKey(asset)
923
+ )
924
+ }));
925
+ const latestSelectedIds = preserveSelection ? authoritativeSelectedIds : [];
926
+ const latestSelectionSources = preserveSelection ? { ...this.walletAssetsState.selectionSources } : {};
927
+ const pendingAutoSelectAssets = this.walletAssetsBehavior.autoSelectAfterSearch ? items.filter(
928
+ (asset) => this.isPendingAutoSelectAsset(asset) && isWalletAssetAvailable(asset)
929
+ ) : [];
930
+ const requestedKeySet = new Set(
931
+ latestSelectedIds.map((id) => String(id))
932
+ );
933
+ pendingAutoSelectAssets.forEach((asset) => {
934
+ const assetId = getWalletAssetId(asset);
935
+ if (assetId === void 0 || assetId === null)
936
+ return;
937
+ const assetKey = String(assetId);
938
+ if (!requestedKeySet.has(assetKey)) {
939
+ requestedKeySet.add(assetKey);
940
+ latestSelectedIds.push(assetId);
941
+ }
942
+ latestSelectionSources[assetKey] = "scan";
943
+ });
944
+ const nextState = this.recalculateWalletAssets(
945
+ items,
946
+ latestSelectedIds,
947
+ latestSelectionSources
948
+ );
949
+ const selectedKeySet = new Set(
950
+ nextState.selectedIds.map((id) => String(id))
951
+ );
952
+ pendingAutoSelectAssets.forEach((asset) => {
953
+ const assetId = getWalletAssetId(asset);
954
+ if (assetId !== void 0 && assetId !== null && selectedKeySet.has(String(assetId))) {
955
+ this.clearPendingAutoSelectForAsset(asset);
956
+ }
957
+ });
958
+ const droppedScanSelectedAssets = scanSelectedAssets.filter((asset) => {
959
+ const assetId = getWalletAssetId(asset);
960
+ return assetId !== void 0 && assetId !== null && !selectedKeySet.has(String(assetId));
961
+ });
962
+ this.queuePendingAutoSelectForAssets(droppedScanSelectedAssets);
963
+ return nextState;
964
+ } catch (error) {
965
+ if (requestSequence !== this.walletAssetsRequestSequence) {
966
+ return this.getWalletAssetsState();
967
+ }
968
+ this.paymentModule.logError(
969
+ "[WalletPass] 刷新共享钱包资产失败,保留刷新前状态",
970
+ error,
971
+ {
972
+ customer_id: businessData.customer_id,
973
+ selected_ids: selectedIds,
974
+ searched_codes: this.getSearchedWalletAssetCodes()
975
+ }
976
+ );
977
+ return this.publishWalletAssetsState({
978
+ status: "error",
979
+ items: preserveSelection ? this.walletAssetsState.items : committedItems,
980
+ selectedIds: preserveSelection ? this.walletAssetsState.selectedIds : [],
981
+ selectedItems: preserveSelection ? this.walletAssetsState.selectedItems : [],
982
+ selectionSources: preserveSelection ? this.walletAssetsState.selectionSources : {},
983
+ totalSelectedAmount: preserveSelection ? this.walletAssetsState.totalSelectedAmount : 0,
984
+ customerId: nextCustomerId,
985
+ error: error instanceof Error ? error.message : String(error)
986
+ });
987
+ }
988
+ }
989
+ getWalletAssetsState() {
990
+ return {
991
+ ...this.walletAssetsState,
992
+ items: [...this.walletAssetsState.items],
993
+ selectedIds: [...this.walletAssetsState.selectedIds],
994
+ selectedItems: [...this.walletAssetsState.selectedItems],
995
+ committedIds: [...this.walletAssetsState.committedIds],
996
+ selectionSources: { ...this.walletAssetsState.selectionSources }
997
+ };
998
+ }
999
+ exportWalletAssetsSnapshot() {
1000
+ return {
1001
+ state: this.getWalletAssetsState(),
1002
+ searchResults: [...this.searchResults],
1003
+ pendingAutoSelectSearchCodes: [
1004
+ ...this.pendingAutoSelectSearchCodes
1005
+ ],
1006
+ walletParams: this.walletParams ? { ...this.walletParams } : null,
1007
+ calculationContext: {
1008
+ orderTotalAmount: this.walletAssetsCalculationContext.orderTotalAmount,
1009
+ products: [...this.walletAssetsCalculationContext.products]
1010
+ }
1011
+ };
1012
+ }
1013
+ importWalletAssetsSnapshot(snapshot) {
1014
+ var _a, _b;
1015
+ if (!(snapshot == null ? void 0 : snapshot.state))
1016
+ return this.getWalletAssetsState();
1017
+ this.walletAssetsRequestSequence += 1;
1018
+ this.searchResults = Array.isArray(snapshot.searchResults) ? [...snapshot.searchResults] : [];
1019
+ this.pendingAutoSelectSearchCodes = this.walletAssetsBehavior.autoSelectAfterSearch && Array.isArray(snapshot.pendingAutoSelectSearchCodes) ? new Set(
1020
+ snapshot.pendingAutoSelectSearchCodes.map(normalizeWalletAssetCode).filter(Boolean)
1021
+ ) : /* @__PURE__ */ new Set();
1022
+ this.walletParams = snapshot.walletParams ? { ...snapshot.walletParams } : null;
1023
+ this.walletAssetsCalculationContext = {
1024
+ orderTotalAmount: Number(
1025
+ ((_a = snapshot.calculationContext) == null ? void 0 : _a.orderTotalAmount) || 0
1026
+ ),
1027
+ products: Array.isArray((_b = snapshot.calculationContext) == null ? void 0 : _b.products) ? [...snapshot.calculationContext.products] : []
1028
+ };
1029
+ return this.publishWalletAssetsState({
1030
+ ...snapshot.state,
1031
+ items: Array.isArray(snapshot.state.items) ? snapshot.state.items : [],
1032
+ selectedIds: Array.isArray(snapshot.state.selectedIds) ? snapshot.state.selectedIds : [],
1033
+ selectedItems: Array.isArray(snapshot.state.selectedItems) ? snapshot.state.selectedItems : [],
1034
+ committedIds: Array.isArray(snapshot.state.committedIds) ? snapshot.state.committedIds : [],
1035
+ selectionSources: snapshot.state.selectionSources || {}
1036
+ });
1037
+ }
1038
+ selectWalletAsset(assetId, options) {
1039
+ const assetKey = String(assetId);
1040
+ const asset = this.walletAssetsState.items.find(
1041
+ (item) => getWalletAssetKey(item) === assetKey
1042
+ );
1043
+ const committedKeys = new Set(
1044
+ this.walletAssetsState.committedIds.map((id) => String(id))
1045
+ );
1046
+ if (committedKeys.has(assetKey))
1047
+ return this.getWalletAssetsState();
1048
+ const selectedIds = this.walletAssetsState.selectedIds.filter(
1049
+ (id) => String(id) !== assetKey
1050
+ );
1051
+ const selectionSources = { ...this.walletAssetsState.selectionSources };
1052
+ delete selectionSources[assetKey];
1053
+ if (options.selected) {
1054
+ if (!asset || !isWalletAssetAvailable(asset)) {
1055
+ return this.getWalletAssetsState();
1056
+ }
1057
+ selectedIds.push(assetId);
1058
+ selectionSources[assetKey] = options.source || "manual";
1059
+ } else if (asset) {
1060
+ this.clearPendingAutoSelectForAsset(asset);
1061
+ }
1062
+ const nextState = this.recalculateWalletAssets(
1063
+ this.walletAssetsState.items,
1064
+ selectedIds,
1065
+ selectionSources
1066
+ );
1067
+ if (options.selected && asset && nextState.selectedIds.some((id) => String(id) === assetKey)) {
1068
+ this.clearPendingAutoSelectForAsset(asset);
1069
+ }
1070
+ return nextState;
1071
+ }
1072
+ async scanWalletAssetAsync(code) {
1073
+ const normalizedCode = String(code || "").trim();
1074
+ if (!normalizedCode) {
1075
+ return {
1076
+ type: "normalCode",
1077
+ selected: false,
1078
+ state: this.getWalletAssetsState()
1079
+ };
1080
+ }
1081
+ const selectedAsset = this.walletAssetsState.selectedItems.find(
1082
+ (asset2) => matchesWalletAssetCode(asset2, normalizedCode)
1083
+ );
1084
+ if (selectedAsset) {
1085
+ return {
1086
+ type: "normalCode",
1087
+ asset: selectedAsset,
1088
+ selected: true,
1089
+ state: this.getWalletAssetsState()
1090
+ };
1091
+ }
1092
+ this.walletAssetsRequestSequence += 1;
1093
+ const result = await this.searchIdentificationCodeAsync({
1094
+ ...this.walletParams || {},
1095
+ code: normalizedCode,
1096
+ prepare_payments: this.formatWalletPassList2PreparePayments(
1097
+ this.walletAssetsState.selectedItems
1098
+ )
1099
+ });
1100
+ const paymentAssets = result.data.filter(isWalletPaymentAsset);
1101
+ const asset = paymentAssets.find(
1102
+ (item) => String((item == null ? void 0 : item.code) || "") === normalizedCode
1103
+ ) || paymentAssets[0];
1104
+ if (!asset || result.type === "walletCode") {
1105
+ return {
1106
+ type: result.type,
1107
+ asset,
1108
+ selected: false,
1109
+ state: this.getWalletAssetsState()
1110
+ };
1111
+ }
1112
+ const items = mergeWalletAssets(this.walletAssetsState.items, [asset]);
1113
+ const recalculatedState = this.recalculateWalletAssets(
1114
+ items,
1115
+ this.walletAssetsState.selectedIds,
1116
+ this.walletAssetsState.selectionSources
1117
+ );
1118
+ const assetId = getWalletAssetId(asset);
1119
+ const selected = this.walletAssetsBehavior.autoSelectAfterSearch && assetId !== void 0 && isWalletAssetAvailable(asset) ? this.selectWalletAsset(assetId, {
1120
+ selected: true,
1121
+ source: "scan"
1122
+ }) : recalculatedState;
1123
+ const isSelected = assetId !== void 0 && selected.selectedIds.some((id) => String(id) === String(assetId));
1124
+ if (isSelected) {
1125
+ this.clearPendingAutoSelectForAsset(asset);
1126
+ } else if (this.walletAssetsBehavior.autoSelectAfterSearch) {
1127
+ this.pendingAutoSelectSearchCodes.add(
1128
+ normalizeWalletAssetCode(normalizedCode)
1129
+ );
1130
+ this.queuePendingAutoSelectForAssets([asset]);
1131
+ }
1132
+ return {
1133
+ type: result.type,
1134
+ asset,
1135
+ selected: isSelected,
1136
+ state: selected
1137
+ };
1138
+ }
1139
+ clearWalletAssetSelection() {
1140
+ this.pendingAutoSelectSearchCodes.clear();
1141
+ return this.recalculateWalletAssets(
1142
+ this.walletAssetsState.items,
1143
+ [],
1144
+ {}
1145
+ );
1146
+ }
1147
+ setCommittedWalletAssets(committedIds, committedAssets = []) {
1148
+ const committedKeys = new Set(committedIds.map((id) => String(id)));
1149
+ const selectedIds = this.walletAssetsState.selectedIds.filter(
1150
+ (id) => !committedKeys.has(String(id))
1151
+ );
1152
+ const selectionSources = Object.entries(
1153
+ this.walletAssetsState.selectionSources
1154
+ ).reduce(
1155
+ (result, [key, source]) => {
1156
+ if (!committedKeys.has(key))
1157
+ result[key] = source;
1158
+ return result;
1159
+ },
1160
+ {}
1161
+ );
1162
+ this.publishWalletAssetsState({
1163
+ items: mergeWalletAssets(this.walletAssetsState.items, committedAssets),
1164
+ committedIds
1165
+ });
1166
+ return this.recalculateWalletAssets(
1167
+ this.walletAssetsState.items,
1168
+ selectedIds,
1169
+ selectionSources
1170
+ );
1171
+ }
472
1172
  async processWalletPayment(amount, orderUuid, voucherId) {
473
1173
  this.paymentModule.logInfo("[WalletPass] 开始处理钱包支付", {
474
1174
  amount,
@@ -552,6 +1252,7 @@ var WalletPassPaymentImpl = class {
552
1252
  */
553
1253
  clearSearchResults() {
554
1254
  this.searchResults = [];
1255
+ this.pendingAutoSelectSearchCodes.clear();
555
1256
  this.paymentModule.logInfo("[WalletPass] 搜索结果缓存已清除");
556
1257
  }
557
1258
  /**
@@ -574,8 +1275,16 @@ var WalletPassPaymentImpl = class {
574
1275
  this.walletRecommendList = [];
575
1276
  this.userIdentificationCodes = [];
576
1277
  this.searchResults = [];
1278
+ this.pendingAutoSelectSearchCodes.clear();
577
1279
  this.walletParams = null;
578
1280
  this.walletInitData = null;
1281
+ this.walletAssetsRequestSequence += 1;
1282
+ this.walletAssetsCalculationContext = {
1283
+ orderTotalAmount: 0,
1284
+ products: []
1285
+ };
1286
+ this.walletAssetsState = createInitialWalletAssetsState();
1287
+ this.publishWalletAssetsState({});
579
1288
  this.emitEvent(import_types.WalletPassHooks.OnWalletCacheCleared, {
580
1289
  clearedTypes: ["all"]
581
1290
  });