1a2b.vue 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310
  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>剩余次数: {{ remainingGuesses }}</text>
  7. </view>
  8. </view>
  9. <view class="instructions" v-if="!isPlaying">
  10. <text class="info-text">电脑会生成一个4位不重复数字</text>
  11. <text class="info-text">A = 数字和位置都正确</text>
  12. <text class="info-text">B = 数字正确但位置错误</text>
  13. <text class="info-text">请在{{ maxGuesses }}次内猜出正确答案</text>
  14. </view>
  15. <view class="input-area" v-if="!isPlaying">
  16. <input
  17. class="guess-input"
  18. type="number"
  19. v-model="userGuess"
  20. maxlength="4"
  21. placeholder="输入4位不重复数字"
  22. />
  23. <button class="start-btn" @click="startGame">开始游戏</button>
  24. </view>
  25. <view class="history" v-if="history.length > 0">
  26. <text class="history-title">猜测历史:</text>
  27. <view class="history-item" v-for="(item, index) in history" :key="index">
  28. <text>{{ index + 1 }}. {{ item.guess }} - {{ item.result }}</text>
  29. </view>
  30. </view>
  31. <view class="input-area" v-if="isPlaying && !gameOver">
  32. <input
  33. class="guess-input"
  34. type="number"
  35. v-model="userGuess"
  36. maxlength="4"
  37. placeholder="输入猜测"
  38. />
  39. <button class="guess-btn" @click="makeGuess">猜测</button>
  40. </view>
  41. <view class="result-modal" v-if="gameOver">
  42. <view class="result-content">
  43. <text class="result-title">{{ isWin ? '猜对了!' : '游戏结束' }}</text>
  44. <text class="result-info">答案是: {{ answer }}</text>
  45. <text class="result-score">获得积分: {{ score }}</text>
  46. <button class="restart-btn" @click="resetGame">再玩一次</button>
  47. </view>
  48. </view>
  49. </view>
  50. </template>
  51. <script>
  52. import config from '@/config.js'
  53. import { completeMiniGame, getChildren } from '@/utils/api.js'
  54. import fontScaleMixin from '@/mixins/font-scale.js'
  55. export default {
  56. mixins: [fontScaleMixin],
  57. data() {
  58. return {
  59. answer: '',
  60. userGuess: '',
  61. isPlaying: false,
  62. gameOver: false,
  63. isWin: false,
  64. maxGuesses: 10,
  65. remainingGuesses: 10,
  66. history: [],
  67. score: 0,
  68. time: 0,
  69. timer: null,
  70. memberId: null,
  71. taskId: null,
  72. pointsEarned: 0,
  73. newBalance: 0
  74. }
  75. },
  76. onLoad(options) {
  77. if (options.taskId) {
  78. this.taskId = parseInt(options.taskId)
  79. }
  80. if (options.memberId) {
  81. this.memberId = parseInt(options.memberId)
  82. }
  83. this.loadChildId()
  84. },
  85. onUnload() {
  86. if (this.timer) clearInterval(this.timer)
  87. },
  88. methods: {
  89. async loadChildId() {
  90. try {
  91. if (!this.memberId) {
  92. const res = await getChildren()
  93. if (res.data && res.data.length > 0) {
  94. this.memberId = res.data[0].id
  95. }
  96. }
  97. } catch (e) {
  98. console.error('获取成员ID失败', e)
  99. }
  100. },
  101. generateAnswer() {
  102. let digits = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9']
  103. let result = ''
  104. for (let i = 0; i < 4; i++) {
  105. const idx = Math.floor(Math.random() * digits.length)
  106. result += digits[idx]
  107. digits.splice(idx, 1)
  108. }
  109. return result
  110. },
  111. startGame() {
  112. if (this.userGuess.length !== 4 || !this.isUnique(this.userGuess)) {
  113. uni.showToast({ title: '请输入4位不重复数字', icon: 'none' })
  114. return
  115. }
  116. this.answer = this.generateAnswer()
  117. this.isPlaying = true
  118. this.gameOver = false
  119. this.remainingGuesses = this.maxGuesses
  120. this.history = []
  121. this.score = 0
  122. this.timer = setInterval(() => { this.time++ }, 1000)
  123. },
  124. isUnique(str) {
  125. return new Set(str).size === str.length
  126. },
  127. checkGuess(guess) {
  128. let a = 0, b = 0
  129. for (let i = 0; i < 4; i++) {
  130. if (guess[i] === this.answer[i]) {
  131. a++
  132. } else if (this.answer.includes(guess[i])) {
  133. b++
  134. }
  135. }
  136. return `${a}A${b}B`
  137. },
  138. makeGuess() {
  139. if (this.userGuess.length !== 4 || !this.isUnique(this.userGuess)) {
  140. uni.showToast({ title: '请输入4位不重复数字', icon: 'none' })
  141. return
  142. }
  143. const result = this.checkGuess(this.userGuess)
  144. this.history.push({ guess: this.userGuess, result })
  145. this.remainingGuesses--
  146. if (result === '4A0B') {
  147. this.score = Math.max(100 - (this.maxGuesses - this.remainingGuesses) * 8, 60)
  148. this.gameOver = true
  149. this.isWin = true
  150. clearInterval(this.timer)
  151. this.submitScore()
  152. } else if (this.remainingGuesses === 0) {
  153. this.gameOver = true
  154. this.isWin = false
  155. this.score = 30
  156. this.submitScore()
  157. }
  158. this.userGuess = ''
  159. },
  160. async submitScore() {
  161. if (!this.memberId) return
  162. try {
  163. if (this.taskId) {
  164. const token = uni.getStorageSync('token')
  165. await uni.request({
  166. url: config.api(`/api/tasks/${this.taskId}/complete-minigame`),
  167. method: 'POST',
  168. header: {
  169. 'Authorization': token ? `Bearer ${token}` : '',
  170. 'Content-Type': 'application/json'
  171. },
  172. data: {
  173. memberId: this.memberId,
  174. completionTime: this.time,
  175. score: this.score
  176. }
  177. })
  178. }
  179. const res = await completeMiniGame(this.memberId, '1a2b', this.time, this.score)
  180. if (res.code === 200) {
  181. uni.showToast({ title: `获得${res.data.pointsEarned}积分!`, icon: 'success' })
  182. }
  183. } catch (e) {
  184. console.error('提交成绩失败', e)
  185. }
  186. },
  187. resetGame() {
  188. this.isPlaying = false
  189. this.gameOver = false
  190. this.userGuess = ''
  191. this.history = []
  192. this.time = 0
  193. if (this.timer) clearInterval(this.timer)
  194. }
  195. }
  196. }
  197. </script>
  198. <style scoped lang="scss">
  199. .game-container {
  200. min-height: 100vh;
  201. background: linear-gradient(135deg, $app-ink-night 0%, $app-ink-night-2 100%);
  202. padding: $uni-space-page;
  203. }
  204. .game-header {
  205. text-align: center;
  206. margin-bottom: 40rpx;
  207. }
  208. .game-title {
  209. font-size: calc(48rpx * var(--font-scale, 1));
  210. color: $app-surface;
  211. display: block;
  212. margin-bottom: 20rpx;
  213. }
  214. .game-stats {
  215. color: $facade-text-faint;
  216. font-size: calc(28rpx * var(--font-scale, 1));
  217. }
  218. .instructions {
  219. background: rgba(255,255,255,0.1);
  220. padding: $uni-space-page;
  221. border-radius: 16rpx;
  222. margin-bottom: $uni-space-lg;
  223. }
  224. .info-text {
  225. color: $text-ghost-soft;
  226. font-size: calc(28rpx * var(--font-scale, 1));
  227. display: block;
  228. margin-bottom: 10rpx;
  229. }
  230. .input-area {
  231. margin: $uni-space-lg 0;
  232. }
  233. .guess-input {
  234. background: rgba(255,255,255,0.1);
  235. border: 1rpx solid rgba(255,255,255,0.2);
  236. border-radius: 16rpx;
  237. padding: 20rpx;
  238. color: $app-surface;
  239. font-size: calc(36rpx * var(--font-scale, 1));
  240. text-align: center;
  241. margin-bottom: 20rpx;
  242. }
  243. .start-btn, .guess-btn {
  244. background: $flat-teal;
  245. color: $app-ink-night;
  246. border-radius: 16rpx;
  247. padding: 20rpx;
  248. }
  249. .history {
  250. background: rgba(255,255,255,0.05);
  251. padding: 20rpx;
  252. border-radius: 16rpx;
  253. margin: $uni-space-lg 0;
  254. }
  255. .history-title {
  256. color: $facade-text-faint;
  257. font-size: calc(28rpx * var(--font-scale, 1));
  258. display: block;
  259. margin-bottom: 16rpx;
  260. }
  261. .history-item {
  262. color: $facade-text-ghost;
  263. font-size: calc(28rpx * var(--font-scale, 1));
  264. padding: 10rpx 0;
  265. border-bottom: 1rpx solid rgba(255,255,255,0.1);
  266. }
  267. .result-modal {
  268. position: fixed;
  269. top: 0;
  270. left: 0;
  271. right: 0;
  272. bottom: 0;
  273. background: rgba(0,0,0,0.8);
  274. display: flex;
  275. align-items: center;
  276. justify-content: center;
  277. }
  278. .result-content {
  279. background: $game-ink;
  280. padding: 60rpx;
  281. border-radius: 24rpx;
  282. text-align: center;
  283. }
  284. .result-title {
  285. font-size: calc(48rpx * var(--font-scale, 1));
  286. color: $accent-yellow-light;
  287. display: block;
  288. margin-bottom: $uni-space-lg;
  289. }
  290. .result-info, .result-score {
  291. color: $app-surface;
  292. font-size: calc(32rpx * var(--font-scale, 1));
  293. display: block;
  294. margin-bottom: 20rpx;
  295. }
  296. .restart-btn {
  297. background: $flat-teal;
  298. color: $app-ink-night;
  299. padding: 20rpx 60rpx;
  300. margin-top: $uni-space-lg;
  301. }
  302. </style>