schulte.vue 7.0 KB

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