index.js 3.8 KB

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