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

392 lines
8.9 KiB

  1. import http from '../../../utils/api'
  2. const baseUrl = require('../../../utils/baseUrl')
  3. Page({
  4. data: {
  5. currentTab: 0,
  6. searchKeyword: '',
  7. articleActiveCategory: 0,
  8. videoActiveCategory: '全部',
  9. baseUrl: baseUrl,
  10. // 文章相关
  11. allArticles: [],
  12. filteredArticles: [],
  13. articlePageNo: 1,
  14. articlePageSize: 10,
  15. articleTotal: 0,
  16. articleHasMore: true,
  17. articleLoading: false,
  18. articleRequested: false, // 标记文章数据是否已请求
  19. // 视频相关
  20. allVideos: [],
  21. filteredVideos: [],
  22. videoPageNo: 1,
  23. videoPageSize: 10,
  24. videoTotal: 0,
  25. videoHasMore: true,
  26. videoLoading: false,
  27. videoRequested: false, // 标记视频数据是否已请求
  28. // 字典数据
  29. wzzd: [],
  30. videoType: []
  31. },
  32. onLoad() {
  33. this.getarticleZd()
  34. this.getvideoZd()
  35. this.loadInitialData()
  36. },
  37. // 加载初始数据
  38. loadInitialData() {
  39. if (this.data.currentTab === 0) {
  40. this.resetArticleParams()
  41. this.getArticleList(true)
  42. } else {
  43. this.resetVideoParams()
  44. this.getVideoList(true)
  45. }
  46. },
  47. // 文章字典
  48. getarticleZd() {
  49. http.articleZd({
  50. data: {
  51. dictType: 'article_category'
  52. },
  53. success: res => {
  54. this.setData({
  55. wzzd: res.rows
  56. })
  57. }
  58. })
  59. },
  60. // 视频字典
  61. getvideoZd() {
  62. http.videoZd({
  63. data: {
  64. dictType: 'video_category'
  65. },
  66. success: res => {
  67. this.setData({
  68. videoType: res.rows
  69. })
  70. }
  71. })
  72. },
  73. // 获取文章列表(支持搜索和分类)
  74. getArticleList(isRefresh = false) {
  75. if (this.data.articleLoading && !isRefresh) return
  76. this.setData({
  77. articleLoading: true
  78. })
  79. const params = {
  80. pageNo: this.data.articlePageNo,
  81. pageSize: this.data.articlePageSize
  82. }
  83. // 添加搜索关键词
  84. if (this.data.searchKeyword && this.data.searchKeyword.trim()) {
  85. params.searchKey = this.data.searchKeyword.trim()
  86. }
  87. // 添加分类筛选
  88. if (this.data.articleActiveCategory !== 0) {
  89. const categoryDict = this.data.wzzd.find(item => item.dictSort === this.data.articleActiveCategory)
  90. if (categoryDict) {
  91. params.category = categoryDict.dictLabel
  92. }
  93. }
  94. http.article({
  95. data: params,
  96. success: res => {
  97. const newArticles = res.rows || []
  98. const total = res.total || 0
  99. let allArticles, filteredArticles
  100. if (isRefresh) {
  101. allArticles = newArticles
  102. filteredArticles = newArticles
  103. } else {
  104. allArticles = [...this.data.allArticles, ...newArticles]
  105. filteredArticles = [...this.data.filteredArticles, ...newArticles]
  106. }
  107. const hasMore = this.data.articlePageNo * this.data.articlePageSize < total
  108. this.setData({
  109. allArticles,
  110. filteredArticles,
  111. articleTotal: total,
  112. articleHasMore: hasMore,
  113. articleLoading: false,
  114. articleRequested: true
  115. })
  116. // 如果是下拉刷新,停止刷新动画
  117. if (isRefresh) {
  118. wx.stopPullDownRefresh()
  119. }
  120. },
  121. fail: () => {
  122. this.setData({
  123. articleLoading: false
  124. })
  125. wx.showToast({
  126. title: '加载失败',
  127. icon: 'error',
  128. duration: 2000
  129. })
  130. }
  131. })
  132. },
  133. // 获取视频列表(支持搜索和分类)
  134. getVideoList(isRefresh = false) {
  135. if (this.data.videoLoading && !isRefresh) return
  136. this.setData({
  137. videoLoading: true
  138. })
  139. const params = {
  140. pageNo: this.data.videoPageNo,
  141. pageSize: this.data.videoPageSize
  142. }
  143. // 添加搜索关键词
  144. if (this.data.searchKeyword && this.data.searchKeyword.trim()) {
  145. params.searchKey = this.data.searchKeyword.trim()
  146. }
  147. // 添加分类筛选
  148. if (this.data.videoActiveCategory !== '全部') {
  149. params.category = this.data.videoActiveCategory
  150. }
  151. http.videoList({
  152. data: params,
  153. success: res => {
  154. const newVideos = res.rows || []
  155. const total = res.total || 0
  156. let allVideos, filteredVideos
  157. if (isRefresh) {
  158. allVideos = newVideos
  159. filteredVideos = newVideos
  160. } else {
  161. allVideos = [...this.data.allVideos, ...newVideos]
  162. filteredVideos = [...this.data.filteredVideos, ...newVideos]
  163. }
  164. const hasMore = this.data.videoPageNo * this.data.videoPageSize < total
  165. this.setData({
  166. allVideos,
  167. filteredVideos,
  168. videoTotal: total,
  169. videoHasMore: hasMore,
  170. videoLoading: false,
  171. videoRequested: true
  172. })
  173. // 如果是下拉刷新,停止刷新动画
  174. if (isRefresh) {
  175. wx.stopPullDownRefresh()
  176. }
  177. },
  178. fail: () => {
  179. this.setData({
  180. videoLoading: false
  181. })
  182. wx.showToast({
  183. title: '加载失败',
  184. icon: 'error',
  185. duration: 2000
  186. })
  187. }
  188. })
  189. },
  190. // 重置文章参数
  191. resetArticleParams() {
  192. this.setData({
  193. articlePageNo: 1,
  194. articleTotal: 0,
  195. articleHasMore: true,
  196. allArticles: [],
  197. filteredArticles: []
  198. })
  199. },
  200. // 重置视频参数
  201. resetVideoParams() {
  202. this.setData({
  203. videoPageNo: 1,
  204. videoTotal: 0,
  205. videoHasMore: true,
  206. allVideos: [],
  207. filteredVideos: []
  208. })
  209. },
  210. // 切换主选项卡
  211. switchTab(e) {
  212. const tab = parseInt(e.currentTarget.dataset.tab)
  213. if (tab === this.data.currentTab) return
  214. this.setData({
  215. currentTab: tab,
  216. searchKeyword: ''
  217. })
  218. // 延迟加载新tab的数据,确保切换动画完成
  219. setTimeout(() => {
  220. if (tab === 0) {
  221. if (!this.data.articleRequested) {
  222. this.resetArticleParams()
  223. this.getArticleList(true)
  224. }
  225. } else {
  226. if (!this.data.videoRequested) {
  227. this.resetVideoParams()
  228. this.getVideoList(true)
  229. }
  230. }
  231. }, 300)
  232. },
  233. // 搜索输入
  234. onSearchInput(e) {
  235. const keyword = e.detail.value
  236. this.setData({
  237. searchKeyword: keyword
  238. })
  239. // 防抖处理,500ms后执行搜索
  240. clearTimeout(this.searchTimer)
  241. this.searchTimer = setTimeout(() => {
  242. this.performSearch()
  243. }, 500)
  244. },
  245. // 执行搜索
  246. performSearch() {
  247. if (this.data.currentTab === 0) {
  248. this.resetArticleParams()
  249. this.getArticleList(true)
  250. } else {
  251. this.resetVideoParams()
  252. this.getVideoList(true)
  253. }
  254. },
  255. // 选择文章分类
  256. selectArticleCategory(e) {
  257. const category = Number(e.currentTarget.dataset.category)
  258. if (category === this.data.articleActiveCategory) return
  259. this.setData({
  260. articleActiveCategory: category
  261. })
  262. // 重置并重新加载数据
  263. this.resetArticleParams()
  264. this.getArticleList(true)
  265. },
  266. // 选择视频分类
  267. selectVideoCategory(e) {
  268. const category = e.currentTarget.dataset.category
  269. if (category === this.data.videoActiveCategory) return
  270. this.setData({
  271. videoActiveCategory: category
  272. })
  273. // 重置并重新加载数据
  274. this.resetVideoParams()
  275. this.getVideoList(true)
  276. },
  277. // 查看文章详情
  278. viewArticleDetail(e) {
  279. const id = e.currentTarget.dataset.id
  280. console.log('查看文章详情', id)
  281. wx.navigateTo({
  282. url: `/pagesB/pages/wzDetails/wzDetails?id=${id}`
  283. })
  284. },
  285. // 播放视频
  286. playVideo(e) {
  287. const id = e.currentTarget.dataset.id
  288. console.log('播放视频', id)
  289. wx.navigateTo({
  290. url: `/pagesB/pages/spDetails/spDetails?id=${id}`
  291. })
  292. },
  293. // 下拉刷新
  294. onPullDownRefresh() {
  295. wx.showNavigationBarLoading()
  296. if (this.data.currentTab === 0) {
  297. this.resetArticleParams()
  298. this.getArticleList(true)
  299. } else {
  300. this.resetVideoParams()
  301. this.getVideoList(true)
  302. }
  303. // 重置搜索关键词
  304. this.setData({
  305. searchKeyword: ''
  306. })
  307. },
  308. // 上拉加载更多
  309. onReachBottom() {
  310. if (this.data.currentTab === 0) {
  311. if (!this.data.articleHasMore || this.data.articleLoading) {
  312. wx.showToast({
  313. title: '已加载全部',
  314. icon: 'none',
  315. duration: 1000
  316. })
  317. return
  318. }
  319. this.setData({
  320. articlePageNo: this.data.articlePageNo + 1
  321. })
  322. this.getArticleList(false)
  323. } else {
  324. if (!this.data.videoHasMore || this.data.videoLoading) {
  325. wx.showToast({
  326. title: '已加载全部',
  327. icon: 'none',
  328. duration: 1000
  329. })
  330. return
  331. }
  332. this.setData({
  333. videoPageNo: this.data.videoPageNo + 1
  334. })
  335. this.getVideoList(false)
  336. }
  337. },
  338. // 页面显示时刷新数据
  339. onShow() {
  340. // 如果需要返回时刷新数据,可以在这里调用
  341. }
  342. })