index.js 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294
  1. import Dialog from 'tdesign-miniprogram/dialog/index';
  2. import Toast from 'tdesign-miniprogram/toast/index';
  3. import { fetchCartGroupData } from '../../services/cart/cart';
  4. Page({
  5. data: {
  6. cartGroupData: null,
  7. },
  8. // 调用自定义tabbar的init函数,使页面与tabbar激活状态保持一致
  9. onShow() {
  10. this.getTabBar().init();
  11. },
  12. onLoad() {
  13. this.refreshData();
  14. },
  15. refreshData() {
  16. this.getCartGroupData().then((res) => {
  17. let isEmpty = true;
  18. const cartGroupData = res.data;
  19. // 一些组件中需要的字段可能接口并没有返回,或者返回的数据结构与预期不一致,需要在此先对数据做一些处理
  20. // 统计门店下加购的商品是否全选、是否存在缺货/无货
  21. for (const store of cartGroupData.storeGoods) {
  22. store.isSelected = true; // 该门店已加购商品是否全选
  23. store.storeStockShortage = false; // 该门店已加购商品是否存在库存不足
  24. if (!store.shortageGoodsList) {
  25. store.shortageGoodsList = []; // 该门店已加购商品如果库存为0需单独分组
  26. }
  27. for (const activity of store.promotionGoodsList) {
  28. activity.goodsPromotionList = activity.goodsPromotionList.filter((goods) => {
  29. goods.originPrice = undefined;
  30. // 统计是否有加购数大于库存数的商品
  31. if (goods.quantity > goods.stockQuantity) {
  32. store.storeStockShortage = true;
  33. }
  34. // 统计是否全选
  35. if (!goods.isSelected) {
  36. store.isSelected = false;
  37. }
  38. // 库存为0(无货)的商品单独分组
  39. if (goods.stockQuantity > 0) {
  40. return true;
  41. }
  42. store.shortageGoodsList.push(goods);
  43. return false;
  44. });
  45. if (activity.goodsPromotionList.length > 0) {
  46. isEmpty = false;
  47. }
  48. }
  49. if (store.shortageGoodsList.length > 0) {
  50. isEmpty = false;
  51. }
  52. }
  53. cartGroupData.invalidGoodItems = cartGroupData.invalidGoodItems.map((goods) => {
  54. goods.originPrice = undefined;
  55. return goods;
  56. });
  57. cartGroupData.isNotEmpty = !isEmpty;
  58. this.setData({ cartGroupData });
  59. });
  60. },
  61. findGoods(spuId, skuId) {
  62. let currentStore;
  63. let currentActivity;
  64. let currentGoods;
  65. const { storeGoods } = this.data.cartGroupData;
  66. for (const store of storeGoods) {
  67. for (const activity of store.promotionGoodsList) {
  68. for (const goods of activity.goodsPromotionList) {
  69. if (goods.spuId === spuId && goods.skuId === skuId) {
  70. currentStore = store;
  71. currentActivity = currentActivity;
  72. currentGoods = goods;
  73. return {
  74. currentStore,
  75. currentActivity,
  76. currentGoods,
  77. };
  78. }
  79. }
  80. }
  81. }
  82. return {
  83. currentStore,
  84. currentActivity,
  85. currentGoods,
  86. };
  87. },
  88. // 注:实际场景时应该调用接口获取购物车数据
  89. getCartGroupData() {
  90. const { cartGroupData } = this.data;
  91. if (!cartGroupData) {
  92. return fetchCartGroupData();
  93. }
  94. return Promise.resolve({ data: cartGroupData });
  95. },
  96. // 选择单个商品
  97. // 注:实际场景时应该调用接口更改选中状态
  98. selectGoodsService({ spuId, skuId, isSelected }) {
  99. this.findGoods(spuId, skuId).currentGoods.isSelected = isSelected;
  100. return Promise.resolve();
  101. },
  102. // 全选门店
  103. // 注:实际场景时应该调用接口更改选中状态
  104. selectStoreService({ storeId, isSelected }) {
  105. const currentStore = this.data.cartGroupData.storeGoods.find((s) => s.storeId === storeId);
  106. currentStore.isSelected = isSelected;
  107. currentStore.promotionGoodsList.forEach((activity) => {
  108. activity.goodsPromotionList.forEach((goods) => {
  109. goods.isSelected = isSelected;
  110. });
  111. });
  112. return Promise.resolve();
  113. },
  114. // 加购数量变更
  115. // 注:实际场景时应该调用接口
  116. changeQuantityService({ spuId, skuId, quantity }) {
  117. this.findGoods(spuId, skuId).currentGoods.quantity = quantity;
  118. return Promise.resolve();
  119. },
  120. // 删除加购商品
  121. // 注:实际场景时应该调用接口
  122. deleteGoodsService({ spuId, skuId }) {
  123. function deleteGoods(group) {
  124. for (const gindex in group) {
  125. const goods = group[gindex];
  126. if (goods.spuId === spuId && goods.skuId === skuId) {
  127. group.splice(gindex, 1);
  128. return gindex;
  129. }
  130. }
  131. return -1;
  132. }
  133. const { storeGoods, invalidGoodItems } = this.data.cartGroupData;
  134. for (const store of storeGoods) {
  135. for (const activity of store.promotionGoodsList) {
  136. if (deleteGoods(activity.goodsPromotionList) > -1) {
  137. return Promise.resolve();
  138. }
  139. }
  140. if (deleteGoods(store.shortageGoodsList) > -1) {
  141. return Promise.resolve();
  142. }
  143. }
  144. if (deleteGoods(invalidGoodItems) > -1) {
  145. return Promise.resolve();
  146. }
  147. return Promise.reject();
  148. },
  149. // 清空失效商品
  150. // 注:实际场景时应该调用接口
  151. clearInvalidGoodsService() {
  152. this.data.cartGroupData.invalidGoodItems = [];
  153. return Promise.resolve();
  154. },
  155. onGoodsSelect(e) {
  156. const {
  157. goods: { spuId, skuId },
  158. isSelected,
  159. } = e.detail;
  160. const { currentGoods } = this.findGoods(spuId, skuId);
  161. Toast({
  162. context: this,
  163. selector: '#t-toast',
  164. message: `${isSelected ? '选择' : '取消'}"${
  165. currentGoods.title.length > 5 ? `${currentGoods.title.slice(0, 5)}...` : currentGoods.title
  166. }"`,
  167. icon: '',
  168. });
  169. this.selectGoodsService({ spuId, skuId, isSelected }).then(() => this.refreshData());
  170. },
  171. onStoreSelect(e) {
  172. const {
  173. store: { storeId },
  174. isSelected,
  175. } = e.detail;
  176. this.selectStoreService({ storeId, isSelected }).then(() => this.refreshData());
  177. },
  178. onQuantityChange(e) {
  179. const {
  180. goods: { spuId, skuId },
  181. quantity,
  182. } = e.detail;
  183. const { currentGoods } = this.findGoods(spuId, skuId);
  184. const stockQuantity = currentGoods.stockQuantity > 0 ? currentGoods.stockQuantity : 0; // 避免后端返回的是-1
  185. // 加购数量超过库存数量
  186. if (quantity > stockQuantity) {
  187. // 加购数量等于库存数量的情况下继续加购
  188. if (currentGoods.quantity === stockQuantity && quantity - stockQuantity === 1) {
  189. Toast({
  190. context: this,
  191. selector: '#t-toast',
  192. message: '当前商品库存不足',
  193. });
  194. return;
  195. }
  196. Dialog.confirm({
  197. title: '商品库存不足',
  198. content: `当前商品库存不足,最大可购买数量为${stockQuantity}件`,
  199. confirmBtn: '修改为最大可购买数量',
  200. cancelBtn: '取消',
  201. })
  202. .then(() => {
  203. this.changeQuantityService({
  204. spuId,
  205. skuId,
  206. quantity: stockQuantity,
  207. }).then(() => this.refreshData());
  208. })
  209. .catch(() => {});
  210. return;
  211. }
  212. this.changeQuantityService({ spuId, skuId, quantity }).then(() => this.refreshData());
  213. },
  214. goCollect() {
  215. /** 活动肯定有一个活动ID,用来获取活动banner,活动商品列表等 */
  216. const promotionID = '123';
  217. wx.navigateTo({
  218. url: `/pages/promotion/promotion-detail/index?promotion_id=${promotionID}`,
  219. });
  220. },
  221. goGoodsDetail(e) {
  222. const { spuId, storeId } = e.detail.goods;
  223. wx.navigateTo({
  224. url: `/pages/goods/details/index?spuId=${spuId}&storeId=${storeId}`,
  225. });
  226. },
  227. clearInvalidGoods() {
  228. // 实际场景时应该调用接口清空失效商品
  229. this.clearInvalidGoodsService().then(() => this.refreshData());
  230. },
  231. onGoodsDelete(e) {
  232. const {
  233. goods: { spuId, skuId },
  234. } = e.detail;
  235. Dialog.confirm({
  236. content: '确认删除该商品吗?',
  237. confirmBtn: '确定',
  238. cancelBtn: '取消',
  239. }).then(() => {
  240. this.deleteGoodsService({ spuId, skuId }).then(() => {
  241. Toast({ context: this, selector: '#t-toast', message: '商品删除成功' });
  242. this.refreshData();
  243. });
  244. });
  245. },
  246. onSelectAll(event) {
  247. const { isAllSelected } = event?.detail ?? {};
  248. Toast({
  249. context: this,
  250. selector: '#t-toast',
  251. message: `${isAllSelected ? '取消' : '点击'}了全选按钮`,
  252. });
  253. // 调用接口改变全选
  254. },
  255. onToSettle() {
  256. const goodsRequestList = [];
  257. this.data.cartGroupData.storeGoods.forEach((store) => {
  258. store.promotionGoodsList.forEach((promotion) => {
  259. promotion.goodsPromotionList.forEach((m) => {
  260. if (m.isSelected == 1) {
  261. goodsRequestList.push(m);
  262. }
  263. });
  264. });
  265. });
  266. wx.setStorageSync('order.goodsRequestList', JSON.stringify(goodsRequestList));
  267. wx.navigateTo({ url: '/pages/order/order-confirm/index?type=cart' });
  268. },
  269. onGotoHome() {
  270. wx.switchTab({ url: '/pages/home/home' });
  271. },
  272. });