stats.vue 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297
  1. <template>
  2. <view class="stats-container">
  3. <view class="header">
  4. <text class="back-btn" @click="goBack">‹ 返回</text>
  5. <text class="title">训练统计</text>
  6. </view>
  7. <view v-if="loading" class="loading-state">
  8. <text>加载中...</text>
  9. </view>
  10. <template v-else>
  11. <!-- 概览卡片 -->
  12. <view class="overview-section">
  13. <view class="overview-card">
  14. <view class="overview-item">
  15. <text class="overview-num">{{ stats.totalGames || 0 }}</text>
  16. <text class="overview-label">总训练次数</text>
  17. </view>
  18. <view class="overview-item">
  19. <text class="overview-num">{{ stats.totalPoints || 0 }}</text>
  20. <text class="overview-label">获得积分</text>
  21. </view>
  22. <view class="overview-item">
  23. <text class="overview-num">{{ stats.bestScore || 0 }}</text>
  24. <text class="overview-label">最高分</text>
  25. </view>
  26. </view>
  27. </view>
  28. <!-- 各游戏最佳成绩 -->
  29. <view class="section">
  30. <text class="section-title">各游戏成绩</text>
  31. <view class="best-card" v-for="item in bestScores" :key="item.game_code">
  32. <text class="best-icon">{{ iconMap[item.game_code] || '🎮' }}</text>
  33. <view class="best-info">
  34. <text class="best-name">{{ gameNameMap[item.game_code] || item.game_code }}</text>
  35. <text class="best-stats">
  36. 最佳 {{ item.best_score }}分 · 共 {{ item.total_plays }}次 · 累计 {{ item.total_points }}分
  37. </text>
  38. </view>
  39. <text class="best-score">{{ item.best_score }}</text>
  40. </view>
  41. <view class="no-data" v-if="bestScores.length === 0">
  42. <text>暂无数据,完成游戏后查看</text>
  43. </view>
  44. </view>
  45. <!-- 排行榜 -->
  46. <view class="section">
  47. <text class="section-title">游戏排行榜</text>
  48. <view class="leaderboard-tabs" v-if="gameCodes.length > 0">
  49. <view
  50. v-for="code in gameCodes"
  51. :key="code"
  52. :class="['lb-tab', currentLb === code ? 'active' : '']"
  53. @click="onLeaderboardChange(code)"
  54. >
  55. {{ gameNameMap[code] || code }}
  56. </view>
  57. </view>
  58. <view class="leaderboard-list">
  59. <view class="lb-item" v-for="(entry, idx) in leaderboard" :key="idx">
  60. <text :class="['lb-rank', idx < 3 ? 'top' : '']">{{ idx + 1 }}</text>
  61. <text class="lb-name">{{ entry.child_name || '未知' }}</text>
  62. <text class="lb-score">{{ entry.score }}分</text>
  63. </view>
  64. </view>
  65. <view class="no-data" v-if="leaderboard.length === 0">
  66. <text>排行榜暂无数据</text>
  67. </view>
  68. </view>
  69. </template>
  70. </view>
  71. </template>
  72. <script>
  73. import { getGameBestScores, getGameStats, getGameLeaderboard } from '../../utils/api.js'
  74. export default {
  75. data() {
  76. return {
  77. loading: false,
  78. stats: {},
  79. bestScores: [],
  80. leaderboard: [],
  81. currentLb: 'schulte',
  82. gameCodes: ['schulte', '1a2b', 'sudoku'],
  83. gameNameMap: {
  84. 'schulte': '舒尔特方格',
  85. '1a2b': '猜数字',
  86. 'sudoku': '数独'
  87. },
  88. iconMap: {
  89. 'schulte': '🔢',
  90. '1a2b': '🔍',
  91. 'sudoku': '🧩'
  92. }
  93. }
  94. },
  95. onLoad() {
  96. this.loadData()
  97. },
  98. methods: {
  99. async loadData() {
  100. const childId = uni.getStorageSync('currentChildId')
  101. if (!childId) return
  102. this.loading = true
  103. try {
  104. const [statsRes, bestRes] = await Promise.all([
  105. getGameStats({ childId }),
  106. getGameBestScores({ childId })
  107. ])
  108. this.stats = statsRes.data || {}
  109. this.bestScores = bestRes.data || []
  110. this.loadLeaderboard()
  111. } catch (e) {
  112. console.error('加载统计数据失败', e)
  113. } finally {
  114. this.loading = false
  115. }
  116. },
  117. async loadLeaderboard() {
  118. try {
  119. const res = await getGameLeaderboard({ gameCode: this.currentLb, limit: 10 })
  120. this.leaderboard = res.data || []
  121. } catch (e) {
  122. console.error('加载排行榜失败', e)
  123. }
  124. },
  125. onLeaderboardChange(code) {
  126. this.currentLb = code
  127. this.loadLeaderboard()
  128. },
  129. goBack() {
  130. uni.navigateBack()
  131. }
  132. }
  133. }
  134. </script>
  135. <style scoped>
  136. .stats-container {
  137. min-height: 100vh;
  138. background: #f5f7fa;
  139. padding-bottom: 40rpx;
  140. }
  141. .header {
  142. display: flex;
  143. align-items: center;
  144. padding: 20rpx 30rpx;
  145. background: #fff;
  146. border-bottom: 1rpx solid #f0f0f0;
  147. }
  148. .back-btn {
  149. font-size: 28rpx;
  150. color: #F97316;
  151. padding-right: 20rpx;
  152. }
  153. .title {
  154. font-size: 32rpx;
  155. font-weight: bold;
  156. color: #333;
  157. }
  158. .loading-state {
  159. text-align: center;
  160. padding: 120rpx 30rpx;
  161. font-size: 28rpx;
  162. color: #999;
  163. }
  164. .overview-section {
  165. padding: 30rpx;
  166. }
  167. .overview-card {
  168. display: flex;
  169. background: linear-gradient(135deg, #F97316, #FB923C);
  170. border-radius: 20rpx;
  171. padding: 40rpx 20rpx;
  172. }
  173. .overview-item {
  174. flex: 1;
  175. text-align: center;
  176. }
  177. .overview-num {
  178. display: block;
  179. font-size: 48rpx;
  180. font-weight: bold;
  181. color: #fff;
  182. margin-bottom: 8rpx;
  183. }
  184. .overview-label {
  185. display: block;
  186. font-size: 24rpx;
  187. color: rgba(255,255,255,0.8);
  188. }
  189. .section {
  190. margin: 0 30rpx 30rpx;
  191. }
  192. .section-title {
  193. display: block;
  194. font-size: 30rpx;
  195. font-weight: bold;
  196. color: #333;
  197. margin-bottom: 20rpx;
  198. }
  199. .best-card {
  200. display: flex;
  201. align-items: center;
  202. background: #fff;
  203. border-radius: 16rpx;
  204. padding: 24rpx;
  205. margin-bottom: 12rpx;
  206. box-shadow: 0 2rpx 8rpx rgba(0,0,0,0.04);
  207. }
  208. .best-icon {
  209. font-size: 48rpx;
  210. margin-right: 20rpx;
  211. }
  212. .best-info {
  213. flex: 1;
  214. }
  215. .best-name {
  216. display: block;
  217. font-size: 28rpx;
  218. color: #333;
  219. font-weight: bold;
  220. margin-bottom: 4rpx;
  221. }
  222. .best-stats {
  223. display: block;
  224. font-size: 22rpx;
  225. color: #999;
  226. }
  227. .best-score {
  228. font-size: 40rpx;
  229. font-weight: bold;
  230. color: #F97316;
  231. }
  232. .leaderboard-tabs {
  233. display: flex;
  234. gap: 16rpx;
  235. margin-bottom: 20rpx;
  236. }
  237. .lb-tab {
  238. padding: 8rpx 24rpx;
  239. border-radius: 30rpx;
  240. font-size: 24rpx;
  241. color: #666;
  242. background: #f5f5f5;
  243. }
  244. .lb-tab.active {
  245. color: #fff;
  246. background: #F97316;
  247. }
  248. .leaderboard-list {
  249. background: #fff;
  250. border-radius: 16rpx;
  251. overflow: hidden;
  252. }
  253. .lb-item {
  254. display: flex;
  255. align-items: center;
  256. padding: 20rpx 30rpx;
  257. border-bottom: 1rpx solid #f0f0f0;
  258. }
  259. .lb-item:last-child {
  260. border-bottom: none;
  261. }
  262. .lb-rank {
  263. width: 50rpx;
  264. font-size: 28rpx;
  265. color: #999;
  266. font-weight: bold;
  267. }
  268. .lb-rank.top {
  269. color: #F97316;
  270. }
  271. .lb-name {
  272. flex: 1;
  273. font-size: 26rpx;
  274. color: #333;
  275. }
  276. .lb-score {
  277. font-size: 28rpx;
  278. color: #F97316;
  279. font-weight: bold;
  280. }
  281. .no-data {
  282. text-align: center;
  283. padding: 60rpx 30rpx;
  284. font-size: 26rpx;
  285. color: #999;
  286. background: #fff;
  287. border-radius: 16rpx;
  288. }
  289. </style>