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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
// pages/news/list.js
const app = getApp()
const utils = require('../../lib/utils.js')
 
Page({
  data: {
    newsList: [],
    loading: true,
    hasMore: true,
    currentPage: 1,
    pageSize: 10
  },
 
  onLoad(options) {
    this.loadNewsList()
  },
 
  onShow() {
    // 统一系统导航栏标题
    try { wx.setNavigationBarTitle({ title: '新闻资讯' }) } catch (e) {}
  },
 
  onPullDownRefresh() {
    this.refreshData()
  },
 
  onReachBottom() {
    if (this.data.hasMore && !this.data.loading) {
      this.loadMoreNews()
    }
  },
 
  // 刷新数据
  refreshData() {
    this.setData({
      currentPage: 1,
      hasMore: true,
      newsList: []
    })
    this.loadNewsList().finally(() => {
      wx.stopPullDownRefresh()
    })
  },
 
  // 加载新闻列表
  loadNewsList(isLoadMore = false) {
    this.setData({ loading: true })
    
    const { currentPage, pageSize } = this.data
    
    return app.graphqlRequest(`
      query getPublishedNewsList($page: Int!, $size: Int!) {
        publishedNewsList(page: $page, size: $size) {
          content {
            id
            title
            summary
            coverImage
            author
            viewCount
            createTime
          }
          totalElements
          page
          size
        }
      }
    `, {
      page: currentPage,
      size: pageSize
    }).then(data => {
      if (data.publishedNewsList) {
        const newNewsList = data.publishedNewsList.content
        
        // 格式化时间显示
        newNewsList.forEach(news => {
          if (news.createTime) {
            news.createTime = utils.formatDate(news.createTime, 'YYYY-MM-DD HH:mm:ss');
          }
        });
        
        // 合并数据:只有在真正的加载更多时才追加,其他情况都是全量替换
        const mergedNewsList = isLoadMore && this.data.newsList.length > 0
          ? [...this.data.newsList, ...newNewsList]
          : newNewsList
        
        this.setData({
          newsList: mergedNewsList,
          hasMore: data.publishedNewsList.totalElements > (currentPage * pageSize) && newNewsList.length > 0,
          loading: false
        })
      }
    }).catch(err => {
      console.error('加载新闻列表失败:', err)
      wx.showToast({
        title: '加载失败,请重试',
        icon: 'none'
      })
      this.setData({ loading: false })
    })
  },
 
  // 加载更多新闻
  loadMoreNews() {
    this.setData({
      currentPage: this.data.currentPage + 1
    })
    this.loadNewsList(true)
  },
 
  // 跳转到新闻详情
  goToNewsDetail(e) {
    const newsId = e.currentTarget.dataset.id
    if (newsId) {
      wx.navigateTo({
        url: `/pages/news/detail?id=${newsId}`
      })
    }
  }
})