schulte.vue 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299
  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. taskId: null,
  49. pointsEarned: 0,
  50. newBalance: 0
  51. }
  52. },
  53. onLoad(options) {
  54. // 支持从任务跳转过来
  55. if (options.taskId) {
  56. this.taskId = parseInt(options.taskId)
  57. }
  58. if (options.childId) {
  59. this.childId = parseInt(options.childId)
  60. }
  61. this.loadChildId()
  62. },
  63. onUnload() {
  64. if (this.timer) clearInterval(this.timer)
  65. },
  66. methods: {
  67. initGrid() {
  68. // 生成1-25的随机排列
  69. let nums = []
  70. for (let i = 1; i <= 25; i++) {
  71. nums.push(i)
  72. }
  73. // 洗牌算法
  74. for (let i = nums.length - 1; i > 0; i--) {
  75. const j = Math.floor(Math.random() * (i + 1))
  76. ;[nums[i], nums[j]] = [nums[j], nums[i]]
  77. }
  78. this.numbers = nums
  79. },
  80. async loadChildId() {
  81. try {
  82. if (!this.childId) {
  83. const res = await getChildren()
  84. if (res.data && res.data.length > 0) {
  85. this.childId = res.data[0].id
  86. }
  87. }
  88. } catch (e) {
  89. console.error('获取孩子ID失败', e)
  90. }
  91. },
  92. startGame() {
  93. this.currentNumber = 1
  94. this.isPlaying = true
  95. this.time = 0
  96. this.score = 0
  97. this.showResult = false
  98. this.initGrid()
  99. this.timer = setInterval(() => {
  100. this.time++
  101. }, 1000)
  102. },
  103. handleCellClick(num) {
  104. if (!this.isPlaying) return
  105. if (num === this.currentNumber) {
  106. // 正确点击
  107. if (this.currentNumber === 25) {
  108. // 完成游戏
  109. this.finishGame()
  110. } else {
  111. this.currentNumber++
  112. }
  113. }
  114. },
  115. finishGame() {
  116. clearInterval(this.timer)
  117. this.isPlaying = false
  118. // 分数计算: 用时越短分数越高, 满分100
  119. this.score = Math.max(100 - Math.floor(this.time * 2), 60)
  120. this.showResult = true
  121. // 调用API记录积分
  122. if (this.childId) {
  123. this.submitScore()
  124. }
  125. },
  126. async submitScore() {
  127. try {
  128. const BASE_URL = 'http://localhost:8080'
  129. const token = uni.getStorageSync('token')
  130. // 如果有taskId,说明是从任务跳转来的,使用任务完成接口
  131. if (this.taskId) {
  132. const res = await uni.request({
  133. url: `${BASE_URL}/api/tasks/${this.taskId}/complete-minigame`,
  134. method: 'POST',
  135. header: {
  136. 'Authorization': token ? `Bearer ${token}` : '',
  137. 'Content-Type': 'application/json'
  138. },
  139. data: {
  140. childId: this.childId,
  141. completionTime: this.time,
  142. score: this.score
  143. }
  144. })
  145. if (res.data && res.data.code === 200) {
  146. this.pointsEarned = res.data.data.pointsEarned
  147. this.newBalance = res.data.data.newBalance
  148. uni.showToast({
  149. title: `获得${this.pointsEarned}积分!`,
  150. icon: 'success'
  151. })
  152. }
  153. } else {
  154. // 普通小游戏完成
  155. const res = await completeMiniGame(this.childId, 'schulte', this.time, this.score)
  156. if (res.code === 200) {
  157. this.pointsEarned = res.data.pointsEarned
  158. this.newBalance = res.data.newBalance
  159. uni.showToast({
  160. title: `获得${this.pointsEarned}积分!`,
  161. icon: 'success'
  162. })
  163. }
  164. }
  165. } catch (e) {
  166. console.error('提交成绩失败', e)
  167. }
  168. }
  169. }
  170. }
  171. </script>
  172. <style scoped>
  173. .game-container {
  174. min-height: 100vh;
  175. background: linear-gradient(135deg, #1a1a2e 0%, #16213e 100%);
  176. padding: 30rpx;
  177. }
  178. .game-header {
  179. text-align: center;
  180. margin-bottom: 40rpx;
  181. }
  182. .game-title {
  183. display: block;
  184. font-size: 48rpx;
  185. font-weight: bold;
  186. color: #FFE66D;
  187. }
  188. .game-stats {
  189. display: flex;
  190. justify-content: center;
  191. gap: 60rpx;
  192. margin-top: 20rpx;
  193. color: #fff;
  194. font-size: 32rpx;
  195. }
  196. .grid-container {
  197. display: grid;
  198. grid-template-columns: repeat(5, 1fr);
  199. gap: 10rpx;
  200. margin: 40rpx 0;
  201. }
  202. .grid-cell {
  203. aspect-ratio: 1;
  204. background: #2a2a4a;
  205. display: flex;
  206. align-items: center;
  207. justify-content: center;
  208. font-size: 40rpx;
  209. font-weight: bold;
  210. color: #fff;
  211. border-radius: 10rpx;
  212. cursor: pointer;
  213. }
  214. .grid-cell:active {
  215. background: #4ECDC4;
  216. }
  217. .grid-cell.correct {
  218. background: #4ECDC4;
  219. color: #1a1a2e;
  220. }
  221. .game-controls {
  222. text-align: center;
  223. }
  224. .start-btn {
  225. background: linear-gradient(135deg, #FF6B6B, #FF8E53);
  226. color: #fff;
  227. padding: 20rpx 60rpx;
  228. border-radius: 50rpx;
  229. font-size: 36rpx;
  230. border: none;
  231. }
  232. .tip {
  233. display: block;
  234. color: rgba(255,255,255,0.5);
  235. font-size: 24rpx;
  236. margin-top: 20rpx;
  237. }
  238. .result-modal {
  239. position: fixed;
  240. top: 0;
  241. left: 0;
  242. right: 0;
  243. bottom: 0;
  244. background: rgba(0,0,0,0.8);
  245. display: flex;
  246. align-items: center;
  247. justify-content: center;
  248. }
  249. .result-content {
  250. background: #2a2a4a;
  251. padding: 60rpx;
  252. border-radius: 20rpx;
  253. text-align: center;
  254. }
  255. .result-title {
  256. display: block;
  257. font-size: 48rpx;
  258. color: #FFE66D;
  259. margin-bottom: 30rpx;
  260. }
  261. .result-score {
  262. display: block;
  263. color: #fff;
  264. font-size: 32rpx;
  265. margin-bottom: 20rpx;
  266. }
  267. .restart-btn {
  268. background: #4ECDC4;
  269. color: #1a1a2e;
  270. padding: 20rpx 60rpx;
  271. border-radius: 50rpx;
  272. border: none;
  273. margin-top: 30rpx;
  274. }
  275. </style>