index.js 3.8 KB

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