UNPKG

@pisell/pisellos

Version:

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

170 lines (168 loc) 5.19 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/ProductList/index.ts var ProductList_exports = {}; __export(ProductList_exports, { ProductList: () => ProductList }); module.exports = __toCommonJS(ProductList_exports); var import_BaseModule = require("../BaseModule"); var import_Product = require("../Product"); var import_types = require("./types"); var import_lodash_es = require("lodash-es"); __reExport(ProductList_exports, require("./types"), module.exports); var ProductList = class extends import_BaseModule.BaseModule { constructor(name, version) { super(name, version); this.defaultName = "productList"; this.defaultVersion = "1.0.0"; } async initialize(core, options) { var _a; 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); this.storeChange(); } else { this.store.list = []; this.store.productMap = /* @__PURE__ */ new Map(); this.store.selectProducts = []; } this.request = core.getPlugin("request"); } async storeChange(path, value) { var _a; (_a = this.store.list) == null ? void 0 : _a.forEach((product) => { const productModule = this.store.productMap.get(`product-${product.id}`); if (!productModule) { const newProductModule = new import_Product.Product( `product_${product.id.toString()}` ); this.core.registerModule(newProductModule, { initialState: product }); this.store.productMap.set(product.id.toString(), newProductModule); } else { productModule.updateData(product); } }); } async loadProducts({ category_ids = [], product_ids = [], collection = [], schedule_date, cacheId }) { var _a; let userPlugin = this.core.getPlugin("user"); let customer_id = void 0; try { customer_id = (_a = userPlugin == null ? void 0 : userPlugin.get()) == null ? void 0 : _a.id; } catch (error) { console.error(error); } const productsData = await this.request.post( `/product/query`, { open_quotation: 1, open_bundle: 0, exclude_extension_type: [ "product_party", "product_event", "product_series_event", "product_package_ticket", "ticket", "event_item" ], with: ["category", "collection", "resourceRelation"], status: "published", num: 500, skip: 1, customer_id, category_ids, ids: product_ids, collection, front_end_cache_id: cacheId, // client_schedule_ids: schedule_ids, schedule_date }, { useCache: true } ); this.addProduct(productsData.data.list); return productsData.data.list; } async loadProductsPrice({ ids = [], customer_id, schedule_date, channel }) { const productsData = await this.request.post( `/product/query/price`, { ids, customer_id, schedule_date, channel }, { useCache: true } ); return productsData.data; } async getProducts() { await this.core.effects.emit( import_types.ProductListHooks.onGetProducts, this.store.list ); return (0, import_lodash_es.cloneDeep)(this.store.list); } async getProduct(id) { await this.core.effects.emit( import_types.ProductListHooks.onGetProduct, this.store.list ); const product = this.store.productMap.get(`${id}`); if (product) return product; return void 0; } async addProduct(products) { if (!this.store.list) { this.store.list = []; } products.forEach((n) => { const index = this.store.list.findIndex((m) => m.id === n.id); if (index === -1) { this.store.list.push(n); } else { this.store.list[index] = n; } }); this.storeChange(); } async selectProducts(products) { this.store.selectProducts = products; } }; // Annotate the CommonJS export names for ESM import in node: 0 && (module.exports = { ProductList, ...require("./types") });