@pisell/pisellos 0.0.459 → 0.0.460

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.
@@ -0,0 +1,447 @@
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 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; }
3
+ 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; }
4
+ function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
5
+ 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); } }
6
+ function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); Object.defineProperty(Constructor, "prototype", { writable: false }); return Constructor; }
7
+ 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; }
8
+ function _toPropertyKey(t) { var i = _toPrimitive(t, "string"); return "symbol" == _typeof(i) ? i : String(i); }
9
+ 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); }
10
+ import { PROMOTION_ACTION_TYPES } from "./type";
11
+ // 导出类型定义
12
+ // export * from './type';
13
+
14
+ // 导出评估器
15
+ import { PromotionEvaluator } from "./evaluator";
16
+ export { PromotionEvaluator };
17
+
18
+ // ============================================
19
+ // Promotion 适配器实现
20
+ // ============================================
21
+
22
+ /**
23
+ * Promotion 适配器
24
+ *
25
+ * 用于将促销活动业务数据转换为策略引擎可识别的格式
26
+ * 策略引擎只负责匹配,具体的优惠计算由业务层完成
27
+ */
28
+ export var PromotionAdapter = /*#__PURE__*/function () {
29
+ function PromotionAdapter() {
30
+ _classCallCheck(this, PromotionAdapter);
31
+ _defineProperty(this, "name", 'PromotionAdapter');
32
+ _defineProperty(this, "version", '1.0.0');
33
+ }
34
+ _createClass(PromotionAdapter, [{
35
+ key: "prepareContext",
36
+ value:
37
+ /**
38
+ * 准备运行时上下文
39
+ *
40
+ * 将业务数据转换为策略引擎可识别的 RuntimeContext
41
+ */
42
+ function prepareContext(businessData) {
43
+ var products = businessData.products,
44
+ currentProduct = businessData.currentProduct,
45
+ channel = businessData.channel,
46
+ custom = businessData.custom;
47
+
48
+ // 当前时间(用于时间条件判断)
49
+ var now = new Date();
50
+ var currentDateTime = this.formatDateTime(now);
51
+
52
+ // 如果有 currentProduct,用于单商品场景(商品卡片展示)
53
+ var evaluatingProduct = currentProduct || (products.length > 0 ? products[0] : null);
54
+ return {
55
+ entities: {
56
+ products: products,
57
+ currentProduct: evaluatingProduct
58
+ },
59
+ attributes: _objectSpread({
60
+ // 当前时间(格式化字符串,用于时间条件判断)
61
+ currentDateTime: currentDateTime,
62
+ // 当前评估的商品信息(用于 product_match 运算符)
63
+ productIdAndVariantId: evaluatingProduct ? {
64
+ product_id: evaluatingProduct.product_id,
65
+ product_variant_id: evaluatingProduct.product_variant_id
66
+ } : null,
67
+ // 渠道
68
+ channel: channel || '',
69
+ // 商品总数量
70
+ totalQuantity: products.reduce(function (sum, p) {
71
+ return sum + p.quantity;
72
+ }, 0),
73
+ // 商品总金额
74
+ totalAmount: products.reduce(function (sum, p) {
75
+ return sum + p.price * p.quantity;
76
+ }, 0)
77
+ }, custom),
78
+ metadata: {
79
+ timestamp: Date.now()
80
+ }
81
+ };
82
+ }
83
+
84
+ /**
85
+ * 转换执行结果
86
+ *
87
+ * 将策略引擎的通用结果转换为业务层需要的格式
88
+ * 主要是整理 matchedActions,让业务层更容易使用
89
+ */
90
+ }, {
91
+ key: "transformResult",
92
+ value: function transformResult(result, businessData) {
93
+ // 获取适用的商品列表
94
+ var applicableProducts = businessData ? this.getApplicableProducts(result, businessData) : [];
95
+ if (!result.applicable) {
96
+ return {
97
+ isApplicable: false,
98
+ applicableProducts: [],
99
+ reason: result.message,
100
+ reasonCode: result.code,
101
+ strategyResult: result
102
+ };
103
+ }
104
+
105
+ // 获取第一个匹配的 action(按 priority 排序后的)
106
+ var matchedAction = result.matchedActions[0];
107
+ if (!matchedAction) {
108
+ return {
109
+ isApplicable: false,
110
+ applicableProducts: [],
111
+ reason: 'No matched action',
112
+ reasonCode: 'NO_ACTION',
113
+ strategyResult: result
114
+ };
115
+ }
116
+
117
+ // 解析 action 详情
118
+ var actionDetail = this.parseActionDetail(matchedAction);
119
+ return {
120
+ isApplicable: true,
121
+ actionType: matchedAction.type,
122
+ actionDetail: actionDetail,
123
+ applicableProducts: applicableProducts,
124
+ strategyResult: result
125
+ };
126
+ }
127
+
128
+ /**
129
+ * 格式化配置
130
+ */
131
+ }, {
132
+ key: "formatConfig",
133
+ value: function formatConfig(result, businessData) {
134
+ return {
135
+ result: result,
136
+ businessData: businessData
137
+ };
138
+ }
139
+
140
+ // ============================================
141
+ // 私有辅助方法
142
+ // ============================================
143
+
144
+ /**
145
+ * 格式化日期时间
146
+ */
147
+ }, {
148
+ key: "formatDateTime",
149
+ value: function formatDateTime(date) {
150
+ var year = date.getFullYear();
151
+ var month = String(date.getMonth() + 1).padStart(2, '0');
152
+ var day = String(date.getDate()).padStart(2, '0');
153
+ var hours = String(date.getHours()).padStart(2, '0');
154
+ var minutes = String(date.getMinutes()).padStart(2, '0');
155
+ var seconds = String(date.getSeconds()).padStart(2, '0');
156
+ return "".concat(year, "-").concat(month, "-").concat(day, " ").concat(hours, ":").concat(minutes, ":").concat(seconds);
157
+ }
158
+
159
+ /**
160
+ * 解析 Action 详情
161
+ *
162
+ * 将 matchedAction 转换为更易用的结构
163
+ */
164
+ }, {
165
+ key: "parseActionDetail",
166
+ value: function parseActionDetail(action) {
167
+ switch (action.type) {
168
+ case PROMOTION_ACTION_TYPES.X_ITEMS_FOR_Y_PRICE:
169
+ return this.parseXItemsForYPriceAction(action);
170
+ case PROMOTION_ACTION_TYPES.BUY_X_GET_Y_FREE:
171
+ return this.parseBuyXGetYFreeAction(action);
172
+ default:
173
+ return undefined;
174
+ }
175
+ }
176
+
177
+ /**
178
+ * 解析 X件Y元 Action
179
+ */
180
+ }, {
181
+ key: "parseXItemsForYPriceAction",
182
+ value: function parseXItemsForYPriceAction(action) {
183
+ var _config$allowCrossPro, _config$cumulative;
184
+ var value = action.value || {};
185
+ var config = action.config || {};
186
+ return {
187
+ type: PROMOTION_ACTION_TYPES.X_ITEMS_FOR_Y_PRICE,
188
+ x: value.x || 2,
189
+ price: value.price || 0,
190
+ allowCrossProduct: (_config$allowCrossPro = config.allowCrossProduct) !== null && _config$allowCrossPro !== void 0 ? _config$allowCrossPro : true,
191
+ cumulative: (_config$cumulative = config.cumulative) !== null && _config$cumulative !== void 0 ? _config$cumulative : true
192
+ };
193
+ }
194
+
195
+ /**
196
+ * 解析 买X送Y Action
197
+ */
198
+ }, {
199
+ key: "parseBuyXGetYFreeAction",
200
+ value: function parseBuyXGetYFreeAction(action) {
201
+ var _config$cumulative2;
202
+ var value = action.value || {};
203
+ var config = action.config || {};
204
+ return {
205
+ type: PROMOTION_ACTION_TYPES.BUY_X_GET_Y_FREE,
206
+ buyQuantity: value.buyQuantity || 1,
207
+ freeQuantity: value.freeQuantity || 1,
208
+ giftSelectionMode: config.giftSelectionMode || 'user_select',
209
+ cumulative: (_config$cumulative2 = config.cumulative) !== null && _config$cumulative2 !== void 0 ? _config$cumulative2 : true,
210
+ giftProducts: config.giftProducts || []
211
+ };
212
+ }
213
+
214
+ /**
215
+ * 获取适用的商品列表
216
+ *
217
+ * 从购物车商品中筛选出符合策略条件的商品
218
+ */
219
+ }, {
220
+ key: "getApplicableProducts",
221
+ value: function getApplicableProducts(result, businessData) {
222
+ var _this = this;
223
+ var products = businessData.products;
224
+
225
+ // 从策略配置中获取商品范围条件
226
+ var productMatchRule = this.findProductMatchRule(result.config);
227
+ if (!productMatchRule) {
228
+ // 如果没有商品范围条件,所有商品都适用
229
+ return products;
230
+ }
231
+ var configProducts = productMatchRule.value;
232
+
233
+ // 筛选适用的商品
234
+ return products.filter(function (product) {
235
+ return _this.isProductMatch(product, configProducts);
236
+ });
237
+ }
238
+
239
+ /**
240
+ * 查找商品匹配规则
241
+ */
242
+ }, {
243
+ key: "findProductMatchRule",
244
+ value: function findProductMatchRule(config) {
245
+ var _config$conditions;
246
+ if (!(config !== null && config !== void 0 && (_config$conditions = config.conditions) !== null && _config$conditions !== void 0 && _config$conditions.rules)) {
247
+ return null;
248
+ }
249
+ return config.conditions.rules.find(function (rule) {
250
+ return rule.field === 'productIdAndVariantId' && (rule.operator === 'product_match' || rule.operator === 'object_in');
251
+ });
252
+ }
253
+
254
+ /**
255
+ * 检查商品是否匹配
256
+ */
257
+ }, {
258
+ key: "isProductMatch",
259
+ value: function isProductMatch(product, configProducts) {
260
+ return configProducts.some(function (config) {
261
+ // 首先检查 product_id
262
+ if (config.product_id !== product.product_id) {
263
+ return false;
264
+ }
265
+
266
+ // 如果配置的 product_variant_id = 0,只需要 product_id 匹配
267
+ if (config.product_variant_id === 0) {
268
+ return true;
269
+ }
270
+
271
+ // 否则需要 product_variant_id 精确匹配
272
+ return config.product_variant_id === product.product_variant_id;
273
+ });
274
+ }
275
+ }]);
276
+ return PromotionAdapter;
277
+ }();
278
+
279
+ // 导出默认实例
280
+ export default PromotionAdapter;
281
+
282
+ /**
283
+ * X件Y元策略配置示例
284
+ *
285
+ * 业务规则:
286
+ * - 每X件商品固定价格Y元(可累计)
287
+ * - 支持跨商品组合(A+B可以凑成一组)
288
+ * - 买5件 = 2组优惠 + 1件原价
289
+ *
290
+ * 商品匹配规则:
291
+ * - product_variant_id = 0 表示匹配任意变体
292
+ * - product_variant_id != 0 表示精确匹配该变体
293
+ */
294
+ export var X_ITEMS_FOR_Y_PRICE_STRATEGY = {
295
+ metadata: {
296
+ id: 'STRATEGY_001',
297
+ name: {
298
+ en: '2 items for $10',
299
+ 'zh-CN': '2杯10元',
300
+ 'zh-HK': '2杯10元'
301
+ },
302
+ type: 'promotion',
303
+ custom: {
304
+ display: {
305
+ product_card: {
306
+ text: {
307
+ en: '2 for $10',
308
+ 'zh-CN': '2杯10元',
309
+ 'zh-HK': '2杯10元'
310
+ },
311
+ type: 'tag'
312
+ }
313
+ }
314
+ }
315
+ },
316
+ conditions: {
317
+ operator: 'and',
318
+ rules: [{
319
+ type: 'operator',
320
+ field: 'currentDateTime',
321
+ value: '2023-01-01 00:00:00',
322
+ operator: '>='
323
+ }, {
324
+ type: 'operator',
325
+ field: 'currentDateTime',
326
+ value: '2027-01-01 00:00:00',
327
+ operator: '<='
328
+ }, {
329
+ type: 'operator',
330
+ field: 'productIdAndVariantId',
331
+ value: [{
332
+ product_id: 60736,
333
+ product_variant_id: 0 // 0 = 匹配任意变体
334
+ }, {
335
+ product_id: 60737,
336
+ product_variant_id: 0 // 0 = 匹配任意变体
337
+ }],
338
+ operator: 'product_match'
339
+ }],
340
+ actionIds: ['1']
341
+ },
342
+ actions: [{
343
+ id: '1',
344
+ type: 'X_ITEMS_FOR_Y_PRICE',
345
+ value: {
346
+ x: 2,
347
+ // 每X件
348
+ price: 10 // 固定价格Y元
349
+ },
350
+ valueType: 'object',
351
+ target: 'product',
352
+ priority: 1,
353
+ config: {
354
+ allowCrossProduct: true,
355
+ // 允许跨商品组合(A+B可以凑成一组)
356
+ cumulative: true // 可累计(买5件 = 2组优惠 + 1件原价)
357
+ }
358
+ }]
359
+ };
360
+
361
+ /**
362
+ * 买X送Y策略配置示例
363
+ *
364
+ * 业务规则:
365
+ * - 买X件送Y件(可累计:买2送2、买3送3...)
366
+ * - 赠品由用户从列表中选择
367
+ *
368
+ * 商品匹配规则:
369
+ * - product_variant_id = 0 表示匹配任意变体
370
+ * - product_variant_id != 0 表示精确匹配该变体
371
+ */
372
+ export var BUY_X_GET_Y_FREE_STRATEGY = {
373
+ metadata: {
374
+ id: 'STRATEGY_002',
375
+ name: {
376
+ en: 'Buy 1 Get 1 Free',
377
+ 'zh-CN': '买1送1',
378
+ 'zh-HK': '買1送1'
379
+ },
380
+ type: 'promotion',
381
+ custom: {
382
+ display: {
383
+ product_card: {
384
+ text: {
385
+ en: 'Buy 1 Get 1',
386
+ 'zh-CN': '买1送1',
387
+ 'zh-HK': '買1送1'
388
+ },
389
+ type: 'tag'
390
+ }
391
+ }
392
+ }
393
+ },
394
+ conditions: {
395
+ operator: 'and',
396
+ rules: [{
397
+ type: 'operator',
398
+ field: 'currentDateTime',
399
+ value: '2023-01-01 00:00:00',
400
+ operator: '>='
401
+ }, {
402
+ type: 'operator',
403
+ field: 'currentDateTime',
404
+ value: '2027-01-01 00:00:00',
405
+ operator: '<='
406
+ }, {
407
+ type: 'operator',
408
+ field: 'productIdAndVariantId',
409
+ value: [{
410
+ product_id: 60736,
411
+ product_variant_id: 0 // 0 = 匹配任意变体
412
+ }, {
413
+ product_id: 60737,
414
+ product_variant_id: 0 // 0 = 匹配任意变体
415
+ }],
416
+ operator: 'product_match'
417
+ }],
418
+ actionIds: ['1']
419
+ },
420
+ actions: [{
421
+ id: '1',
422
+ type: 'BUY_X_GET_Y_FREE',
423
+ value: {
424
+ buyQuantity: 1,
425
+ // 买X件
426
+ freeQuantity: 1 // 送Y件
427
+ },
428
+ valueType: 'object',
429
+ target: 'product',
430
+ priority: 1,
431
+ config: {
432
+ // 可累计(买2送2、买3送3...)
433
+ cumulative: true,
434
+ // 可选的赠品列表
435
+ giftProducts: [{
436
+ product_id: 60757,
437
+ product_variant_id: 0
438
+ }, {
439
+ product_id: 38782,
440
+ product_variant_id: 0
441
+ }, {
442
+ product_id: 60749,
443
+ product_variant_id: 27267
444
+ }]
445
+ }
446
+ }]
447
+ };
@@ -311,7 +311,7 @@ export declare class BookingByStepImpl extends BaseModule implements Module {
311
311
  date: string;
312
312
  status: string;
313
313
  week: string;
314
- weekNum: 0 | 2 | 1 | 3 | 5 | 4 | 6;
314
+ weekNum: 0 | 1 | 2 | 3 | 4 | 5 | 6;
315
315
  }[]>;
