123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179 |
- "use strict";
- var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
- exports.__esModule = true;
- exports.default = exports.getSelectedProperties = exports.getSelectedPropValues = exports.isSkuChoosable = exports.getSelectedSkuValues = exports.getSkuComb = exports.isAllSelected = exports.normalizePropList = exports.normalizeSkuTree = void 0;
- var _extends3 = _interopRequireDefault(require("@babel/runtime/helpers/extends"));
- var _constants = require("../constants");
- /*
- normalize sku tree
- [
- {
- count: 2,
- k: "品种", // 规格名称 skuKeyName
- k_id: "1200", // skuKeyId
- k_s: "s1" // skuKeyStr
- v: [ // skuValues
- { // skuValue
- id: "1201", // skuValueId
- name: "萌" // 具体的规格值 skuValueName
- }, {
- id: "973",
- name: "帅"
- }
- ]
- },
- ...
- ]
- |
- v
- {
- s1: [{
- id: "1201",
- name: "萌"
- }, {
- id: "973",
- name: "帅"
- }],
- ...
- }
- */
- var normalizeSkuTree = function normalizeSkuTree(skuTree) {
- var normalizedTree = {};
- skuTree.forEach(function (treeItem) {
- normalizedTree[treeItem.k_s] = treeItem.v;
- });
- return normalizedTree;
- };
- exports.normalizeSkuTree = normalizeSkuTree;
- var normalizePropList = function normalizePropList(propList) {
- var normalizedProp = {};
- propList.forEach(function (item) {
- var itemObj = {};
- item.v.forEach(function (it) {
- itemObj[it.id] = it;
- });
- normalizedProp[item.k_id] = itemObj;
- });
- return normalizedProp;
- }; // 判断是否所有的sku都已经选中
- exports.normalizePropList = normalizePropList;
- var isAllSelected = function isAllSelected(skuTree, selectedSku) {
- // 筛选selectedSku对象中key值不为空的值
- var selected = Object.keys(selectedSku).filter(function (skuKeyStr) {
- return selectedSku[skuKeyStr] !== _constants.UNSELECTED_SKU_VALUE_ID;
- });
- return skuTree.length === selected.length;
- }; // 根据已选择的 sku 获取 skuComb
- exports.isAllSelected = isAllSelected;
- var getSkuComb = function getSkuComb(skuList, selectedSku) {
- var skuComb = skuList.filter(function (item) {
- return Object.keys(selectedSku).every(function (skuKeyStr) {
- return String(item[skuKeyStr]) === String(selectedSku[skuKeyStr]);
- });
- });
- return skuComb[0];
- }; // 获取已选择的sku名称
- exports.getSkuComb = getSkuComb;
- var getSelectedSkuValues = function getSelectedSkuValues(skuTree, selectedSku) {
- var normalizedTree = normalizeSkuTree(skuTree);
- return Object.keys(selectedSku).reduce(function (selectedValues, skuKeyStr) {
- var skuValues = normalizedTree[skuKeyStr] || [];
- var skuValueId = selectedSku[skuKeyStr];
- if (skuValueId !== _constants.UNSELECTED_SKU_VALUE_ID && skuValues.length > 0) {
- var skuValue = skuValues.filter(function (value) {
- return value.id === skuValueId;
- })[0];
- skuValue && selectedValues.push(skuValue);
- }
- return selectedValues;
- }, []);
- }; // 判断sku是否可选
- exports.getSelectedSkuValues = getSelectedSkuValues;
- var isSkuChoosable = function isSkuChoosable(skuList, selectedSku, skuToChoose) {
- var _extends2;
- var key = skuToChoose.key,
- valueId = skuToChoose.valueId; // 先假设sku已选中,拼入已选中sku对象中
- var matchedSku = (0, _extends3.default)({}, selectedSku, (_extends2 = {}, _extends2[key] = valueId, _extends2)); // 再判断剩余sku是否全部不可选,若不可选则当前sku不可选中
- var skusToCheck = Object.keys(matchedSku).filter(function (skuKey) {
- return matchedSku[skuKey] !== _constants.UNSELECTED_SKU_VALUE_ID;
- });
- var filteredSku = skuList.filter(function (sku) {
- return skusToCheck.every(function (skuKey) {
- return String(matchedSku[skuKey]) === String(sku[skuKey]);
- });
- });
- var stock = filteredSku.reduce(function (total, sku) {
- total += sku.stock_num;
- return total;
- }, 0);
- return stock > 0;
- };
- exports.isSkuChoosable = isSkuChoosable;
- var getSelectedPropValues = function getSelectedPropValues(propList, selectedProp) {
- var normalizeProp = normalizePropList(propList);
- return Object.keys(selectedProp).reduce(function (acc, cur) {
- selectedProp[cur].forEach(function (it) {
- acc.push((0, _extends3.default)({}, normalizeProp[cur][it]));
- });
- return acc;
- }, []);
- };
- exports.getSelectedPropValues = getSelectedPropValues;
- var getSelectedProperties = function getSelectedProperties(propList, selectedProp) {
- var list = [];
- (propList || []).forEach(function (prop) {
- if (selectedProp[prop.k_id] && selectedProp[prop.k_id].length > 0) {
- var v = [];
- prop.v.forEach(function (it) {
- if (selectedProp[prop.k_id].indexOf(it.id) > -1) {
- v.push((0, _extends3.default)({}, it));
- }
- });
- list.push((0, _extends3.default)({}, prop, {
- v: v
- }));
- }
- });
- return list;
- };
- exports.getSelectedProperties = getSelectedProperties;
- var _default = {
- normalizeSkuTree: normalizeSkuTree,
- getSkuComb: getSkuComb,
- getSelectedSkuValues: getSelectedSkuValues,
- isAllSelected: isAllSelected,
- isSkuChoosable: isSkuChoosable,
- getSelectedPropValues: getSelectedPropValues,
- getSelectedProperties: getSelectedProperties
- };
- exports.default = _default;
|