@pisell/pisellos
Version:
一个可扩展的前端模块化SDK框架,支持插件系统
417 lines (415 loc) • 16.4 kB
JavaScript
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 import_utils2 = require("../../../solution/ShopDiscount/utils");
var handleVariantProduct = (product) => {
var _a;
if (product == null ? void 0 : product.product_variant_id) {
const targetVariant = (_a = product == null ? void 0 : product.variant) == null ? void 0 : _a.find(
(n) => n.id === product.product_variant_id
);
if (targetVariant) {
product.price = targetVariant.price;
if (Number(targetVariant.original_price) > 0) {
product.original_price = targetVariant.original_price;
}
product.is_charge_tax = targetVariant.is_charge_tax;
}
}
return product;
};
var formatProductToCartItem = (params) => {
var _a, _b, _c;
console.log("formatProductToCartItem>>>>>>", params);
const {
cartItem,
product,
bundle,
options,
product_variant_id,
quantity = 1,
discounts
} = 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, discounts });
cartItem.summaryTotal = cartItem.total * (num || 1);
cartItem.origin_total = getProductOriginTotalPrice({
product,
bundle,
options,
num,
discounts
});
const origin_total = getProductTotalPrice({
product: cartItem._productInit || cartItem._productOrigin,
bundle,
options,
num,
discounts: []
});
cartItem.summaryOriginTotal = (origin_total || 0) * (num || 1);
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 = (_b = (_a = product == null ? void 0 : product.variant) == null ? void 0 : _a.find(
(n) => n.id === product_variant_id
)) == null ? void 0 : _b.sku_key;
if (sku_key) {
const sku_key_arr = sku_key.split("_");
const product_option_string = [];
sku_key_arr.forEach((item) => {
var _a2, _b2;
const keyMap = item.split(":");
const targetVariantGroup = (_a2 = product == null ? void 0 : product.variant_group) == null ? void 0 : _a2.find(
(n) => String(n.position_id) === keyMap[0]
);
const targetVariantItem = (_b2 = targetVariantGroup == null ? void 0 : targetVariantGroup.variant_item) == null ? void 0 : _b2.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 (((_c = product == null ? void 0 : product.custom_deposit_data) == null ? void 0 : _c.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, _e;
const {
cartItem,
product,
bundle,
options,
product_variant_id,
quantity = 1
} = params;
let origin = cartItem._origin;
const product_discount_difference = (((_a = origin.product) == null ? void 0 : _a.discount_list) || []).filter((item) => {
var _a2;
return !((_a2 = item == null ? void 0 : item.metadata) == null ? void 0 : _a2.custom_product_bundle_map_id);
}).reduce((pre, cur) => {
var _a2;
return pre + (((_a2 = cur == null ? void 0 : cur.metadata) == null ? void 0 : _a2.product_discount_difference) || 0);
}, 0);
if (product) {
origin.product = {
...origin.product || {},
num: quantity,
product_id: product.id,
product_variant_id: product_variant_id || 0,
product_bundle: ((_b = origin.product) == null ? void 0 : _b.product_bundle) || [],
product_option_item: ((_c = origin.product) == null ? void 0 : _c.product_option_item) || [],
metadata: {
product_discount_difference
}
};
origin.sub_type = (_d = product == null ? void 0 : product.duration) == null ? void 0 : _d.type;
origin.duration = (_e = product == null ? void 0 : product.duration) == null ? void 0 : _e.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, discounts } = params;
let price = Number(product.price);
if (discounts == null ? void 0 : discounts.length) {
discounts.forEach((currentValue) => {
var _a;
price = (0, import_utils2.getDiscountAmount)({
amount: currentValue.amount,
tag: currentValue.type,
par_value: currentValue.discount.percent,
config: currentValue.config,
metadata: {
discount_card_type: (_a = currentValue == null ? void 0 : currentValue.discount) == null ? void 0 : _a.discount_card_type
}
}, price, 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 Math.max(0, price);
};
var getProductOriginTotalPrice = (params) => {
const { product, bundle, options, discounts } = params;
const num = (params == null ? void 0 : params.num) || 1;
let price = Number(product.original_price);
if ((isNaN(price) || price === 0) && !(discounts == null ? void 0 : discounts.length)) {
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,
original_price: item == null ? void 0 : item.original_price,
original_total: item == null ? void 0 : item.original_total,
price_type_ext: item == null ? void 0 : item.price_type_ext,
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),
_bundle_product_id: item._bundle_product_id,
discount_list: item.discount_list,
originBundleItem: item.originBundleItem || item
};
});
};
var formatBundleToOrigin = (bundle) => {
if (!(bundle == null ? void 0 : bundle.length))
return [];
return bundle.map((d) => {
const getBundleValueByKey = (key) => {
var _a;
return (d == null ? void 0 : d[key]) || ((_a = d == null ? void 0 : d.originBundleItem) == null ? void 0 : _a[key]);
};
const product_discount_difference = ((d == null ? void 0 : d.discount_list) || []).reduce((pre, cur) => {
var _a;
return pre + (((_a = cur == null ? void 0 : cur.metadata) == null ? void 0 : _a.product_discount_difference) || 0);
}, 0);
return {
bundle_group_id: getBundleValueByKey("group_id"),
bundle_id: d.id,
bundle_product_id: getBundleValueByKey("_bundle_product_id") || getBundleValueByKey("product_id"),
bundle_variant_id: getBundleValueByKey("bundle_variant_id"),
num: d.num,
extension_id: getBundleValueByKey("extension_id"),
extension_type: getBundleValueByKey("extension_type"),
option: formatOptionsToOrigin(getBundleValueByKey("option")),
discount_list: d.discount_list,
"bundle_selling_price": d == null ? void 0 : d.price,
metadata: {
custom_product_bundle_map_id: d._id,
product_discount_difference
}
};
});
};
var getProductDeposit = (params) => {
var _a, _b;
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 (product == null ? void 0 : product.product_variant_id) {
const variantProduct = (_b = product == null ? void 0 : product.variant) == null ? void 0 : _b.find(
(item) => (item == null ? void 0 : item.id) === (product == null ? void 0 : product.product_variant_id)
);
if (variantProduct) {
const depositData = variantProduct == null ? void 0 : variantProduct.custom_deposit_data;
const { depositTotal, result } = handleProductDeposit({
depositData,
total: (cartItem == null ? void 0 : cartItem.total) || 0,
num
});
if (result) {
total = depositTotal;
protocolIds.push(...(depositData == null ? void 0 : depositData.self_deposit_policy_ids) || []);
productHasDeposit = true;
}
}
}
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
});