index.js 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. import { OrderStatus } from '../config';
  2. import { fetchOrders, fetchOrdersCount } from '../../../services/order/orderList';
  3. import { cosThumb } from '../../../utils/util';
  4. Page({
  5. page: {
  6. size: 5,
  7. num: 1,
  8. },
  9. data: {
  10. tabs: [
  11. { key: -1, text: '全部' },
  12. { key: OrderStatus.PENDING_PAYMENT, text: '待付款', info: '' },
  13. { key: OrderStatus.PENDING_DELIVERY, text: '待发货', info: '' },
  14. { key: OrderStatus.PENDING_RECEIPT, text: '待收货', info: '' },
  15. { key: OrderStatus.COMPLETE, text: '已完成', info: '' },
  16. ],
  17. curTab: -1,
  18. orderList: [],
  19. listLoading: 0,
  20. pullDownRefreshing: false,
  21. emptyImg: 'https://tdesign.gtimg.com/miniprogram/template/retail/order/empty-order-list.png',
  22. backRefresh: false,
  23. status: -1,
  24. },
  25. onLoad(query) {
  26. let status = parseInt(query.status);
  27. status = this.data.tabs.map((t) => t.key).includes(status) ? status : -1;
  28. this.init(status);
  29. this.pullDownRefresh = this.selectComponent('#wr-pull-down-refresh');
  30. },
  31. onShow() {
  32. if (!this.data.backRefresh) return;
  33. this.onRefresh();
  34. this.setData({ backRefresh: false });
  35. },
  36. onReachBottom() {
  37. if (this.data.listLoading === 0) {
  38. this.getOrderList(this.data.curTab);
  39. }
  40. },
  41. onPageScroll(e) {
  42. this.pullDownRefresh && this.pullDownRefresh.onPageScroll(e);
  43. },
  44. onPullDownRefresh_(e) {
  45. const { callback } = e.detail;
  46. this.setData({ pullDownRefreshing: true });
  47. this.refreshList(this.data.curTab)
  48. .then(() => {
  49. this.setData({ pullDownRefreshing: false });
  50. callback && callback();
  51. })
  52. .catch((err) => {
  53. this.setData({ pullDownRefreshing: false });
  54. Promise.reject(err);
  55. });
  56. },
  57. init(status) {
  58. status = status !== undefined ? status : this.data.curTab;
  59. this.setData({
  60. status,
  61. });
  62. this.refreshList(status);
  63. },
  64. getOrderList(statusCode = -1, reset = false) {
  65. const params = {
  66. parameter: {
  67. pageSize: this.page.size,
  68. pageNum: this.page.num,
  69. },
  70. };
  71. if (statusCode !== -1) params.parameter.orderStatus = statusCode;
  72. this.setData({ listLoading: 1 });
  73. return fetchOrders(params)
  74. .then((res) => {
  75. this.page.num++;
  76. let orderList = [];
  77. if (res && res.data && res.data.orders) {
  78. orderList = (res.data.orders || []).map((order) => {
  79. return {
  80. id: order.orderId,
  81. orderNo: order.orderNo,
  82. parentOrderNo: order.parentOrderNo,
  83. storeId: order.storeId,
  84. storeName: order.storeName,
  85. status: order.orderStatus,
  86. statusDesc: order.orderStatusName,
  87. amount: order.paymentAmount,
  88. totalAmount: order.totalAmount,
  89. logisticsNo: order.logisticsVO.logisticsNo,
  90. createTime: order.createTime,
  91. goodsList: (order.orderItemVOs || []).map((goods) => ({
  92. id: goods.id,
  93. thumb: cosThumb(goods.goodsPictureUrl, 70),
  94. title: goods.goodsName,
  95. skuId: goods.skuId,
  96. spuId: goods.spuId,
  97. specs: (goods.specifications || []).map((spec) => spec.specValue),
  98. price: goods.tagPrice ? goods.tagPrice : goods.actualPrice,
  99. num: goods.buyQuantity,
  100. titlePrefixTags: goods.tagText ? [{ text: goods.tagText }] : [],
  101. })),
  102. buttons: order.buttonVOs || [],
  103. groupInfoVo: order.groupInfoVo,
  104. freightFee: order.freightFee,
  105. };
  106. });
  107. }
  108. return new Promise((resolve) => {
  109. if (reset) {
  110. this.setData({ orderList: [] }, () => resolve());
  111. } else resolve();
  112. }).then(() => {
  113. this.setData({
  114. orderList: this.data.orderList.concat(orderList),
  115. listLoading: orderList.length > 0 ? 0 : 2,
  116. });
  117. });
  118. })
  119. .catch((err) => {
  120. this.setData({ listLoading: 3 });
  121. return Promise.reject(err);
  122. });
  123. },
  124. onReTryLoad() {
  125. this.getOrderList(this.data.curTab);
  126. },
  127. onTabChange(e) {
  128. const { value } = e.detail;
  129. this.setData({
  130. status: value,
  131. });
  132. this.refreshList(value);
  133. },
  134. getOrdersCount() {
  135. return fetchOrdersCount().then((res) => {
  136. const tabsCount = res.data || [];
  137. const { tabs } = this.data;
  138. tabs.forEach((tab) => {
  139. const tabCount = tabsCount.find((c) => c.tabType === tab.key);
  140. if (tabCount) {
  141. tab.info = tabCount.orderNum;
  142. }
  143. });
  144. this.setData({ tabs });
  145. });
  146. },
  147. refreshList(status = -1) {
  148. this.page = {
  149. size: this.page.size,
  150. num: 1,
  151. };
  152. this.setData({ curTab: status, orderList: [] });
  153. return Promise.all([this.getOrderList(status, true), this.getOrdersCount()]);
  154. },
  155. onRefresh() {
  156. this.refreshList(this.data.curTab);
  157. },
  158. onOrderCardTap(e) {
  159. const { order } = e.currentTarget.dataset;
  160. wx.navigateTo({
  161. url: `/pages/order/order-detail/index?orderNo=${order.orderNo}`,
  162. });
  163. },
  164. });