与牧同行-小程序用户端
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.

205 lines
4.3 KiB

  1. import http from '../../../utils/api'
  2. const baseUrl = require('../../../utils/baseUrl')
  3. Page({
  4. data: {
  5. // 搜索文本
  6. searchText: '',
  7. baseUrl: baseUrl,
  8. // 当前选中的分类
  9. activeCategory: '',
  10. // 分类数据
  11. categories: [],
  12. // 经验分享列表
  13. experienceList: [],
  14. // 分页参数
  15. pageNum: 1,
  16. pageSize: 10,
  17. total: 0,
  18. // 加载状态
  19. loading: false,
  20. hasMore: true,
  21. // 搜索防抖定时器
  22. searchTimer: null
  23. },
  24. onLoad() {
  25. // 获取分类数据
  26. this.getCategories()
  27. // 获取经验分享列表
  28. this.getExperienceList(true)
  29. },
  30. // 获取经验分享列表
  31. getExperienceList(isRefresh = false) {
  32. if (this.data.loading) return
  33. const params = {
  34. pageNum: isRefresh ? 1 : this.data.pageNum,
  35. pageSize: this.data.pageSize
  36. }
  37. // 添加搜索关键词
  38. if (this.data.searchText) {
  39. params.searchKey = this.data.searchText.trim()
  40. }
  41. // 添加分类筛选
  42. if (this.data.activeCategory) {
  43. params.categoryName = this.data.activeCategory
  44. }
  45. this.setData({ loading: true })
  46. http.experience({
  47. data: params,
  48. success: res => {
  49. console.log('经验列表数据:', res)
  50. if (res.code === 200) {
  51. const newList = isRefresh ? res.rows : [...this.data.experienceList, ...res.rows]
  52. const total = res.total || 0
  53. const hasMore = newList.length < total
  54. this.setData({
  55. experienceList: newList,
  56. total: total,
  57. hasMore: hasMore,
  58. pageNum: isRefresh ? 2 : this.data.pageNum + 1,
  59. loading: false
  60. })
  61. } else {
  62. wx.showToast({
  63. title: res.msg || '加载失败',
  64. icon: 'none'
  65. })
  66. this.setData({ loading: false })
  67. }
  68. },
  69. fail: err => {
  70. console.error('请求失败:', err)
  71. wx.showToast({
  72. title: '网络错误,请重试',
  73. icon: 'none'
  74. })
  75. this.setData({ loading: false })
  76. }
  77. })
  78. },
  79. // 获取分类数据
  80. getCategories() {
  81. http.experiencezd({
  82. data: {
  83. categoryName: 'category_id'
  84. },
  85. success: res => {
  86. console.log('分类数据:', res)
  87. if (res.code === 200) {
  88. this.setData({
  89. categories: res.data || []
  90. })
  91. }
  92. },
  93. fail: err => {
  94. console.error('获取分类失败:', err)
  95. }
  96. })
  97. },
  98. // 处理搜索输入(带防抖)
  99. onSearchInput(e) {
  100. const searchText = e.detail.value
  101. this.setData({
  102. searchText: searchText
  103. })
  104. // 清除之前的定时器
  105. if (this.data.searchTimer) {
  106. clearTimeout(this.data.searchTimer)
  107. }
  108. // 设置新的定时器,500ms后执行搜索
  109. const timer = setTimeout(() => {
  110. this.handleSearch()
  111. }, 500)
  112. this.setData({
  113. searchTimer: timer
  114. })
  115. },
  116. // 搜索确认
  117. onSearchConfirm() {
  118. if (this.data.searchTimer) {
  119. clearTimeout(this.data.searchTimer)
  120. }
  121. this.handleSearch()
  122. },
  123. // 执行搜索
  124. handleSearch() {
  125. this.setData({
  126. pageNum: 1,
  127. hasMore: true
  128. })
  129. this.getExperienceList(true)
  130. },
  131. // 清空搜索
  132. clearSearch() {
  133. this.setData({
  134. searchText: '',
  135. pageNum: 1,
  136. hasMore: true
  137. })
  138. this.getExperienceList(true)
  139. },
  140. // 分类点击事件
  141. onCategoryTap(e) {
  142. const categoryId = e.currentTarget.dataset.id
  143. if (this.data.activeCategory === categoryId) {
  144. return
  145. }
  146. this.setData({
  147. activeCategory: categoryId,
  148. pageNum: 1,
  149. hasMore: true
  150. })
  151. this.getExperienceList(true)
  152. },
  153. // 滚动到底部加载更多
  154. onScrollToLower() {
  155. if (!this.data.loading && this.data.hasMore) {
  156. this.getExperienceList()
  157. }
  158. },
  159. // 点击加载更多
  160. loadMoreData() {
  161. if (!this.data.loading && this.data.hasMore) {
  162. this.getExperienceList()
  163. }
  164. },
  165. // 经验分享点击事件
  166. onExperienceTap(e) {
  167. const id = e.currentTarget.dataset.id
  168. wx.navigateTo({
  169. url: `/pagesB/pages/experienceDetails/experienceDetails?id=${id}`,
  170. })
  171. },
  172. onUnload() {
  173. // 清理定时器
  174. if (this.data.searchTimer) {
  175. clearTimeout(this.data.searchTimer)
  176. }
  177. }
  178. })