UNPKG

@pisell/pisellos

Version:

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

402 lines (387 loc) 18.9 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 _toConsumableArray(arr) { return _arrayWithoutHoles(arr) || _iterableToArray(arr) || _unsupportedIterableToArray(arr) || _nonIterableSpread(); } function _nonIterableSpread() { throw new TypeError("Invalid attempt to spread non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); } function _unsupportedIterableToArray(o, minLen) { if (!o) return; if (typeof o === "string") return _arrayLikeToArray(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(o); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); } function _iterableToArray(iter) { if (typeof Symbol !== "undefined" && iter[Symbol.iterator] != null || iter["@@iterator"] != null) return Array.from(iter); } function _arrayWithoutHoles(arr) { if (Array.isArray(arr)) return _arrayLikeToArray(arr); } function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len = arr.length; for (var i = 0, arr2 = new Array(len); i < len; i++) arr2[i] = arr[i]; return arr2; } function ownKeys(e, r) { var t = Object.keys(e); if (Object.getOwnPropertySymbols) { var o = Object.getOwnPropertySymbols(e); r && (o = o.filter(function (r) { return Object.getOwnPropertyDescriptor(e, r).enumerable; })), t.push.apply(t, o); } return t; } function _objectSpread(e) { for (var r = 1; r < arguments.length; r++) { var t = null != arguments[r] ? arguments[r] : {}; r % 2 ? ownKeys(Object(t), !0).forEach(function (r) { _defineProperty(e, r, t[r]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(e, Object.getOwnPropertyDescriptors(t)) : ownKeys(Object(t)).forEach(function (r) { Object.defineProperty(e, r, Object.getOwnPropertyDescriptor(t, r)); }); } return e; } 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 Decimal from 'decimal.js'; import { isNormalProduct } from "../../Product/utils"; /** * @title 处理组合商品 * @description 组合商品需要直接修改 product 里的价格和 is_charge_tax * @param product 商品 * @returns 处理后的商品 */ export var handleVariantProduct = function handleVariantProduct(product) { // 判断组合商品,然后直接修改 product 里的价格和 is_charge_tax if (product !== null && product !== void 0 && product.product_variant_id) { // 还需要判断 能从 variant 里找到对应的组合规格信息,才可以替换 var targetVariant = product.variant.find(function (n) { return 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; }; /** * 格式化商品到购物车 * @param product 商品 * @returns 格式化后的商品 */ export var formatProductToCartItem = function formatProductToCartItem(params) { var _product$custom_depos; var cartItem = params.cartItem, product = params.product, bundle = params.bundle, options = params.options, product_variant_id = params.product_variant_id, _params$quantity = params.quantity, quantity = _params$quantity === void 0 ? 1 : _params$quantity; var num = quantity; // 当前按照单个商品加入购物车 if (product) { cartItem.id = product === null || product === void 0 ? void 0 : product.id; cartItem.name = product === null || product === void 0 ? void 0 : product.title; cartItem.price = product === null || product === void 0 ? void 0 : product.price; cartItem.num = num; cartItem.total = getProductTotalPrice({ product: product, bundle: bundle, options: options, num: num }); cartItem.summaryTotal = cartItem.total * (num || 1); cartItem.origin_total = getProductOriginTotalPrice({ product: product, bundle: bundle, options: options, num: num }); cartItem.image = product === null || product === void 0 ? void 0 : product.cover; cartItem.like_status = 'common'; cartItem.duration = product === null || product === void 0 ? void 0 : product.duration; cartItem.isNormalProduct = isNormalProduct(product); cartItem.is_charge_tax = product === null || product === void 0 ? void 0 : product.is_charge_tax; } if (bundle !== null && bundle !== void 0 && bundle.length) { cartItem.bundle = formatBundle(bundle); } if (options !== null && options !== void 0 && options.length) { cartItem.options = formatOptions(options); } // 拼装出 product_option_string if (product_variant_id) { var _product$variant$find; var sku_key = (_product$variant$find = product.variant.find(function (n) { return n.id === product_variant_id; })) === null || _product$variant$find === void 0 ? void 0 : _product$variant$find.sku_key; if (sku_key) { var sku_key_arr = sku_key.split('_'); var product_option_string = []; sku_key_arr.forEach(function (item) { var _targetVariantGroup$v; var keyMap = item.split(':'); var targetVariantGroup = product.variant_group.find(function (n) { return String(n.position_id) === keyMap[0]; }); var targetVariantItem = targetVariantGroup === null || targetVariantGroup === void 0 || (_targetVariantGroup$v = targetVariantGroup.variant_item) === null || _targetVariantGroup$v === void 0 ? void 0 : _targetVariantGroup$v.find(function (n) { return String(n.position_id) === keyMap[1]; }); if (targetVariantGroup && targetVariantItem) { product_option_string.push("".concat(targetVariantGroup.name, ":").concat(targetVariantItem.name)); } }); cartItem.product_option_string = product_option_string.join(', '); } } // 如果商品有定金规则,则计算定金 if ((product === null || product === void 0 || (_product$custom_depos = product.custom_deposit_data) === null || _product$custom_depos === void 0 ? void 0 : _product$custom_depos.has_deposit) == 1) { cartItem.deposit = getProductDeposit({ cartItem: cartItem, product: product, bundle: bundle, options: options, num: num }); } var oringin = formatProductToCartItemOrigin(params); cartItem._origin = oringin; return cartItem; }; export var formatProductToCartItemOrigin = function formatProductToCartItemOrigin(params) { var cartItem = params.cartItem, product = params.product, bundle = params.bundle, options = params.options, product_variant_id = params.product_variant_id, _params$quantity2 = params.quantity, quantity = _params$quantity2 === void 0 ? 1 : _params$quantity2; var origin = cartItem._origin; if (product) { var _origin$product, _origin$product2, _product$duration, _product$duration2; origin.product = _objectSpread(_objectSpread({}, origin.product || {}), {}, { num: quantity, product_id: product.id, product_variant_id: product_variant_id || 0, product_bundle: ((_origin$product = origin.product) === null || _origin$product === void 0 ? void 0 : _origin$product.product_bundle) || [], product_option_item: ((_origin$product2 = origin.product) === null || _origin$product2 === void 0 ? void 0 : _origin$product2.product_option_item) || [] }); // 时长类型 origin.sub_type = product === null || product === void 0 || (_product$duration = product.duration) === null || _product$duration === void 0 ? void 0 : _product$duration.type; // 时长 origin.duration = product === null || product === void 0 || (_product$duration2 = product.duration) === null || _product$duration2 === void 0 ? void 0 : _product$duration2.value; if (bundle !== null && bundle !== void 0 && bundle.length) { origin.product.product_bundle = formatBundleToOrigin(bundle); } if (options !== null && options !== void 0 && options.length) { origin.product.product_option_item = formatOptionsToOrigin(options); } } return origin; }; /** * 获取商品总价 * @description 当前总价计算基于商品数量为1 * @param item 商品 * @returns 商品总价 */ export var getProductTotalPrice = function getProductTotalPrice(params) { var product = params.product, bundle = params.bundle, options = params.options; // const num = params.num || 1; var price = Number(product.price); if (bundle !== null && bundle !== void 0 && bundle.length) { price = bundle.reduce(function (accumulator, currentValue) { return accumulator + Number(currentValue.price) * Number(currentValue.num); }, price); } // 单规格 if (options !== null && options !== void 0 && options.length) { price = options.reduce(function (accumulator, currentValue) { return accumulator + Number(currentValue.price) * Number(currentValue.num); }, price); } return price; }; /** * 获取商品原始总价 * @description 当前总价计算基于商品数量为1 * @param item 商品 * @returns 商品原始总价 */ export var getProductOriginTotalPrice = function getProductOriginTotalPrice(params) { var product = params.product, bundle = params.bundle, options = params.options; var num = (params === null || params === void 0 ? void 0 : params.num) || 1; var price = Number(product.original_price); // if (product.discount_list?.length) { // price = product.discount_list.reduce((accumulator: any, currentValue: any) => { // return ( // accumulator + Number(currentValue.price) * Number(currentValue.num) // ); // }, price); // } if (isNaN(price) || price === 0) { return undefined; } if (bundle !== null && bundle !== void 0 && bundle.length) { price = bundle.reduce(function (accumulator, currentValue) { return accumulator + Number(currentValue.price) * Number(currentValue.num); }, price); } // 单规格 if (options !== null && options !== void 0 && options.length) { price = options.reduce(function (accumulator, currentValue) { return accumulator + Number(currentValue.price) * Number(currentValue.num); }, price); } return price; }; /** * 格式化套餐规格信息 * @param options 套餐规格信息 * @returns 格式化后的套餐规格信息 */ export var formatOptions = function formatOptions(options) { if (!(options !== null && options !== void 0 && options.length)) return []; return options.map(function (item) { return { id: item === null || item === void 0 ? void 0 : item.id, name: item === null || item === void 0 ? void 0 : item.name, num: item === null || item === void 0 ? void 0 : item.num, price: item === null || item === void 0 ? void 0 : item.price, total: item === null || item === void 0 ? void 0 : item.price, origin_total: item === null || item === void 0 ? void 0 : item.original_price }; }); }; export var formatOptionsToOrigin = function formatOptionsToOrigin(options) { if (!(options !== null && options !== void 0 && options.length)) { return []; } return options.map(function (d) { return { num: d.num, option_group_item_id: d.product_option_item_id, option_group_id: d.option_group_id }; }); }; /** * 格式化商品套餐信息 * @param bundle 商品套餐信息 * @returns 格式化后的商品套餐信息 */ export var formatBundle = function formatBundle(bundle) { if (!(bundle !== null && bundle !== void 0 && bundle.length)) return []; return bundle.map(function (item) { return { id: item === null || item === void 0 ? void 0 : item.id, title: item === null || item === void 0 ? void 0 : item.title, num: item === null || item === void 0 ? void 0 : item.num, price: item === null || item === void 0 ? void 0 : item.price, total: item === null || item === void 0 ? void 0 : item.price, price_type: item === null || item === void 0 ? void 0 : item.price_type, bundle_sum_price: item === null || item === void 0 ? void 0 : item.bundle_sum_price, bundle_sum_type: item === null || item === void 0 ? void 0 : item.bundle_sum_type, options: formatOptions(item === null || item === void 0 ? void 0 : item.option) }; }); }; export var formatBundleToOrigin = function formatBundleToOrigin(bundle) { if (!(bundle !== null && bundle !== void 0 && bundle.length)) return []; return bundle.map(function (d) { return { bundle_group_id: d.group_id, bundle_id: d.id, bundle_product_id: (d === null || d === void 0 ? void 0 : d._bundle_product_id) || (d === null || d === void 0 ? 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) }; }); }; /** * 获取商品定金 * @description 定金基于商品售价来算,最终乘商品数量 * 1、如果套餐主商品有定金规则,则定金为:(套餐商品总售价 * 定金百分比 + 定金固定金额)* 商品数量 * 2、如果套餐主商品没有定金规则,子商品有定金规则,则定金为:(套餐子商品售价 * 套餐子商品定金百分比 + 套餐子商品定金固定金额)* 商品数量,最终将所有子商品定金相加 * 3、普通商品/单规格商品/组合规则商品,定金规则:(商品总售价 * 定金百分比 + 定金固定金额)* 商品数量 * @param params 参数 * @returns 商品定金 */ export var getProductDeposit = function getProductDeposit(params) { var _product$custom_depos2; var cartItem = params.cartItem, product = params.product, bundle = params.bundle, options = params.options; var num = (params === null || params === void 0 ? void 0 : params.num) || 1; // 判定商品是否有定金规则 if ((product === null || product === void 0 || (_product$custom_depos2 = product.custom_deposit_data) === null || _product$custom_depos2 === void 0 ? void 0 : _product$custom_depos2.has_deposit) == 1) { var total = new Decimal(0); var _ref = (product === null || product === void 0 ? void 0 : product.custom_deposit_data) || {}, deposit_fixed = _ref.deposit_fixed, deposit_percentage = _ref.deposit_percentage, deposit_policy_data = _ref.deposit_policy_data, self_deposit_policy_ids = _ref.self_deposit_policy_ids; // 所有协议数据 var allProtocols = deposit_policy_data || []; // 是否存在定金,主商品has_deposit为1,但是自身没有定金设置,需要判定子商品是否存在定金,如果不存在最终判定也是不存在定金 var productHasDeposit = false; // 关联的定金协议id var protocolIds = []; // 判断主商品是否有定金规则 if (typeof deposit_fixed === 'string' && typeof deposit_percentage === 'string') { var _handleProductDeposit = handleProductDeposit({ depositData: product === null || product === void 0 ? void 0 : product.custom_deposit_data, total: cartItem.total || 0, num: num }), depositTotal = _handleProductDeposit.depositTotal, result = _handleProductDeposit.result; if (result) { total = depositTotal; protocolIds.push.apply(protocolIds, _toConsumableArray(self_deposit_policy_ids || [])); productHasDeposit = true; } } else { if (bundle !== null && bundle !== void 0 && bundle.length) { total = bundle.reduce(function (accumulator, currBundleProduct) { var depositData = currBundleProduct === null || currBundleProduct === void 0 ? void 0 : currBundleProduct.custom_deposit_data; var _handleProductDeposit2 = handleProductDeposit({ depositData: depositData, total: currBundleProduct === null || currBundleProduct === void 0 ? void 0 : currBundleProduct.price, num: (currBundleProduct === null || currBundleProduct === void 0 ? void 0 : currBundleProduct.num) || 1 }), depositTotal = _handleProductDeposit2.depositTotal, result = _handleProductDeposit2.result; if (result) { protocolIds.push.apply(protocolIds, _toConsumableArray((depositData === null || depositData === void 0 ? void 0 : depositData.self_deposit_policy_ids) || [])); productHasDeposit = true; return accumulator.plus(depositTotal); } return accumulator; }, new Decimal(0)); } } if (productHasDeposit) { var ids = new Set(protocolIds); var protocols = allProtocols.filter(function (item) { return ids.has(item.id); }); return { total: total.toNumber(), protocols: protocols }; } return null; } return null; }; /** * 计算商品定金 * @param params 参数 * @returns 商品定金 */ export var handleProductDeposit = function handleProductDeposit(params) { var depositData = params.depositData, total = params.total, num = params.num; if (!depositData) { return { result: false, depositTotal: new Decimal(0) }; } var _ref2 = depositData || {}, deposit_fixed = _ref2.deposit_fixed, deposit_percentage = _ref2.deposit_percentage; if (typeof deposit_fixed === 'string' && typeof deposit_percentage === 'string') { var depositFixed = Decimal(deposit_fixed || 0); var depositPercent = Decimal(deposit_percentage || 0); var productTotalPrice = Decimal(total || 0); var deposit = depositFixed.plus(depositPercent.times(productTotalPrice)); return { depositTotal: deposit.times(num), result: true }; } return { result: false, depositTotal: new Decimal(0) }; };