dapaijizhang3/pages/profile/index/index.js

187 lines
4.3 KiB
JavaScript
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

// pages/profile/index/index.js
import request from '../../../utils/request'
Page({
data: {
userInfo: null,
isLogin: false,
navbarHeight: 0,
stats: {
total_games: 0,
total_win: 0,
total_loss: 0,
win_rate: 0,
net_profit: 0
},
recentSessions: [],
achievements: []
},
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.loadUserData()
},
onShow() {
// 每次显示时刷新数据
this.loadUserData()
},
// 加载用户数据
async loadUserData() {
const app = getApp()
const userInfo = app.getUserInfo()
if (userInfo) {
this.setData({
userInfo,
isLogin: true
})
await this.loadUserStats()
await this.loadRecentSessions()
this.calculateAchievements()
} else {
this.setData({
isLogin: false
})
}
},
// 加载用户统计
async loadUserStats() {
try {
const stats = await request.get('/auth/profile')
// 计算净盈利
const netProfit = (stats.total_win || 0) - (stats.total_loss || 0)
this.setData({
stats: {
total_games: stats.total_games || 0,
total_win: stats.total_win || 0,
total_loss: stats.total_loss || 0,
win_rate: stats.win_rate || 0,
net_profit: netProfit
}
})
} catch (error) {
console.error('加载统计失败:', error)
}
},
// 加载最近牌局
async loadRecentSessions() {
try {
const data = await request.get('/rooms/my-sessions', {
page: 1,
pageSize: 5
})
this.setData({
recentSessions: data.list || []
})
} catch (error) {
console.error('加载牌局失败:', error)
}
},
// 计算成就
calculateAchievements() {
const { stats } = this.data
const achievements = []
// 根据游戏场次
if (stats.total_games >= 100) {
achievements.push({ icon: '🏆', name: '百战老将', desc: '参与100场游戏' })
} else if (stats.total_games >= 50) {
achievements.push({ icon: '🎖️', name: '资深玩家', desc: '参与50场游戏' })
} else if (stats.total_games >= 10) {
achievements.push({ icon: '🌟', name: '初出茅庐', desc: '参与10场游戏' })
}
// 根据胜率
if (stats.win_rate >= 70) {
achievements.push({ icon: '👑', name: '牌桌霸主', desc: '胜率超过70%' })
} else if (stats.win_rate >= 60) {
achievements.push({ icon: '💎', name: '高手', desc: '胜率超过60%' })
}
// 根据盈利
if (stats.net_profit > 10000) {
achievements.push({ icon: '💰', name: '财运亨通', desc: '累计盈利过万' })
} else if (stats.net_profit > 5000) {
achievements.push({ icon: '💵', name: '小有盈余', desc: '累计盈利5000+' })
}
this.setData({ achievements })
},
// 处理登录
handleLogin() {
wx.navigateTo({
url: '/pages/login/login'
})
},
// 编辑资料
editProfile() {
wx.navigateTo({
url: '/pages/login/login'
})
},
// 查看个人统计
viewStats() {
wx.navigateTo({
url: '/pages/stats/personal/personal'
})
},
// 查看全部牌局
viewAllSessions() {
wx.navigateTo({
url: '/pages/record/list/list'
})
},
// 进入牌局
goToSession(e) {
const id = e.currentTarget.dataset.id
wx.navigateTo({
url: `/pages/game/detail/detail?id=${id}`
})
},
// 关于
showAbout() {
wx.showModal({
title: '关于打牌记账',
content: '版本v1.0.0\n\n一款专业的打牌记账小程序\n支持麻将、扑克等多种游戏类型\n实时记录、智能统计、便捷结算',
showCancel: false
})
},
// 联系客服
contactService() {
wx.showModal({
title: '联系客服',
content: '客服微信:请添加公众号\n客服电话暂未开通',
showCancel: false
})
},
// 下拉刷新
onPullDownRefresh() {
this.loadUserData().then(() => {
wx.stopPullDownRefresh()
})
}
})