index.js 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. import Toast from 'tdesign-miniprogram/toast/index';
  2. const shortageImg = 'https://tdesign.gtimg.com/miniprogram/template/retail/cart/shortage.png';
  3. Component({
  4. isSpecsTap: false, // 标记本次点击事件是否因为点击specs触发(由于底层goods-card组件没有catch specs点击事件,只能在此处加状态来避免点击specs时触发跳转商品详情)
  5. externalClasses: ['wr-class'],
  6. properties: {
  7. storeGoods: {
  8. type: Array,
  9. observer(storeGoods) {
  10. for (const store of storeGoods) {
  11. for (const activity of store.promotionGoodsList) {
  12. for (const goods of activity.goodsPromotionList) {
  13. goods.specs = goods.specInfo.map((item) => item.specValue); // 目前仅展示商品已选规格的值
  14. }
  15. }
  16. for (const goods of store.shortageGoodsList) {
  17. goods.specs = goods.specInfo.map((item) => item.specValue); // 目前仅展示商品已选规格的值
  18. }
  19. }
  20. this.setData({ _storeGoods: storeGoods });
  21. },
  22. },
  23. invalidGoodItems: {
  24. type: Array,
  25. observer(invalidGoodItems) {
  26. invalidGoodItems.forEach((goods) => {
  27. goods.specs = goods.specInfo.map((item) => item.specValue); // 目前仅展示商品已选规格的值
  28. });
  29. this.setData({ _invalidGoodItems: invalidGoodItems });
  30. },
  31. },
  32. thumbWidth: { type: null },
  33. thumbHeight: { type: null },
  34. },
  35. data: {
  36. shortageImg,
  37. isShowSpecs: false,
  38. currentGoods: {},
  39. isShowToggle: false,
  40. _storeGoods: [],
  41. _invalidGoodItems: [],
  42. },
  43. methods: {
  44. // 删除商品
  45. deleteGoods(e) {
  46. const { goods } = e.currentTarget.dataset;
  47. this.triggerEvent('delete', { goods });
  48. },
  49. // 清空失效商品
  50. clearInvalidGoods() {
  51. this.triggerEvent('clearinvalidgoods');
  52. },
  53. // 选中商品
  54. selectGoods(e) {
  55. const { goods } = e.currentTarget.dataset;
  56. this.triggerEvent('selectgoods', {
  57. goods,
  58. isSelected: !goods.isSelected,
  59. });
  60. },
  61. changeQuantity(num, goods) {
  62. this.triggerEvent('changequantity', {
  63. goods,
  64. quantity: num,
  65. });
  66. },
  67. changeStepper(e) {
  68. const { value } = e.detail;
  69. const { goods } = e.currentTarget.dataset;
  70. let num = value;
  71. if (value > goods.stack) {
  72. num = goods.stack;
  73. }
  74. this.changeQuantity(num, goods);
  75. },
  76. input(e) {
  77. const { value } = e.detail;
  78. const { goods } = e.currentTarget.dataset;
  79. const num = value;
  80. this.changeQuantity(num, goods);
  81. },
  82. overlimit(e) {
  83. const text = e.detail.type === 'minus' ? '该商品数量不能减少了哦' : '同一商品最多购买999件';
  84. Toast({
  85. context: this,
  86. selector: '#t-toast',
  87. message: text,
  88. });
  89. },
  90. // 去凑单/再逛逛
  91. gotoBuyMore(e) {
  92. const { promotion, storeId = '' } = e.currentTarget.dataset;
  93. this.triggerEvent('gocollect', { promotion, storeId });
  94. },
  95. // 选中门店
  96. selectStore(e) {
  97. const { storeIndex } = e.currentTarget.dataset;
  98. const store = this.data.storeGoods[storeIndex];
  99. const isSelected = !store.isSelected;
  100. if (store.storeStockShortage && isSelected) {
  101. Toast({
  102. context: this,
  103. selector: '#t-toast',
  104. message: '部分商品库存不足',
  105. });
  106. return;
  107. }
  108. this.triggerEvent('selectstore', {
  109. store,
  110. isSelected,
  111. });
  112. },
  113. // 展开/收起切换
  114. showToggle() {
  115. this.setData({
  116. isShowToggle: !this.data.isShowToggle,
  117. });
  118. },
  119. // 展示规格popup
  120. specsTap(e) {
  121. this.isSpecsTap = true;
  122. const { goods } = e.currentTarget.dataset;
  123. this.setData({
  124. isShowSpecs: true,
  125. currentGoods: goods,
  126. });
  127. },
  128. hideSpecsPopup() {
  129. this.setData({
  130. isShowSpecs: false,
  131. });
  132. },
  133. goGoodsDetail(e) {
  134. if (this.isSpecsTap) {
  135. this.isSpecsTap = false;
  136. return;
  137. }
  138. const { goods } = e.currentTarget.dataset;
  139. this.triggerEvent('goodsclick', { goods });
  140. },
  141. gotoCoupons() {
  142. wx.navigateTo({ url: '/pages/coupon/coupon-list/index' });
  143. },
  144. },
  145. });