316
316
  submitTimeSlot(timeSlots: TimeSliceItem): void;
317
317
  private getScheduleDataByIds;
@@ -358,7 +358,7 @@ export declare class BookingByStepImpl extends BaseModule implements Module {
358
358
  };
359
359
  setOtherData(key: string, value: any): void;
360
360
  getOtherData(key: string): any;
361
- getProductTypeById(id: number): Promise<"normal" | "duration" | "session">;
361
+ getProductTypeById(id: number): Promise<"duration" | "session" | "normal">;
362
362
  /**
363
363
  * 提供给 UI 的方法,减轻 UI 层的计算压力,UI 层只需要传递 cartItemId 和 resourceCode 即返回对应的 renderList
364
364
  *
@@ -2390,6 +2390,7 @@ export var BookingByStepImpl = /*#__PURE__*/function (_BaseModule) {
2390
2390
  // 计算容量的辅助函数
2391
2391
  calculateCapacityFromCartItems = function calculateCapacityFromCartItems(items) {
2392
2392
  var _items$0$_resourceOri2;
2393
+ if (items.length === 0) return 0;
2393
2394
  // 还需要增加一个判断,maxCapacity 必须是我单个人里选择的资源只有同一种的情况下才可以累加,否则单个账号都是 1
2394
2395
  var firstResource = (_items$0$_resourceOri2 = items[0]._resourceOrigin) === null || _items$0$_resourceOri2 === void 0 ? void 0 : _items$0$_resourceOri2[0].id;
2395
2396
  var sameResourceLength = items.filter(function (item) {
@@ -311,7 +311,7 @@ export declare class BookingByStepImpl extends BaseModule implements Module {
311
311
  date: string;
312
312
  status: string;
313
313
  week: string;
314
- weekNum: 0 | 2 | 1 | 3 | 5 | 4 | 6;
314
+ weekNum: 0 | 1 | 2 | 3 | 4 | 5 | 6;
315
315
  }[]>;
316
316
  submitTimeSlot(timeSlots: TimeSliceItem): void;
317
317
  private getScheduleDataByIds;
@@ -358,7 +358,7 @@ export declare class BookingByStepImpl extends BaseModule implements Module {
358
358
  };
359
359
  setOtherData(key: string, value: any): void;
360
360
  getOtherData(key: string): any;
361
- getProductTypeById(id: number): Promise<"normal" | "duration" | "session">;
361
+ getProductTypeById(id: number): Promise<"duration" | "session" | "normal">;
362
362
  /**
363
363
  * 提供给 UI 的方法,减轻 UI 层的计算压力,UI 层只需要传递 cartItemId 和 resourceCode 即返回对应的 renderList
364
364
  *
@@ -1670,6 +1670,8 @@ var BookingByStepImpl = class extends import_BaseModule.BaseModule {
1670
1670
  }
1671
1671
  const calculateCapacityFromCartItems = (items) => {
1672
1672
  var _a2;
1673
+ if (items.length === 0)
1674
+ return 0;
1673
1675
  const firstResource = (_a2 = items[0]._resourceOrigin) == null ? void 0 : _a2[0].id;
1674
1676
  const sameResourceLength = items.filter((item) => {
1675
1677
  var _a3;
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "private": false,
3
3
  "name": "@pisell/pisellos",
4
- "version": "0.0.459",
4
+ "version": "0.0.460",
5
5
  "description": "一个可扩展的前端模块化SDK框架,支持插件系统",
6
6
  "main": "dist/index.js",
7
7
  "types": "dist/index.d.ts",