lrj
4 小时以前 dc643ba44fd2a426263015491268a0f0d6b4671d
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
// pages/message/message.js
const app = getApp()
 
Page({
  data: {
    messages: [],
    loading: false
  },
 
  onLoad() {
    this.loadMessages()
  },
 
  onShow() {
    this.loadMessages()
  },
 
  onPullDownRefresh() {
    this.loadMessages()
  },
 
  // 加载消息列表
  loadMessages() {
    // 检查用户是否已登录
    const userInfo = app.globalData.userInfo
    if (!userInfo || !userInfo.userId) {
      console.error('用户未登录或userId不存在')
      wx.showToast({
        title: '请先登录',
        icon: 'error'
      })
      return
    }
 
    this.setData({ loading: true })
    
    const query = `
      query GetMessagesByUserId($userId: Long!) {
        getMessagesByUserId(userId: $userId) {
          id
          userId
          content
          wxMsgSuccess
          wxMsgErrCount
          state
          createTime
          updateTime
        }
      }
    `
 
    const variables = {
      userId: userInfo.userId
    }
 
    app.graphqlRequest(query, variables)
      .then(data => {
        console.log('消息数据:', data)
        this.setData({
          messages: data.getMessagesByUserId || [],
          loading: false
        })
        wx.stopPullDownRefresh()
      })
      .catch(error => {
        console.error('加载消息失败:', error)
        wx.showToast({
          title: '加载失败',
          icon: 'error'
        })
        this.setData({ loading: false })
        wx.stopPullDownRefresh()
      })
  },
 
  // 格式化消息时间
  formatMessageTime(timeStr) {
    if (!timeStr) return ''
    
    try {
      const date = new Date(timeStr)
      const now = new Date()
      const diff = now.getTime() - date.getTime()
      
      // 如果是今天
      if (diff < 24 * 60 * 60 * 1000) {
        const hours = date.getHours().toString().padStart(2, '0')
        const minutes = date.getMinutes().toString().padStart(2, '0')
        return `${hours}:${minutes}`
      }
      
      // 如果是昨天
      if (diff < 48 * 60 * 60 * 1000) {
        const hours = date.getHours().toString().padStart(2, '0')
        const minutes = date.getMinutes().toString().padStart(2, '0')
        return `昨天 ${hours}:${minutes}`
      }
      
      // 其他日期
      const month = (date.getMonth() + 1).toString().padStart(2, '0')
      const day = date.getDate().toString().padStart(2, '0')
      const hours = date.getHours().toString().padStart(2, '0')
      const minutes = date.getMinutes().toString().padStart(2, '0')
      return `${month}-${day} ${hours}:${minutes}`
    } catch (error) {
      console.error('时间格式化失败:', error)
      return timeStr
    }
  }
})