@pisell/pisellos 2.1.59 → 2.1.60

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 (95) hide show
  1. package/dist/index.d.ts +1 -0
  2. package/dist/index.js +1 -0
  3. package/dist/model/index.d.ts +1 -0
  4. package/dist/model/index.js +1 -0
  5. package/dist/model/strategy/adapter/index.d.ts +3 -0
  6. package/dist/model/strategy/adapter/index.js +4 -0
  7. package/dist/model/strategy/adapter/type.d.ts +28 -0
  8. package/dist/model/strategy/adapter/type.js +1 -0
  9. package/dist/model/strategy/adapter/walletPass/evaluator.d.ts +84 -0
  10. package/dist/model/strategy/adapter/walletPass/evaluator.js +496 -0
  11. package/dist/model/strategy/adapter/walletPass/example.d.ts +4 -0
  12. package/dist/model/strategy/adapter/walletPass/example.js +258 -0
  13. package/dist/model/strategy/adapter/walletPass/index.d.ts +32 -0
  14. package/dist/model/strategy/adapter/walletPass/index.js +182 -0
  15. package/dist/model/strategy/adapter/walletPass/locales.d.ts +1 -0
  16. package/dist/model/strategy/adapter/walletPass/locales.js +23 -0
  17. package/dist/model/strategy/adapter/walletPass/type.d.ts +163 -0
  18. package/dist/model/strategy/adapter/walletPass/type.js +1 -0
  19. package/dist/model/strategy/adapter/walletPass/utils.d.ts +50 -0
  20. package/dist/model/strategy/adapter/walletPass/utils.js +965 -0
  21. package/dist/model/strategy/index.d.ts +94 -0
  22. package/dist/model/strategy/index.js +551 -0
  23. package/dist/model/strategy/strategy-example.d.ts +5 -0
  24. package/dist/model/strategy/strategy-example.js +331 -0
  25. package/dist/model/strategy/type.d.ts +228 -0
  26. package/dist/model/strategy/type.js +94 -0
  27. package/dist/modules/Cart/types.d.ts +2 -0
  28. package/dist/modules/Cart/utils/cartProduct.js +11 -0
  29. package/dist/modules/Discount/types.d.ts +15 -0
  30. package/dist/modules/Payment/index.d.ts +2 -1
  31. package/dist/modules/Payment/index.js +10 -7
  32. package/dist/modules/Payment/utils.js +3 -0
  33. package/dist/modules/Payment/walletpass.d.ts +23 -0
  34. package/dist/modules/Payment/walletpass.js +191 -95
  35. package/dist/modules/Rules/index.d.ts +8 -3
  36. package/dist/modules/Rules/index.js +152 -26
  37. package/dist/modules/Rules/types.d.ts +1 -0
  38. package/dist/modules/Summary/types.d.ts +2 -0
  39. package/dist/modules/Summary/utils.d.ts +6 -0
  40. package/dist/modules/Summary/utils.js +21 -0
  41. package/dist/plugins/window.d.ts +2 -0
  42. package/dist/solution/BookingByStep/index.d.ts +2 -1
  43. package/dist/solution/Checkout/index.js +2 -0
  44. package/dist/solution/ShopDiscount/index.d.ts +1 -0
  45. package/dist/solution/ShopDiscount/index.js +11 -1
  46. package/dist/solution/ShopDiscount/types.d.ts +1 -0
  47. package/dist/solution/ShopDiscount/utils.js +26 -11
  48. package/lib/index.d.ts +1 -0
  49. package/lib/index.js +3 -1
  50. package/lib/model/index.d.ts +1 -0
  51. package/lib/model/index.js +23 -0
  52. package/lib/model/strategy/adapter/index.d.ts +3 -0
  53. package/lib/model/strategy/adapter/index.js +45 -0
  54. package/lib/model/strategy/adapter/type.d.ts +28 -0
  55. package/lib/model/strategy/adapter/type.js +17 -0
  56. package/lib/model/strategy/adapter/walletPass/evaluator.d.ts +84 -0
  57. package/lib/model/strategy/adapter/walletPass/evaluator.js +420 -0
  58. package/lib/model/strategy/adapter/walletPass/example.d.ts +4 -0
  59. package/lib/model/strategy/adapter/walletPass/example.js +207 -0
  60. package/lib/model/strategy/adapter/walletPass/index.d.ts +32 -0
  61. package/lib/model/strategy/adapter/walletPass/index.js +142 -0
  62. package/lib/model/strategy/adapter/walletPass/locales.d.ts +1 -0
  63. package/lib/model/strategy/adapter/walletPass/locales.js +51 -0
  64. package/lib/model/strategy/adapter/walletPass/type.d.ts +163 -0
  65. package/lib/model/strategy/adapter/walletPass/type.js +17 -0
  66. package/lib/model/strategy/adapter/walletPass/utils.d.ts +50 -0
  67. package/lib/model/strategy/adapter/walletPass/utils.js +660 -0
  68. package/lib/model/strategy/index.d.ts +94 -0
  69. package/lib/model/strategy/index.js +413 -0
  70. package/lib/model/strategy/strategy-example.d.ts +5 -0
  71. package/lib/model/strategy/strategy-example.js +318 -0
  72. package/lib/model/strategy/type.d.ts +228 -0
  73. package/lib/model/strategy/type.js +44 -0
  74. package/lib/modules/Cart/types.d.ts +2 -0
  75. package/lib/modules/Cart/utils/cartProduct.js +9 -0
  76. package/lib/modules/Discount/types.d.ts +15 -0
  77. package/lib/modules/Payment/index.d.ts +2 -1
  78. package/lib/modules/Payment/index.js +1 -0
  79. package/lib/modules/Payment/utils.js +3 -0
  80. package/lib/modules/Payment/walletpass.d.ts +23 -0
  81. package/lib/modules/Payment/walletpass.js +94 -17
  82. package/lib/modules/Rules/index.d.ts +8 -3
  83. package/lib/modules/Rules/index.js +370 -192
  84. package/lib/modules/Rules/types.d.ts +1 -0
  85. package/lib/modules/Summary/types.d.ts +2 -0
  86. package/lib/modules/Summary/utils.d.ts +6 -0
  87. package/lib/modules/Summary/utils.js +15 -0
  88. package/lib/plugins/window.d.ts +2 -0
  89. package/lib/solution/BookingByStep/index.d.ts +2 -1
  90. package/lib/solution/Checkout/index.js +2 -0
  91. package/lib/solution/ShopDiscount/index.d.ts +1 -0
  92. package/lib/solution/ShopDiscount/index.js +6 -0
  93. package/lib/solution/ShopDiscount/types.d.ts +1 -0
  94. package/lib/solution/ShopDiscount/utils.js +10 -6
  95. package/package.json +1 -1
