与牧同行-小程序用户端
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

232 lines
5.3 KiB

1 month ago
1 month ago
1 month ago
1 month ago
1 month ago
1 month ago
1 month ago
1 month ago
1 month ago
1 month ago
1 month ago
1 month ago
1 month ago
1 month ago
1 month ago
1 month ago
1 month ago
1 month ago
1 month ago
1 month ago
1 month ago
1 month ago
1 month ago
  1. import http from '../../../utils/api'
  2. const baseUr = require('../../../utils/baseUrl')
  3. Page({
  4. data: {
  5. // 药品列表数据
  6. filteredList: [],
  7. // 搜索相关
  8. searchKeyword: '',
  9. // 筛选相关
  10. currentCategory: '全部',
  11. baseUr: baseUr,
  12. // 分页相关
  13. pageNum: 1,
  14. pageSize: 10,
  15. hasMore: true,
  16. loading: false,
  17. isRefreshing: false,
  18. // 滚动相关
  19. showBackToTop: false,
  20. scrollTop: 0
  21. },
  22. onLoad: function() {
  23. // 初始化加载数据
  24. this.resetListAndLoad();
  25. },
  26. onShow: function() {
  27. // 页面显示时刷新数据
  28. this.refreshList();
  29. },
  30. onPullDownRefresh: function() {
  31. // 下拉刷新
  32. this.refreshList();
  33. },
  34. onReachBottom: function() {
  35. // 上拉加载更多
  36. if (this.data.hasMore && !this.data.loading) {
  37. this.loadMore();
  38. }
  39. },
  40. onPageScroll: function(e) {
  41. // 显示/隐藏返回顶部按钮
  42. if (e.scrollTop > 300 && !this.data.showBackToTop) {
  43. this.setData({ showBackToTop: true });
  44. } else if (e.scrollTop <= 300 && this.data.showBackToTop) {
  45. this.setData({ showBackToTop: false });
  46. }
  47. this.setData({ scrollTop: e.scrollTop });
  48. },
  49. /**
  50. * 获取药品列表数据
  51. * @param {number} pageNum - 页码
  52. * @param {boolean} isLoadMore - 是否为加载更多
  53. * @param {string} searchKeywords - 搜索关键词
  54. * @param {string} medicineType - 药品类型筛选
  55. */
  56. getRecommendationList: function(pageNum = 1, isLoadMore = false, searchKeywords = '', medicineType = '') {
  57. // 如果正在加载,防止重复请求
  58. if (this.data.loading) return;
  59. this.setData({
  60. loading: true,
  61. pageNum: pageNum
  62. });
  63. // 构建请求参数
  64. const params = {
  65. pageNum: pageNum,
  66. pageSize: this.data.pageSize,
  67. searchKeywords: searchKeywords || this.data.searchKeyword
  68. };
  69. // 添加药品类型筛选(如果不是"全部")
  70. if (medicineType && medicineType !== '全部') {
  71. params.medicineType = medicineType;
  72. } else if (this.data.currentCategory && this.data.currentCategory !== '全部') {
  73. params.medicineType = this.data.currentCategory;
  74. }
  75. http.recommendationList({
  76. data: params,
  77. success: res => {
  78. const newData = res.rows || [];
  79. const total = res.total || 0;
  80. // 计算是否还有更多数据
  81. const hasMore = newData.length >= this.data.pageSize &&
  82. (this.data.filteredList.length + newData.length) < total;
  83. if (isLoadMore) {
  84. // 加载更多:追加数据
  85. this.setData({
  86. filteredList: [...this.data.filteredList, ...newData],
  87. hasMore: hasMore,
  88. loading: false
  89. });
  90. } else {
  91. // 刷新或首次加载:替换数据
  92. this.setData({
  93. filteredList: newData,
  94. hasMore: hasMore,
  95. loading: false
  96. });
  97. }
  98. // 停止下拉刷新
  99. if (this.data.isRefreshing) {
  100. wx.stopPullDownRefresh();
  101. this.setData({ isRefreshing: false });
  102. }
  103. console.log('加载成功,当前数据量:', this.data.filteredList.length, '是否有更多:', hasMore);
  104. },
  105. fail: err => {
  106. console.error('加载数据失败:', err);
  107. wx.showToast({
  108. title: '加载失败',
  109. icon: 'none'
  110. });
  111. this.setData({
  112. loading: false,
  113. isRefreshing: false
  114. });
  115. // 停止下拉刷新
  116. wx.stopPullDownRefresh();
  117. }
  118. });
  119. },
  120. /**
  121. * 重置列表并加载第一页
  122. */
  123. resetListAndLoad: function() {
  124. this.setData({
  125. pageNum: 1,
  126. hasMore: true,
  127. filteredList: []
  128. }, () => {
  129. this.getRecommendationList(1, false);
  130. });
  131. },
  132. /**
  133. * 刷新列表第一页
  134. */
  135. refreshList: function() {
  136. this.setData({
  137. pageNum: 1,
  138. hasMore: true,
  139. isRefreshing: true
  140. });
  141. this.getRecommendationList(1, false);
  142. },
  143. /**
  144. * 加载更多数据
  145. */
  146. loadMore: function() {
  147. if (!this.data.hasMore || this.data.loading) return;
  148. const nextPage = this.data.pageNum + 1;
  149. this.getRecommendationList(nextPage, true);
  150. },
  151. /**
  152. * 搜索输入处理
  153. */
  154. onSearchInput: function(e) {
  155. const keyword = e.detail.value.trim();
  156. this.setData({
  157. searchKeyword: keyword
  158. }, () => {
  159. // 防抖处理,避免频繁请求
  160. clearTimeout(this.searchTimer);
  161. this.searchTimer = setTimeout(() => {
  162. this.resetListAndLoad();
  163. }, 500);
  164. });
  165. },
  166. /**
  167. * 清除搜索
  168. */
  169. clearSearch: function() {
  170. this.setData({
  171. searchKeyword: ''
  172. }, () => {
  173. this.resetListAndLoad();
  174. });
  175. },
  176. /**
  177. * 分类筛选切换
  178. */
  179. onCategoryChange: function(e) {
  180. const category = e.currentTarget.dataset.category;
  181. this.setData({
  182. currentCategory: category
  183. }, () => {
  184. this.resetListAndLoad();
  185. });
  186. },
  187. /**
  188. * 显示药品详情
  189. */
  190. showMedicineDetail: function(e) {
  191. const medId = e.currentTarget.dataset.id;
  192. wx.navigateTo({
  193. url: `/pagesA/pages/medicineDetails/medicineDetails?id=${medId}`,
  194. });
  195. },
  196. /**
  197. * 返回顶部
  198. */
  199. scrollToTop: function() {
  200. wx.pageScrollTo({
  201. scrollTop: 0,
  202. duration: 300
  203. });
  204. }
  205. });