UNPKG

@pisell/pisellos

Version:

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

340 lines (338 loc) 12.7 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/Cart/utils/cartProduct.ts var cartProduct_exports = {}; __export(cartProduct_exports, { formatBundle: () => formatBundle, formatBundleToOrigin: () => formatBundleToOrigin, formatOptions: () => formatOptions, formatOptionsToOrigin: () => formatOptionsToOrigin, formatProductToCartItem: () => formatProductToCartItem, formatProductToCartItemOrigin: () => formatProductToCartItemOrigin, getProductDeposit: () => getProductDeposit, getProductOriginTotalPrice: () => getProductOriginTotalPrice, getProductTotalPrice: () => getProductTotalPrice, handleProductDeposit: () => handleProductDeposit, handleVariantProduct: () => handleVariantProduct }); module.exports = __toCommonJS(cartProduct_exports); var import_decimal = __toESM(require("decimal.js")); var import_utils = require("../../Product/utils"); var handleVariantProduct = (product) => { if (product == null ? void 0 : product.product_variant_id) { const targetVariant = product.variant.find( (n) => n.id === product.product_variant_id ); if (targetVariant) { product.price = targetVariant.price; product.original_price = targetVariant.original_price; product.is_charge_tax = targetVariant.is_charge_tax; } } return product; }; var formatProductToCartItem = (params) => { var _a, _b; const { cartItem, product, bundle, options, product_variant_id, quantity = 1 } = params; const num = quantity; if (product) { cartItem.id = product == null ? void 0 : product.id; cartItem.name = product == null ? void 0 : product.title; cartItem.price = product == null ? void 0 : product.price; cartItem.num = num; cartItem.total = getProductTotalPrice({ product, bundle, options, num }); cartItem.summaryTotal = cartItem.total * (num || 1); cartItem.origin_total = getProductOriginTotalPrice({ product, bundle, options, num }); cartItem.image = product == null ? void 0 : product.cover; cartItem.like_status = "common"; cartItem.duration = product == null ? void 0 : product.duration; cartItem.isNormalProduct = (0, import_utils.isNormalProduct)(product); cartItem.is_charge_tax = product == null ? void 0 : product.is_charge_tax; } if (bundle == null ? void 0 : bundle.length) { cartItem.bundle = formatBundle(bundle); } if (options == null ? void 0 : options.length) { cartItem.options = formatOptions(options); } if (product_variant_id) { const sku_key = (_a = product.variant.find( (n) => n.id === product_variant_id )) == null ? void 0 : _a.sku_key; if (sku_key) { const sku_key_arr = sku_key.split("_"); const product_option_string = []; sku_key_arr.forEach((item) => { var _a2; const keyMap = item.split(":"); const targetVariantGroup = product.variant_group.find( (n) => String(n.position_id) === keyMap[0] ); const targetVariantItem = (_a2 = targetVariantGroup == null ? void 0 : targetVariantGroup.variant_item) == null ? void 0 : _a2.find( (n) => String(n.position_id) === keyMap[1] ); if (targetVariantGroup && targetVariantItem) { product_option_string.push( `${targetVariantGroup.name}:${targetVariantItem.name}` ); } }); cartItem.product_option_string = product_option_string.join(", "); } } if (((_b = product == null ? void 0 : product.custom_deposit_data) == null ? void 0 : _b.has_deposit) == 1) { cartItem.deposit = getProductDeposit({ cartItem, product, bundle, options, num }); } const oringin = formatProductToCartItemOrigin(params); cartItem._origin = oringin; return cartItem; }; var formatProductToCartItemOrigin = (params) => { var _a, _b, _c, _d; const { cartItem, product, bundle, options, product_variant_id, quantity = 1 } = params; let origin = cartItem._origin; if (product) { origin.product = { ...origin.product || {}, num: quantity, product_id: product.id, product_variant_id: product_variant_id || 0, product_bundle: ((_a = origin.product) == null ? void 0 : _a.product_bundle) || [], product_option_item: ((_b = origin.product) == null ? void 0 : _b.product_option_item) || [] }; origin.sub_type = (_c = product == null ? void 0 : product.duration) == null ? void 0 : _c.type; origin.duration = (_d = product == null ? void 0 : product.duration) == null ? void 0 : _d.value; if (bundle == null ? void 0 : bundle.length) { origin.product.product_bundle = formatBundleToOrigin(bundle); } if (options == null ? void 0 : options.length) { origin.product.product_option_item = formatOptionsToOrigin(options); } } return origin; }; var getProductTotalPrice = (params) => { const { product, bundle, options } = params; let price = Number(product.price); if (bundle == null ? void 0 : bundle.length) { price = bundle.reduce((accumulator, currentValue) => { return accumulator + Number(currentValue.price) * Number(currentValue.num); }, price); } if (options == null ? void 0 : options.length) { price = options.reduce((accumulator, currentValue) => { return accumulator + Number(currentValue.price) * Number(currentValue.num); }, price); } return price; }; var getProductOriginTotalPrice = (params) => { const { product, bundle, options } = params; const num = (params == null ? void 0 : params.num) || 1; let price = Number(product.original_price); if (isNaN(price) || price === 0) { return void 0; } if (bundle == null ? void 0 : bundle.length) { price = bundle.reduce((accumulator, currentValue) => { return accumulator + Number(currentValue.price) * Number(currentValue.num); }, price); } if (options == null ? void 0 : options.length) { price = options.reduce((accumulator, currentValue) => { return accumulator + Number(currentValue.price) * Number(currentValue.num); }, price); } return price; }; var formatOptions = (options) => { if (!(options == null ? void 0 : options.length)) return []; return options.map((item) => { return { id: item == null ? void 0 : item.id, name: item == null ? void 0 : item.name, num: item == null ? void 0 : item.num, price: item == null ? void 0 : item.price, total: item == null ? void 0 : item.price, origin_total: item == null ? void 0 : item.original_price }; }); }; var formatOptionsToOrigin = (options) => { if (!(options == null ? void 0 : options.length)) { return []; } return options.map((d) => { return { num: d.num, option_group_item_id: d.product_option_item_id, option_group_id: d.option_group_id }; }); }; var formatBundle = (bundle) => { if (!(bundle == null ? void 0 : bundle.length)) return []; return bundle.map((item) => { return { id: item == null ? void 0 : item.id, title: item == null ? void 0 : item.title, num: item == null ? void 0 : item.num, price: item == null ? void 0 : item.price, total: item == null ? void 0 : item.price, price_type: item == null ? void 0 : item.price_type, bundle_sum_price: item == null ? void 0 : item.bundle_sum_price, bundle_sum_type: item == null ? void 0 : item.bundle_sum_type, options: formatOptions(item == null ? void 0 : item.option) }; }); }; var formatBundleToOrigin = (bundle) => { if (!(bundle == null ? void 0 : bundle.length)) return []; return bundle.map((d) => { return { bundle_group_id: d.group_id, bundle_id: d.id, bundle_product_id: (d == null ? void 0 : d._bundle_product_id) || (d == null ? void 0 : d.product_id), bundle_variant_id: d.bundle_variant_id, num: d.num, extension_id: d.extension_id, extension_type: d.extension_type, option: formatOptionsToOrigin(d.option) }; }); }; var getProductDeposit = (params) => { var _a; const { cartItem, product, bundle, options } = params; const num = (params == null ? void 0 : params.num) || 1; if (((_a = product == null ? void 0 : product.custom_deposit_data) == null ? void 0 : _a.has_deposit) == 1) { let total = new import_decimal.default(0); const { deposit_fixed, deposit_percentage, deposit_policy_data, self_deposit_policy_ids } = (product == null ? void 0 : product.custom_deposit_data) || {}; const allProtocols = deposit_policy_data || []; let productHasDeposit = false; const protocolIds = []; if (typeof deposit_fixed === "string" && typeof deposit_percentage === "string") { const { depositTotal, result } = handleProductDeposit({ depositData: product == null ? void 0 : product.custom_deposit_data, total: cartItem.total || 0, num }); if (result) { total = depositTotal; protocolIds.push(...self_deposit_policy_ids || []); productHasDeposit = true; } } else { if (bundle == null ? void 0 : bundle.length) { total = bundle.reduce((accumulator, currBundleProduct) => { const depositData = currBundleProduct == null ? void 0 : currBundleProduct.custom_deposit_data; const { depositTotal, result } = handleProductDeposit({ depositData, total: currBundleProduct == null ? void 0 : currBundleProduct.price, num: (currBundleProduct == null ? void 0 : currBundleProduct.num) || 1 }); if (result) { protocolIds.push(...(depositData == null ? void 0 : depositData.self_deposit_policy_ids) || []); productHasDeposit = true; return accumulator.plus(depositTotal); } return accumulator; }, new import_decimal.default(0)); } } if (productHasDeposit) { const ids = new Set(protocolIds); const protocols = allProtocols.filter((item) => ids.has(item.id)); return { total: total.toNumber(), protocols }; } return null; } return null; }; var handleProductDeposit = (params) => { const { depositData, total, num } = params; if (!depositData) { return { result: false, depositTotal: new import_decimal.default(0) }; } const { deposit_fixed, deposit_percentage } = depositData || {}; if (typeof deposit_fixed === "string" && typeof deposit_percentage === "string") { const depositFixed = (0, import_decimal.default)(deposit_fixed || 0); const depositPercent = (0, import_decimal.default)(deposit_percentage || 0); const productTotalPrice = (0, import_decimal.default)(total || 0); const deposit = depositFixed.plus(depositPercent.times(productTotalPrice)); return { depositTotal: deposit.times(num), result: true }; } return { result: false, depositTotal: new import_decimal.default(0) }; }; // Annotate the CommonJS export names for ESM import in node: 0 && (module.exports = { formatBundle, formatBundleToOrigin, formatOptions, formatOptionsToOrigin, formatProductToCartItem, formatProductToCartItemOrigin, getProductDeposit, getProductOriginTotalPrice, getProductTotalPrice, handleProductDeposit, handleVariantProduct });