peng
2025-11-06 c4938f6f4e839890b032c75c7a57333a6a9157a9
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
// 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)
    })
  }
})