| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297 |
- <template>
- <view class="stats-container">
- <view class="header">
- <text class="back-btn" @click="goBack">‹ 返回</text>
- <text class="title">训练统计</text>
- </view>
- <view v-if="loading" class="loading-state">
- <text>加载中...</text>
- </view>
- <template v-else>
- <!-- 概览卡片 -->
- <view class="overview-section">
- <view class="overview-card">
- <view class="overview-item">
- <text class="overview-num">{{ stats.totalGames || 0 }}</text>
- <text class="overview-label">总训练次数</text>
- </view>
- <view class="overview-item">
- <text class="overview-num">{{ stats.totalPoints || 0 }}</text>
- <text class="overview-label">获得积分</text>
- </view>
- <view class="overview-item">
- <text class="overview-num">{{ stats.bestScore || 0 }}</text>
- <text class="overview-label">最高分</text>
- </view>
- </view>
- </view>
- <!-- 各游戏最佳成绩 -->
- <view class="section">
- <text class="section-title">各游戏成绩</text>
- <view class="best-card" v-for="item in bestScores" :key="item.game_code">
- <text class="best-icon">{{ iconMap[item.game_code] || '🎮' }}</text>
- <view class="best-info">
- <text class="best-name">{{ gameNameMap[item.game_code] || item.game_code }}</text>
- <text class="best-stats">
- 最佳 {{ item.best_score }}分 · 共 {{ item.total_plays }}次 · 累计 {{ item.total_points }}分
- </text>
- </view>
- <text class="best-score">{{ item.best_score }}</text>
- </view>
- <view class="no-data" v-if="bestScores.length === 0">
- <text>暂无数据,完成游戏后查看</text>
- </view>
- </view>
- <!-- 排行榜 -->
- <view class="section">
- <text class="section-title">游戏排行榜</text>
- <view class="leaderboard-tabs" v-if="gameCodes.length > 0">
- <view
- v-for="code in gameCodes"
- :key="code"
- :class="['lb-tab', currentLb === code ? 'active' : '']"
- @click="onLeaderboardChange(code)"
- >
- {{ gameNameMap[code] || code }}
- </view>
- </view>
- <view class="leaderboard-list">
- <view class="lb-item" v-for="(entry, idx) in leaderboard" :key="idx">
- <text :class="['lb-rank', idx < 3 ? 'top' : '']">{{ idx + 1 }}</text>
- <text class="lb-name">{{ entry.child_name || '未知' }}</text>
- <text class="lb-score">{{ entry.score }}分</text>
- </view>
- </view>
- <view class="no-data" v-if="leaderboard.length === 0">
- <text>排行榜暂无数据</text>
- </view>
- </view>
- </template>
- </view>
- </template>
- <script>
- import { getGameBestScores, getGameStats, getGameLeaderboard } from '../../utils/api.js'
- export default {
- data() {
- return {
- loading: false,
- stats: {},
- bestScores: [],
- leaderboard: [],
- currentLb: 'schulte',
- gameCodes: ['schulte', '1a2b', 'sudoku'],
- gameNameMap: {
- 'schulte': '舒尔特方格',
- '1a2b': '猜数字',
- 'sudoku': '数独'
- },
- iconMap: {
- 'schulte': '🔢',
- '1a2b': '🔍',
- 'sudoku': '🧩'
- }
- }
- },
- onLoad() {
- this.loadData()
- },
- methods: {
- async loadData() {
- const childId = uni.getStorageSync('currentChildId')
- if (!childId) return
- this.loading = true
- try {
- const [statsRes, bestRes] = await Promise.all([
- getGameStats({ childId }),
- getGameBestScores({ childId })
- ])
- this.stats = statsRes.data || {}
- this.bestScores = bestRes.data || []
- this.loadLeaderboard()
- } catch (e) {
- console.error('加载统计数据失败', e)
- } finally {
- this.loading = false
- }
- },
- async loadLeaderboard() {
- try {
- const res = await getGameLeaderboard({ gameCode: this.currentLb, limit: 10 })
- this.leaderboard = res.data || []
- } catch (e) {
- console.error('加载排行榜失败', e)
- }
- },
- onLeaderboardChange(code) {
- this.currentLb = code
- this.loadLeaderboard()
- },
- goBack() {
- uni.navigateBack()
- }
- }
- }
- </script>
- <style scoped>
- .stats-container {
- min-height: 100vh;
- background: #f5f7fa;
- padding-bottom: 40rpx;
- }
- .header {
- display: flex;
- align-items: center;
- padding: 20rpx 30rpx;
- background: #fff;
- border-bottom: 1rpx solid #f0f0f0;
- }
- .back-btn {
- font-size: 28rpx;
- color: #F97316;
- padding-right: 20rpx;
- }
- .title {
- font-size: 32rpx;
- font-weight: bold;
- color: #333;
- }
- .loading-state {
- text-align: center;
- padding: 120rpx 30rpx;
- font-size: 28rpx;
- color: #999;
- }
- .overview-section {
- padding: 30rpx;
- }
- .overview-card {
- display: flex;
- background: linear-gradient(135deg, #F97316, #FB923C);
- border-radius: 20rpx;
- padding: 40rpx 20rpx;
- }
- .overview-item {
- flex: 1;
- text-align: center;
- }
- .overview-num {
- display: block;
- font-size: 48rpx;
- font-weight: bold;
- color: #fff;
- margin-bottom: 8rpx;
- }
- .overview-label {
- display: block;
- font-size: 24rpx;
- color: rgba(255,255,255,0.8);
- }
- .section {
- margin: 0 30rpx 30rpx;
- }
- .section-title {
- display: block;
- font-size: 30rpx;
- font-weight: bold;
- color: #333;
- margin-bottom: 20rpx;
- }
- .best-card {
- display: flex;
- align-items: center;
- background: #fff;
- border-radius: 16rpx;
- padding: 24rpx;
- margin-bottom: 12rpx;
- box-shadow: 0 2rpx 8rpx rgba(0,0,0,0.04);
- }
- .best-icon {
- font-size: 48rpx;
- margin-right: 20rpx;
- }
- .best-info {
- flex: 1;
- }
- .best-name {
- display: block;
- font-size: 28rpx;
- color: #333;
- font-weight: bold;
- margin-bottom: 4rpx;
- }
- .best-stats {
- display: block;
- font-size: 22rpx;
- color: #999;
- }
- .best-score {
- font-size: 40rpx;
- font-weight: bold;
- color: #F97316;
- }
- .leaderboard-tabs {
- display: flex;
- gap: 16rpx;
- margin-bottom: 20rpx;
- }
- .lb-tab {
- padding: 8rpx 24rpx;
- border-radius: 30rpx;
- font-size: 24rpx;
- color: #666;
- background: #f5f5f5;
- }
- .lb-tab.active {
- color: #fff;
- background: #F97316;
- }
- .leaderboard-list {
- background: #fff;
- border-radius: 16rpx;
- overflow: hidden;
- }
- .lb-item {
- display: flex;
- align-items: center;
- padding: 20rpx 30rpx;
- border-bottom: 1rpx solid #f0f0f0;
- }
- .lb-item:last-child {
- border-bottom: none;
- }
- .lb-rank {
- width: 50rpx;
- font-size: 28rpx;
- color: #999;
- font-weight: bold;
- }
- .lb-rank.top {
- color: #F97316;
- }
- .lb-name {
- flex: 1;
- font-size: 26rpx;
- color: #333;
- }
- .lb-score {
- font-size: 28rpx;
- color: #F97316;
- font-weight: bold;
- }
- .no-data {
- text-align: center;
- padding: 60rpx 30rpx;
- font-size: 26rpx;
- color: #999;
- background: #fff;
- border-radius: 16rpx;
- }
- </style>
|