@pisell/pisellos 0.0.40 → 0.0.42

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.
@@ -21,9 +21,9 @@ function _getPrototypeOf(o) { _getPrototypeOf = Object.setPrototypeOf ? Object.g
21
21
  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; }
22
22
  function _toPropertyKey(t) { var i = _toPrimitive(t, "string"); return "symbol" == _typeof(i) ? i : String(i); }
23
23
  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); }
24
+ import { cloneDeep } from "lodash-es";
24
25
  import { BaseModule } from "../BaseModule";
25
26
  import { createCartItemOrigin, deleteResourceFromCartItem, deleteTimeFromCartItem, formatAccountToCartItem, formatDiscountToCartItem, formatNoteToCartItem, formatProductToCartItem, formatResourceToCartItem, getUniqueId } from "./utils";
26
- import { cloneDeep } from "lodash-es";
27
27
  export * from "./types";
28
28
 
29
29
  /**
@@ -84,6 +84,18 @@ export interface CartItem {
84
84
  resource_id?: string | number;
85
85
  /** 资源名称 */
86
86
  relation_form_name?: string;
87
+ /** 定金 */
88
+ deposit?: {
89
+ /** 定金总价 */
90
+ total: number | string;
91
+ /** 定金协议 */
92
+ protocols: {
93
+ /** 定金协议ID */
94
+ id: number;
95
+ /** 定金协议标题 */
96
+ title: string;
97
+ }[];
98
+ } | null;
87
99
  /** 原始数据 */
88
100
  _origin: any;
89
101
  /** 商品原始数据 */
@@ -2,6 +2,7 @@ import { Account } from "../Account";
2
2
  import { ProductData } from "../Product/types";
3
3
  import { Resource } from "../Resource/types";
4
4
  import { CartItem, IDiscount } from "./types";
5
+ import Decimal from "decimal.js";
5
6
  /**
6
7
  * 生成一个唯一的 ID
7
8
  */
@@ -141,6 +142,7 @@ export declare const formatBundleToOrigin: (bundle: any) => any;
141
142
  export declare const isNormalProduct: (product: any) => boolean;
142
143
  /**
143
144
  * 获取商品总价
145
+ * @description 当前总价计算基于商品数量为1
144
146
  * @param item 商品
145
147
  * @returns 商品总价
146
148
  */
@@ -148,9 +150,11 @@ export declare const getProductTotalPrice: (params: {
148
150
  product: ProductData;
149
151
  bundle?: any;
150
152
  options?: any;
153
+ num?: number;
151
154
  }) => number;
152
155
  /**
153
156
  * 获取商品原始总价
157
+ * @description 当前总价计算基于商品数量为1
154
158
  * @param item 商品
155
159
  * @returns 商品原始总价
156
160
  */
@@ -158,4 +162,43 @@ export declare const getProductOriginTotalPrice: (params: {
158
162
  product: ProductData;
159
163
  bundle?: any;
160
164
  options?: any;
165
+ num?: number;
161
166
  }) => number | undefined;
167
+ /**
168
+ * 获取商品定金
169
+ * @description 定金基于商品售价来算,最终乘商品数量
170
+ * 1、如果套餐主商品有定金规则,则定金为:(套餐商品总售价 * 定金百分比 + 定金固定金额)* 商品数量
171
+ * 2、如果套餐主商品没有定金规则,子商品有定金规则,则定金为:(套餐子商品售价 * 套餐子商品定金百分比 + 套餐子商品定金固定金额)* 商品数量,最终将所有子商品定金相加
172
+ * 3、普通商品/单规格商品/组合规则商品,定金规则:(商品总售价 * 定金百分比 + 定金固定金额)* 商品数量
173
+ * @param params 参数
174
+ * @returns 商品定金
175
+ */
176
+ export declare const getProductDeposit: (params: {
177
+ cartItem: CartItem;
178
+ product: ProductData;
179
+ bundle?: any;
180
+ options?: any;
181
+ num?: number;
182
+ }) => {
183
+ total: number;
184
+ protocols: {
185
+ id: number;
186
+ title: string;
187
+ }[];
188
+ } | null;
189
+ /**
190
+ * 计算商品定金
191
+ * @param params 参数
192
+ * @returns 商品定金
193
+ */
194
+ export declare const handleProductDeposit: (params: {
195
+ depositData: {
196
+ deposit_fixed?: string;
197
+ deposit_percentage?: string;
198
+ };
199
+ total: string | number;
200
+ num: number;
201
+ }) => {
202
+ depositTotal: Decimal;
203
+ result: boolean;
204
+ };
@@ -1,10 +1,18 @@
1
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 _toConsumableArray(arr) { return _arrayWithoutHoles(arr) || _iterableToArray(arr) || _unsupportedIterableToArray(arr) || _nonIterableSpread(); }
3
+ function _nonIterableSpread() { throw new TypeError("Invalid attempt to spread non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); }
4
+ 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); }
5
+ function _iterableToArray(iter) { if (typeof Symbol !== "undefined" && iter[Symbol.iterator] != null || iter["@@iterator"] != null) return Array.from(iter); }
6
+ function _arrayWithoutHoles(arr) { if (Array.isArray(arr)) return _arrayLikeToArray(arr); }
7
+ 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; }
2
8
  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
9
  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
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; }
5
11
  function _toPropertyKey(t) { var i = _toPrimitive(t, "string"); return "symbol" == _typeof(i) ? i : String(i); }
6
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); }
7
13
  import dayjs from "dayjs";
14
+ import Decimal from "decimal.js";
15
+
8
16
  /**
9
17
  * 生成一个唯一的 ID
10
18
  */
