sku-helper.js 4.4 KB

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