const app = getApp() Page({ data: { // 文章ID articleId: 0, // 文章详情 articleDetail: {} as any, // 是否已点赞 isLiked: false, }, onLoad(option: { id?: string }) { // 药店端文章详情页面,仅允许药店人员访问 app.waitLogin({ types: [4] }).then(() => { const articleId = option.id ? Number.parseInt(option.id) : 0 this.setData({ articleId }) if (articleId) { this.getArticleDetail(articleId) } }) }, // 获取文章详情 getArticleDetail(articleId: number) { wx.ajax({ method: 'GET', url: '/app/pharmacist/pharmacist/edu-article-detail', data: { articleId, }, }) .then((res: any) => { this.setData({ articleDetail: { ...res, updateTimeFormatted: res.updateTime ? this.formatDate(res.updateTime) : '', }, isLiked: res.isLiked || false, }) }) .catch(() => { wx.showToast({ title: '获取文章详情失败', icon: 'none', }) }) }, // 点赞/取消点赞 handleLike() { const { articleId, isLiked, articleDetail } = this.data if (!articleId) return wx.ajax({ method: 'POST', url: '/app/pharmacist/pharmacist/edu-article-like', data: { articleId, }, }) .then(() => { const newLikeCount = isLiked ? (articleDetail.likeCount || 0) - 1 : (articleDetail.likeCount || 0) + 1 this.setData({ isLiked: !isLiked, articleDetail: { ...articleDetail, likeCount: newLikeCount, }, }) wx.showToast({ title: isLiked ? '取消点赞' : '点赞成功', icon: 'none', }) }) .catch(() => { wx.showToast({ title: '操作失败', icon: 'none', }) }) }, // 格式化日期 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}` }, handleBack() { wx.navigateBack() }, }) export {}