@@ -68,25 +76,29 @@ export var createCartItemOrigin = function createCartItemOrigin() {
68
76
  * @returns 格式化后的商品
69
77
  */
70
78
  export var formatProductToCartItem = function formatProductToCartItem(params) {
79
+ var _product$custom_depos;
71
80
  var cartItem = params.cartItem,
72
81
  product = params.product,
73
82
  bundle = params.bundle,
74
83
  options = params.options;
84
+ var num = 1; // 当前按照单个商品加入购物车
75
85
  if (product) {
76
86
  cartItem.id = product === null || product === void 0 ? void 0 : product.id;
77
87
  cartItem.name = product === null || product === void 0 ? void 0 : product.title;
78
88
  cartItem.price = product === null || product === void 0 ? void 0 : product.price;
89
+ cartItem.num = num;
79
90
  cartItem.total = getProductTotalPrice({
80
91
  product: product,
81
92
  bundle: bundle,
82
- options: options
93
+ options: options,
94
+ num: num
83
95
  });
84
96
  cartItem.origin_total = getProductOriginTotalPrice({
85
97
  product: product,
86
98
  bundle: bundle,
87
- options: options
99
+ options: options,
100
+ num: num
88
101
  });
89
- cartItem.num = 1;
90
102
  cartItem.image = product === null || product === void 0 ? void 0 : product.cover;
91
103
  cartItem.like_status = "common";
92
104
  cartItem.duration = product === null || product === void 0 ? void 0 : product.duration;
@@ -99,6 +111,17 @@ export var formatProductToCartItem = function formatProductToCartItem(params) {
99
111
  if (options !== null && options !== void 0 && options.length) {
100
112
  cartItem.options = formatOptions(options);
101
113
  }
114
+
115
+ // 如果商品有定金规则,则计算定金
116
+ if ((product === null || product === void 0 || (_product$custom_depos = product.custom_deposit_data) === null || _product$custom_depos === void 0 ? void 0 : _product$custom_depos.has_deposit) == 1) {
117
+ cartItem.deposit = getProductDeposit({
118
+ cartItem: cartItem,
119
+ product: product,
120
+ bundle: bundle,
121
+ options: options,
122
+ num: num
123
+ });
124
+ }
102
125
  var oringin = formatProductToCartItemOrigin(params);
103
126
  cartItem._origin = oringin;
104
127
  return cartItem;
@@ -429,6 +452,7 @@ export var isNormalProduct = function isNormalProduct(product) {
429
452
 
430
453
  /**
431
454
  * 获取商品总价
455
+ * @description 当前总价计算基于商品数量为1
432
456
  * @param item 商品
433
457
  * @returns 商品总价
434
458
  */
@@ -436,6 +460,7 @@ export var getProductTotalPrice = function getProductTotalPrice(params) {
436
460
  var product = params.product,
437
461
  bundle = params.bundle,
438
462
  options = params.options;
463
+ var num = params.num || 1;
439
464
  var price = Number(product.price);
440
465
  if (bundle !== null && bundle !== void 0 && bundle.length) {
441
466
  price = bundle.reduce(function (accumulator, currentValue) {
@@ -449,11 +474,12 @@ export var getProductTotalPrice = function getProductTotalPrice(params) {
449
474
  return accumulator + Number(currentValue.price) * Number(currentValue.num);
450
475
  }, price);
451
476
  }
452
- return price;
477
+ return price * num;
453
478
  };
454
479
 
455
480
  /**
456
481
  * 获取商品原始总价
482
+ * @description 当前总价计算基于商品数量为1
457
483
  * @param item 商品
458
484
  * @returns 商品原始总价
459
485
  */
@@ -461,6 +487,7 @@ export var getProductOriginTotalPrice = function getProductOriginTotalPrice(para
461
487
  var product = params.product,
462
488
  bundle = params.bundle,
463
489
  options = params.options;
490
+ var num = (params === null || params === void 0 ? void 0 : params.num) || 1;
464
491
  var price = Number(product.original_price);
465
492
  if (isNaN(price) || price === 0) {
466
493
  return undefined;
@@ -477,5 +504,96 @@ export var getProductOriginTotalPrice = function getProductOriginTotalPrice(para
477
504
  return accumulator + Number(currentValue.price) * Number(currentValue.num);
478
505
  }, price);
479
506
  }
480
- return price;
507
+ return price * num;
508
+ };
509
+
510
+ /**
511
+ * 获取商品定金
512
+ * @description 定金基于商品售价来算,最终乘商品数量
513
+ * 1、如果套餐主商品有定金规则,则定金为:(套餐商品总售价 * 定金百分比 + 定金固定金额)* 商品数量
514
+ * 2、如果套餐主商品没有定金规则,子商品有定金规则,则定金为:(套餐子商品售价 * 套餐子商品定金百分比 + 套餐子商品定金固定金额)* 商品数量,最终将所有子商品定金相加
515
+ * 3、普通商品/单规格商品/组合规则商品,定金规则:(商品总售价 * 定金百分比 + 定金固定金额)* 商品数量
516
+ * @param params 参数
517
+ * @returns 商品定金
518
+ */
519
+ export var getProductDeposit = function getProductDeposit(params) {
520
+ var _product$custom_depos2;
521
+ var cartItem = params.cartItem,
522
+ product = params.product,
523
+ bundle = params.bundle,
524
+ options = params.options;
525
+ var num = (params === null || params === void 0 ? void 0 : params.num) || 1;
526
+
527
+ // 判定商品是否有定金规则
528
+ if ((product === null || product === void 0 || (_product$custom_depos2 = product.custom_deposit_data) === null || _product$custom_depos2 === void 0 ? void 0 : _product$custom_depos2.has_deposit) == 1) {
529
+ var protocols = [];
530
+ var total = new Decimal(0);
531
+ var _ref = (product === null || product === void 0 ? void 0 : product.custom_deposit_data) || {},
532
+ deposit_fixed = _ref.deposit_fixed,
533
+ deposit_percentage = _ref.deposit_percentage,
534
+ deposit_policy_data = _ref.deposit_policy_data;
535
+ // 协议数据
536
+ protocols.push.apply(protocols, _toConsumableArray(deposit_policy_data || []));
537
+ // 判断主商品是否有定金规则
538
+ if (typeof deposit_fixed === "string" && typeof deposit_percentage === "string") {
539
+ var _handleProductDeposit = handleProductDeposit({
540
+ depositData: product === null || product === void 0 ? void 0 : product.custom_deposit_data,
541
+ total: cartItem.total || 0,
542
+ num: num
543
+ }),
544
+ depositTotal = _handleProductDeposit.depositTotal,
545
+ result = _handleProductDeposit.result;
546
+ if (result) {
547
+ total = depositTotal;
548
+ }
549
+ } else {
550
+ if (bundle !== null && bundle !== void 0 && bundle.length) {
551
+ total = bundle.reduce(function (accumulator, currentBundleProduct) {
552
+ var _handleProductDeposit2 = handleProductDeposit({
553
+ depositData: currentBundleProduct === null || currentBundleProduct === void 0 ? void 0 : currentBundleProduct.custom_deposit_data,
554
+ total: currentBundleProduct.price,
555
+ num: currentBundleProduct.num
556
+ }),
557
+ depositTotal = _handleProductDeposit2.depositTotal,
558
+ result = _handleProductDeposit2.result;
559
+ if (result) {
560
+ return accumulator.plus(depositTotal);
561
+ }
562
+ return accumulator;
563
+ }, new Decimal(0));
564
+ }
565
+ }
566
+ return {
567
+ total: total.toNumber(),
568
+ protocols: protocols
569
+ };
570
+ }
571
+ return null;
572
+ };
573
+
574
+ /**
575
+ * 计算商品定金
576
+ * @param params 参数
577
+ * @returns 商品定金
578
+ */
579
+ export var handleProductDeposit = function handleProductDeposit(params) {
580
+ var depositData = params.depositData,
581
+ total = params.total,
582
+ num = params.num;
583
+ var deposit_fixed = depositData.deposit_fixed,
584
+ deposit_percentage = depositData.deposit_percentage;
585
+ if (typeof deposit_fixed === "string" && typeof deposit_percentage === "string") {
586
+ var depositFixed = Decimal(deposit_fixed || 0);
587
+ var depositPercent = Decimal(deposit_percentage || 0);
588
+ var productTotalPrice = Decimal(total || 0);
589
+ var deposit = depositFixed.plus(depositPercent.times(productTotalPrice));
590
+ return {
591
+ depositTotal: deposit.times(num),
592
+ result: true
593
+ };
594
+ }
595
+ return {
596
+ result: false,
597
+ depositTotal: new Decimal(0)
598
+ };
481
599
  };
@@ -156,6 +156,22 @@ export interface ProductData {
156
156
  capacity?: any;
157
157
  /** 商品套餐 */
158
158
  product_bundle?: any;
159
+ /** 商品定金信息 */
160
+ custom_deposit_data?: {
161
+ /** 商品是否包含定金 1:是 0:否 */
162
+ has_deposit: 0 | 1;
163
+ /** 定金固定金额 */
164
+ deposit_fixed?: string;
165
+ /** 定金百分比 */
166
+ deposit_percentage?: string;
167
+ /** 定金协议数据 */
168
+ deposit_policy_data?: Array<{
169
+ /** 定金协议id */
170
+ id: number;
171
+ /** 定金协议标题 */
172
+ title: string;
173
+ }>;
174
+ };
159
175
  }
160
176
  /**
161
177
  * 商品媒体信息接口
@@ -7,11 +7,18 @@ export declare class SummaryModule extends BaseModule implements Module, ISummar
7
7
  protected defaultVersion: string;
8
8
  private shopStore;
9
9
  private store;
10
+ private request;
10
11
  private cacheId;
11
12
  private openCache;
12
13
  private fatherModule;
13
14
  constructor(name?: string, version?: string);
14
15
  initialize(core: PisellCore, options: ModuleOptions): Promise<void>;
15
16
  getSummary(cartItems: CartItem[]): Promise<ISummaryState["summary"]>;
17
+ /**
18
+ * 获取协议
19
+ * @param protocolId 协议ID
20
+ * @returns 协议
21
+ */
22
+ getProtocol(protocolId: string): Promise<any>;
16
23
  storeChange(): void;
17
24
  }
@@ -28,6 +28,7 @@ export var SummaryModule = /*#__PURE__*/function (_BaseModule) {
28
28
  _defineProperty(_assertThisInitialized(_this), "defaultVersion", "1.0.0");
29
29
  _defineProperty(_assertThisInitialized(_this), "shopStore", void 0);
30
30
  _defineProperty(_assertThisInitialized(_this), "store", void 0);
31
+ _defineProperty(_assertThisInitialized(_this), "request", void 0);
31
32
  _defineProperty(_assertThisInitialized(_this), "cacheId", void 0);
32
33
  _defineProperty(_assertThisInitialized(_this), "openCache", false);
33
34
  _defineProperty(_assertThisInitialized(_this), "fatherModule", void 0);
@@ -44,6 +45,7 @@ export var SummaryModule = /*#__PURE__*/function (_BaseModule) {
44
45
  this.core = core;
45
46
  this.store = options.store;
46
47
  this.shopStore = this.core.getPlugin("shopStore");
48
+ this.request = this.core.getPlugin("request");
47
49
  if (options.initialState) {
48
50
  this.store.summary = options.initialState.summary;
49
51
  }
@@ -55,11 +57,11 @@ export var SummaryModule = /*#__PURE__*/function (_BaseModule) {
55
57
  this.fatherModule = options.otherParams.fatherModule;
56
58
  }
57
59
  if (this.shopStore) {
58
- _context.next = 8;
60
+ _context.next = 9;
59
61
  break;
60
62
  }
61
63
  throw new Error("SummaryModule 需要 shopStore 插件支持");
62
- case 8:
64
+ case 9:
63
65
  case "end":
64
66
  return _context.stop();
65
67
  }
@@ -94,6 +96,30 @@ export var SummaryModule = /*#__PURE__*/function (_BaseModule) {
94
96
  }
95
97
  return getSummary;
96
98
  }()
99
+ /**
100
+ * 获取协议
101
+ * @param protocolId 协议ID
102
+ * @returns 协议
103
+ */
104
+ }, {
105
+ key: "getProtocol",
106
+ value: (function () {
107
+ var _getProtocol = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee3(protocolId) {
108
+ return _regeneratorRuntime().wrap(function _callee3$(_context3) {
109
+ while (1) switch (_context3.prev = _context3.next) {
110
+ case 0:
111
+ return _context3.abrupt("return", this.request.get("/shop-policy/".concat(protocolId)));
112
+ case 1:
113
+ case "end":
114
+ return _context3.stop();
115
+ }
116
+ }, _callee3, this);
117
+ }));
118
+ function getProtocol(_x4) {
119
+ return _getProtocol.apply(this, arguments);
120
+ }
121
+ return getProtocol;
122
+ }())
97
123
  }, {
98
124
  key: "storeChange",
99
125
  value: function storeChange() {
@@ -102,7 +128,7 @@ export var SummaryModule = /*#__PURE__*/function (_BaseModule) {
102
128
  cacheId: this.cacheId,
103
129
  fatherModule: this.fatherModule,
104
130
  store: this.store,
105
- cacheKey: ['summary']
131
+ cacheKey: ["summary"]
106
132
  });
107
133
  }
108
134
  }
