product.js 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. // ========== 商品相关的 Util ==========
  2. /**
  3. * 从商品 SKU 数组中,转换出商品属性的数组
  4. *
  5. * 类似结构:[{
  6. * id: // 属性的编号
  7. * name: // 属性的名字
  8. * values: [{
  9. * id: // 属性值的编号
  10. * name: // 属性值的名字
  11. * }]
  12. * }]
  13. *
  14. * @param skus 商品 SKU 数组
  15. */
  16. export function convertProductPropertyList(skus) {
  17. let result = [];
  18. for (const sku of skus) {
  19. if (!sku.properties) {
  20. continue
  21. }
  22. for (const property of sku.properties) {
  23. // ① 先处理属性
  24. let resultProperty = result.find(item => item.id === property.propertyId)
  25. if (!resultProperty) {
  26. resultProperty = {
  27. id: property.propertyId,
  28. name: property.propertyName,
  29. values: []
  30. }
  31. result.push(resultProperty)
  32. }
  33. // ② 再处理属性值
  34. let resultValue = resultProperty.values.find(item => item.id === property.valueId)
  35. if (!resultValue) {
  36. resultProperty.values.push({
  37. id: property.valueId,
  38. name: property.valueName
  39. })
  40. }
  41. }
  42. }
  43. return result;
  44. }
  45. /**
  46. * 从商品 SKU 数组中,转换出商品 SKU Map
  47. *
  48. * key: 属性值的拼接,使用 , 间隔,例如说 黑色,CH510
  49. *
  50. * @param skus 商品 SKU Map
  51. */
  52. export function convertProductSkuMap(skus) {
  53. let result = {};
  54. for (const sku of skus) {
  55. let key = '';
  56. if (!sku.properties) {
  57. continue
  58. }
  59. for (const property of sku.properties) {
  60. if (key !== '') {
  61. key += ','
  62. }
  63. key += property.valueName
  64. }
  65. result[key] = sku
  66. }
  67. return result
  68. }
  69. /**
  70. * 根据 SPU 的活动展示顺序,排序活动列表
  71. *
  72. * @param activityList 原始的活动列表
  73. * @param spu
  74. * @return 新的活动列表
  75. */
  76. export function sortActivityList(activityList, spu) {
  77. if (!spu || !spu.activityOrders) {
  78. return activityList;
  79. }
  80. const result = [];
  81. for (const activityOrder of spu.activityOrders) {
  82. for (const activity of activityList) {
  83. if (activity.type === activityOrder) {
  84. result.push(activity)
  85. }
  86. }
  87. }
  88. return result;
  89. }
  90. /**
  91. * 将营销活动,设置到 SPU 中
  92. *
  93. * @param spuList SPU 数组
  94. * @param activityListMap 营销活动 Map,key 为 SPU ID,value 为营销活动数组
  95. */
  96. export function setActivityList(spuList, activityListMap) {
  97. if (!spuList || spuList.length === 0) {
  98. return;
  99. }
  100. for (const spu of spuList) {
  101. spu.activityList = activityListMap[spu.id + ''] || [];
  102. spu.activityList = sortActivityList(spu.activityList, spu);
  103. }
  104. }