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

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