sku-helper.js 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. "use strict";
  2. var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
  3. exports.__esModule = true;
  4. exports.default = exports.getSelectedProperties = exports.getSelectedPropValues = exports.isSkuChoosable = exports.getSelectedSkuValues = exports.getSkuComb = exports.isAllSelected = exports.normalizePropList = exports.normalizeSkuTree = void 0;
  5. var _extends3 = _interopRequireDefault(require("@babel/runtime/helpers/extends"));
  6. var _constants = require("../constants");
  7. /*
  8. normalize sku tree
  9. [
  10. {
  11. count: 2,
  12. k: "品种", // 规格名称 skuKeyName
  13. k_id: "1200", // skuKeyId
  14. k_s: "s1" // skuKeyStr
  15. v: [ // skuValues
  16. { // skuValue
  17. id: "1201", // skuValueId
  18. name: "萌" // 具体的规格值 skuValueName
  19. }, {
  20. id: "973",
  21. name: "帅"
  22. }
  23. ]
  24. },
  25. ...
  26. ]
  27. |
  28. v
  29. {
  30. s1: [{
  31. id: "1201",
  32. name: "萌"
  33. }, {
  34. id: "973",
  35. name: "帅"
  36. }],
  37. ...
  38. }
  39. */
  40. var normalizeSkuTree = function normalizeSkuTree(skuTree) {
  41. var normalizedTree = {};
  42. skuTree.forEach(function (treeItem) {
  43. normalizedTree[treeItem.k_s] = treeItem.v;
  44. });
  45. return normalizedTree;
  46. };
  47. exports.normalizeSkuTree = normalizeSkuTree;
  48. var normalizePropList = function normalizePropList(propList) {
  49. var normalizedProp = {};
  50. propList.forEach(function (item) {
  51. var itemObj = {};
  52. item.v.forEach(function (it) {
  53. itemObj[it.id] = it;
  54. });
  55. normalizedProp[item.k_id] = itemObj;
  56. });
  57. return normalizedProp;
  58. }; // 判断是否所有的sku都已经选中
  59. exports.normalizePropList = normalizePropList;
  60. var isAllSelected = function isAllSelected(skuTree, selectedSku) {
  61. // 筛选selectedSku对象中key值不为空的值
  62. var selected = Object.keys(selectedSku).filter(function (skuKeyStr) {
  63. return selectedSku[skuKeyStr] !== _constants.UNSELECTED_SKU_VALUE_ID;
  64. });
  65. return skuTree.length === selected.length;
  66. }; // 根据已选择的 sku 获取 skuComb
  67. exports.isAllSelected = isAllSelected;
  68. var getSkuComb = function getSkuComb(skuList, selectedSku) {
  69. var skuComb = skuList.filter(function (item) {
  70. return Object.keys(selectedSku).every(function (skuKeyStr) {
  71. return String(item[skuKeyStr]) === String(selectedSku[skuKeyStr]);
  72. });
  73. });
  74. return skuComb[0];
  75. }; // 获取已选择的sku名称
  76. exports.getSkuComb = getSkuComb;
  77. var getSelectedSkuValues = function getSelectedSkuValues(skuTree, selectedSku) {
  78. var normalizedTree = normalizeSkuTree(skuTree);
  79. return Object.keys(selectedSku).reduce(function (selectedValues, skuKeyStr) {
  80. var skuValues = normalizedTree[skuKeyStr] || [];
  81. var skuValueId = selectedSku[skuKeyStr];
  82. if (skuValueId !== _constants.UNSELECTED_SKU_VALUE_ID && skuValues.length > 0) {
  83. var skuValue = skuValues.filter(function (value) {
  84. return value.id === skuValueId;
  85. })[0];
  86. skuValue && selectedValues.push(skuValue);
  87. }
  88. return selectedValues;
  89. }, []);
  90. }; // 判断sku是否可选
  91. exports.getSelectedSkuValues = getSelectedSkuValues;
  92. var isSkuChoosable = function isSkuChoosable(skuList, selectedSku, skuToChoose) {
  93. var _extends2;
  94. var key = skuToChoose.key,
  95. valueId = skuToChoose.valueId; // 先假设sku已选中,拼入已选中sku对象中
  96. var matchedSku = (0, _extends3.default)({}, selectedSku, (_extends2 = {}, _extends2[key] = valueId, _extends2)); // 再判断剩余sku是否全部不可选,若不可选则当前sku不可选中
  97. var skusToCheck = Object.keys(matchedSku).filter(function (skuKey) {
  98. return matchedSku[skuKey] !== _constants.UNSELECTED_SKU_VALUE_ID;
  99. });
  100. var filteredSku = skuList.filter(function (sku) {
  101. return skusToCheck.every(function (skuKey) {
  102. return String(matchedSku[skuKey]) === String(sku[skuKey]);
  103. });
  104. });
  105. var stock = filteredSku.reduce(function (total, sku) {
  106. total += sku.stock_num;
  107. return total;
  108. }, 0);
  109. return stock > 0;
  110. };
  111. exports.isSkuChoosable = isSkuChoosable;
  112. var getSelectedPropValues = function getSelectedPropValues(propList, selectedProp) {
  113. var normalizeProp = normalizePropList(propList);
  114. return Object.keys(selectedProp).reduce(function (acc, cur) {
  115. selectedProp[cur].forEach(function (it) {
  116. acc.push((0, _extends3.default)({}, normalizeProp[cur][it]));
  117. });
  118. return acc;
  119. }, []);
  120. };
  121. exports.getSelectedPropValues = getSelectedPropValues;
  122. var getSelectedProperties = function getSelectedProperties(propList, selectedProp) {
  123. var list = [];
  124. (propList || []).forEach(function (prop) {
  125. if (selectedProp[prop.k_id] && selectedProp[prop.k_id].length > 0) {
  126. var v = [];
  127. prop.v.forEach(function (it) {
  128. if (selectedProp[prop.k_id].indexOf(it.id) > -1) {
  129. v.push((0, _extends3.default)({}, it));
  130. }
  131. });
  132. list.push((0, _extends3.default)({}, prop, {
  133. v: v
  134. }));
  135. }
  136. });
  137. return list;
  138. };
  139. exports.getSelectedProperties = getSelectedProperties;
  140. var _default = {
  141. normalizeSkuTree: normalizeSkuTree,
  142. getSkuComb: getSkuComb,
  143. getSelectedSkuValues: getSelectedSkuValues,
  144. isAllSelected: isAllSelected,
  145. isSkuChoosable: isSkuChoosable,
  146. getSelectedPropValues: getSelectedPropValues,
  147. getSelectedProperties: getSelectedProperties
  148. };
  149. exports.default = _default;