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

212 lines
4.4 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. bindPublish(){
  100. wx.navigateTo({
  101. url: '/pagesA/pages/releaseSuffer/releaseSuffer',
  102. })
  103. },
  104. // 处理搜索输入(带防抖)
  105. onSearchInput(e) {
  106. const searchText = e.detail.value
  107. this.setData({
  108. searchText: searchText
  109. })
  110. // 清除之前的定时器
  111. if (this.data.searchTimer) {
  112. clearTimeout(this.data.searchTimer)
  113. }
  114. // 设置新的定时器,500ms后执行搜索
  115. const timer = setTimeout(() => {
  116. this.handleSearch()
  117. }, 500)
  118. this.setData({
  119. searchTimer: timer
  120. })
  121. },
  122. // 搜索确认
  123. onSearchConfirm() {
  124. if (this.data.searchTimer) {
  125. clearTimeout(this.data.searchTimer)
  126. }
  127. this.handleSearch()
  128. },
  129. // 执行搜索
  130. handleSearch() {
  131. this.setData({
  132. pageNum: 1,
  133. hasMore: true
  134. })
  135. this.getExperienceList(true)
  136. },
  137. // 清空搜索
  138. clearSearch() {
  139. this.setData({
  140. searchText: '',
  141. pageNum: 1,
  142. hasMore: true
  143. })
  144. this.getExperienceList(true)
  145. },
  146. // 分类点击事件
  147. onCategoryTap(e) {
  148. const categoryId = e.currentTarget.dataset.id
  149. if (this.data.activeCategory === categoryId) {
  150. return
  151. }
  152. this.setData({
  153. activeCategory: categoryId,
  154. pageNum: 1,
  155. hasMore: true
  156. })
  157. this.getExperienceList(true)
  158. },
  159. // 滚动到底部加载更多
  160. onScrollToLower() {
  161. if (!this.data.loading && this.data.hasMore) {
  162. this.getExperienceList()
  163. }
  164. },
  165. // 点击加载更多
  166. loadMoreData() {
  167. if (!this.data.loading && this.data.hasMore) {
  168. this.getExperienceList()
  169. }
  170. },
  171. // 经验分享点击事件
  172. onExperienceTap(e) {
  173. const id = e.currentTarget.dataset.id
  174. wx.navigateTo({
  175. url: `/pagesB/pages/experienceDetails/experienceDetails?id=${id}`,
  176. })
  177. },
  178. onUnload() {
  179. // 清理定时器
  180. if (this.data.searchTimer) {
  181. clearTimeout(this.data.searchTimer)
  182. }
  183. }
  184. })