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.

132 lines
3.2 KiB

const app = getApp<IAppOption>()
Page({
data: {
// 分类列表
categoryList: [] as any[],
currentCategoryId: 0,
// Banner列表
bannerList: [] as any[],
// 文章列表
articleList: [] as any[],
// 分页
page: 1,
pageSize: 20,
loading: false,
hasMore: true,
pagination: {
count: 0,
page: 1,
pages: 1,
},
},
onLoad() {
// 药店端教育页面,仅允许药店人员访问
app.waitLogin({ types: [4] }).then(() => {
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,
}))
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 })
})
},
// 切换分类
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 {}