home.js 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. import { fetchHome } from '../../services/home/home';
  2. import { fetchGoodsList } from '../../services/good/fetchGoods';
  3. import Toast from 'tdesign-miniprogram/toast/index';
  4. Page({
  5. data: {
  6. imgSrcs: [],
  7. tabList: [],
  8. goodsList: [],
  9. goodsListLoadStatus: 0,
  10. pageLoading: false,
  11. current: 1,
  12. autoplay: true,
  13. duration: '500',
  14. interval: 5000,
  15. navigation: { type: 'dots' },
  16. swiperImageProps: { mode: 'scaleToFill' },
  17. },
  18. goodListPagination: {
  19. index: 0,
  20. num: 20,
  21. },
  22. privateData: {
  23. tabIndex: 0,
  24. },
  25. onShow() {
  26. this.getTabBar().init();
  27. },
  28. onLoad() {
  29. this.init();
  30. },
  31. onReachBottom() {
  32. if (this.data.goodsListLoadStatus === 0) {
  33. this.loadGoodsList();
  34. }
  35. },
  36. onPullDownRefresh() {
  37. this.init();
  38. },
  39. init() {
  40. this.loadHomePage();
  41. },
  42. loadHomePage() {
  43. wx.stopPullDownRefresh();
  44. this.setData({
  45. pageLoading: true,
  46. });
  47. fetchHome().then(({ swiper, tabList }) => {
  48. this.setData({
  49. tabList,
  50. imgSrcs: swiper,
  51. pageLoading: false,
  52. });
  53. this.loadGoodsList(true);
  54. });
  55. },
  56. tabChangeHandle(e) {
  57. this.privateData.tabIndex = e.detail;
  58. this.loadGoodsList(true);
  59. },
  60. onReTry() {
  61. this.loadGoodsList();
  62. },
  63. async loadGoodsList(fresh = false) {
  64. if (fresh) {
  65. wx.pageScrollTo({
  66. scrollTop: 0,
  67. });
  68. }
  69. this.setData({ goodsListLoadStatus: 1 });
  70. const pageSize = this.goodListPagination.num;
  71. let pageIndex = this.privateData.tabIndex * pageSize + this.goodListPagination.index + 1;
  72. if (fresh) {
  73. pageIndex = 0;
  74. }
  75. try {
  76. const nextList = await fetchGoodsList(pageIndex, pageSize);
  77. this.setData({
  78. goodsList: fresh ? nextList : this.data.goodsList.concat(nextList),
  79. goodsListLoadStatus: 0,
  80. });
  81. this.goodListPagination.index = pageIndex;
  82. this.goodListPagination.num = pageSize;
  83. } catch (err) {
  84. this.setData({ goodsListLoadStatus: 3 });
  85. }
  86. },
  87. goodListClickHandle(e) {
  88. const { index } = e.detail;
  89. const { spuId } = this.data.goodsList[index];
  90. wx.navigateTo({
  91. url: `/pages/goods/details/index?spuId=${spuId}`,
  92. });
  93. },
  94. goodListAddCartHandle() {
  95. Toast({
  96. context: this,
  97. selector: '#t-toast',
  98. message: '点击加入购物车',
  99. });
  100. },
  101. navToSearchPage() {
  102. wx.navigateTo({ url: '/pages/goods/search/index' });
  103. },
  104. navToActivityDetail({ detail }) {
  105. const { index: promotionID = 0 } = detail || {};
  106. wx.navigateTo({
  107. url: `/pages/promotion/promotion-detail/index?promotion_id=${promotionID}`,
  108. });
  109. },
  110. });