schulte.vue 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  1. <template>
  2. <view class="game-container">
  3. <view class="game-header">
  4. <text class="game-title">🎯 舒尔特方格</text>
  5. <view class="game-stats">
  6. <text>时间: {{ time }}秒</text>
  7. <text>得分: {{ score }}</text>
  8. </view>
  9. </view>
  10. <view class="grid-container">
  11. <view
  12. class="grid-cell"
  13. v-for="(num, index) in numbers"
  14. :key="index"
  15. :class="{ correct: index + 1 === currentNumber }"
  16. @click="handleCellClick(num)"
  17. >
  18. {{ num }}
  19. </view>
  20. </view>
  21. <view class="game-controls" v-if="!isPlaying">
  22. <button class="start-btn" @click="startGame">开始游戏</button>
  23. <text class="tip">请按顺序点击1-25,计时开始后不可暂停</text>
  24. </view>
  25. <view class="result-modal" v-if="showResult">
  26. <view class="result-content">
  27. <text class="result-title">🎉 恭喜完成!</text>
  28. <text class="result-score">用时: {{ time }}秒</text>
  29. <text class="result-score">获得积分: {{ score }}</text>
  30. <button class="restart-btn" @click="startGame">再来一次</button>
  31. </view>
  32. </view>
  33. </view>
  34. </template>
  35. <script>
  36. import { completeMiniGame, getChildren } from '../../utils/api.js'
  37. export default {
  38. data() {
  39. return {
  40. numbers: [],
  41. currentNumber: 1,
  42. isPlaying: false,
  43. time: 0,
  44. timer: null,
  45. score: 0,
  46. showResult: false,
  47. childId: null,
  48. pointsEarned: 0,
  49. newBalance: 0
  50. }
  51. },
  52. onLoad() {
  53. this.loadChildId()
  54. },
  55. onUnload() {
  56. if (this.timer) clearInterval(this.timer)
  57. },
  58. methods: {
  59. initGrid() {
  60. // 生成1-25的随机排列
  61. let nums = []
  62. for (let i = 1; i <= 25; i++) {
  63. nums.push(i)
  64. }
  65. // 洗牌算法
  66. for (let i = nums.length - 1; i > 0; i--) {
  67. const j = Math.floor(Math.random() * (i + 1))
  68. ;[nums[i], nums[j]] = [nums[j], nums[i]]
  69. }
  70. this.numbers = nums
  71. },
  72. async loadChildId() {
  73. try {
  74. const res = await getChildren()
  75. if (res.data && res.data.length > 0) {
  76. this.childId = res.data[0].id
  77. }
  78. } catch (e) {
  79. console.error('获取孩子ID失败', e)
  80. }
  81. },
  82. startGame() {
  83. this.currentNumber = 1
  84. this.isPlaying = true
  85. this.time = 0
  86. this.score = 0
  87. this.showResult = false
  88. this.initGrid()
  89. this.timer = setInterval(() => {
  90. this.time++
  91. }, 1000)
  92. },
  93. handleCellClick(num) {
  94. if (!this.isPlaying) return
  95. if (num === this.currentNumber) {
  96. // 正确点击
  97. if (this.currentNumber === 25) {
  98. // 完成游戏
  99. this.finishGame()
  100. } else {
  101. this.currentNumber++
  102. }
  103. }
  104. },
  105. finishGame() {
  106. clearInterval(this.timer)
  107. this.isPlaying = false
  108. // 分数计算: 用时越短分数越高, 满分100
  109. this.score = Math.max(100 - Math.floor(this.time * 2), 60)
  110. this.showResult = true
  111. // 调用API记录积分
  112. if (this.childId) {
  113. this.submitScore()
  114. }
  115. },
  116. async submitScore() {
  117. try {
  118. const res = await completeMiniGame(this.childId, 'schulte', this.time, this.score)
  119. if (res.code === 200) {
  120. this.pointsEarned = res.data.pointsEarned
  121. this.newBalance = res.data.newBalance
  122. uni.showToast({
  123. title: `获得${this.pointsEarned}积分!`,
  124. icon: 'success'
  125. })
  126. }
  127. } catch (e) {
  128. console.error('提交成绩失败', e)
  129. }
  130. }
  131. }
  132. }
  133. </script>
  134. <style scoped>
  135. .game-container {
  136. min-height: 100vh;
  137. background: linear-gradient(135deg, #1a1a2e 0%, #16213e 100%);
  138. padding: 30rpx;
  139. }
  140. .game-header {
  141. text-align: center;
  142. margin-bottom: 40rpx;
  143. }
  144. .game-title {
  145. display: block;
  146. font-size: 48rpx;
  147. font-weight: bold;
  148. color: #FFE66D;
  149. }
  150. .game-stats {
  151. display: flex;
  152. justify-content: center;
  153. gap: 60rpx;
  154. margin-top: 20rpx;
  155. color: #fff;
  156. font-size: 32rpx;
  157. }
  158. .grid-container {
  159. display: grid;
  160. grid-template-columns: repeat(5, 1fr);
  161. gap: 10rpx;
  162. margin: 40rpx 0;
  163. }
  164. .grid-cell {
  165. aspect-ratio: 1;
  166. background: #2a2a4a;
  167. display: flex;
  168. align-items: center;
  169. justify-content: center;
  170. font-size: 40rpx;
  171. font-weight: bold;
  172. color: #fff;
  173. border-radius: 10rpx;
  174. cursor: pointer;
  175. }
  176. .grid-cell:active {
  177. background: #4ECDC4;
  178. }
  179. .grid-cell.correct {
  180. background: #4ECDC4;
  181. color: #1a1a2e;
  182. }
  183. .game-controls {
  184. text-align: center;
  185. }
  186. .start-btn {
  187. background: linear-gradient(135deg, #FF6B6B, #FF8E53);
  188. color: #fff;
  189. padding: 20rpx 60rpx;
  190. border-radius: 50rpx;
  191. font-size: 36rpx;
  192. border: none;
  193. }
  194. .tip {
  195. display: block;
  196. color: rgba(255,255,255,0.5);
  197. font-size: 24rpx;
  198. margin-top: 20rpx;
  199. }
  200. .result-modal {
  201. position: fixed;
  202. top: 0;
  203. left: 0;
  204. right: 0;
  205. bottom: 0;
  206. background: rgba(0,0,0,0.8);
  207. display: flex;
  208. align-items: center;
  209. justify-content: center;
  210. }
  211. .result-content {
  212. background: #2a2a4a;
  213. padding: 60rpx;
  214. border-radius: 20rpx;
  215. text-align: center;
  216. }
  217. .result-title {
  218. display: block;
  219. font-size: 48rpx;
  220. color: #FFE66D;
  221. margin-bottom: 30rpx;
  222. }
  223. .result-score {
  224. display: block;
  225. color: #fff;
  226. font-size: 32rpx;
  227. margin-bottom: 20rpx;
  228. }
  229. .restart-btn {
  230. background: #4ECDC4;
  231. color: #1a1a2e;
  232. padding: 20rpx 60rpx;
  233. border-radius: 50rpx;
  234. border: none;
  235. margin-top: 30rpx;
  236. }
  237. </style>