Files
mzltMpfqGS e4a087e67e init
2026-04-15 19:45:16 +08:00

190 lines
4.5 KiB
JavaScript
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

// pages/article-list/article-list.js
// 文章列表逻辑处理
const app = getApp()
const api = require('../../services/api')
Page({
data: {
categoryId: null,
tagId: null,
keyword: '',
articleList: [],
categoryList: [],
tagList: [],
page: 1,
pageSize: 10,
hasMore: true,
loading: false,
selectedCategory: null,
selectedTag: null,
sortBy: 'createTime',
sortOrder: 'desc'
},
onLoad(options) {
if (options.categoryId) {
this.setData({ categoryId: options.categoryId })
}
if (options.tagId) {
this.setData({ tagId: options.tagId })
}
if (options.keyword) {
this.setData({ keyword: options.keyword })
}
this.loadCategoryList()
this.loadArticleList()
},
onPullDownRefresh() {
this.setData({ page: 1, articleList: [], hasMore: true })
this.loadArticleList()
wx.stopPullDownRefresh()
},
onReachBottom() {
if (this.data.hasMore && !this.data.loading) {
this.setData({ page: this.data.page + 1 })
this.loadArticleList()
}
},
// 加载分类列表
loadCategoryList() {
api.getCategoryList().then(res => {
if (res.code === 200) {
this.setData({ categoryList: res.data || [] })
}
})
},
// 加载文章列表
loadArticleList() {
if (this.data.loading || !this.data.hasMore) return
this.setData({ loading: true })
const params = {
page: this.data.page,
pageSize: this.data.pageSize,
sortBy: this.data.sortBy,
sortOrder: this.data.sortOrder
}
if (this.data.categoryId) {
params.categoryId = this.data.categoryId
}
if (this.data.tagId) {
params.tagId = this.data.tagId
}
if (this.data.keyword) {
params.keyword = this.data.keyword
}
api.getArticleList(params).then(res => {
this.setData({ loading: false })
if (res.code === 200) {
const list = res.data.list || []
this.setData({
articleList: this.data.page === 1 ? list : this.data.articleList.concat(list),
hasMore: list.length >= this.data.pageSize
})
}
}).catch(() => {
this.setData({ loading: false })
})
},
// 跳转到文章详情
goToDetail(e) {
const articleId = e.currentTarget.dataset.id
wx.navigateTo({
url: `/pages/article-detail/article-detail?id=${articleId}`
})
},
// 选择分类
onCategoryChange(e) {
const index = e.detail.value
const category = this.data.categoryList[index]
this.setData({
selectedCategory: category,
categoryId: category ? category.id : null,
page: 1,
articleList: [],
hasMore: true
})
this.loadArticleList()
},
// 选择标签
onTagChange(e) {
const index = e.detail.value
const tag = this.data.tagList[index]
this.setData({
selectedTag: tag,
tagId: tag ? tag.id : null,
page: 1,
articleList: [],
hasMore: true
})
this.loadArticleList()
},
// 排序方式变更
onSortChange(e) {
const value = e.detail.value
const [sortBy, sortOrder] = value.split(':')
this.setData({
sortBy,
sortOrder,
page: 1,
articleList: [],
hasMore: true
})
this.loadArticleList()
},
// 收藏文章
onFavorite(e) {
if (!app.checkLogin()) return
const articleId = e.currentTarget.dataset.id
const articleList = this.data.articleList
const index = articleList.findIndex(item => item.id === articleId)
if (index === -1) return
const article = articleList[index]
const isFavorite = !article.isFavorite
api.updateFavorite({ articleId, isFavorite }).then(res => {
if (res.code === 200) {
article.isFavorite = isFavorite
article.favoriteCount = isFavorite ? article.favoriteCount + 1 : article.favoriteCount - 1
this.setData({ [`articleList[${index}]`]: article })
wx.showToast({
title: isFavorite ? '收藏成功' : '取消收藏',
icon: 'success'
})
}
})
},
// 分享文章
onShareAppMessage(e) {
const article = e.target.dataset.article
return {
title: article.title,
path: `/pages/article-detail/article-detail?id=${article.id}`,
imageUrl: article.coverImage
}
}
})
/**
* @联系作者:16768118056
* @描述:(此项目非免费分享)源码百分百可用,搞定毕设不发愁,支持远程调试安装、二次开发、定制、讲解、文档类。
* @访问:https://www.notmaker.com/detail/8326a897119d4542896c863a69d7bbe7/gtt20260415,查看完整运行演示。
*/