package/dist/index.d.ts CHANGED
@@ -6,6 +6,7 @@ export * from './types';
6
6
  export * from './modules';
7
7
  export * from './plugins';
8
8
  export * from './solution';
9
+ export * from './model';
9
10
  export { PisellOSCore } from './core';
10
11
  import PisellOSCore from './core';
11
12
  export default PisellOSCore;
package/dist/index.js CHANGED
@@ -8,6 +8,7 @@ export * from "./types";
8
8
  export * from "./modules";
9
9
  export * from "./plugins";
10
10
  export * from "./solution";
11
+ export * from "./model";
11
12
 
12
13
  // 导出核心实现
13
14
  export { PisellOSCore } from "./core";
@@ -0,0 +1 @@
1
+ export * from './strategy';
@@ -0,0 +1 @@
1
+ export * from "./strategy";
@@ -0,0 +1,3 @@
1
+ export * from './type';
2
+ export { default as WalletPassAdapter } from './walletPass';
3
+ export { WalletPassEvaluator } from './walletPass/evaluator';
@@ -0,0 +1,4 @@
1
+ export * from "./type";
2
+ export { default as WalletPassAdapter } from "./walletPass";
3
+ export { WalletPassEvaluator } from "./walletPass/evaluator";
4
+ // export * from './walletPass/type';
@@ -0,0 +1,28 @@
1
+ import { EvaluationResult, RuntimeContext } from '../type';
2
+ export interface BusinessAdapter {
3
+ /**
4
+ * 适配器名称
5
+ */
6
+ name: string;
7
+ /**
8
+ * 适配器版本
9
+ */
10
+ version: string;
11
+ /**
12
+ * 准备运行时上下文
13
+ * 将业务数据转换为策略引擎可识别的上下文
14
+ */
15
+ prepareContext(businessData: any): RuntimeContext;
16
+ /**
17
+ * 转换执行结果
18
+ * 将策略引擎的通用结果转换为业务层需要的格式
19
+ */
20
+ transformResult(result: EvaluationResult, businessData?: any): any;
21
+ /**
22
+ * 根据业务数据重新格式化配置
23
+ */
24
+ formatConfig(result: EvaluationResult, businessData?: any): {
25
+ result: EvaluationResult;
26
+ businessData?: any;
27
+ };
28
+ }
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,84 @@
1
+ import { StrategyConfig } from '../../type';
2
+ import { Voucher, Product, VoucherEvaluationResult, EvaluatorInput } from './type';
3
+ /**
4
+ * Wallet Pass 评估器
5
+ * 用于评估哪些 voucher 可以在当前订单中使用
6
+ */
7
+ export declare class WalletPassEvaluator {
8
+ private engine;
9
+ private strategyConfigs;
10
+ private locale;
11
+ constructor();
12
+ setStrategyConfigs(strategyConfigs: StrategyConfig[]): void;
13
+ getStrategyConfigs(): StrategyConfig[];
14
+ setLocale(locale: string): void;
15
+ getText(key: string): string;
16
+ /**
17
+ * 搜索券的格式
18
+ * @param input 输入
19
+ * @returns 返回搜索券的格式
20
+ */
21
+ searchVoucherFormat(input: EvaluatorInput): Voucher[];
22
+ /**
23
+ * 评估可用的 vouchers
24
+ */
25
+ evaluate(input: EvaluatorInput): VoucherEvaluationResult[];
26
+ /** 检查代金券/折扣卡是否可用 并返回config */
27
+ checkVoucherAvailability(input: EvaluatorInput): {
28
+ isAvailable: boolean;
29
+ config: {
30
+ [x: string]: any;
31
+ };
32
+ };
33
+ /**
34
+ * 获取推荐券
35
+ * @param input 输入
36
+ * @returns 返回推荐券列表
37
+ */
38
+ getRecommendedVouchers(input: EvaluatorInput): {
39
+ recommendedAmount: any;
40
+ recommended: any[];
41
+ transformList: any[];
42
+ noApplicableVoucher: Voucher[];
43
+ };
44
+ enhanceWithReason(voucher: any): any;
45
+ /**
46
+ * 优惠券处理函数
47
+ * @param applicableVouchers 可用的券列表
48
+ * @param orderTotalAmount 订单总金额
49
+ * @param products 订单商品列表
50
+ * @returns 返回推荐券列表和全部列表,每个券包含 _available_max_amount 和 _unified_available_status
51
+ */
52
+ processVouchers(vouchers: Voucher[], orderTotalAmount: number, products: Product[]): {
53
+ recommended: any[];
54
+ transformList: any[];
55
+ };
56
+ /**
57
+ * 重新计算优惠券状态(基于已选券的增量计算)
58
+ * @param allVouchers 所有原始券列表
59
+ * @param selectedVouchers 已选中的券列表(按选中顺序)
60
+ * @param orderTotalAmount 订单总金额
61
+ * @param products 订单商品列表
62
+ * @returns 返回更新后的所有券列表和已选券的详细抵扣信息
63
+ */
64
+ recalculateVouchers(allVouchers: any[], selectedVouchers: any[], orderTotalAmount: number, products: any[]): {
65
+ allWithUpdatedStatus: any[];
66
+ selectedWithDetails: any[];
67
+ };
68
+ /**
69
+ * 查找适用于指定 voucher 的策略配置
70
+ */
71
+ private findApplicableStrategies;
72
+ /**
73
+ * 检查策略中是否包含指定的 voucher ID
74
+ */
75
+ private checkVoucherInStrategy;
76
+ /**
77
+ * 递归检查条件组中是否包含 voucher ID
78
+ */
79
+ private checkVoucherInConditionGroup;
80
+ /**
81
+ * 计算适用商品的总金额和数量
82
+ */
83
+ private calculateApplicableProducts;
84
+ }
@@ -0,0 +1,496 @@
1
+ function _typeof(o) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (o) { return typeof o; } : function (o) { return o && "function" == typeof Symbol && o.constructor === Symbol && o !== Symbol.prototype ? "symbol" : typeof o; }, _typeof(o); }
2
+ function _createForOfIteratorHelper(o, allowArrayLike) { var it = typeof Symbol !== "undefined" && o[Symbol.iterator] || o["@@iterator"]; if (!it) { if (Array.isArray(o) || (it = _unsupportedIterableToArray(o)) || allowArrayLike && o && typeof o.length === "number") { if (it) o = it; var i = 0; var F = function F() {}; return { s: F, n: function n() { if (i >= o.length) return { done: true }; return { done: false, value: o[i++] }; }, e: function e(_e) { throw _e; }, f: F }; } throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); } var normalCompletion = true, didErr = false, err; return { s: function s() { it = it.call(o); }, n: function n() { var step = it.next(); normalCompletion = step.done; return step; }, e: function e(_e2) { didErr = true; err = _e2; }, f: function f() { try { if (!normalCompletion && it.return != null) it.return(); } finally { if (didErr) throw err; } } }; }
3
+ function _unsupportedIterableToArray(o, minLen) { if (!o) return; if (typeof o === "string") return _arrayLikeToArray(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(o); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); }
4
+ function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len = arr.length; for (var i = 0, arr2 = new Array(len); i < len; i++) arr2[i] = arr[i]; return arr2; }
5
+ function ownKeys(e, r) { var t = Object.keys(e); if (Object.getOwnPropertySymbols) { var o = Object.getOwnPropertySymbols(e); r && (o = o.filter(function (r) { return Object.getOwnPropertyDescriptor(e, r).enumerable; })), t.push.apply(t, o); } return t; }
6
+ function _objectSpread(e) { for (var r = 1; r < arguments.length; r++) { var t = null != arguments[r] ? arguments[r] : {}; r % 2 ? ownKeys(Object(t), !0).forEach(function (r) { _defineProperty(e, r, t[r]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(e, Object.getOwnPropertyDescriptors(t)) : ownKeys(Object(t)).forEach(function (r) { Object.defineProperty(e, r, Object.getOwnPropertyDescriptor(t, r)); }); } return e; }
7
+ function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
8
+ function _defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, _toPropertyKey(descriptor.key), descriptor); } }
9
+ function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); Object.defineProperty(Constructor, "prototype", { writable: false }); return Constructor; }
10
+ function _defineProperty(obj, key, value) { key = _toPropertyKey(key); if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
11
+ function _toPropertyKey(t) { var i = _toPrimitive(t, "string"); return "symbol" == _typeof(i) ? i : String(i); }
12
+ function _toPrimitive(t, r) { if ("object" != _typeof(t) || !t) return t; var e = t[Symbol.toPrimitive]; if (void 0 !== e) { var i = e.call(t, r || "default"); if ("object" != _typeof(i)) return i; throw new TypeError("@@toPrimitive must return a primitive value."); } return ("string" === r ? String : Number)(t); }
13
+ import { StrategyEngine } from "../../index";
14
+ import { processVouchers as _processVouchers, recalculateVouchers as _recalculateVouchers, getApplicableProductIds, getMainProductPrice, getBundleItemPrice, getProductQuantity, getBundleItemIsOriginalPrice } from "./utils";
15
+ import { locales } from "./locales";
16
+ import Decimal from 'decimal.js';
17
+ var defaultStrategyMetadataCustom = {
18
+ maxDeductionAmount: 99999,
19
+ maxUsagePerOrder: 0,
20
+ allowCrossProduct: true,
21
+ applicableProductLimit: 0,
22
+ deductTaxAndFee: true
23
+ };
24
+
25
+ /**
26
+ * Wallet Pass 评估器
27
+ * 用于评估哪些 voucher 可以在当前订单中使用
28
+ */
29
+ export var WalletPassEvaluator = /*#__PURE__*/function () {
30
+ function WalletPassEvaluator() {
31
+ _classCallCheck(this, WalletPassEvaluator);
32
+ _defineProperty(this, "engine", void 0);
33
+ _defineProperty(this, "strategyConfigs", []);
34
+ _defineProperty(this, "locale", 'en');
35
+ this.engine = new StrategyEngine({
36
+ debug: false,
37
+ enableTrace: false
38
+ });
39
+ }
40
+ _createClass(WalletPassEvaluator, [{
41
+ key: "setStrategyConfigs",
42
+ value: function setStrategyConfigs(strategyConfigs) {
43
+ this.strategyConfigs = strategyConfigs;
44
+ }
45
+ }, {
46
+ key: "getStrategyConfigs",
47
+ value: function getStrategyConfigs() {
48
+ return this.strategyConfigs;
49
+ }
50
+ }, {
51
+ key: "setLocale",
52
+ value: function setLocale(locale) {
53
+ this.locale = locale;
54
+ }
55
+ }, {
56
+ key: "getText",
57
+ value: function getText(key) {
58
+ var _locales$this$locale;
59
+ return locales === null || locales === void 0 || (_locales$this$locale = locales[this.locale]) === null || _locales$this$locale === void 0 ? void 0 : _locales$this$locale[key];
60
+ }
61
+
62
+ /**
63
+ * 搜索券的格式
64
+ * @param input 输入
65
+ * @returns 返回搜索券的格式
66
+ */
67
+ }, {
68
+ key: "searchVoucherFormat",
69
+ value: function searchVoucherFormat(input) {
70
+ var results = this.evaluate(input);
71
+ return results.map(function (item) {
72
+ if (item.isApplicable) {
73
+ var _item$strategyResult;
74
+ return _objectSpread(_objectSpread({}, item.voucher), {}, {
75
+ config: _objectSpread({}, item === null || item === void 0 || (_item$strategyResult = item.strategyResult) === null || _item$strategyResult === void 0 ? void 0 : _item$strategyResult.config.metadata.custom)
76
+ });
77
+ } else {
78
+ var _item$strategyResult2;
79
+ return _objectSpread(_objectSpread({}, item.voucher), {}, {
80
+ config: _objectSpread({}, item === null || item === void 0 || (_item$strategyResult2 = item.strategyResult) === null || _item$strategyResult2 === void 0 ? void 0 : _item$strategyResult2.config.metadata.custom),
81
+ reason: item.reason,
82
+ reasonCode: item.reasonCode
83
+ });
84
+ }
85
+ });
86
+ }
87
+
88
+ /**
89
+ * 评估可用的 vouchers
90
+ */
91
+ }, {
92
+ key: "evaluate",
93
+ value: function evaluate(input) {
94
+ var orderTotalAmount = input.orderTotalAmount,
95
+ products = input.products,
96
+ vouchers = input.vouchers,
97
+ strategyConfigs = input.strategyConfigs;
98
+
99
+ // 提取订单中的商品ID
100
+ var productIds = products.map(function (p) {
101
+ return p.product_id;
102
+ });
103
+ var results = [];
104
+
105
+ // 遍历每个 voucher 进行评估
106
+ var _iterator = _createForOfIteratorHelper(vouchers),
107
+ _step;
108
+ try {
109
+ for (_iterator.s(); !(_step = _iterator.n()).done;) {
110
+ var _strategy$metadata$cu, _strategy$metadata, _businessConfig$deduc;
111
+ var voucher = _step.value;
112
+ // 查找适用此 voucher 的策略配置
113
+ var applicableStrategies = this.findApplicableStrategies(voucher, strategyConfigs || this.strategyConfigs);
114
+ if (applicableStrategies.length === 0) {
115
+ // 没有匹配的策略 默认可用
116
+ results.push({
117
+ voucher: voucher,
118
+ isApplicable: true,
119
+ canUseCount: 0,
120
+ maxDeduction: 0,
121
+ deductTaxAndFee: true,
122
+ applicableProductIds: [],
123
+ reason: '',
124
+ reasonCode: '',
125
+ strategyResult: {
126
+ config: {
127
+ metadata: {
128
+ custom: _objectSpread({}, defaultStrategyMetadataCustom)
129
+ }
130
+ }
131
+ }
132
+ });
133
+ continue;
134
+ }
135
+
136
+ // 对每个策略进行评估(取第一个匹配的策略)
137
+ var strategy = applicableStrategies[0];
138
+
139
+ // 从 voucher 中获取适用商品ID
140
+ var applicableProductIds = getApplicableProductIds(voucher);
141
+
142
+ // 计算适用商品的总金额和数量
143
+ var _this$calculateApplic = this.calculateApplicableProducts(products, applicableProductIds, (_strategy$metadata$cu = strategy === null || strategy === void 0 || (_strategy$metadata = strategy.metadata) === null || _strategy$metadata === void 0 || (_strategy$metadata = _strategy$metadata.custom) === null || _strategy$metadata === void 0 ? void 0 : _strategy$metadata.deductTaxAndFee) !== null && _strategy$metadata$cu !== void 0 ? _strategy$metadata$cu : true),
144
+ applicableTotal = _this$calculateApplic.applicableTotal,
145
+ applicableCount = _this$calculateApplic.applicableCount;
146
+
147
+ // 准备运行时上下文
148
+ var context = {
149
+ entities: {
150
+ voucher: voucher,
151
+ products: products,
152
+ order: {
153
+ orderTotalAmount: orderTotalAmount,
154
+ products: products
155
+ }
156
+ },
157
+ attributes: {
158
+ orderTotalAmount: orderTotalAmount,
159
+ applicableProductTotalAmount: applicableTotal,
160
+ applicableProductCount: applicableCount,
161
+ voucherProductId: voucher.product_id,
162
+ productIds: productIds
163
+ },
164
+ metadata: {
165
+ timestamp: Date.now()
166
+ }
167
+ };
168
+
169
+ // 执行策略评估
170
+ var evaluationResult = this.engine.evaluate(strategy, context);
171
+
172
+ // 从策略配置中获取业务配置
173
+ var businessConfig = strategy.metadata.custom || {};
174
+ var maxDeduction = businessConfig.maxDeductionAmount || 0;
175
+ var canUseCount = businessConfig.maxUsagePerOrder || 0;
176
+ var deductTaxAndFee = (_businessConfig$deduc = businessConfig.deductTaxAndFee) !== null && _businessConfig$deduc !== void 0 ? _businessConfig$deduc : true;
177
+ if (evaluationResult.applicable) {
178
+ results.push({
179
+ voucher: voucher,
180
+ isApplicable: true,
181
+ canUseCount: canUseCount,
182
+ maxDeduction: maxDeduction,
183
+ deductTaxAndFee: deductTaxAndFee,
184
+ applicableProductIds: applicableProductIds || [],
185
+ strategyResult: evaluationResult
186
+ });
187
+ } else {
188
+ // 根据策略评估结果的 code 映射到 locales 中的原因
189
+ var reasonCode = 'not_meet_the_required_conditions'; // 默认原因
190
+
191
+ // 根据策略引擎返回的 code 映射到本地化的 reasonCode
192
+ if (evaluationResult.code) {
193
+ // 可以根据具体的 code 映射到不同的原因
194
+ // 例如:如果是金额不足、使用次数限制等
195
+ var codeMapping = {
196
+ INSUFFICIENT_AMOUNT: 'not_meet_the_required_conditions',
197
+ USAGE_LIMIT_EXCEEDED: 'usage_limit_reached',
198
+ CHANNEL_NOT_ALLOWED: 'not_available_for_this_channel',
199
+ ORDER_TYPE_NOT_ALLOWED: 'not_valid_for_this_order_type',
200
+ DEDUCTION_LIMIT_EXCEEDED: 'exceeds_the_maximum_deduction_limit'
201
+ };
202
+ reasonCode = codeMapping[evaluationResult.code] || 'not_meet_the_required_conditions';
203
+ }
204
+ results.push({
205
+ voucher: voucher,
206
+ isApplicable: false,
207
+ canUseCount: 0,
208
+ maxDeduction: 0,
209
+ deductTaxAndFee: false,
210
+ applicableProductIds: [],
211
+ reason: this.getText(reasonCode),
212
+ reasonCode: reasonCode,
213
+ strategyResult: evaluationResult
214
+ });
215
+ }
216
+ }
217
+ } catch (err) {
218
+ _iterator.e(err);
219
+ } finally {
220
+ _iterator.f();
221
+ }
222
+ return results;
223
+ }
224
+
225
+ /** 检查代金券/折扣卡是否可用 并返回config */
226
+ }, {
227
+ key: "checkVoucherAvailability",
228
+ value: function checkVoucherAvailability(input) {
229
+ var _results$, _results$2;
230
+ var orderTotalAmount = input.orderTotalAmount,
231
+ products = input.products,
232
+ vouchers = input.vouchers;
233
+ var results = this.evaluate(input);
234
+ var isAvailable = ((_results$ = results[0]) === null || _results$ === void 0 ? void 0 : _results$.isApplicable) || false;
235
+ return {
236
+ isAvailable: isAvailable,
237
+ config: _objectSpread({}, (_results$2 = results[0]) === null || _results$2 === void 0 || (_results$2 = _results$2.strategyResult) === null || _results$2 === void 0 || (_results$2 = _results$2.config) === null || _results$2 === void 0 || (_results$2 = _results$2.metadata) === null || _results$2 === void 0 ? void 0 : _results$2.custom)
238
+ };
239
+ }
240
+
241
+ /**
242
+ * 获取推荐券
243
+ * @param input 输入
244
+ * @returns 返回推荐券列表
245
+ */
246
+ }, {
247
+ key: "getRecommendedVouchers",
248
+ value: function getRecommendedVouchers(input) {
249
+ var orderTotalAmount = input.orderTotalAmount,
250
+ products = input.products,
251
+ vouchers = input.vouchers;
252
+ // 基于后端返回的状态不可用券
253
+ var noApplicableVoucher = [];
254
+ // 基于后端返回的状态可用券
255
+ var applicableVoucher = [];
256
+ vouchers.forEach(function (voucher) {
257
+ if (voucher.unified_available_status === 1) {
258
+ applicableVoucher.push(voucher);
259
+ } else {
260
+ noApplicableVoucher.push(voucher);
261
+ }
262
+ });
263
+ var results = this.evaluate(_objectSpread(_objectSpread({}, input), {}, {
264
+ vouchers: applicableVoucher
265
+ }));
266
+ var newVouchers = [];
267
+ results.forEach(function (item) {
268
+ if (item.isApplicable) {
269
+ var _item$strategyResult3;
270
+ newVouchers.push(_objectSpread(_objectSpread({}, item.voucher), {}, {
271
+ config: _objectSpread(_objectSpread({}, defaultStrategyMetadataCustom), (item === null || item === void 0 || (_item$strategyResult3 = item.strategyResult) === null || _item$strategyResult3 === void 0 || (_item$strategyResult3 = _item$strategyResult3.config) === null || _item$strategyResult3 === void 0 || (_item$strategyResult3 = _item$strategyResult3.metadata) === null || _item$strategyResult3 === void 0 ? void 0 : _item$strategyResult3.custom) || {})
272
+ }));
273
+ } else {
274
+ // 不可用的券,添加原因信息
275
+ noApplicableVoucher.push(_objectSpread(_objectSpread({}, item.voucher), {}, {
276
+ _unified_available_status: 0,
277
+ reason: item.reason,
278
+ reasonCode: item.reasonCode
279
+ }));
280
+ }
281
+ });
282
+ var _this$processVouchers = this.processVouchers(newVouchers, orderTotalAmount, products),
283
+ recommended = _this$processVouchers.recommended,
284
+ transformList = _this$processVouchers.transformList;
285
+ var recommendedAmount = recommended.reduce(function (sum, item) {
286
+ return new Decimal(sum).add(item._available_max_amount || 0).toNumber();
287
+ }, 0);
288
+ return {
289
+ // 推荐券总金额
290
+ recommendedAmount: recommendedAmount,
291
+ // 推荐券列表
292
+ recommended: recommended,
293
+ // 格式化后的可用券列表
294
+ transformList: transformList,
295
+ // 后端不可用券列表 和前端基于策略返回的 不可用列表 合并
296
+ noApplicableVoucher: noApplicableVoucher
297
+ };
298
+ }
299
+ }, {
300
+ key: "enhanceWithReason",
301
+ value: function enhanceWithReason(voucher) {
302
+ if (voucher._unified_available_status === 0 && voucher.reasonCode) {
303
+ return _objectSpread(_objectSpread({}, voucher), {}, {
304
+ reason: this.getText(voucher.reasonCode)
305
+ });
306
+ }
307
+ return voucher;
308
+ }
309
+
310
+ /**
311
+ * 优惠券处理函数
312
+ * @param applicableVouchers 可用的券列表
313
+ * @param orderTotalAmount 订单总金额
314
+ * @param products 订单商品列表
315
+ * @returns 返回推荐券列表和全部列表,每个券包含 _available_max_amount 和 _unified_available_status
316
+ */
317
+ }, {
318
+ key: "processVouchers",
319
+ value: function processVouchers(vouchers, orderTotalAmount, products) {
320
+ var _this = this;
321
+ var result = _processVouchers(vouchers, orderTotalAmount, products);
322
+ return {
323
+ recommended: result.recommended.map(function (item) {
324
+ return _this.enhanceWithReason(item);
325
+ }),
326
+ transformList: result.transformList.map(function (item) {
327
+ return _this.enhanceWithReason(item);
328
+ })
329
+ };
330
+ }
331
+
332
+ /**
333
+ * 重新计算优惠券状态(基于已选券的增量计算)
334
+ * @param allVouchers 所有原始券列表
335
+ * @param selectedVouchers 已选中的券列表(按选中顺序)
336
+ * @param orderTotalAmount 订单总金额
337
+ * @param products 订单商品列表
338
+ * @returns 返回更新后的所有券列表和已选券的详细抵扣信息
339
+ */
340
+ }, {
341
+ key: "recalculateVouchers",
342
+ value: function recalculateVouchers(allVouchers, selectedVouchers, orderTotalAmount, products) {
343
+ var _this2 = this;
344
+ var result = _recalculateVouchers(allVouchers, selectedVouchers, orderTotalAmount, products);
345
+ return {
346
+ allWithUpdatedStatus: result.allWithUpdatedStatus.map(function (item) {
347
+ return _this2.enhanceWithReason(item);
348
+ }),
349
+ selectedWithDetails: result.selectedWithDetails.map(function (item) {
350
+ return _this2.enhanceWithReason(item);
351
+ })
352
+ };
353
+ }
354
+
355
+ /**
356
+ * 查找适用于指定 voucher 的策略配置
357
+ */
358
+ }, {
359
+ key: "findApplicableStrategies",
360
+ value: function findApplicableStrategies(voucher, strategyConfigs) {
361
+ var _this3 = this;
362
+ return strategyConfigs.filter(function (config) {
363
+ // 检查策略的 conditions 中是否包含匹配此 voucher 的规则
364
+ return _this3.checkVoucherInStrategy(voucher.product_id, config);
365
+ });
366
+ }
367
+
368
+ /**
369
+ * 检查策略中是否包含指定的 voucher ID
370
+ */
371
+ }, {
372
+ key: "checkVoucherInStrategy",
373
+ value: function checkVoucherInStrategy(voucherProductId, config) {
374
+ var rules = config.conditions.rules;
375
+ var _iterator2 = _createForOfIteratorHelper(rules),
376
+ _step2;
377
+ try {
378
+ for (_iterator2.s(); !(_step2 = _iterator2.n()).done;) {
379
+ var rule = _step2.value;
380
+ // 如果是条件组,递归检查
381
+ if ('operator' in rule && 'rules' in rule) {
382
+ if (this.checkVoucherInConditionGroup(voucherProductId, rule)) {
383
+ return true;
384
+ }
385
+ } else {
386
+ // 检查是否是 voucherProductId 字段的规则
387
+ var conditionRule = rule;
388
+ if (conditionRule.field === 'voucherProductId' && conditionRule.operator === 'in' && Array.isArray(conditionRule.value)) {
389
+ if (conditionRule.value.includes(voucherProductId)) {
390
+ return true;
391
+ }
392
+ }
393
+ // 支持等于判断
394
+ if (conditionRule.field === 'voucherProductId' && (conditionRule.operator === '=' || conditionRule.operator === '==')) {
395
+ if (conditionRule.value === voucherProductId) {
396
+ return true;
397
+ }
398
+ }
399
+ }
400
+ }
401
+ } catch (err) {
402
+ _iterator2.e(err);
403
+ } finally {
404
+ _iterator2.f();
405
+ }
406
+ return false;
407
+ }
408
+
409
+ /**
410
+ * 递归检查条件组中是否包含 voucher ID
411
+ */
412
+ }, {
413
+ key: "checkVoucherInConditionGroup",
414
+ value: function checkVoucherInConditionGroup(voucherProductId, group) {
415
+ var _iterator3 = _createForOfIteratorHelper(group.rules),
416
+ _step3;
417
+ try {
418
+ for (_iterator3.s(); !(_step3 = _iterator3.n()).done;) {
419
+ var rule = _step3.value;
420
+ if ('operator' in rule && 'rules' in rule) {
421
+ if (this.checkVoucherInConditionGroup(voucherProductId, rule)) {
422
+ return true;
423
+ }
424
+ } else {
425
+ if (rule.field === 'voucherProductId' && rule.operator === 'in' && Array.isArray(rule.value)) {
426
+ if (rule.value.includes(voucherProductId)) {
427
+ return true;
428
+ }
429
+ }
430
+ if (rule.field === 'voucherProductId' && (rule.operator === '=' || rule.operator === '==')) {
431
+ if (rule.value === voucherProductId) {
432
+ return true;
433
+ }
434
+ }
435
+ }
436
+ }
437
+ } catch (err) {
438
+ _iterator3.e(err);
439
+ } finally {
440
+ _iterator3.f();
441
+ }
442
+ return false;
443
+ }
444
+
445
+ /**
446
+ * 计算适用商品的总金额和数量
447
+ */
448
+ }, {
449
+ key: "calculateApplicableProducts",
450
+ value: function calculateApplicableProducts(products, applicableProductIds, deductTaxAndFee) {
451
+ var total = 0;
452
+ var count = 0;
453
+
454
+ // 如果为空数组,不适用任何商品
455
+ if (applicableProductIds !== null && applicableProductIds.length === 0) {
456
+ return {
457
+ applicableTotal: 0,
458
+ applicableCount: 0
459
+ };
460
+ }
461
+ products.forEach(function (product) {
462
+ var productQuantity = getProductQuantity(product);
463
+
464
+ // 检查主商品是否适用
465
+ var isMainProductApplicable = applicableProductIds === null || applicableProductIds.includes(product.product_id);
466
+ if (isMainProductApplicable) {
467
+ // 计算主商品价格(包含加减价子商品)
468
+ total += getMainProductPrice(product, deductTaxAndFee) * productQuantity;
469
+ count += productQuantity;
470
+ }
471
+
472
+ // 检查原价子商品(需要单独判断适用性)
473
+ if (product.product_bundle && product.product_bundle.length > 0) {
474
+ product.product_bundle.forEach(function (bundleItem) {
475
+ // 只处理原价子商品,加减价子商品已包含在主商品价格中
476
+ if (getBundleItemIsOriginalPrice(bundleItem)) {
477
+ // 检查子商品是否适用
478
+ var isBundleItemApplicable = applicableProductIds === null || applicableProductIds.includes(bundleItem.bundle_product_id);
479
+ if (isBundleItemApplicable) {
480
+ // 计算原价子商品价格
481
+ var bundleItemQuantity = bundleItem.num * productQuantity;
482
+ total += getBundleItemPrice(bundleItem, productQuantity, deductTaxAndFee);
483
+ count += bundleItemQuantity;
484
+ }
485
+ }
486
+ });
487
+ }
488
+ });
489
+ return {
490
+ applicableTotal: total,
491
+ applicableCount: count
492
+ };
493
+ }
494
+ }]);
495
+ return WalletPassEvaluator;
496
+ }();
@@ -0,0 +1,4 @@
1
+ /**
2
+ * WalletPass 评估器使用示例
3
+ */
4
+ export {};