stats.vue 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299
  1. <template>
  2. <view class="stats-container" :style="{ '--font-scale': fontScale }">
  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. import fontScaleMixin from '@/mixins/font-scale.js'
  75. export default {
  76. mixins: [fontScaleMixin],
  77. data() {
  78. return {
  79. loading: false,
  80. stats: {},
  81. bestScores: [],
  82. leaderboard: [],
  83. currentLb: 'schulte',
  84. gameCodes: ['schulte', '1a2b', 'sudoku'],
  85. gameNameMap: {
  86. 'schulte': '舒尔特方格',
  87. '1a2b': '猜数字',
  88. 'sudoku': '数独'
  89. },
  90. iconMap: {
  91. 'schulte': '🔢',
  92. '1a2b': '🔍',
  93. 'sudoku': '🧩'
  94. }
  95. }
  96. },
  97. onLoad() {
  98. this.loadData()
  99. },
  100. methods: {
  101. async loadData() {
  102. const memberId = uni.getStorageSync('currentChildId')
  103. if (!memberId) return
  104. this.loading = true
  105. try {
  106. const [statsRes, bestRes] = await Promise.all([
  107. getGameStats({ memberId }),
  108. getGameBestScores({ memberId })
  109. ])
  110. this.stats = statsRes.data || {}
  111. this.bestScores = bestRes.data || []
  112. this.loadLeaderboard()
  113. } catch (e) {
  114. console.error('加载统计数据失败', e)
  115. } finally {
  116. this.loading = false
  117. }
  118. },
  119. async loadLeaderboard() {
  120. try {
  121. const res = await getGameLeaderboard({ gameCode: this.currentLb, limit: 10 })
  122. this.leaderboard = res.data || []
  123. } catch (e) {
  124. console.error('加载排行榜失败', e)
  125. }
  126. },
  127. onLeaderboardChange(code) {
  128. this.currentLb = code
  129. this.loadLeaderboard()
  130. },
  131. goBack() {
  132. uni.navigateBack()
  133. }
  134. }
  135. }
  136. </script>
  137. <style scoped lang="scss">
  138. .stats-container {
  139. min-height: 100vh;
  140. background: $app-bg;
  141. padding-bottom: 40rpx;
  142. }
  143. .header {
  144. display: flex;
  145. align-items: center;
  146. padding: 20rpx $uni-space-page;
  147. background: $app-surface;
  148. border-bottom: 1rpx solid $facade-fill;
  149. }
  150. .back-btn {
  151. font-size: calc(28rpx * var(--font-scale, 1));
  152. color: $brand-primary;
  153. padding-right: 20rpx;
  154. }
  155. .title {
  156. font-size: calc(32rpx * var(--font-scale, 1));
  157. font-weight: bold;
  158. color: $facade-text-strong;
  159. }
  160. .loading-state {
  161. text-align: center;
  162. padding: 120rpx $uni-space-page;
  163. font-size: calc(28rpx * var(--font-scale, 1));
  164. color: $facade-text-muted;
  165. }
  166. .overview-section {
  167. padding: $uni-space-page;
  168. }
  169. .overview-card {
  170. display: flex;
  171. background: linear-gradient(135deg, $brand-primary, $brand-primary-light);
  172. border-radius: 24rpx;
  173. padding: 40rpx 20rpx;
  174. }
  175. .overview-item {
  176. flex: 1;
  177. text-align: center;
  178. }
  179. .overview-num {
  180. display: block;
  181. font-size: calc(48rpx * var(--font-scale, 1));
  182. font-weight: bold;
  183. color: $app-surface;
  184. margin-bottom: 8rpx;
  185. }
  186. .overview-label {
  187. display: block;
  188. font-size: calc(24rpx * var(--font-scale, 1));
  189. color: rgba(255,255,255,0.8);
  190. }
  191. .section {
  192. margin: 0 $uni-space-lg $uni-space-xl;
  193. }
  194. .section-title {
  195. display: block;
  196. font-size: calc(30rpx * var(--font-scale, 1));
  197. font-weight: bold;
  198. color: $facade-text-strong;
  199. margin-bottom: 20rpx;
  200. }
  201. .best-card {
  202. display: flex;
  203. align-items: center;
  204. background: $app-surface;
  205. border-radius: 16rpx;
  206. padding: 24rpx;
  207. margin-bottom: 12rpx;
  208. box-shadow: 0 2rpx 8rpx rgba(0,0,0,0.04);
  209. }
  210. .best-icon {
  211. font-size: calc(48rpx * var(--font-scale, 1));
  212. margin-right: 20rpx;
  213. }
  214. .best-info {
  215. flex: 1;
  216. }
  217. .best-name {
  218. display: block;
  219. font-size: calc(28rpx * var(--font-scale, 1));
  220. color: $facade-text-strong;
  221. font-weight: bold;
  222. margin-bottom: 4rpx;
  223. }
  224. .best-stats {
  225. display: block;
  226. font-size: calc(22rpx * var(--font-scale, 1));
  227. color: $facade-text-muted;
  228. }
  229. .best-score {
  230. font-size: calc(40rpx * var(--font-scale, 1));
  231. font-weight: bold;
  232. color: $brand-primary;
  233. }
  234. .leaderboard-tabs {
  235. display: flex;
  236. gap: 16rpx;
  237. margin-bottom: 20rpx;
  238. }
  239. .lb-tab {
  240. padding: 8rpx 24rpx;
  241. border-radius: 24rpx;
  242. font-size: calc(24rpx * var(--font-scale, 1));
  243. color: $facade-text-body;
  244. background: $facade-fill-2;
  245. }
  246. .lb-tab.active {
  247. color: $app-surface;
  248. background: $brand-primary;
  249. }
  250. .leaderboard-list {
  251. background: $app-surface;
  252. border-radius: 16rpx;
  253. overflow: hidden;
  254. }
  255. .lb-item {
  256. display: flex;
  257. align-items: center;
  258. padding: 20rpx $uni-space-page;
  259. border-bottom: 1rpx solid $facade-fill;
  260. }
  261. .lb-item:last-child {
  262. border-bottom: none;
  263. }
  264. .lb-rank {
  265. width: 50rpx;
  266. font-size: calc(28rpx * var(--font-scale, 1));
  267. color: $facade-text-muted;
  268. font-weight: bold;
  269. }
  270. .lb-rank.top {
  271. color: $brand-primary;
  272. }
  273. .lb-name {
  274. flex: 1;
  275. font-size: calc(26rpx * var(--font-scale, 1));
  276. color: $facade-text-strong;
  277. }
  278. .lb-score {
  279. font-size: calc(28rpx * var(--font-scale, 1));
  280. color: $brand-primary;
  281. font-weight: bold;
  282. }
  283. .no-data {
  284. text-align: center;
  285. padding: 60rpx $uni-space-page;
  286. font-size: calc(26rpx * var(--font-scale, 1));
  287. color: $facade-text-muted;
  288. background: $app-surface;
  289. border-radius: 16rpx;
  290. }
  291. </style>