|
|
import http from '../../../utils/api'const baseUrl = require('../../../utils/baseUrl')
Page({ data: { currentTab: 0, searchKeyword: '', articleActiveCategory: 0, videoActiveCategory: '全部', baseUrl: baseUrl, // 文章相关
allArticles: [], filteredArticles: [], articlePageNo: 1, articlePageSize: 10, articleTotal: 0, articleHasMore: true, articleLoading: false, articleRequested: false, // 标记文章数据是否已请求
// 视频相关
allVideos: [], filteredVideos: [], videoPageNo: 1, videoPageSize: 10, videoTotal: 0, videoHasMore: true, videoLoading: false, videoRequested: false, // 标记视频数据是否已请求
// 字典数据
wzzd: [], videoType: [] },
onLoad() { this.getarticleZd() this.getvideoZd() this.loadInitialData() },
// 加载初始数据
loadInitialData() { if (this.data.currentTab === 0) { this.resetArticleParams() this.getArticleList(true) } else { this.resetVideoParams() this.getVideoList(true) } },
// 文章字典
getarticleZd() { http.articleZd({ data: { dictType: 'article_category' }, success: res => { this.setData({ wzzd: res.rows }) } }) },
// 视频字典
getvideoZd() { http.videoZd({ data: { dictType: 'video_category' }, success: res => { this.setData({ videoType: res.rows }) } }) },
// 获取文章列表(支持搜索和分类)
getArticleList(isRefresh = false) { if (this.data.articleLoading && !isRefresh) return this.setData({ articleLoading: true }) const params = { pageNo: this.data.articlePageNo, pageSize: this.data.articlePageSize } // 添加搜索关键词
if (this.data.searchKeyword && this.data.searchKeyword.trim()) { params.searchKey = this.data.searchKeyword.trim() } // 添加分类筛选
if (this.data.articleActiveCategory !== 0) { const categoryDict = this.data.wzzd.find(item => item.dictSort === this.data.articleActiveCategory) if (categoryDict) { params.category = categoryDict.dictLabel } } http.article({ data: params, success: res => { const newArticles = res.rows || [] const total = res.total || 0 let allArticles, filteredArticles if (isRefresh) { allArticles = newArticles filteredArticles = newArticles } else { allArticles = [...this.data.allArticles, ...newArticles] filteredArticles = [...this.data.filteredArticles, ...newArticles] } const hasMore = this.data.articlePageNo * this.data.articlePageSize < total this.setData({ allArticles, filteredArticles, articleTotal: total, articleHasMore: hasMore, articleLoading: false, articleRequested: true }) // 如果是下拉刷新,停止刷新动画
if (isRefresh) { wx.stopPullDownRefresh() } }, fail: () => { this.setData({ articleLoading: false }) wx.showToast({ title: '加载失败', icon: 'error', duration: 2000 }) } }) },
// 获取视频列表(支持搜索和分类)
getVideoList(isRefresh = false) { if (this.data.videoLoading && !isRefresh) return this.setData({ videoLoading: true }) const params = { pageNo: this.data.videoPageNo, pageSize: this.data.videoPageSize } // 添加搜索关键词
if (this.data.searchKeyword && this.data.searchKeyword.trim()) { params.searchKey = this.data.searchKeyword.trim() } // 添加分类筛选
if (this.data.videoActiveCategory !== '全部') { params.category = this.data.videoActiveCategory } http.videoList({ data: params, success: res => { const newVideos = res.rows || [] const total = res.total || 0 let allVideos, filteredVideos if (isRefresh) { allVideos = newVideos filteredVideos = newVideos } else { allVideos = [...this.data.allVideos, ...newVideos] filteredVideos = [...this.data.filteredVideos, ...newVideos] } const hasMore = this.data.videoPageNo * this.data.videoPageSize < total this.setData({ allVideos, filteredVideos, videoTotal: total, videoHasMore: hasMore, videoLoading: false, videoRequested: true }) // 如果是下拉刷新,停止刷新动画
if (isRefresh) { wx.stopPullDownRefresh() } }, fail: () => { this.setData({ videoLoading: false }) wx.showToast({ title: '加载失败', icon: 'error', duration: 2000 }) } }) },
// 重置文章参数
resetArticleParams() { this.setData({ articlePageNo: 1, articleTotal: 0, articleHasMore: true, allArticles: [], filteredArticles: [] }) },
// 重置视频参数
resetVideoParams() { this.setData({ videoPageNo: 1, videoTotal: 0, videoHasMore: true, allVideos: [], filteredVideos: [] }) },
// 切换主选项卡
switchTab(e) { const tab = parseInt(e.currentTarget.dataset.tab) if (tab === this.data.currentTab) return this.setData({ currentTab: tab, searchKeyword: '' }) // 延迟加载新tab的数据,确保切换动画完成
setTimeout(() => { if (tab === 0) { if (!this.data.articleRequested) { this.resetArticleParams() this.getArticleList(true) } } else { if (!this.data.videoRequested) { this.resetVideoParams() this.getVideoList(true) } } }, 300) },
// 搜索输入
onSearchInput(e) { const keyword = e.detail.value this.setData({ searchKeyword: keyword }) // 防抖处理,500ms后执行搜索
clearTimeout(this.searchTimer) this.searchTimer = setTimeout(() => { this.performSearch() }, 500) },
// 执行搜索
performSearch() { if (this.data.currentTab === 0) { this.resetArticleParams() this.getArticleList(true) } else { this.resetVideoParams() this.getVideoList(true) } },
// 选择文章分类
selectArticleCategory(e) { const category = Number(e.currentTarget.dataset.category) if (category === this.data.articleActiveCategory) return this.setData({ articleActiveCategory: category }) // 重置并重新加载数据
this.resetArticleParams() this.getArticleList(true) },
// 选择视频分类
selectVideoCategory(e) { const category = e.currentTarget.dataset.category if (category === this.data.videoActiveCategory) return this.setData({ videoActiveCategory: category }) // 重置并重新加载数据
this.resetVideoParams() this.getVideoList(true) },
// 查看文章详情
viewArticleDetail(e) { const id = e.currentTarget.dataset.id console.log('查看文章详情', id) wx.navigateTo({ url: `/pagesB/pages/wzDetails/wzDetails?id=${id}` }) },
// 播放视频
playVideo(e) { const id = e.currentTarget.dataset.id console.log('播放视频', id) wx.navigateTo({ url: `/pagesB/pages/spDetails/spDetails?id=${id}` }) },
// 下拉刷新
onPullDownRefresh() { wx.showNavigationBarLoading() if (this.data.currentTab === 0) { this.resetArticleParams() this.getArticleList(true) } else { this.resetVideoParams() this.getVideoList(true) } // 重置搜索关键词
this.setData({ searchKeyword: '' }) },
// 上拉加载更多
onReachBottom() { if (this.data.currentTab === 0) { if (!this.data.articleHasMore || this.data.articleLoading) { wx.showToast({ title: '已加载全部', icon: 'none', duration: 1000 }) return } this.setData({ articlePageNo: this.data.articlePageNo + 1 }) this.getArticleList(false) } else { if (!this.data.videoHasMore || this.data.videoLoading) { wx.showToast({ title: '已加载全部', icon: 'none', duration: 1000 }) return } this.setData({ videoPageNo: this.data.videoPageNo + 1 }) this.getVideoList(false) } },
// 页面显示时刷新数据
onShow() { // 如果需要返回时刷新数据,可以在这里调用
}})
|