UNPKG

@pisell/pisellos

Version:

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

155 lines (153 loc) 6.19 kB
var __create = Object.create; var __defProp = Object.defineProperty; var __getOwnPropDesc = Object.getOwnPropertyDescriptor; var __getOwnPropNames = Object.getOwnPropertyNames; var __getProtoOf = Object.getPrototypeOf; 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 __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps( // If the importer is in node compatibility mode or this is not an ESM // file that has been converted to a CommonJS file using a Babel- // compatible transform (i.e. "__esModule" has not been set), then set // "default" to the CommonJS "module.exports" for node compatibility. isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target, mod )); var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod); // src/modules/Discount/index.ts var Discount_exports = {}; __export(Discount_exports, { DiscountModule: () => DiscountModule }); module.exports = __toCommonJS(Discount_exports); var import_utils = require("../../solution/ShopDiscount/utils"); var import_BaseModule = require("../BaseModule"); var import_types = require("./types"); var import_decimal = __toESM(require("decimal.js")); var DiscountModule = class extends import_BaseModule.BaseModule { constructor(name, version) { super(name, version); this.defaultName = "discount"; this.defaultVersion = "1.0.0"; this.openCache = false; } async initialize(core, options) { var _a, _b, _c; this.core = core; this.store = options == null ? void 0 : options.store; if (Array.isArray((_a = options == null ? void 0 : options.initialState) == null ? void 0 : _a.discountList)) { this.store.discountList = options == null ? void 0 : options.initialState.discountList; } else { this.store.discountList = []; } if ((_b = options == null ? void 0 : options.otherParams) == null ? void 0 : _b.cacheId) { this.openCache = options.otherParams.openCache; this.cacheId = options.otherParams.cacheId; } if ((_c = options == null ? void 0 : 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("discount模块需要 request 插件支持"); } if (!this.window) { throw new Error("discount模块需要 window 插件支持"); } } async setDiscountList(discountList) { this.store.discountList = discountList; await this.core.effects.emit( import_types.DiscountHooks.OnDiscountListChange, discountList ); } getDiscountList() { return this.store.discountList; } async loadPrepareConfig(params) { var _a, _b; const prepareConfig = await this.request.post( `/order/prepare/config`, params ); const goodPassList = this.filterEnabledDiscountList( [...((_a = prepareConfig == null ? void 0 : prepareConfig.data) == null ? void 0 : _a.good_pass_list) || [], ...((_b = prepareConfig == null ? void 0 : prepareConfig.data) == null ? void 0 : _b.discount_card_list) || []] ) || []; this.setDiscountList(goodPassList); return goodPassList; } async batchSearch(code) { const result = await this.request.get(`/machinecode/batch-search`, { code, translate_flag: 1, tags: ["good_pass", "product_discount_card"], available: 1, relation_product: 1 }); const resultDiscountList = this.filterEnabledDiscountList((result == null ? void 0 : result.data) || []) || []; return resultDiscountList; } filterEnabledDiscountList(discountList) { return discountList.filter( (discount) => discount.limit_status === "enable" && new import_decimal.default((discount == null ? void 0 : discount.par_value) || 0).minus(new import_decimal.default((discount == null ? void 0 : discount.used_par_value) || 0)).greaterThan(0) ); } // 根据productIds去重 uniqueByProductId(discountList) { return (0, import_utils.uniqueById)(discountList, "product_id"); } filterDiscountListByProductIds(discountList, productIds) { return discountList.filter((discount) => { const applicableProductIds = discount.applicableProductIds || []; return applicableProductIds.some((id) => productIds.includes(id)); }); } calcDiscountApplicableProductTotalPrice(discount) { if (discount == null ? void 0 : discount.savedAmount) { return (discount == null ? void 0 : discount.savedAmount) || 0; } if (discount.appliedProductDetails) { return discount.appliedProductDetails.reduce((total, product) => { const price = new import_decimal.default((product == null ? void 0 : product.amount) || 0).mul((product == null ? void 0 : product.num) || 1); return new import_decimal.default(total).plus(price).toNumber(); }, 0); } } async destroy() { this.store.discountList = []; this.core.effects.offByModuleDestroy(this.name); await this.core.effects.emit(import_types.DiscountHooks.OnDestroy, {}); console.log("[Discount] 已销毁"); } async clear() { this.store.discountList = []; console.log("[Discount] clear"); } storeChange() { if (this.openCache) { this.checkSaveCache({ cacheId: this.cacheId, fatherModule: this.fatherModule, store: this.store, cacheKey: ["discountList"] }); } } }; // Annotate the CommonJS export names for ESM import in node: 0 && (module.exports = { DiscountModule });