// pages/message/message.js
|
const app = getApp()
|
|
Page({
|
data: {
|
messages: [],
|
loading: false
|
},
|
|
onLoad() {
|
this.loadMessages()
|
},
|
|
onShow() {
|
this.loadMessages()
|
},
|
|
onPullDownRefresh() {
|
this.loadMessages()
|
},
|
|
// 加载消息列表
|
loadMessages() {
|
// 检查用户是否已登录,如果globalData中没有,尝试从本地存储恢复
|
let userInfo = app.globalData.userInfo
|
if (!userInfo || !userInfo.userId) {
|
console.log('globalData中没有userInfo,尝试从本地存储恢复')
|
try {
|
const storedUserInfo = wx.getStorageSync('userInfo')
|
if (storedUserInfo && storedUserInfo.userId) {
|
console.log('从本地存储恢复userInfo成功')
|
app.globalData.userInfo = storedUserInfo
|
userInfo = storedUserInfo
|
}
|
} catch (error) {
|
console.error('从本地存储恢复userInfo失败:', error)
|
}
|
}
|
|
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()
|
})
|
},
|
|
})
|