@pisell/pisellos 0.0.13 → 0.0.15

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.
@@ -5,13 +5,12 @@ export * from "./types";
5
5
  /**
6
6
  * 购物车模块实现
7
7
  */
8
- export declare class CartModule extends BaseModule implements Module {
8
+ export declare class CartModule extends BaseModule implements Module, CartModuleAPI {
9
9
  protected defaultName: string;
10
10
  protected defaultVersion: string;
11
11
  private request;
12
12
  private window;
13
13
  private store;
14
- exports: CartModuleAPI;
15
14
  constructor(name?: string, version?: string);
16
15
  /**
17
16
  * 初始化购物车模块
@@ -25,17 +24,36 @@ export declare class CartModule extends BaseModule implements Module {
25
24
  * 更新购物车信息
26
25
  */
27
26
  updateItem(params: IUpdateItemParams): Promise<void>;
27
+ /**
28
+ * 格式化数据到购物车
29
+ */
30
+ formatCartItem(params: IUpdateItemParams): CartItem;
28
31
  /**
29
32
  * 批量设置购物车数据
30
33
  */
31
34
  batchSetItems(items: CartItem[]): Promise<void>;
32
- getItemCount(): number;
33
- getTotalPrice(): number;
35
+ /**
36
+ * 获取购物车商品
37
+ */
34
38
  getItems(): CartItem[];
39
+ /**
40
+ * 根据账户ID获取购物车
41
+ */
42
+ getCartByAccount(account_id: string | number): CartItem[];
43
+ /**
44
+ * 移除购物车商品
45
+ */
35
46
  removeItem(id: string): Promise<void>;
36
- storeTemp(key: string, value: any): Promise<void>;
37
- getTemp(key?: string): any;
38
- clear(): void;
47
+ /**
48
+ * 根据账户ID清除购物车
49
+ */
39
50
  clearCartByAccount(account_id: string): void;
40
- getCartByAccount(account_id: string | number): CartItem[];
51
+ /**
52
+ * 清除购物车商品中的信息
53
+ */
54
+ deleteCartItemInfo(types: "resource" | "time"): void;
55
+ /**
56
+ * 清除购物车
57
+ */
58
+ clear(): void;
41
59
  }
@@ -22,8 +22,8 @@ function _defineProperty(obj, key, value) { key = _toPropertyKey(key); if (key i
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
24
  import { BaseModule } from "../BaseModule";
25
- import { CartHooks } from "./types";
26
- import { createCartItemOrigin, formatAccountToCartItem, formatProductToCartItem, formatResourceToCartItem, getUniqueId } from "./utils";
25
+ 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
  /**
@@ -42,15 +42,6 @@ export var CartModule = /*#__PURE__*/function (_BaseModule) {
42
42
  _defineProperty(_assertThisInitialized(_this), "request", void 0);
43
43
  _defineProperty(_assertThisInitialized(_this), "window", void 0);
44
44
  _defineProperty(_assertThisInitialized(_this), "store", void 0);
45
- // 导出的模块 API
46
- _defineProperty(_assertThisInitialized(_this), "exports", {
47
- addItem: _this.addItem.bind(_assertThisInitialized(_this)),
48
- getItemCount: _this.getItemCount.bind(_assertThisInitialized(_this)),
49
- getTotalPrice: _this.getTotalPrice.bind(_assertThisInitialized(_this)),
50
- getItems: _this.getItems.bind(_assertThisInitialized(_this)),
51
- updateItem: _this.updateItem.bind(_assertThisInitialized(_this)),
52
- removeItem: _this.removeItem.bind(_assertThisInitialized(_this))
53
- });
54
45
  return _this;
55
46
  }
56
47
 
@@ -83,9 +74,9 @@ export var CartModule = /*#__PURE__*/function (_BaseModule) {
83
74
  }
84
75
  throw new Error("购物车模块需要 window 插件支持");
85
76
  case 9:
86
- this.store.temp = {};
77
+ // this.store.temp = {};
87
78
  console.log("[CartModule] 初始化完成");
88
- case 11:
79
+ case 10:
89
80
  case "end":
90
81
  return _context.stop();
91
82
  }
@@ -111,10 +102,10 @@ export var CartModule = /*#__PURE__*/function (_BaseModule) {
111
102
  account = params.account, product = params.product, bundle = params.bundle, options = params.options; // 生成一个唯一的 ID
112
103
  cartItem = {
113
104
  _id: getUniqueId(),
114
- _origin: createCartItemOrigin(),
115
- _productOrigin: product
105
+ _origin: createCartItemOrigin()
116
106
  };
117
107
  if (product) {
108
+ cartItem._productOrigin = cloneDeep(product);
118
109
  cartItem = formatProductToCartItem({
119
110
  cartItem: cartItem,
120
111
  product: product,
@@ -135,11 +126,8 @@ export var CartModule = /*#__PURE__*/function (_BaseModule) {
135
126
 
136
127
  // 触发事件
137
128
  _context2.next = 8;
138
- return this.core.effects.emit(CartHooks.OnAddItem, cartItem);
129
+ return this.core.effects.emit("".concat(this.name, ":onAddItem"), cartItem);
139
130
  case 8:
140
- _context2.next = 10;
141
- return this.core.effects.emit("".concat(this.name, ":changed"), items);
142
- case 10:
143
131
  case "end":
144
132
  return _context2.stop();
145
133
  }
@@ -158,66 +146,34 @@ export var CartModule = /*#__PURE__*/function (_BaseModule) {
158
146
  key: "updateItem",
159
147
  value: (function () {
160
148
  var _updateItem = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee3(params) {
161
- var _tempCartItem$_origin;
162
- var _id, cartItem, account, product, resources, note, tempCartItem, items;
149
+ var tempCartItem, items;
163
150
  return _regeneratorRuntime().wrap(function _callee3$(_context3) {
164
151
  while (1) switch (_context3.prev = _context3.next) {
165
152
  case 0:
166
- _id = params._id, cartItem = params.cartItem, account = params.account, product = params.product, resources = params.resources, note = params.note;
167
- tempCartItem = this.store.list.find(function (i) {
168
- return i._id === _id;
169
- });
170
- if (tempCartItem) {
171
- _context3.next = 5;
172
- break;
173
- }
174
- console.warn("[CartModule] 更新购物车商品失败,商品不存在");
175
- return _context3.abrupt("return");
176
- case 5:
177
- if (cartItem) {
178
- tempCartItem = cartItem;
179
- }
180
- if (product) {
181
- tempCartItem = formatProductToCartItem({
182
- cartItem: tempCartItem,
183
- product: product
184
- });
185
- }
186
- if (account) {
187
- tempCartItem = formatAccountToCartItem({
188
- cartItem: tempCartItem,
189
- account: account
190
- });
191
- }
192
- if (resources) {
193
- tempCartItem = formatResourceToCartItem({
194
- cartItem: tempCartItem,
195
- resources: resources
196
- });
197
- }
198
- if (note && (_tempCartItem$_origin = tempCartItem._origin) !== null && _tempCartItem$_origin !== void 0 && _tempCartItem$_origin.product) {
199
- tempCartItem.note = note;
200
- tempCartItem._origin.product.note = note;
201
- }
202
-
203
- // 更新购物车数据
153
+ _context3.prev = 0;
154
+ tempCartItem = this.formatCartItem(params); // 更新购物车数据
204
155
  items = this.store.list.map(function (item) {
205
- var _tempCartItem;
206
- if (item._id === ((_tempCartItem = tempCartItem) === null || _tempCartItem === void 0 ? void 0 : _tempCartItem._id)) {
156
+ if (item._id === (tempCartItem === null || tempCartItem === void 0 ? void 0 : tempCartItem._id)) {
207
157
  return tempCartItem;
208
158
  }
209
159
  return item;
210
160
  });
211
- this.store.list = items;
212
-
161
+ this.store.list = _toConsumableArray(items);
213
162
  // 触发事件
214
- _context3.next = 14;
215
- return this.core.effects.emit("".concat(this.name, ":changed"), items);
216
- case 14:
163
+ _context3.next = 6;
164
+ return this.core.effects.emit("".concat(this.name, ":onUpdateItem"), tempCartItem);
165
+ case 6:
166
+ _context3.next = 11;
167
+ break;
168
+ case 8:
169
+ _context3.prev = 8;
170
+ _context3.t0 = _context3["catch"](0);
171
+ console.error("[CartModule] 更新购物车商品失败", _context3.t0);
172
+ case 11:
217
173
  case "end":
218
174
  return _context3.stop();
219
175
  }
220
- }, _callee3, this);
176
+ }, _callee3, this, [[0, 8]]);
221
177
  }));
222
178
  function updateItem(_x4) {
223
179
  return _updateItem.apply(this, arguments);
@@ -225,9 +181,65 @@ export var CartModule = /*#__PURE__*/function (_BaseModule) {
225
181
  return updateItem;
226
182
  }()
227
183
  /**
228
- * 批量设置购物车数据
184
+ * 格式化数据到购物车
229
185
  */
230
186
  )
187
+ }, {
188
+ key: "formatCartItem",
189
+ value: function formatCartItem(params) {
190
+ var _id = params._id,
191
+ cartItem = params.cartItem,
192
+ account = params.account,
193
+ product = params.product,
194
+ discounts = params.discounts,
195
+ resources = params.resources,
196
+ note = params.note;
197
+ var tempCartItem = this.store.list.find(function (i) {
198
+ return i._id === _id;
199
+ });
200
+ if (!tempCartItem) {
201
+ throw new Error("[CartModule] 格式化数据到购物车失败,商品不存在");
202
+ }
203
+ if (cartItem) {
204
+ tempCartItem = cartItem;
205
+ }
206
+ if (product) {
207
+ tempCartItem._productOrigin = cloneDeep(product);
208
+ tempCartItem = formatProductToCartItem({
209
+ cartItem: tempCartItem,
210
+ product: product
211
+ });
212
+ }
213
+ if (discounts) {
214
+ tempCartItem = formatDiscountToCartItem({
215
+ cartItem: tempCartItem,
216
+ discounts: discounts
217
+ });
218
+ }
219
+ if (account) {
220
+ tempCartItem = formatAccountToCartItem({
221
+ cartItem: tempCartItem,
222
+ account: account
223
+ });
224
+ }
225
+ if (resources) {
226
+ tempCartItem = formatResourceToCartItem({
227
+ cartItem: tempCartItem,
228
+ resources: resources
229
+ });
230
+ }
231
+ if (note) {
232
+ tempCartItem = formatNoteToCartItem({
233
+ cartItem: tempCartItem,
234
+ note: note
235
+ });
236
+ }
237
+ return tempCartItem;
238
+ }
239
+
240
+ /**
241
+ * 批量设置购物车数据
242
+ */
231
243
  }, {
232
244
  key: "batchSetItems",
233
245
  value: (function () {
@@ -235,7 +247,7 @@ export var CartModule = /*#__PURE__*/function (_BaseModule) {
235
247
  return _regeneratorRuntime().wrap(function _callee4$(_context4) {
236
248
  while (1) switch (_context4.prev = _context4.next) {
237
249
  case 0:
238
- this.store.list = items || [];
250
+ this.store.list = _toConsumableArray(items || []);
239
251
  case 1:
240
252
  case "end":
241
253
  return _context4.stop();
@@ -246,26 +258,34 @@ export var CartModule = /*#__PURE__*/function (_BaseModule) {
246
258
  return _batchSetItems.apply(this, arguments);
247
259
  }
248
260
  return batchSetItems;
249
- }())
250
- }, {
251
- key: "getItemCount",
252
- value: function getItemCount() {
253
- return this.store.list.length;
254
- }
255
- }, {
256
- key: "getTotalPrice",
257
- value: function getTotalPrice() {
258
- // return this.store.list.reduce((total: number, item: CartItem) => total + item.price * item.quantity, 0);
259
- return 0;
260
- }
261
+ }()
262
+ /**
263
+ * 获取购物车商品
264
+ */
265
+ )
261
266
  }, {
262
267
  key: "getItems",
263
268
  value: function getItems() {
264
269
  return this.store.list;
265
270
  }
271
+
272
+ /**
273
+ * 根据账户ID获取购物车
274
+ */
275
+ }, {
276
+ key: "getCartByAccount",
277
+ value: function getCartByAccount(account_id) {
278
+ return this.store.list.filter(function (item) {
279
+ return item.holder_id === account_id;
280
+ });
281
+ }
282
+
283
+ /**
284
+ * 移除购物车商品
285
+ */
266
286
  }, {
267
287
  key: "removeItem",
268
- value: function () {
288
+ value: (function () {
269
289
  var _removeItem = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee5(id) {
270
290
  var items;
271
291
  return _regeneratorRuntime().wrap(function _callee5$(_context5) {
@@ -276,7 +296,7 @@ export var CartModule = /*#__PURE__*/function (_BaseModule) {
276
296
  });
277
297
  this.store.list = items;
278
298
  _context5.next = 4;
279
- return this.core.effects.emit(CartHooks.OnCartRemove, items);
299
+ return this.core.effects.emit("".concat(this.name, ":onRemoveItem"), items);
280
300
  case 4:
281
301
  case "end":
282
302
  return _context5.stop();
@@ -288,53 +308,45 @@ export var CartModule = /*#__PURE__*/function (_BaseModule) {
288
308
  }
289
309
  return removeItem;
290
310
  }()
291
- }, {
292
- key: "storeTemp",
293
- value: function () {
294
- var _storeTemp = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee6(key, value) {
295
- return _regeneratorRuntime().wrap(function _callee6$(_context6) {
296
- while (1) switch (_context6.prev = _context6.next) {
297
- case 0:
298
- this.store.temp[key] = value;
299
- _context6.next = 3;
300
- return this.core.effects.emit("".concat(this.name, ":changed"), this.store.temp);
301
- case 3:
302
- case "end":
303
- return _context6.stop();
304
- }
305
- }, _callee6, this);
306
- }));
307
- function storeTemp(_x7, _x8) {
308
- return _storeTemp.apply(this, arguments);
309
- }
310
- return storeTemp;
311
- }()
312
- }, {
313
- key: "getTemp",
314
- value: function getTemp(key) {
315
- if (!key) {
316
- return this.store.temp;
317
- }
318
- return this.store.temp[key];
319
- }
320
- }, {
321
- key: "clear",
322
- value: function clear() {
323
- this.store.list = [];
324
- }
311
+ /**
312
+ * 根据账户ID清除购物车
313
+ */
314
+ )
325
315
  }, {
326
316
  key: "clearCartByAccount",
327
317
  value: function clearCartByAccount(account_id) {
328
- this.store.list = this.store.list.filter(function (item) {
318
+ var items = this.store.list.filter(function (item) {
329
319
  return item.holder_id !== account_id;
330
320
  });
321
+ this.store.list = _toConsumableArray(items || []);
331
322
  }
323
+
324
+ /**
325
+ * 清除购物车商品中的信息
326
+ */
332
327
  }, {
333
- key: "getCartByAccount",
334
- value: function getCartByAccount(account_id) {
335
- return this.store.list.filter(function (item) {
336
- return item.holder_id === account_id;
328
+ key: "deleteCartItemInfo",
329
+ value: function deleteCartItemInfo(types) {
330
+ var list = this.store.list.map(function (item) {
331
+ // 清除购物车商品中的资源信息
332
+ if (types === "resource") {
333
+ item = deleteResourceFromCartItem(item);
334
+ } else if (types === "time") {
335
+ // 清除购物车商品中的时间信息
336
+ item = deleteTimeFromCartItem(item);
337
+ }
338
+ return item;
337
339
  });
340
+ this.store.list = _toConsumableArray(list);
341
+ }
342
+
343
+ /**
344
+ * 清除购物车
345
+ */
346
+ }, {
347
+ key: "clear",
348
+ value: function clear() {
349
+ this.store.list = [];
338
350
  }
339
351
  }]);
340
352
  return CartModule;
@@ -7,6 +7,26 @@ export declare enum CartHooks {
7
7
  OnCartUpdateItem = "card:onCartUpdateItem",
8
8
  OnCartRemove = "card:onCartRemove"
9
9
  }
10
+ export interface CartModuleAPI {
11
+ /** 添加商品到购物车 */
12
+ addItem: (item: IAddItemParams) => Promise<void>;
13
+ /** 获取购物车商品 */
14
+ getItems: () => CartItem[];
15
+ /** 更新购物车商品 */
16
+ updateItem: (params: IUpdateItemParams) => Promise<void>;
17
+ /** 格式化购物车商品 */
18
+ formatCartItem: (params: IUpdateItemParams) => CartItem;
19
+ /** 批量设置购物车商品 */
20
+ batchSetItems: (items: CartItem[]) => Promise<void>;
21
+ /** 移除购物车商品 */
22
+ removeItem: (id: string) => Promise<void>;
23
+ /** 根据账户ID清除购物车 */
24
+ clearCartByAccount: (account_id: string) => void;
25
+ /** 根据账户ID获取购物车 */
26
+ getCartByAccount: (account_id: string) => CartItem[];
27
+ /** 清除购物车 */
28
+ clear: () => void;
29
+ }
10
30
  /**
11
31
  * 购物车商品接口
12
32
  */
@@ -67,26 +87,15 @@ export interface CartItem {
67
87
  /** 原始数据 */
68
88
  _origin: any;
69
89
  /** 商品原始数据 */
70
- _productOrigin: ProductData;
90
+ _productOrigin?: ProductData;
91
+ /** 资源原始数据 */
92
+ _resourceOrigin?: Resource[];
71
93
  }
72
94
  /**
73
95
  * 购物车状态接口
74
96
  */
75
97
  export interface CartState {
76
98
  list: CartItem[];
77
- total: number;
78
- temp: Record<string, any>;
79
- }
80
- /**
81
- * 购物车模块 API
82
- */
83
- export interface CartModuleAPI {
84
- addItem: (item: IAddItemParams) => Promise<void>;
85
- getItemCount: () => number;
86
- getTotalPrice: () => number;
87
- getItems: () => CartItem[];
88
- updateItem: (params: IUpdateItemParams) => Promise<void>;
89
- removeItem: (id: string) => Promise<void>;
90
99
  }
91
100
  /**
92
101
  * 添加商品到购物车参数
@@ -108,13 +117,15 @@ export interface IAddItemParams {
108
117
  */
109
118
  export interface IUpdateItemParams {
110
119
  /** 购物车_id字段 */
111
- _id?: string;
120
+ _id: string;
112
121
  /** 购物车商品 */
113
122
  cartItem?: CartItem;
114
123
  /** 账户 */
115
124
  account?: Account;
116
125
  /** 商品 */
117
126
  product?: ProductData;
127
+ /** 折扣列表 */
128
+ discounts?: IDiscount[];
118
129
  /** 数量 */
119
130
  quantity?: number;
120
131
  /** 资源 */
@@ -122,3 +133,13 @@ export interface IUpdateItemParams {
122
133
  /** 备注 */
123
134
  note?: string;
124
135
  }
136
+ /**
137
+ * 折扣
138
+ */
139
+ export interface IDiscount {
140
+ amount?: number;
141
+ type?: string;
142
+ resource_id?: string;
143
+ title?: any;
144
+ original_amount?: number;
145
+ }
@@ -15,13 +15,13 @@ export var CartHooks = /*#__PURE__*/function (CartHooks) {
15
15
  */
16
16
 
17
17
  /**
18
- * 购物车模块 API
18
+ * 添加商品到购物车参数
19
19
  */
20
20
 
21
21
  /**
22
- * 添加商品到购物车参数
22
+ * 更新购物车信息参数
23
23
  */
24
24
 
25
25
  /**
26
- * 更新购物车信息参数
26
+ * 折扣
27
27
  */
@@ -1,7 +1,7 @@
1
1
  import { Account } from "../Account";
2
2
  import { ProductData } from "../Product/types";
3
3
  import { Resource } from "../Resource/types";
4
- import { CartItem } from "./types";
4
+ import { CartItem, IDiscount } from "./types";
5
5
  /**
6
6
  * 生成一个唯一的 ID
7
7
  */
@@ -46,6 +46,24 @@ export declare const formatProductToCartItemOrigin: (params: {
46
46
  bundle?: any;
47
47
  options?: any;
48
48
  }) => any;
49
+ /**
50
+ * 格式化折扣到购物车
51
+ * @param params 参数
52
+ * @returns 格式化后的购物车
53
+ */
54
+ export declare const formatDiscountToCartItem: (params: {
55
+ cartItem: CartItem;
56
+ discounts: IDiscount[];
57
+ }) => CartItem;
58
+ /**
59
+ * 格式化折扣到购物车原始数据
60
+ * @param params 参数
61
+ * @returns 格式化后的购物车原始数据
62
+ */
63
+ export declare const formatDiscountToCartItemOrigin: (params: {
64
+ cartItem: CartItem;
65
+ discounts: IDiscount[];
66
+ }) => any;
49
67
  /**
50
68
  * 格式化账户到购物车
51
69
  * @param account 账户
@@ -72,6 +90,36 @@ export declare const formatResourceToCartItemOrigin: (params: {
72
90
  cartItem: CartItem;
73
91
  resources: Resource[];
74
92
  }) => any;
93
+ /**
94
+ * 从购物车中删除资源相关信息
95
+ * @param cartItem 购物车
96
+ * @returns 删除后的购物车
97
+ */
98
+ export declare const deleteResourceFromCartItem: (cartItem: CartItem) => CartItem;
99
+ /**
100
+ * 从购物车中删除时间
101
+ * @param cartItem 购物车
102
+ * @returns 删除后的购物车
103
+ */
104
+ export declare const deleteTimeFromCartItem: (cartItem: CartItem) => CartItem;
105
+ /**
106
+ * 格式化备注到购物车
107
+ * @param params 参数
108
+ * @returns 格式化后的购物车
109
+ */
110
+ export declare const formatNoteToCartItem: (params: {
111
+ cartItem: CartItem;
112
+ note: string;
113
+ }) => CartItem;
114
+ /**
115
+ * 格式化备注到购物车原始数据
116
+ * @param params 参数
117
+ * @returns 格式化后的购物车原始数据
118
+ */
119
+ export declare const formatNoteToCartItemOrigin: (params: {
120
+ cartItem: CartItem;
121
+ note: string;
122
+ }) => any;
75
123
  /**
76
124
  * 格式化套餐规格信息
77
125
  * @param options 套餐规格信息