index.js 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. // pages/collection/index.js
  2. const { museumApi } = require('../../utils/api.js');
  3. const { navigateToWebview } = require('../../utils/util.js');
  4. Page({
  5. /**
  6. * 页面的初始数据
  7. */
  8. data: {
  9. artifactList: [], // 收藏列表
  10. loading: false, // 加载状态
  11. hasMore: true, // 是否还有更多数据
  12. searchText: '', // 搜索文本
  13. activeCategory: 'all', // 当前激活的分类
  14. selectedType: 0, // 当前选中的类型:0-全部,1-平面纸质类,2-棉麻丝绸类,3-专业器物类
  15. pageNum: 1, // 当前页码
  16. pageSize: 10, // 每页数量
  17. categories: [
  18. { id: '1', name: '平面纸质类' },
  19. { id: '2', name: '棉麻丝绸类' },
  20. { id: '3', name: '专业器物类' }
  21. ]
  22. },
  23. /**
  24. * 生命周期函数--监听页面加载
  25. */
  26. onLoad(options) {
  27. this.initData();
  28. },
  29. /**
  30. * 初始化数据
  31. */
  32. async initData() {
  33. await this.getCollectionList(true);
  34. },
  35. /**
  36. * 获取收藏列表
  37. */
  38. async getCollectionList(reset = false) {
  39. if (this.data.loading) return;
  40. this.setData({ loading: true });
  41. try {
  42. const params = {
  43. pageNum: reset ? 1 : this.data.pageNum,
  44. pageSize: this.data.pageSize,
  45. type: this.data.selectedType || undefined,
  46. title: this.data.searchText || undefined
  47. };
  48. const res = await museumApi.getArtifactList(params);
  49. console.log('获取收藏列表参数:', res);
  50. if (res.records) {
  51. const newList = res.records || [];
  52. const artifactList = reset ? newList : [...this.data.artifactList, ...newList];
  53. const hasMore = newList.length === this.data.pageSize;
  54. console.log('获取收藏列表成功:', artifactList);
  55. this.setData({
  56. artifactList,
  57. hasMore,
  58. pageNum: reset ? 2 : this.data.pageNum + 1
  59. });
  60. }
  61. } catch (error) {
  62. console.error('获取收藏列表失败:', error);
  63. wx.showToast({
  64. title: '获取数据失败',
  65. icon: 'none'
  66. });
  67. } finally {
  68. this.setData({ loading: false });
  69. wx.stopPullDownRefresh();
  70. }
  71. },
  72. /**
  73. * 处理分类选择
  74. */
  75. async selectCategory(e) {
  76. const { type } = e.currentTarget.dataset;
  77. const typeId = type === 'all' ? 0 : parseInt(type);
  78. this.setData({
  79. activeCategory: type,
  80. selectedType: typeId,
  81. pageNum: 1
  82. });
  83. await this.getCollectionList(true);
  84. },
  85. /**
  86. * 处理搜索输入
  87. */
  88. onSearchInput(e) {
  89. this.setData({ searchText: e.detail.value });
  90. },
  91. /**
  92. * 处理搜索
  93. */
  94. async onSearch() {
  95. this.setData({ pageNum: 1 });
  96. await this.getCollectionList(true);
  97. },
  98. /**
  99. * 清空搜索
  100. */
  101. async onSearchClear() {
  102. this.setData({
  103. searchText: '',
  104. pageNum: 1
  105. });
  106. await this.getCollectionList(true);
  107. },
  108. /**
  109. * 跳转到收藏详情页
  110. */
  111. goCollectDetail(e) {
  112. const { item } = e.currentTarget.dataset;
  113. const url = `/collectDetail?id=${item.artifactId}&isFrom=weixin`;
  114. navigateToWebview(url);
  115. },
  116. /**
  117. * 页面相关事件处理函数--监听用户下拉动作
  118. */
  119. onPullDownRefresh() {
  120. this.getCollectionList(true);
  121. },
  122. /**
  123. * 页面上拉触底事件的处理函数
  124. */
  125. onReachBottom() {
  126. if (this.data.hasMore && !this.data.loading) {
  127. this.getCollectionList();
  128. }
  129. },
  130. /**
  131. * 用户点击右上角分享
  132. */
  133. onShareAppMessage() {
  134. return {
  135. title: '克拉玛依博物馆 - 典藏',
  136. path: '/pages/collection/index'
  137. };
  138. },
  139. /**
  140. * 用户点击右上角分享到朋友圈
  141. */
  142. onShareTimeline() {
  143. return {
  144. title: '克拉玛依博物馆 - 珍贵典藏文物展示',
  145. query: '',
  146. imageUrl: '' // 可以设置自定义分享图片
  147. };
  148. }
  149. })