@@ -1,13 +1,34 @@
1
1
  import { CartItem } from "../Cart/types";
2
2
  export interface ISummaryState {
3
3
  summary: {
4
+ /** 商品总价 */
4
5
  subtotal: string | number;
6
+ /** 最终总价 */
5
7
  total: string | number;
8
+ /** 税率标题 */
6
9
  taxTitle?: string;
10
+ /** 商品总费率 */
7
11
  totalTaxFee?: string | number;
12
+ /** 商品价格是否包含费率 */
8
13
  isPriceIncludeTax?: 0 | 1;
14
+ /** 定金 */
15
+ deposit?: {
16
+ /** 定金总价 */
17
+ total?: string | number;
18
+ /** */
19
+ policies?: IProtocol[];
20
+ };
9
21
  };
10
22
  }
23
+ /**
24
+ * 定金协议
25
+ */
26
+ export interface IProtocol {
27
+ /** 协议名称 */
28
+ title: string;
29
+ /** 协议id */
30
+ id: string;
31
+ }
11
32
  export interface ISummaryModuleAPI {
12
33
  getSummary(cartItems: CartItem[]): Promise<ISummaryState["summary"]>;
13
34
  }
@@ -17,3 +17,13 @@ export declare const calculateSubtotal: (items: CartItem[]) => string;
17
17
  * @Author: xiangfeng.xue
