// pages/news/detail.js const app = getApp() const utils = require('../../lib/utils.js') Page({ data: { news: null, loading: true }, onLoad(options) { const newsId = options.id if (newsId) { this.loadNewsDetail(newsId) } else { wx.showToast({ title: '参数错误', icon: 'none' }) setTimeout(() => { wx.navigateBack() }, 1500) } }, onShow() { // 统一系统导航栏标题 try { wx.setNavigationBarTitle({ title: '新闻详情' }) } catch (e) {} }, // 加载新闻详情 loadNewsDetail(newsId) { this.setData({ loading: true }) app.graphqlRequest(` query getPublishedNews($id: ID!) { publishedNews(id: $id) { id title content summary coverImage author viewCount createTime } } `, { id: newsId }).then(data => { if (data.publishedNews) { // 格式化时间显示 const news = data.publishedNews; if (news.createTime) { news.createTime = utils.formatDate(news.createTime, 'YYYY-MM-DD HH:mm:ss'); } this.setData({ news: news, loading: false }) } else { throw new Error('新闻不存在') } }).catch(err => { console.error('加载新闻详情失败:', err) wx.showToast({ title: '加载失败,请重试', icon: 'none' }) this.setData({ loading: false }) setTimeout(() => { wx.navigateBack() }, 1500) }) } })