dapaijizhang3/pages/record/list/list.js

149 lines
3.7 KiB
JavaScript
Raw Normal View History

2025-11-20 16:42:59 +08:00
// pages/record/list/list.js
import request from '../../../utils/request'
Page({
data: {
sessions: [],
loading: false,
page: 1,
pageSize: 10,
hasMore: true,
navbarHeight: 0,
// 统计数据
totalSessions: 0,
totalGames: 0,
totalProfit: 0,
// 游戏类型文本映射
gameTypeText: {
'mahjong': '麻将',
'poker': '扑克',
'other': '其他'
}
},
onLoad() {
// 计算导航栏高度
const windowInfo = wx.getWindowInfo()
const menuButtonInfo = wx.getMenuButtonBoundingClientRect()
const statusBarHeight = windowInfo.statusBarHeight
const menuButtonTop = menuButtonInfo.top
const navbarHeight = menuButtonInfo.bottom + (menuButtonTop - statusBarHeight)
this.setData({ navbarHeight })
this.loadSessions()
this.loadStatistics()
},
// 加载牌局列表
async loadSessions(isRefresh = false) {
if (this.data.loading) return
if (isRefresh) {
this.setData({
page: 1,
sessions: [],
hasMore: true
})
}
this.setData({ loading: true })
try {
const data = await request.get('/rooms/my-sessions', {
page: this.data.page,
pageSize: this.data.pageSize
})
const newSessions = (data.list || []).map(session => {
// 格式化时间
const createTime = new Date(session.created_at * 1000)
const now = new Date()
const diffDays = Math.floor((now - createTime) / (1000 * 60 * 60 * 24))
let timeText = ''
if (diffDays === 0) {
timeText = '今天'
} else if (diffDays === 1) {
timeText = '昨天'
} else if (diffDays < 7) {
timeText = `${diffDays}天前`
} else {
timeText = `${createTime.getMonth() + 1}/${createTime.getDate()}`
}
// 格式化其他玩家昵称显示
let otherPlayersText = ''
if (session.other_players && session.other_players.length > 0) {
otherPlayersText = session.other_players.join('、')
} else {
otherPlayersText = '无其他玩家'
}
return {
...session,
timeText,
otherPlayersText,
gameTypeName: this.data.gameTypeText[session.game_type] || session.game_type,
profitText: session.my_win_loss > 0 ? `+${session.my_win_loss}` : session.my_win_loss
}
})
this.setData({
sessions: isRefresh ? newSessions : [...this.data.sessions, ...newSessions],
hasMore: newSessions.length >= this.data.pageSize,
loading: false
})
} catch (error) {
console.error('加载牌局失败:', error)
this.setData({ loading: false })
wx.showToast({
title: '加载失败',
icon: 'none'
})
}
},
// 加载统计数据
async loadStatistics() {
try {
const stats = await request.get('/auth/profile')
const totalProfit = (stats.total_win || 0) - (stats.total_loss || 0)
this.setData({
totalSessions: stats.total_games || 0,
totalGames: stats.total_games || 0,
totalProfit
})
} catch (error) {
console.error('加载统计失败:', error)
}
},
// 进入牌局详情
goToSession(e) {
const id = e.currentTarget.dataset.id
wx.navigateTo({
url: `/pages/game/detail/detail?id=${id}`
})
},
// 下拉刷新
onPullDownRefresh() {
this.loadSessions(true).then(() => {
this.loadStatistics()
wx.stopPullDownRefresh()
})
},
// 上拉加载更多
onReachBottom() {
if (this.data.hasMore && !this.data.loading) {
this.setData({ page: this.data.page + 1 })
this.loadSessions()
}
}
})