18
18
  */
19
19
  export declare const calculateTaxFee: (shopInfo: any, items: CartItem[]) => Decimal | "0.00";
20
+ /**
21
+ * @title: 计算定金
22
+ * @param items - 购物车商品数组
23
+ * @returns 定金字符串,保留2位小数
24
+ */
25
+ export declare const calculateDeposit: (items: CartItem[]) => {
26
+ total: string;
27
+ protocols: any[];
28
+ hasDeposit: never;
29
+ } | undefined;
@@ -5,12 +5,16 @@ export var calculatePriceDetails = function calculatePriceDetails(shopInfo, item
5
5
 
6
6
  // 计算总价
7
7
  var total = shopInfo !== null && shopInfo !== void 0 && shopInfo.is_price_include_tax ? subtotal : subtotal.plus(totalTaxFee);
8
+
9
+ // 计算定金
10
+ var deposit = calculateDeposit(items);
8
11
  return {
9
12
  subtotal: subtotal.toFixed(2),
10
13
  total: total.toFixed(2),
11
14
  taxTitle: shopInfo === null || shopInfo === void 0 ? void 0 : shopInfo.tax_title,
12
15
  totalTaxFee: totalTaxFee.toFixed(2),
13
- isPriceIncludeTax: shopInfo === null || shopInfo === void 0 ? void 0 : shopInfo.is_price_include_tax
16
+ isPriceIncludeTax: shopInfo === null || shopInfo === void 0 ? void 0 : shopInfo.is_price_include_tax,
17
+ deposit: deposit
14
18
  };
15
19
  };
16
20
 
@@ -52,4 +56,42 @@ export var calculateTaxFee = function calculateTaxFee(shopInfo, items) {
52
56
  return sum.plus(productTaxRate);
53
57
  }, new Decimal(0));
54
58
  return totalTaxFee;
59
+ };
60
+
61
+ /**
62
+ * @title: 计算定金
63
+ * @param items - 购物车商品数组
64
+ * @returns 定金字符串,保留2位小数
65
+ */
66
+ export var calculateDeposit = function calculateDeposit(items) {
67
+ if (!(items !== null && items !== void 0 && items.length)) {
68
+ return undefined;
69
+ }
70
+ var protocols = [];
71
+ var hasDeposit = false;
72
+ var total = items.reduce(function (sum, item) {
73
+ if (item !== null && item !== void 0 && item.deposit) {
74
+ var _item$deposit, _item$deposit2;
75
+ hasDeposit = true;
76
+ var cartItemTotalPrice = new Decimal((item === null || item === void 0 || (_item$deposit = item.deposit) === null || _item$deposit === void 0 ? void 0 : _item$deposit.total) || 0);
77
+ // 将协议数据去重合并
78
+ item === null || item === void 0 || (_item$deposit2 = item.deposit) === null || _item$deposit2 === void 0 || (_item$deposit2 = _item$deposit2.protocols) === null || _item$deposit2 === void 0 || _item$deposit2.forEach(function (protocol) {
79
+ if (protocols.findIndex(function (p) {
80
+ return p.id === protocol.id;
81
+ }) === -1) {
82
+ protocols.push(protocol);
83
+ }
84
+ });
85
+ return sum.plus(cartItemTotalPrice);
86
+ }
87
+ return sum;
88
+ }, new Decimal(0));
89
+ if (hasDeposit) {
90
+ return {
91
+ total: total.toFixed(2),
92
+ protocols: protocols,
93
+ hasDeposit: hasDeposit
94
+ };
95
+ }
96
+ return undefined;
55
97
  };
