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.

173 lines
4.5 KiB

const app = getApp<IAppOption>()
Page({
data: {
// 分类列表(模拟数据,接口上线后删除)
categoryList: [
{ id: 1, name: '学习专栏', images: [] },
{ id: 2, name: '社区热帖', images: [] },
{ id: 3, name: '行业热点', images: [] },
{ id: 4, name: '中国药店', images: [] },
] as any[],
currentCategoryId: 1,
// Banner列表
bannerList: [] as any[],
// 文章列表(模拟数据,接口上线后删除)
articleList: [
{
id: 1,
title: '银屑病日常护理指南',
imageUrl: '',
viewCount: 100,
likeCount: 10,
updateTime: 1700000000,
updateTimeFormatted: '2023-11-14',
},
{
id: 2,
title: '从强降糖到防事件,那些糖尿病指南走过的路',
imageUrl: '',
viewCount: 256,
likeCount: 32,
updateTime: 1700086400,
updateTimeFormatted: '2023-11-15',
},
{
id: 3,
title: '特诺雅患者援助项目介绍',
imageUrl: '',
viewCount: 89,
likeCount: 15,
updateTime: 1700172800,
updateTimeFormatted: '2023-11-16',
},
] as any[],
// 分页
page: 1,
pageSize: 20,
loading: false,
hasMore: true,
pagination: {
count: 3,
page: 1,
pages: 1,
},
},
onLoad() {
// 药店端教育页面,仅允许药店人员访问
app.waitLogin({ types: [4] }).then(() => {
// TODO: 接口上线后取消注释
// this.getCategoryList()
})
},
// 获取教育分类列表
getCategoryList() {
wx.ajax({
method: 'GET',
url: '/app/pharmacist/pharmacist/edu-category-list',
}).then((res: any) => {
const list = res || []
this.setData({
categoryList: list,
currentCategoryId: list.length > 0 ? list[0].id : 0,
})
// 获取第一个分类的文章列表和Banner
if (list.length > 0) {
this.getBannerList(list[0].id)
this.getArticleList()
}
})
},
// 获取分类Banner列表
getBannerList(categoryId: number) {
const category = this.data.categoryList.find((item: any) => item.id === categoryId)
if (category && category.images && category.images.length > 0) {
this.setData({
bannerList: category.images.map((url: string, index: number) => ({
id: index,
imageUrl: url,
})),
})
} else {
this.setData({ bannerList: [] })
}
},
// 获取文章列表
getArticleList() {
if (this.data.loading || !this.data.hasMore) return
if (!this.data.currentCategoryId) return
this.setData({ loading: true })
wx.ajax({
method: 'GET',
url: '/app/pharmacist/pharmacist/edu-article-list',
data: {
categoryId: this.data.currentCategoryId,
page: this.data.page,
pageSize: this.data.pageSize,
},
}).then((res: any) => {
const list = (res.list || []).map((item: any) => ({
...item,
updateTimeFormatted: item.updateTime ? this.formatDate(item.updateTime) : '',
}))
const total = res.total || 0
const currentPage = this.data.page
this.setData({
articleList: [...this.data.articleList, ...list],
page: currentPage + 1,
hasMore: list.length >= this.data.pageSize,
loading: false,
pagination: {
count: total,
page: currentPage,
pages: Math.ceil(total / this.data.pageSize) || 1,
},
})
}).catch(() => {
this.setData({ loading: false })
})
},
// 格式化日期
formatDate(timestamp: number): string {
const date = new Date(timestamp * 1000)
const year = date.getFullYear()
const month = String(date.getMonth() + 1).padStart(2, '0')
const day = String(date.getDate()).padStart(2, '0')
return `${year}-${month}-${day}`
},
// 切换分类
handleTabChange(e: WechatMiniprogram.CustomEvent) {
const categoryId = e.currentTarget.dataset.id
this.setData({
currentCategoryId: categoryId,
articleList: [],
page: 1,
hasMore: true,
})
this.getBannerList(categoryId)
this.getArticleList()
},
// 页面上拉触底事件
onReachBottom() {
if (this.data.loading || !this.data.hasMore) {
return
}
this.getArticleList()
},
// 查看文章详情
handleDetail(e: WechatMiniprogram.CustomEvent) {
const { id } = e.currentTarget.dataset
wx.navigateTo({
url: `/doctor/pages/article/index?id=${id}`,
})
},
})
export {}