UNPKG

@pisell/pisellos

Version:

一个可扩展的前端模块化SDK框架,支持插件系统

369 lines (367 loc) 11.8 kB
var __defProp = Object.defineProperty; var __getOwnPropDesc = Object.getOwnPropertyDescriptor; var __getOwnPropNames = Object.getOwnPropertyNames; var __hasOwnProp = Object.prototype.hasOwnProperty; var __export = (target, all) => { for (var name in all) __defProp(target, name, { get: all[name], enumerable: true }); }; var __copyProps = (to, from, except, desc) => { if (from && typeof from === "object" || typeof from === "function") { for (let key of __getOwnPropNames(from)) if (!__hasOwnProp.call(to, key) && key !== except) __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable }); } return to; }; var __reExport = (target, mod, secondTarget) => (__copyProps(target, mod, "default"), secondTarget && __copyProps(secondTarget, mod, "default")); var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod); // src/modules/Cart/index.ts var Cart_exports = {}; __export(Cart_exports, { CartModule: () => CartModule, getProductDeposit: () => import_utils3.getProductDeposit }); module.exports = __toCommonJS(Cart_exports); var import_BaseModule = require("../BaseModule"); var import_types = require("./types"); var import_utils = require("./utils"); var import_lodash_es = require("lodash-es"); var import_utils2 = require("../Product/utils"); __reExport(Cart_exports, require("./types"), module.exports); var import_utils3 = require("./utils"); var CartModule = class extends import_BaseModule.BaseModule { constructor(name, version) { super(name || "cart", version || "1.0.0"); // 提供给 baseModule 用的默认名称 和 默认版本 this.defaultName = "cart"; this.defaultVersion = "1.0.0"; this.openCache = false; } /** * 初始化购物车模块 */ async initialize(core, options) { var _a, _b, _c; this.core = core; this.store = options.store; if (Array.isArray((_a = options.initialState) == null ? void 0 : _a.list)) { this.store.list = options.initialState.list; this.core.effects.emit(`${this.name}:changed`, this.store.list); } else { this.store.list = []; } if ((_b = options.otherParams) == null ? void 0 : _b.cacheId) { this.openCache = options.otherParams.openCache; this.cacheId = options.otherParams.cacheId; } if ((_c = options.otherParams) == null ? void 0 : _c.fatherModule) { this.fatherModule = options.otherParams.fatherModule; } this.request = core.getPlugin("request"); this.window = core.getPlugin("window"); if (!this.request) { throw new Error("购物车模块需要 request 插件支持"); } if (!this.window) { throw new Error("购物车模块需要 window 插件支持"); } console.log("[CartModule] 初始化完成"); } /** * 添加商品到购物车 */ async addItem(params) { const { account, product, bundle, options, date, quantity = 1 } = params; let cartItem = { _id: (0, import_utils.getUniqueId)(), _origin: (0, import_utils.createCartItemOrigin)() }; if (product) { const newProduct = (0, import_utils.handleVariantProduct)(product); cartItem._productOrigin = (0, import_lodash_es.cloneDeep)(newProduct); cartItem._productInit = (0, import_lodash_es.cloneDeep)(newProduct); cartItem._bundleOrigin = (0, import_lodash_es.cloneDeep)(bundle); cartItem._optionsOrigin = (0, import_lodash_es.cloneDeep)(options); cartItem = (0, import_utils.formatProductToCartItem)({ cartItem, product: newProduct, bundle, options, product_variant_id: newProduct.product_variant_id, quantity }); } if (account) { cartItem = (0, import_utils.formatAccountToCartItem)({ cartItem, account }); } if (date) { cartItem = (0, import_utils.formatDateToCartItem)({ cartItem, date }); } const items = [...this.store.list, cartItem]; this.store.list = items; await this.core.effects.emit(`${this.name}:onAddItem`, cartItem); } /** * 更新购物车信息 */ async updateItem(params) { try { const tempCartItem = this.formatCartItem(params); const items = this.store.list.map((item) => { if (item._id === (tempCartItem == null ? void 0 : tempCartItem._id)) { return tempCartItem; } return item; }); this.store.list = [...items]; await this.core.effects.emit(`${this.name}:onUpdateItem`, tempCartItem); } catch (error) { console.error("[CartModule] 更新购物车商品失败", error); } } // 更新购物车中商品的初始信息 // 为什么会有这个操作,主要服务于报价单和商品券 // 商品券那边会使用 init 的 price 做计算,而报价单那边就需要修改 init 的 price 来辅助商品计算 async updateItemInitInfo(params) { const { _id, product } = params; const cartItem = this.store.list.find((i) => i._id === _id); if (cartItem) { cartItem._productInit = (0, import_lodash_es.cloneDeep)(product); } } /** * 格式化数据到购物车 */ formatCartItem(params) { const { _id, cartItem, account, product, bundle, options, discounts, resources, note, date, relationForms, osWarnTips } = params; let quantity = params.quantity; let tempCartItem = this.store.list.find((i) => i._id === _id); if (!quantity && (tempCartItem == null ? void 0 : tempCartItem.num)) { quantity = (tempCartItem == null ? void 0 : tempCartItem.num) || 1; } if (!tempCartItem) { throw new Error("[CartModule] 格式化数据到购物车失败,商品不存在"); } if (cartItem) { tempCartItem = cartItem; } if (product) { const newProduct = (0, import_utils.handleVariantProduct)(product); tempCartItem._productOrigin = (0, import_lodash_es.cloneDeep)(newProduct); tempCartItem._bundleOrigin = (0, import_lodash_es.cloneDeep)(bundle); tempCartItem._optionsOrigin = (0, import_lodash_es.cloneDeep)(options); tempCartItem = (0, import_utils.formatProductToCartItem)({ cartItem: tempCartItem, product: newProduct, bundle, options, product_variant_id: newProduct.product_variant_id, quantity, discounts }); } if (discounts) { tempCartItem = (0, import_utils.formatDiscountToCartItem)({ cartItem: tempCartItem, discounts }); } if (account) { tempCartItem = (0, import_utils.formatAccountToCartItem)({ cartItem: tempCartItem, account }); } if (resources) { tempCartItem = (0, import_utils.formatResourceToCartItem)({ cartItem: tempCartItem, resources }); } if (relationForms) { tempCartItem = (0, import_utils.formatRelationFormsToCartItem)({ cartItem: tempCartItem, relationForms }); } if (date) { tempCartItem = (0, import_utils.formatDateToCartItem)({ cartItem: tempCartItem, date }); } if (note) { tempCartItem = (0, import_utils.formatNoteToCartItem)({ cartItem: tempCartItem, note }); } if (osWarnTips) { tempCartItem.osWarnTips = osWarnTips; } return tempCartItem; } /** * 批量设置购物车数据 */ async batchSetItems(items) { this.store.list = [...items || []]; } /** * 获取购物车商品 */ getItems() { return this.store.list; } /** * 获取购物车商品--通过购物车 id 获取单条 */ getItem(id) { return this.store.list.find((item) => item._id === id); } /** * 根据账户ID获取购物车 */ getCartByAccount(account_id) { return this.store.list.filter( (item) => item.holder_id === account_id ); } /** * 移除购物车商品 */ async removeItem(id) { const items = this.store.list.filter((i) => i._id !== id); this.store.list = items; await this.core.effects.emit(`${this.name}:onRemoveItem`, items); } /** * 根据账户ID清除购物车 */ clearCartByAccount(account_id) { const items = this.store.list.filter( (item) => item.holder_id !== account_id ); this.store.list = [...items || []]; } /** * 清除购物车商品中的信息 * @param type 清除购物车商品中的信息类型 * @param _id 购物车商品ID */ deleteCartItemInfo(type, _id) { const list = this.store.list.map((item) => { const needDelete = _id ? item._id === _id : true; if (!needDelete) { return item; } switch (type) { case import_types.ECartItemInfoType.Resource: item = (0, import_utils.deleteResourceFromCartItem)(item); break; case import_types.ECartItemInfoType.Time: item = (0, import_utils.deleteTimeFromCartItem)(item); break; case import_types.ECartItemInfoType.RelationForms: item = (0, import_utils.deleteRelationFormsFromCartItem)(item); break; case import_types.ECartItemInfoType.Holder: item = (0, import_utils.deleteHolderFromCartItem)(item); break; } return item; }); this.store.list = [...list]; } /** * 清除购物车 */ clear() { this.store.list = []; } storeChange() { if (this.openCache) { const store = (0, import_lodash_es.cloneDeep)(this.store); store.list = store.list.filter( (item, index, self) => index === self.findIndex((t) => t._id === item._id) ); this.checkSaveCache({ cacheId: this.cacheId, fatherModule: this.fatherModule, store, cacheKey: ["list"] }); } } checkCartItemByType(cartItem, type) { let result = false; const { holder, metadata, resources, start_date, end_date, relation_forms } = cartItem._origin || {}; switch (type) { case import_types.ECartItemCheckType.Account: result = !!holder || !!(metadata == null ? void 0 : metadata.account); break; case import_types.ECartItemCheckType.Resources: result = !!(resources == null ? void 0 : resources.length); break; case import_types.ECartItemCheckType.Date: result = !!start_date && !!end_date; break; case import_types.ECartItemCheckType.RelationForms: result = !!(relation_forms == null ? void 0 : relation_forms.length); break; } return result; } /** * 基于rowkey 合并商品,目前只有普通商品需要合并 * * @param {string} rowKey * @param {number} quantity * @return {*} * @memberof CartModule */ mergeCartItemByRowKey(params) { const { rowKey, quantity, account } = params; let flag = false; const cartItems = this.getItems(); const targetCartItem = cartItems.find( (n) => { var _a; return ((_a = n._productOrigin) == null ? void 0 : _a.rowKey) === rowKey; } ); const isSameAccount = (account == null ? void 0 : account.id) === (targetCartItem == null ? void 0 : targetCartItem.holder_id); if (rowKey && targetCartItem && (0, import_utils2.isNormalProduct)(targetCartItem._productOrigin) && isSameAccount) { this.updateItem({ _id: targetCartItem._id, product: { ...targetCartItem._productOrigin, quantity: (targetCartItem.num || 1) + (quantity || 1) } }); flag = true; } return flag; } }; // Annotate the CommonJS export names for ESM import in node: 0 && (module.exports = { CartModule, getProductDeposit, ...require("./types") });