index.js 3.9 KB

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