@@ -65,6 +65,10 @@ export declare class BookingByStepImpl extends BaseModule implements Module {
65
65
  taxTitle?: string | undefined;
66
66
  totalTaxFee?: string | number | undefined;
67
67
  isPriceIncludeTax?: 0 | 1 | undefined;
68
+ deposit?: {
69
+ total?: string | number | undefined;
70
+ policies?: import("../../modules/Summary/types").IProtocol[] | undefined;
71
+ } | undefined;
68
72
  }>;
69
73
  getProducts(): Promise<ProductData[]>;
70
74
  setLoginAccount(accountId: string, accountInfo: Account): Promise<void>;
@@ -23,9 +23,9 @@ __export(Cart_exports, {
23
23
  CartModule: () => CartModule
24
24
  });
25
25
  module.exports = __toCommonJS(Cart_exports);
26
+ var import_lodash_es = require("lodash-es");
26
27
  var import_BaseModule = require("../BaseModule");
27
28
  var import_utils = require("./utils");
28
- var import_lodash_es = require("lodash-es");
29
29
  __reExport(Cart_exports, require("./types"), module.exports);
30
30
  var CartModule = class extends import_BaseModule.BaseModule {
31
31
  constructor(name, version) {
@@ -172,9 +172,7 @@ var CartModule = class extends import_BaseModule.BaseModule {
172
172
  * 根据账户ID获取购物车
173
173
  */
174
174
  getCartByAccount(account_id) {
175
- return this.store.list.filter(
176
- (item) => item.holder_id === account_id
177
- );
175
+ return this.store.list.filter((item) => item.holder_id === account_id);
178
176
  }
179
177
  /**
180
178
  * 移除购物车商品
@@ -188,9 +186,7 @@ var CartModule = class extends import_BaseModule.BaseModule {
188
186
  * 根据账户ID清除购物车
189
187
  */
190
188
  clearCartByAccount(account_id) {
191
- const items = this.store.list.filter(
192
- (item) => item.holder_id !== account_id
193
- );
189
+ const items = this.store.list.filter((item) => item.holder_id !== account_id);
194
190
  this.store.list = [...items || []];
195
191
  }
196
192
  /**
@@ -216,9 +212,7 @@ var CartModule = class extends import_BaseModule.BaseModule {
216
212
  storeChange() {
217
213
  if (this.openCache) {
218
214
  const store = (0, import_lodash_es.cloneDeep)(this.store);
219
- store.list = store.list.filter(
220
- (item, index, self) => index === self.findIndex((t) => t._id === item._id)
221
- );
215
+ store.list = store.list.filter((item, index, self) => index === self.findIndex((t) => t._id === item._id));
222
216
  this.checkSaveCache({
223
217
  cacheId: this.cacheId,
224
218
  fatherModule: this.fatherModule,
@@ -84,6 +84,18 @@ export interface CartItem {
84
84
  resource_id?: string | number;
85
85
  /** 资源名称 */
86
86
  relation_form_name?: string;
87
+ /** 定金 */
88
+ deposit?: {
89
+ /** 定金总价 */
90
+ total: number | string;
91
+ /** 定金协议 */
92
+ protocols: {
93
+ /** 定金协议ID */
94
+ id: number;
95
+ /** 定金协议标题 */
96
+ title: string;
97
+ }[];
98
+ } | null;
87
99
  /** 原始数据 */
88
100
  _origin: any;
89
101
  /** 商品原始数据 */
@@ -2,6 +2,7 @@ import { Account } from "../Account";
2
2
  import { ProductData } from "../Product/types";
3
3
  import { Resource } from "../Resource/types";
4
4
  import { CartItem, IDiscount } from "./types";
5
+ import Decimal from "decimal.js";
5
6
  /**
6
7
  * 生成一个唯一的 ID
7
8
  */
@@ -141,6 +142,7 @@ export declare const formatBundleToOrigin: (bundle: any) => any;
141
142
  export declare const isNormalProduct: (product: any) => boolean;
142
143
  /**
143
144
  * 获取商品总价
145
+ * @description 当前总价计算基于商品数量为1
144
146
  * @param item 商品
145
147
  * @returns 商品总价
146
148
  */
@@ -148,9 +150,11 @@ export declare const getProductTotalPrice: (params: {
148
150
  product: ProductData;
149
151
  bundle?: any;
150
152
  options?: any;
153
+ num?: number;
151
154
  }) => number;
152
155
  /**
153
156
  * 获取商品原始总价
157
+ * @description 当前总价计算基于商品数量为1
154
158
  * @param item 商品
155
159
  * @returns 商品原始总价
156
160
  */
@@ -158,4 +162,43 @@ export declare const getProductOriginTotalPrice: (params: {
158
162
  product: ProductData;
159
163
  bundle?: any;
160
164
  options?: any;
165
+ num?: number;
161
166
  }) => number | undefined;
167
+ /**
168
+ * 获取商品定金
169
+ * @description 定金基于商品售价来算,最终乘商品数量
170
+ * 1、如果套餐主商品有定金规则,则定金为:(套餐商品总售价 * 定金百分比 + 定金固定金额)* 商品数量
171
+ * 2、如果套餐主商品没有定金规则,子商品有定金规则,则定金为:(套餐子商品售价 * 套餐子商品定金百分比 + 套餐子商品定金固定金额)* 商品数量,最终将所有子商品定金相加
172
+ * 3、普通商品/单规格商品/组合规则商品,定金规则:(商品总售价 * 定金百分比 + 定金固定金额)* 商品数量
173
+ * @param params 参数
174
+ * @returns 商品定金
175
+ */
176
+ export declare const getProductDeposit: (params: {
177
+ cartItem: CartItem;
178
+ product: ProductData;
179
+ bundle?: any;
180
+ options?: any;
181
+ num?: number;
182
+ }) => {
183
+ total: number;
184
+ protocols: {
185
+ id: number;
186
+ title: string;
187
+ }[];
188
+ } | null;
189
+ /**
190
+ * 计算商品定金
191
+ * @param params 参数
192
+ * @returns 商品定金
193
+ */
194
+ export declare const handleProductDeposit: (params: {
195
+ depositData: {
196
+ deposit_fixed?: string;
197
+ deposit_percentage?: string;
198
+ };
199
+ total: string | number;
200
+ num: number;
201
+ }) => {
202
+ depositTotal: Decimal;
203
+ result: boolean;
204
+ };
@@ -46,13 +46,16 @@ __export(utils_exports, {
46
46
  formatProductToCartItemOrigin: () => formatProductToCartItemOrigin,
47
47
  formatResourceToCartItem: () => formatResourceToCartItem,
48
48
  formatResourceToCartItemOrigin: () => formatResourceToCartItemOrigin,
49
+ getProductDeposit: () => getProductDeposit,
49
50
  getProductOriginTotalPrice: () => getProductOriginTotalPrice,
50
51
  getProductTotalPrice: () => getProductTotalPrice,
51
52
  getUniqueId: () => getUniqueId,
53
+ handleProductDeposit: () => handleProductDeposit,
52
54
  isNormalProduct: () => isNormalProduct
53
55
  });
54
56
  module.exports = __toCommonJS(utils_exports);
55
57
  var import_dayjs = __toESM(require("dayjs"));
58
+ var import_decimal = __toESM(require("decimal.js"));
56
59
  var getUniqueId = (prefix = "", maxLength = 11) => {
57
60
  return prefix + (Math.random() + "").replace(/\D/g, "").substring(0, maxLength);
58
61
  };
@@ -100,14 +103,16 @@ var createCartItemOrigin = () => {
100
103
  };
101
104
  };
102
105
  var formatProductToCartItem = (params) => {
106
+ var _a;
103
107
  const { cartItem, product, bundle, options } = params;
108
+ const num = 1;
104
109
  if (product) {
105
110
  cartItem.id = product == null ? void 0 : product.id;
106
111
  cartItem.name = product == null ? void 0 : product.title;
107
112
  cartItem.price = product == null ? void 0 : product.price;
108
- cartItem.total = getProductTotalPrice({ product, bundle, options });
109
- cartItem.origin_total = getProductOriginTotalPrice({ product, bundle, options });
110
- cartItem.num = 1;
113
+ cartItem.num = num;
114
+ cartItem.total = getProductTotalPrice({ product, bundle, options, num });
115
+ cartItem.origin_total = getProductOriginTotalPrice({ product, bundle, options, num });
111
116
  cartItem.image = product == null ? void 0 : product.cover;
112
117
  cartItem.like_status = "common";
113
118
  cartItem.duration = product == null ? void 0 : product.duration;
@@ -120,6 +125,9 @@ var formatProductToCartItem = (params) => {
120
125
  if (options == null ? void 0 : options.length) {
121
126
  cartItem.options = formatOptions(options);
122
127
  }
128
+ if (((_a = product == null ? void 0 : product.custom_deposit_data) == null ? void 0 : _a.has_deposit) == 1) {
129
+ cartItem.deposit = getProductDeposit({ cartItem, product, bundle, options, num });
130
+ }
123
131
  const oringin = formatProductToCartItemOrigin(params);
124
132
  cartItem._origin = oringin;
125
133
  return cartItem;
@@ -365,6 +373,7 @@ var isNormalProduct = (product) => {
365
373
  };
366
374
  var getProductTotalPrice = (params) => {
367
375
  const { product, bundle, options } = params;
376
+ const num = params.num || 1;
368
377
  let price = Number(product.price);
369
378
  if (bundle == null ? void 0 : bundle.length) {
370
379
  price = bundle.reduce((accumulator, currentValue) => {
@@ -376,10 +385,11 @@ var getProductTotalPrice = (params) => {
376
385
  return accumulator + Number(currentValue.price) * Number(currentValue.num);
377
386
  }, price);
378
387
  }
379
- return price;
388
+ return price * num;
380
389
  };
381
390
  var getProductOriginTotalPrice = (params) => {
382
391
  const { product, bundle, options } = params;
392
+ const num = (params == null ? void 0 : params.num) || 1;
383
393
  let price = Number(product.original_price);
384
394
  if (isNaN(price) || price === 0) {
385
395
  return void 0;
@@ -394,7 +404,56 @@ var getProductOriginTotalPrice = (params) => {
394
404
  return accumulator + Number(currentValue.price) * Number(currentValue.num);
395
405
  }, price);
396
406
  }
397
- return price;
407
+ return price * num;
408
+ };
409
+ var getProductDeposit = (params) => {
410
+ var _a;
411
+ const { cartItem, product, bundle, options } = params;
412
+ const num = (params == null ? void 0 : params.num) || 1;
413
+ if (((_a = product == null ? void 0 : product.custom_deposit_data) == null ? void 0 : _a.has_deposit) == 1) {
414
+ const protocols = [];
415
+ let total = new import_decimal.default(0);
416
+ const { deposit_fixed, deposit_percentage, deposit_policy_data } = (product == null ? void 0 : product.custom_deposit_data) || {};
417
+ protocols.push(...deposit_policy_data || []);
418
+ if (typeof deposit_fixed === "string" && typeof deposit_percentage === "string") {
419
+ const { depositTotal, result } = handleProductDeposit({
420
+ depositData: product == null ? void 0 : product.custom_deposit_data,
421
+ total: cartItem.total || 0,
422
+ num
423
+ });
424
+ if (result) {
425
+ total = depositTotal;
426
+ }
427
+ } else {
428
+ if (bundle == null ? void 0 : bundle.length) {
429
+ total = bundle.reduce((accumulator, currentBundleProduct) => {
430
+ const { depositTotal, result } = handleProductDeposit({
431
+ depositData: currentBundleProduct == null ? void 0 : currentBundleProduct.custom_deposit_data,
432
+ total: currentBundleProduct.price,
433
+ num: currentBundleProduct.num
434
+ });
435
+ if (result) {
436
+ return accumulator.plus(depositTotal);
437
+ }
438
+ return accumulator;
439
+ }, new import_decimal.default(0));
440
+ }
441
+ }
442
+ return { total: total.toNumber(), protocols };
443
+ }
444
+ return null;
445
+ };
446
+ var handleProductDeposit = (params) => {
447
+ const { depositData, total, num } = params;
448
+ const { deposit_fixed, deposit_percentage } = depositData;
449
+ if (typeof deposit_fixed === "string" && typeof deposit_percentage === "string") {
450
+ const depositFixed = (0, import_decimal.default)(deposit_fixed || 0);
451
+ const depositPercent = (0, import_decimal.default)(deposit_percentage || 0);
452
+ const productTotalPrice = (0, import_decimal.default)(total || 0);
453
+ const deposit = depositFixed.plus(depositPercent.times(productTotalPrice));
454
+ return { depositTotal: deposit.times(num), result: true };
455
+ }
456
+ return { result: false, depositTotal: new import_decimal.default(0) };
398
457
  };
399
458
  // Annotate the CommonJS export names for ESM import in node:
400
459
  0 && (module.exports = {
@@ -415,8 +474,10 @@ var getProductOriginTotalPrice = (params) => {
415
474
  formatProductToCartItemOrigin,
416
475
  formatResourceToCartItem,
417
476
  formatResourceToCartItemOrigin,
477
+ getProductDeposit,
418
478
  getProductOriginTotalPrice,
419
479
  getProductTotalPrice,
420
480
  getUniqueId,
481
+ handleProductDeposit,
421
482
  isNormalProduct
422
483
  });
@@ -156,6 +156,22 @@ export interface ProductData {
156
156
  capacity?: any;
157
157
  /** 商品套餐 */
158
158
  product_bundle?: any;
159
+ /** 商品定金信息 */
160
+ custom_deposit_data?: {
161
+ /** 商品是否包含定金 1:是 0:否 */
162
+ has_deposit: 0 | 1;
163
+ /** 定金固定金额 */
164
+ deposit_fixed?: string;
165
+ /** 定金百分比 */
166
+ deposit_percentage?: string;
167
+ /** 定金协议数据 */
168
+ deposit_policy_data?: Array<{
169
+ /** 定金协议id */
170
+ id: number;
171
+ /** 定金协议标题 */
172
+ title: string;
173
+ }>;
174
+ };
159
175
  }
160
176
  /**
161
177
  * 商品媒体信息接口
@@ -7,11 +7,18 @@ export declare class SummaryModule extends BaseModule implements Module, ISummar
7
7
  protected defaultVersion: string;
8
8
  private shopStore;
9
9
  private store;
10
+ private request;
10
11
  private cacheId;
11
12
  private openCache;
12
13
  private fatherModule;
13
14
  constructor(name?: string, version?: string);
14
15
  initialize(core: PisellCore, options: ModuleOptions): Promise<void>;
15
16
  getSummary(cartItems: CartItem[]): Promise<ISummaryState["summary"]>;
17
+ /**
18
+ * 获取协议
19
+ * @param protocolId 协议ID
20
+ * @returns 协议
21
+ */
22
+ getProtocol(protocolId: string): Promise<any>;
16
23
  storeChange(): void;
17
24
  }
@@ -36,6 +36,7 @@ var SummaryModule = class extends import_BaseModule.BaseModule {
36
36
  this.core = core;
37
37
  this.store = options.store;
38
38
  this.shopStore = this.core.getPlugin("shopStore");
39
+ this.request = this.core.getPlugin("request");
39
40
  if (options.initialState) {
40
41
  this.store.summary = options.initialState.summary;
41
42
  }
@@ -57,9 +58,22 @@ var SummaryModule = class extends import_BaseModule.BaseModule {
57
58
  this.store.summary = summary;
58
59
  return this.store.summary;
59
60
  }
61
+ /**
62
+ * 获取协议
63
+ * @param protocolId 协议ID
64
+ * @returns 协议
65
+ */
66
+ async getProtocol(protocolId) {
67
+ return this.request.get(`/shop-policy/${protocolId}`);
68
+ }
60
69
  storeChange() {
61
70
  if (this.openCache) {
62
- this.checkSaveCache({ cacheId: this.cacheId, fatherModule: this.fatherModule, store: this.store, cacheKey: ["summary"] });
71
+ this.checkSaveCache({
72
+ cacheId: this.cacheId,
73
+ fatherModule: this.fatherModule,
74
+ store: this.store,
75
+ cacheKey: ["summary"]
76
+ });
63
77
  }
64
78
  }
65
79
  };
@@ -1,13 +1,34 @@
1
1
  import { CartItem } from "../Cart/types";
2
2
  export interface ISummaryState {
3
3
  summary: {
4
+ /** 商品总价 */
4
5
  subtotal: string | number;
6
+ /** 最终总价 */
5
7
  total: string | number;
8
+ /** 税率标题 */
6
9
  taxTitle?: string;
10
+ /** 商品总费率 */
7
11
  totalTaxFee?: string | number;
12
+ /** 商品价格是否包含费率 */
8
13
  isPriceIncludeTax?: 0 | 1;
14
+ /** 定金 */
15
+ deposit?: {
16
+ /** 定金总价 */
17
+ total?: string | number;
18
+ /** */
19
+ policies?: IProtocol[];
20
+ };
9
21
  };
10
22
  }
23
+ /**
24
+ * 定金协议
25
+ */
26
+ export interface IProtocol {
27
+ /** 协议名称 */
28
+ title: string;
29
+ /** 协议id */
30
+ id: string;
31
+ }
11
32
  export interface ISummaryModuleAPI {
12
33
  getSummary(cartItems: CartItem[]): Promise<ISummaryState["summary"]>;
13
34
  }
@@ -17,3 +17,13 @@ export declare const calculateSubtotal: (items: CartItem[]) => string;
17
17
  * @Author: xiangfeng.xue
18
18
  */
19
19
  export declare const calculateTaxFee: (shopInfo: any, items: CartItem[]) => Decimal | "0.00";
20
+ /**
21
+ * @title: 计算定金
22
+ * @param items - 购物车商品数组
23
+ * @returns 定金字符串,保留2位小数
24
+ */
25
+ export declare const calculateDeposit: (items: CartItem[]) => {
26
+ total: string;
27
+ protocols: any[];
28
+ hasDeposit: never;
29
+ } | undefined;
@@ -29,6 +29,7 @@ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: tru
29
29
  // src/modules/Summary/utils.ts
30
30
  var utils_exports = {};
31
31
  __export(utils_exports, {
32
+ calculateDeposit: () => calculateDeposit,
32
33
  calculatePriceDetails: () => calculatePriceDetails,
33
34
  calculateSubtotal: () => calculateSubtotal,
34
35
  calculateTaxFee: () => calculateTaxFee
@@ -39,12 +40,14 @@ var calculatePriceDetails = (shopInfo, items) => {
39
40
  const subtotal = new import_decimal.default(calculateSubtotal(items));
40
41
  const totalTaxFee = new import_decimal.default(calculateTaxFee(shopInfo, items));
41
42
  const total = (shopInfo == null ? void 0 : shopInfo.is_price_include_tax) ? subtotal : subtotal.plus(totalTaxFee);
43
+ const deposit = calculateDeposit(items);
42
44
  return {
43
45
  subtotal: subtotal.toFixed(2),
44
46
  total: total.toFixed(2),
45
47
  taxTitle: shopInfo == null ? void 0 : shopInfo.tax_title,
46
48
  totalTaxFee: totalTaxFee.toFixed(2),
47
- isPriceIncludeTax: shopInfo == null ? void 0 : shopInfo.is_price_include_tax
49
+ isPriceIncludeTax: shopInfo == null ? void 0 : shopInfo.is_price_include_tax,
50
+ deposit
48
51
  };
49
52
  };
50
53
  var calculateSubtotal = (items) => {
@@ -70,8 +73,34 @@ var calculateTaxFee = (shopInfo, items) => {
70
73
  }, new import_decimal.default(0));
71
74
  return totalTaxFee;
72
75
  };
76
+ var calculateDeposit = (items) => {
77
+ if (!(items == null ? void 0 : items.length)) {
78
+ return void 0;
79
+ }
80
+ const protocols = [];
81
+ let hasDeposit = false;
82
+ const total = items.reduce((sum, item) => {
83
+ var _a, _b, _c;
84
+ if (item == null ? void 0 : item.deposit) {
85
+ hasDeposit = true;
86
+ const cartItemTotalPrice = new import_decimal.default(((_a = item == null ? void 0 : item.deposit) == null ? void 0 : _a.total) || 0);
87
+ (_c = (_b = item == null ? void 0 : item.deposit) == null ? void 0 : _b.protocols) == null ? void 0 : _c.forEach((protocol) => {
88
+ if (protocols.findIndex((p) => p.id === protocol.id) === -1) {
89
+ protocols.push(protocol);
90
+ }
91
+ });
92
+ return sum.plus(cartItemTotalPrice);
93
+ }
94
+ return sum;
95
+ }, new import_decimal.default(0));
96
+ if (hasDeposit) {
97
+ return { total: total.toFixed(2), protocols, hasDeposit };
98
+ }
99
+ return void 0;
100
+ };
73
101
  // Annotate the CommonJS export names for ESM import in node:
74
102
  0 && (module.exports = {
103
+ calculateDeposit,
75
104
  calculatePriceDetails,
76
105
  calculateSubtotal,
77
106
  calculateTaxFee
@@ -65,6 +65,10 @@ export declare class BookingByStepImpl extends BaseModule implements Module {
65
65
  taxTitle?: string | undefined;
66
66
  totalTaxFee?: string | number | undefined;
67
67
  isPriceIncludeTax?: 0 | 1 | undefined;
68
+ deposit?: {
69
+ total?: string | number | undefined;
70
+ policies?: import("../../modules/Summary/types").IProtocol[] | undefined;
71
+ } | undefined;
68
72
  }>;
69
73
  getProducts(): Promise<ProductData[]>;
70
74
  setLoginAccount(accountId: string, accountInfo: Account): Promise<void>;
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "private": false,
3
3
  "name": "@pisell/pisellos",
4
- "version": "0.0.40",
4
+ "version": "0.0.42",
5
5
  "description": "一个可扩展的前端模块化SDK框架,支持插件系统",
6
6
  "main": "dist/index.js",
7
7
  "types": "dist/index.d.ts",