UNPKG

@pisell/pisellos

Version:

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

182 lines (169 loc) 8.87 kB
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); } function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } } function _defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, _toPropertyKey(descriptor.key), descriptor); } } function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); Object.defineProperty(Constructor, "prototype", { writable: false }); return Constructor; } 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; } function _toPropertyKey(t) { var i = _toPrimitive(t, "string"); return "symbol" == _typeof(i) ? i : String(i); } 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); } import { getApplicableProductIds, getMainProductPrice, getBundleItemPrice, getProductQuantity, getBundleItemIsOriginalPrice, getTaxAndFeeRoundingRemainder, getBundleItemTaxAndFeeRoundingRemainder, getProductDiscountDifference, getBundleItemDiscountDifference } from "./utils"; /** * Wallet Pass 适配器 * 用于将 wallet pass 业务数据转换为策略引擎可识别的格式 */ var WalletPassAdapter = /*#__PURE__*/function () { function WalletPassAdapter() { _classCallCheck(this, WalletPassAdapter); _defineProperty(this, "name", 'WalletPassAdapter'); _defineProperty(this, "version", '1.0.0'); } _createClass(WalletPassAdapter, [{ key: "prepareContext", value: /** * 准备运行时上下文 * 将业务数据转换为策略引擎可识别的 RuntimeContext */ function prepareContext(businessData) { var _strategyConfig$metad, _strategyConfig$metad2; var orderTotalAmount = businessData.orderTotalAmount, products = businessData.products, voucher = businessData.voucher, strategyConfig = businessData.strategyConfig; // 提取订单中的商品ID var productIds = products.map(function (p) { return p.product_id; }); // 从 voucher 中获取适用商品ID var applicableProductIds = getApplicableProductIds(voucher); // 获取 deductTaxAndFee 配置 var deductTaxAndFee = (_strategyConfig$metad = strategyConfig === null || strategyConfig === void 0 || (_strategyConfig$metad2 = strategyConfig.metadata) === null || _strategyConfig$metad2 === void 0 || (_strategyConfig$metad2 = _strategyConfig$metad2.custom) === null || _strategyConfig$metad2 === void 0 ? void 0 : _strategyConfig$metad2.deductTaxAndFee) !== null && _strategyConfig$metad !== void 0 ? _strategyConfig$metad : true; // 计算适用商品的总金额和数量 var _this$calculateApplic = this.calculateApplicableProducts(products, applicableProductIds, deductTaxAndFee), applicableTotal = _this$calculateApplic.applicableTotal, applicableCount = _this$calculateApplic.applicableCount; return { entities: { voucher: voucher, products: products, order: { orderTotalAmount: orderTotalAmount, products: products } }, attributes: { // 订单消费金额 orderTotalAmount: orderTotalAmount, // 适用商品金额 applicableProductTotalAmount: applicableTotal, // 适用商品购买数量 applicableProductCount: applicableCount, // 当前评估的 voucher ID voucherProductId: voucher.product_id, // 订单中的商品ID productIds: productIds }, metadata: { timestamp: Date.now() } }; } /** * 转换执行结果 * 将策略引擎的通用结果转换为业务层需要的格式 */ }, { key: "transformResult", value: function transformResult(result, businessData) { // 从策略配置中获取业务配置 var strategyConfig = businessData === null || businessData === void 0 ? void 0 : businessData.strategyConfig; var businessConfig = (strategyConfig === null || strategyConfig === void 0 ? void 0 : strategyConfig.metadata.custom) || {}; // 从 voucher 中获取适用商品ID var voucher = businessData === null || businessData === void 0 ? void 0 : businessData.voucher; var applicableProductIds = voucher ? getApplicableProductIds(voucher) : null; if (!result.applicable) { return { isApplicable: false, canUseCount: 0, maxDeduction: 0, deductTaxAndFee: false, applicableProductIds: [], reason: result.message, reasonCode: result.code, strategyResult: result }; } // 从配置中获取业务字段(不需要计算) var maxDeduction = businessConfig.maxDeductionAmount || 0; var canUseCount = businessConfig.maxUsagePerOrder || 0; var deductTaxAndFee = businessConfig.deductTaxAndFee || false; return { isApplicable: true, canUseCount: canUseCount, maxDeduction: maxDeduction, deductTaxAndFee: deductTaxAndFee, applicableProductIds: applicableProductIds || [], strategyResult: result }; } /** * 格式化配置 */ }, { key: "formatConfig", value: function formatConfig(result, businessData) { return { result: result, businessData: businessData }; } /** * 计算适用商品的总金额和数量 */ }, { key: "calculateApplicableProducts", value: function calculateApplicableProducts(products, applicableProductIds) { var deductTaxAndFee = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : true; var total = 0; var count = 0; // 如果为空数组,不适用任何商品 if (applicableProductIds !== null && applicableProductIds.length === 0) { return { applicableTotal: 0, applicableCount: 0 }; } products.forEach(function (product) { var productQuantity = getProductQuantity(product); // 检查主商品是否适用 var isMainProductApplicable = applicableProductIds === null || applicableProductIds.includes(product.product_id); if (isMainProductApplicable) { // 计算主商品价格(包含加减价子商品)= 单价 * 数量 + 舍入余数 - 商品差额 total += getMainProductPrice(product, deductTaxAndFee) * productQuantity + getTaxAndFeeRoundingRemainder(product, deductTaxAndFee) - getProductDiscountDifference(product); count += productQuantity; } // 检查原价子商品(需要单独判断适用性) if (product.product_bundle && product.product_bundle.length > 0) { product.product_bundle.forEach(function (bundleItem) { // 只处理原价子商品,加减价子商品已包含在主商品价格中 if (getBundleItemIsOriginalPrice(bundleItem)) { // 检查子商品是否适用 var isBundleItemApplicable = applicableProductIds === null || applicableProductIds.includes(bundleItem.bundle_product_id); if (isBundleItemApplicable) { // 计算原价子商品价格 = 总价 + 舍入余数 - 商品差额 var bundleItemQuantity = bundleItem.num * productQuantity; total += getBundleItemPrice(bundleItem, productQuantity, deductTaxAndFee) + getBundleItemTaxAndFeeRoundingRemainder(bundleItem, deductTaxAndFee) - getBundleItemDiscountDifference(bundleItem); count += bundleItemQuantity; } } }); } }); return { applicableTotal: total, applicableCount: count }; } }]); return WalletPassAdapter; }(); export { WalletPassAdapter as default };