index.js 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. // index.js
  2. const { newRequest, cosBaseUrl } = require('../../utils/newServices');
  3. Page({
  4. data: {
  5. // 轮播图配置
  6. indicatorDots: true,
  7. autoplay: true,
  8. interval: 5000,
  9. duration: 1000,
  10. circular: true,
  11. // 轮播图数据
  12. swiperList: [],
  13. // 最新上传数据
  14. latestUploads: [],
  15. // 分页数据
  16. currentPage: 1,
  17. pageSize: 9,
  18. hasMoreData: true,
  19. loading: false
  20. },
  21. onLoad: function(options) {
  22. // 加载轮播图数据
  23. this.loadSwiperData();
  24. // 初始化最新上传数据
  25. this.loadLatestUploads();
  26. },
  27. // 加载轮播图数据
  28. loadSwiperData: function() {
  29. const params = {
  30. sortBy: 'DESC',
  31. hot: 1,
  32. pageNo: 1,
  33. pageSize: 6
  34. };
  35. newRequest.getAntiqueList(
  36. params,
  37. 'GET',
  38. (res) => {
  39. console.log('轮播图数据加载成功:', res.data.data.pageData);
  40. if (res.data && res.data.data && res.data.data.pageData) {
  41. const swiperData = res.data.data.pageData.map(item => ({
  42. id: item.id,
  43. imageUrl: item.coverImgUrl.includes('http') ? item.coverImgUrl : cosBaseUrl + item.coverImgUrl,
  44. title: item.name,
  45. date: item.createTime ? item.createTime.split(' ')[0] : '',
  46. source: item.source || '归藏'
  47. }));
  48. this.setData({
  49. swiperList: swiperData
  50. });
  51. }
  52. },
  53. (err) => {
  54. console.error('轮播图数据加载失败:', err);
  55. // 加载失败时使用默认数据
  56. this.setData({
  57. swiperList: []
  58. });
  59. }
  60. );
  61. },
  62. // 加载最新上传数据
  63. loadLatestUploads: function() {
  64. if (!this.data.hasMoreData || this.data.loading) return;
  65. this.setData({ loading: true });
  66. const params = {
  67. pageNo: this.data.currentPage,
  68. pageSize: 20
  69. };
  70. newRequest.getAntiqueList(
  71. params,
  72. 'GET',
  73. (res) => {
  74. console.log('最新上传数据加载成功:', res);
  75. if (res.data && res.data.data && res.data.data.pageData) {
  76. const newItems = res.data.data.pageData.map(item => ({
  77. id: item.id,
  78. imageUrl: item.coverImgUrl.includes('http') ? item.coverImgUrl : cosBaseUrl + item.coverImgUrl,
  79. title: item.name,
  80. date: item.createTime ? item.createTime.split(' ')[0] : '',
  81. source: item.source || '归藏'
  82. }));
  83. const hasMore = res.data.data.pageData.length === 20; // 如果返回数据等于pageSize,说明可能还有更多数据
  84. this.setData({
  85. latestUploads: [...this.data.latestUploads, ...newItems],
  86. currentPage: this.data.currentPage + 1,
  87. hasMoreData: hasMore,
  88. loading: false
  89. });
  90. } else {
  91. this.setData({
  92. hasMoreData: false,
  93. loading: false
  94. });
  95. }
  96. },
  97. (err) => {
  98. console.error('最新上传数据加载失败:', err);
  99. this.setData({
  100. loading: false
  101. });
  102. }
  103. );
  104. },
  105. // 跳转到详情页
  106. goToDetail: function(e) {
  107. const id = e.currentTarget.dataset.id;
  108. wx.navigateTo({
  109. url: '../guicangDetails/index?id=' + id
  110. });
  111. },
  112. // 下拉刷新
  113. onPullDownRefresh: function() {
  114. this.setData({
  115. latestUploads: [],
  116. currentPage: 1,
  117. hasMoreData: true
  118. });
  119. this.loadLatestUploads();
  120. wx.stopPullDownRefresh();
  121. },
  122. // 上拉加载更多
  123. onReachBottom: function() {
  124. if (!this.data.loading && this.data.hasMoreData) {
  125. this.loadLatestUploads();
  126. }
  127. },
  128. onReady: function() {
  129. // 页面初次渲染完成时执行的函数
  130. },
  131. onShow: function() {
  132. // 页面显示时执行的函数
  133. },
  134. onHide: function() {
  135. // 页面隐藏时执行的函数
  136. },
  137. onUnload: function() {
  138. // 页面卸载时执行的函数
  139. }
  140. })