import { trackClick, trackPageView } from '@/utils/tracking' const app = getApp() Page({ data: { // 文章ID articleId: 0, // 文章详情 articleDetail: {} as any, // 是否已点赞 isLiked: false, }, onLoad(option: { id?: string }) { // 药店端文章详情页面,仅允许药店人员访问 app.waitLogin({ types: [4] }).then(() => { trackPageView('doctor_article_detail_view') 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() { trackClick('click_doctor_article_detail_like') const { articleId, articleDetail } = this.data if (!articleId) return wx.ajax({ method: 'POST', url: '/app/pharmacist/pharmacist/edu-article-like', data: { articleId, }, }) .then(() => { this.setData({ articleDetail: { ...articleDetail, likeCount: (articleDetail.likeCount || 0) + 1, }, isLiked: true, }) wx.showToast({ title: '点赞成功', 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 {}