activity.js 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. // pages/index/activity/activity.js
  2. const { museumApi } = require('../../../utils/api.js');
  3. const { navigateToWebview, urlImg } = require('../../../utils/util.js');
  4. Page({
  5. data: {
  6. activeList: [],
  7. loading: false,
  8. urlImg: urlImg,
  9. currentPage: 1,
  10. pageSize: 10,
  11. hasMore: true,
  12. loadingMore: false
  13. },
  14. onLoad() {
  15. this.getActivityList(true);
  16. },
  17. onPullDownRefresh() {
  18. this.getActivityList(true);
  19. },
  20. // 页面滚动到底部时触发
  21. onReachBottom() {
  22. if (this.data.hasMore && !this.data.loadingMore) {
  23. this.loadMore();
  24. }
  25. },
  26. // 获取活动列表
  27. async getActivityList(isRefresh = false) {
  28. const page = isRefresh ? 1 : this.data.currentPage;
  29. if (isRefresh) {
  30. this.setData({ loading: true });
  31. } else {
  32. this.setData({ loadingMore: true });
  33. }
  34. try {
  35. const response = await museumApi.getSocialActivityList({
  36. pageNum: page,
  37. pageSize: this.data.pageSize,
  38. status: 1
  39. });
  40. console.log('活动数据:', response);
  41. // 获取活动数据并进行筛选
  42. let activeList = response.records || response.list || response.data || response || [];
  43. // 筛选有效的活动数据 - 确保有activityId和title
  44. activeList = activeList.filter(item => {
  45. return item && item.activityId && item.title;
  46. });
  47. if (isRefresh) {
  48. // 刷新时替换数据
  49. this.setData({
  50. activeList,
  51. currentPage: 1,
  52. hasMore: activeList.length >= this.data.pageSize
  53. });
  54. } else {
  55. // 加载更多时追加数据
  56. this.setData({
  57. activeList: [...this.data.activeList, ...activeList],
  58. hasMore: activeList.length >= this.data.pageSize
  59. });
  60. }
  61. } catch (error) {
  62. console.error('获取活动数据失败:', error);
  63. wx.showToast({
  64. title: '加载失败',
  65. icon: 'none'
  66. });
  67. if (isRefresh) {
  68. this.setData({ activeList: [] });
  69. }
  70. } finally {
  71. this.setData({
  72. loading: false,
  73. loadingMore: false
  74. });
  75. if (isRefresh) {
  76. wx.stopPullDownRefresh();
  77. }
  78. }
  79. },
  80. // 加载更多数据
  81. loadMore() {
  82. const nextPage = this.data.currentPage + 1;
  83. this.setData({ currentPage: nextPage });
  84. this.getActivityList(false);
  85. },
  86. // 返回首页
  87. goBack() {
  88. wx.navigateBack();
  89. },
  90. // 查看活动详情
  91. viewActivity(e) {
  92. const item = e.currentTarget.dataset.item;
  93. console.log('查看活动详情:', item);
  94. // navigateToWebview(`/allDetailsShow?isFrom=weixin&id=${item.activityId}&type=activity`);
  95. wx.navigateTo({
  96. url: `/pages/exhibition/activeDetails/index?isFrom=weixin&id=${item.activityId}&type=activity`
  97. });
  98. }
  99. });