与牧同行-小程序用户端
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.
 

99 lines
2.0 KiB

import http from '../../../utils/api'
const baseUrl = require('../../../utils/baseUrl')
Page({
data: {
baseUrl: baseUrl,
article: {}, // 文章详情
loading: true, // 加载状态
},
onLoad(options) {
this.getArticleDetails(options.id)
},
// 获取文章详情
getArticleDetails(id) {
this.setData({
loading: true
})
http.articleDetails({
data: { id },
success: res => {
console.log('文章详情:', res)
if (res.code === 200 && res.data) {
// 解析富文本内容
const article = res.data
// 这里可以处理富文本内容的样式
article.content = this.formatRichContent(article.content)
this.setData({
article: article,
loading: false
})
// 设置页面标题
wx.setNavigationBarTitle({
title: article.title.substring(0, 10) + (article.title.length > 10 ? '...' : '')
})
} else {
wx.showToast({
title: '文章不存在',
icon: 'error'
})
setTimeout(() => {
wx.navigateBack()
}, 2000)
}
},
fail: err => {
console.error('获取文章详情失败:', err)
this.setData({
loading: false
})
wx.showToast({
title: '加载失败',
icon: 'error'
})
}
})
},
// 格式化富文本内容
formatRichContent(content) {
// 这里可以添加自定义样式,比如给图片添加样式
return content.replace(/<img/g, '<img style="max-width:100%;height:auto;border-radius:15rpx;margin:20rpx 0;"')
},
// 滚动事件
onPageScroll(e) {
this.setData({
scrollTop: e.scrollTop
})
},
// 回到顶部
scrollToTop() {
wx.pageScrollTo({
scrollTop: 0,
duration: 300
})
},
// 下拉刷新
onPullDownRefresh() {
},
// 页面卸载
onUnload() {
}
})