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.

110 lines
3.0 KiB

const app = getApp<IAppOption>()
Page({
data: {
// 文章ID
articleId: 0,
// 文章详情(模拟数据,接口上线后删除)
articleDetail: {
id: 1,
title: '银屑病日常护理指南',
content:
'<p>银屑病是一种慢性、复发性、炎症性皮肤病,需要长期的管理和护理。</p><h3>一、日常护理要点</h3><p>1. 保持皮肤湿润,每天使用保湿霜</p><p>2. 避免过度清洁,使用温和的洗浴产品</p><p>3. 穿着宽松、透气的棉质衣物</p><h3>二、饮食建议</h3><p>1. 多吃新鲜蔬菜水果</p><p>2. 避免辛辣刺激食物</p><p>3. 适量补充优质蛋白</p><h3>三、心理调节</h3><p>保持良好的心态,积极配合治疗,定期复诊。</p>',
imageUrl: '',
viewCount: 100,
likeCount: 10,
isLiked: false,
updateTime: 1700000000,
},
// 是否已点赞
isLiked: false,
},
onLoad(option: { id?: string }) {
// 药店端文章详情页面,仅允许药店人员访问
app.waitLogin({ types: [4] }).then(() => {
const articleId = option.id ? Number.parseInt(option.id) : 0
this.setData({ articleId })
// TODO: 接口上线后取消注释
// 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 {}