memory-test.vue 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675
  1. <template>
  2. <view class="page">
  3. <!-- ============ 1. 说明页 ============ -->
  4. <view class="intro" v-if="step === 'intro'">
  5. <view class="intro-icon">
  6. <text class="intro-icon-text">🧠</text>
  7. </view>
  8. <text class="intro-title">记忆力测试</text>
  9. <text class="intro-desc">屏幕上会快速闪现一串数字,数字消失后,用下方数字键盘把它重新输入出来。每答对一关,数字就会增加一位,看看你能记住多少位!</text>
  10. <view class="intro-rule">
  11. <text class="rule-item">起始位数:3 位</text>
  12. <text class="rule-item">最高位数:8 位</text>
  13. <text class="rule-item">共 6 关</text>
  14. </view>
  15. <button class="primary-btn" @click="startTest">开始测试</button>
  16. </view>
  17. <!-- ============ 2. 测试中 ============ -->
  18. <view class="playing" v-if="step === 'playing'">
  19. <view class="progress-header">
  20. <text class="progress-level">第 {{ levelIndex }} 关</text>
  21. <text class="progress-digit">{{ digitCount }} 位数</text>
  22. </view>
  23. <view class="progress-track">
  24. <view class="progress-fill" :style="{ width: progressPct + '%' }"></view>
  25. </view>
  26. <!-- 展示阶段:数字闪现 + 计时条 -->
  27. <view class="show-area" v-if="phase === 'show'">
  28. <text class="show-hint">记住这串数字</text>
  29. <view class="digit-row">
  30. <view class="show-digit" v-for="(d, i) in showDigits" :key="getKey(d, i)">
  31. <text>{{ d }}</text>
  32. </view>
  33. </view>
  34. <view class="timer-bar-wrap">
  35. <view class="timer-bar" :style="{ width: timerProgress * 100 + '%' }"></view>
  36. </view>
  37. </view>
  38. <!-- 输入阶段:数字键盘 -->
  39. <view class="input-area" v-if="phase === 'input'">
  40. <text class="input-hint">请输入刚才的数字</text>
  41. <view class="input-display">
  42. <view
  43. class="input-cell"
  44. v-for="(c, i) in inputCells"
  45. :key="getCellKey(i)"
  46. :class="{ filled: i < userInput.length }"
  47. ></view>
  48. </view>
  49. <view class="pad">
  50. <view class="pad-row" v-for="(row, ri) in padRows" :key="getRowKey(ri)">
  51. <view
  52. class="pad-key"
  53. :class="{ 'pad-key-ctrl': key.ctrl }"
  54. v-for="(key, ci) in row"
  55. :key="getPadKey(key, ri, ci)"
  56. @click="onPadPress(key)"
  57. >
  58. <text>{{ key.v }}</text>
  59. </view>
  60. </view>
  61. </view>
  62. </view>
  63. <!-- 反馈动画 -->
  64. <view class="feedback feedback-correct" v-if="feedback === 'correct'">
  65. <text class="feedback-text">回答正确</text>
  66. </view>
  67. <view class="feedback feedback-wrong" v-if="feedback === 'wrong'">
  68. <text class="feedback-text">回答错误</text>
  69. </view>
  70. </view>
  71. <!-- ============ 3. 结果页 ============ -->
  72. <view class="result" v-if="step === 'result'">
  73. <view class="score-circle">
  74. <text class="score-num">{{ score }}</text>
  75. <text class="score-unit">分</text>
  76. </view>
  77. <view class="max-level">
  78. <text class="max-level-label">最高记忆</text>
  79. <text class="max-level-value">{{ maxLevel === 0 ? 3 : maxLevel }}位数</text>
  80. </view>
  81. <view class="dimension-tag">
  82. <text>记忆力</text>
  83. </view>
  84. <button class="primary-btn submit-btn" :disabled="submitting" @click="submitScore">
  85. {{ submitting ? '提交中...' : '提交成绩' }}
  86. </button>
  87. <button class="ghost-btn" @click="startTest">再测一次</button>
  88. </view>
  89. </view>
  90. </template>
  91. <script>
  92. import { submitCognitiveSelfTest } from '../../utils/api.js'
  93. export default {
  94. data() {
  95. return {
  96. step: 'intro', // intro | playing | result
  97. phase: 'show', // show | input
  98. digitCount: 3, // 当前关卡位数 3-8
  99. currentNumber: '', // 需要记忆的数字串
  100. userInput: [], // 用户已输入的数字
  101. inputLocked: false, // 反馈期间锁定键盘
  102. feedback: '', // '' | correct | wrong
  103. showStart: 0, // 数字展示开始时间戳
  104. showDuration: 0, // 数字展示时长(ms)
  105. timerProgress: 1, // 计时条剩余比例 0-1
  106. showTimer: null,
  107. correctTimer: null,
  108. wrongTimer: null,
  109. submitTimer: null,
  110. maxLevel: 0, // 成功记住的最高位数
  111. score: 0,
  112. submitting: false,
  113. memberId: '',
  114. padRows: [
  115. [{ v: 1 }, { v: 2 }, { v: 3 }],
  116. [{ v: 4 }, { v: 5 }, { v: 6 }],
  117. [{ v: 7 }, { v: 8 }, { v: 9 }],
  118. [{ v: '清除', ctrl: true }, { v: 0 }, { v: '确认', ctrl: true }]
  119. ]
  120. }
  121. },
  122. computed: {
  123. levelIndex() {
  124. return this.digitCount - 2
  125. },
  126. progressPct() {
  127. return (this.levelIndex / 6) * 100
  128. },
  129. showDigits() {
  130. return (this.currentNumber || '').split('')
  131. },
  132. inputCells() {
  133. var arr = []
  134. for (var i = 0; i < this.digitCount; i++) {
  135. arr.push(i)
  136. }
  137. return arr
  138. }
  139. },
  140. onLoad(options) {
  141. var id = (options && options.memberId) || uni.getStorageSync('currentChildId') || ''
  142. this.memberId = id
  143. },
  144. onUnload() {
  145. this.clearAllTimers()
  146. },
  147. methods: {
  148. // key 一律用方法调用,禁止模板内表达式
  149. getKey(d, i) {
  150. return 'digit-' + i
  151. },
  152. getCellKey(i) {
  153. return 'cell-' + i
  154. },
  155. getRowKey(ri) {
  156. return 'row-' + ri
  157. },
  158. getPadKey(key, ri, ci) {
  159. return 'pad-' + ri + '-' + ci
  160. },
  161. clearAllTimers() {
  162. if (this.showTimer) {
  163. clearInterval(this.showTimer)
  164. this.showTimer = null
  165. }
  166. if (this.correctTimer) {
  167. clearTimeout(this.correctTimer)
  168. this.correctTimer = null
  169. }
  170. if (this.wrongTimer) {
  171. clearTimeout(this.wrongTimer)
  172. this.wrongTimer = null
  173. }
  174. if (this.submitTimer) {
  175. clearTimeout(this.submitTimer)
  176. this.submitTimer = null
  177. }
  178. },
  179. startTest() {
  180. this.clearAllTimers()
  181. this.digitCount = 3
  182. this.maxLevel = 0
  183. this.score = 0
  184. this.userInput = []
  185. this.inputLocked = false
  186. this.feedback = ''
  187. this.step = 'playing'
  188. this.startRound()
  189. },
  190. startRound() {
  191. this.phase = 'show'
  192. this.userInput = []
  193. this.inputLocked = false
  194. this.feedback = ''
  195. // 生成随机数字串
  196. var num = ''
  197. for (var i = 0; i < this.digitCount; i++) {
  198. num += Math.floor(Math.random() * 10)
  199. }
  200. this.currentNumber = num
  201. this.showStart = Date.now()
  202. this.showDuration = Math.max(1.5, this.digitCount * 0.5) * 1000
  203. this.timerProgress = 1
  204. if (this.showTimer) {
  205. clearInterval(this.showTimer)
  206. this.showTimer = null
  207. }
  208. this.showTimer = setInterval(() => {
  209. var remaining = this.showDuration - (Date.now() - this.showStart)
  210. if (remaining <= 0) {
  211. if (this.showTimer) {
  212. clearInterval(this.showTimer)
  213. this.showTimer = null
  214. }
  215. this.timerProgress = 0
  216. this.phase = 'input'
  217. return
  218. }
  219. this.timerProgress = remaining / this.showDuration
  220. }, 50)
  221. },
  222. onPadPress(key) {
  223. if (this.feedback || this.inputLocked) return
  224. var v = key.v
  225. if (typeof v === 'number') {
  226. if (this.userInput.length < this.digitCount) {
  227. this.userInput.push(v)
  228. }
  229. } else if (v === '清除') {
  230. this.userInput.pop()
  231. } else if (v === '确认') {
  232. this.confirmInput()
  233. }
  234. },
  235. confirmInput() {
  236. if (this.userInput.length < this.digitCount) return
  237. this.inputLocked = true
  238. var entered = this.userInput.join('')
  239. if (entered === this.currentNumber) {
  240. this.feedback = 'correct'
  241. this.correctTimer = setTimeout(() => {
  242. this.correctTimer = null
  243. this.feedback = ''
  244. this.maxLevel = this.digitCount
  245. if (this.digitCount >= 8) {
  246. // 最高关卡已通过,测试结束
  247. this.endGame()
  248. } else {
  249. this.digitCount++
  250. this.startRound()
  251. }
  252. }, 700)
  253. } else {
  254. this.feedback = 'wrong'
  255. this.wrongTimer = setTimeout(() => {
  256. this.wrongTimer = null
  257. this.feedback = ''
  258. this.endGame()
  259. }, 900)
  260. }
  261. },
  262. endGame() {
  263. this.clearAllTimers()
  264. // 按最高成功记住的位数映射得分
  265. var score = 100
  266. if (this.maxLevel === 0) {
  267. score = 40
  268. } else if (this.maxLevel === 3) {
  269. score = 55
  270. } else if (this.maxLevel === 4) {
  271. score = 70
  272. } else if (this.maxLevel === 5) {
  273. score = 82
  274. } else if (this.maxLevel === 6) {
  275. score = 92
  276. }
  277. this.score = score
  278. this.step = 'result'
  279. },
  280. submitScore() {
  281. if (this.submitting) return
  282. var memberId = this.memberId
  283. if (!memberId) {
  284. uni.showToast({ title: '未找到测评成员', icon: 'none' })
  285. return
  286. }
  287. this.submitting = true
  288. submitCognitiveSelfTest(memberId, 'memoryScore', this.score)
  289. .then(() => {
  290. uni.showToast({ title: '提交成功', icon: 'success' })
  291. this.submitTimer = setTimeout(() => {
  292. var pages = getCurrentPages()
  293. if (pages.length > 1) {
  294. uni.navigateBack()
  295. } else {
  296. uni.reLaunch({ url: '/pages/index/index' })
  297. }
  298. }, 800)
  299. })
  300. .catch(() => {
  301. // request() 已自动弹出失败提示,这里只需恢复按钮状态
  302. this.submitting = false
  303. })
  304. }
  305. }
  306. }
  307. </script>
  308. <style scoped>
  309. .page {
  310. min-height: 100vh;
  311. background: linear-gradient(135deg, #1a1a2e 0%, #16213e 100%);
  312. display: flex;
  313. flex-direction: column;
  314. align-items: center;
  315. padding: 60rpx 40rpx 80rpx;
  316. box-sizing: border-box;
  317. }
  318. /* ---------- 通用按钮 ---------- */
  319. .primary-btn {
  320. margin-top: 60rpx;
  321. width: 480rpx;
  322. height: 96rpx;
  323. line-height: 96rpx;
  324. border-radius: 48rpx;
  325. background: linear-gradient(135deg, #6366f1 0%, #818cf8 100%);
  326. color: #ffffff;
  327. font-size: 34rpx;
  328. font-weight: 600;
  329. border: none;
  330. box-shadow: 0 8rpx 24rpx rgba(99, 102, 241, 0.35);
  331. }
  332. .primary-btn::after {
  333. border: none;
  334. }
  335. .primary-btn[disabled] {
  336. opacity: 0.6;
  337. color: #ffffff;
  338. background: linear-gradient(135deg, #6366f1 0%, #818cf8 100%);
  339. }
  340. .ghost-btn {
  341. margin-top: 30rpx;
  342. width: 480rpx;
  343. height: 96rpx;
  344. line-height: 96rpx;
  345. border-radius: 48rpx;
  346. background: transparent;
  347. color: #a5b4fc;
  348. font-size: 32rpx;
  349. border: 2rpx solid rgba(99, 102, 241, 0.5);
  350. }
  351. .ghost-btn::after {
  352. border: none;
  353. }
  354. /* ---------- 说明页 ---------- */
  355. .intro {
  356. display: flex;
  357. flex-direction: column;
  358. align-items: center;
  359. width: 100%;
  360. padding-top: 80rpx;
  361. }
  362. .intro-icon {
  363. width: 160rpx;
  364. height: 160rpx;
  365. border-radius: 50%;
  366. background: rgba(99, 102, 241, 0.15);
  367. display: flex;
  368. align-items: center;
  369. justify-content: center;
  370. margin-bottom: 40rpx;
  371. }
  372. .intro-icon-text {
  373. font-size: 80rpx;
  374. }
  375. .intro-title {
  376. font-size: 48rpx;
  377. font-weight: bold;
  378. color: #ffffff;
  379. margin-bottom: 30rpx;
  380. }
  381. .intro-desc {
  382. font-size: 30rpx;
  383. color: rgba(255, 255, 255, 0.75);
  384. line-height: 1.7;
  385. text-align: center;
  386. padding: 0 20rpx;
  387. }
  388. .intro-rule {
  389. display: flex;
  390. flex-direction: row;
  391. flex-wrap: wrap;
  392. justify-content: center;
  393. margin-top: 40rpx;
  394. }
  395. .rule-item {
  396. font-size: 24rpx;
  397. color: #a5b4fc;
  398. background: rgba(99, 102, 241, 0.12);
  399. border-radius: 24rpx;
  400. padding: 10rpx 24rpx;
  401. margin: 8rpx 12rpx;
  402. }
  403. /* ---------- 测试中 ---------- */
  404. .playing {
  405. width: 100%;
  406. display: flex;
  407. flex-direction: column;
  408. align-items: center;
  409. }
  410. .progress-header {
  411. display: flex;
  412. flex-direction: column;
  413. align-items: center;
  414. margin-top: 20rpx;
  415. }
  416. .progress-level {
  417. font-size: 44rpx;
  418. font-weight: bold;
  419. color: #ffffff;
  420. }
  421. .progress-digit {
  422. font-size: 26rpx;
  423. color: #a5b4fc;
  424. margin-top: 8rpx;
  425. }
  426. .progress-track {
  427. width: 480rpx;
  428. height: 8rpx;
  429. border-radius: 4rpx;
  430. background: rgba(255, 255, 255, 0.1);
  431. margin-top: 24rpx;
  432. overflow: hidden;
  433. }
  434. .progress-fill {
  435. height: 100%;
  436. border-radius: 4rpx;
  437. background: linear-gradient(90deg, #6366f1, #818cf8);
  438. transition: width 0.3s ease;
  439. }
  440. /* 展示阶段:数字闪现 */
  441. .show-area {
  442. display: flex;
  443. flex-direction: column;
  444. align-items: center;
  445. width: 100%;
  446. padding-top: 120rpx;
  447. }
  448. .show-hint {
  449. font-size: 26rpx;
  450. color: rgba(255, 255, 255, 0.5);
  451. margin-bottom: 60rpx;
  452. }
  453. .digit-row {
  454. display: flex;
  455. flex-direction: row;
  456. justify-content: center;
  457. align-items: center;
  458. }
  459. .show-digit {
  460. width: 76rpx;
  461. height: 140rpx;
  462. margin: 0 4rpx;
  463. display: flex;
  464. align-items: center;
  465. justify-content: center;
  466. background: rgba(255, 255, 255, 0.06);
  467. border-radius: 16rpx;
  468. }
  469. .show-digit text {
  470. font-size: 120rpx;
  471. font-weight: bold;
  472. color: #ffffff;
  473. }
  474. .timer-bar-wrap {
  475. width: 480rpx;
  476. height: 12rpx;
  477. border-radius: 6rpx;
  478. background: rgba(255, 255, 255, 0.1);
  479. overflow: hidden;
  480. margin-top: 60rpx;
  481. }
  482. .timer-bar {
  483. height: 100%;
  484. border-radius: 6rpx;
  485. background: linear-gradient(90deg, #6366f1, #818cf8);
  486. transition: width 0.05s linear;
  487. }
  488. /* 输入阶段:数字键盘 */
  489. .input-area {
  490. display: flex;
  491. flex-direction: column;
  492. align-items: center;
  493. width: 100%;
  494. padding-top: 60rpx;
  495. }
  496. .input-hint {
  497. font-size: 26rpx;
  498. color: rgba(255, 255, 255, 0.5);
  499. margin-bottom: 50rpx;
  500. }
  501. .input-display {
  502. display: flex;
  503. flex-direction: row;
  504. justify-content: center;
  505. min-height: 100rpx;
  506. margin-bottom: 40rpx;
  507. }
  508. .input-cell {
  509. width: 76rpx;
  510. height: 100rpx;
  511. margin: 0 4rpx;
  512. border-radius: 16rpx;
  513. background: rgba(255, 255, 255, 0.04);
  514. border: 2rpx solid rgba(255, 255, 255, 0.15);
  515. box-sizing: border-box;
  516. }
  517. .input-cell.filled {
  518. background: rgba(99, 102, 241, 0.5);
  519. border-color: #6366f1;
  520. }
  521. .pad {
  522. width: 100%;
  523. max-width: 620rpx;
  524. }
  525. .pad-row {
  526. display: flex;
  527. flex-direction: row;
  528. justify-content: space-between;
  529. margin-bottom: 20rpx;
  530. }
  531. .pad-key {
  532. width: 180rpx;
  533. height: 108rpx;
  534. border-radius: 20rpx;
  535. background: rgba(255, 255, 255, 0.06);
  536. border: 2rpx solid rgba(255, 255, 255, 0.08);
  537. display: flex;
  538. align-items: center;
  539. justify-content: center;
  540. box-sizing: border-box;
  541. }
  542. .pad-key text {
  543. font-size: 44rpx;
  544. color: #ffffff;
  545. font-weight: 500;
  546. }
  547. .pad-key:active {
  548. border-color: #6366f1;
  549. background: rgba(99, 102, 241, 0.2);
  550. }
  551. .pad-key-ctrl {
  552. background: rgba(255, 255, 255, 0.03);
  553. }
  554. .pad-key-ctrl text {
  555. font-size: 32rpx;
  556. color: #a5b4fc;
  557. }
  558. /* 反馈动画 */
  559. .feedback {
  560. position: fixed;
  561. left: 0;
  562. right: 0;
  563. top: 0;
  564. bottom: 0;
  565. display: flex;
  566. align-items: center;
  567. justify-content: center;
  568. z-index: 99;
  569. animation: feedbackFade 0.7s ease both;
  570. }
  571. .feedback-correct {
  572. background: rgba(16, 185, 129, 0.3);
  573. }
  574. .feedback-wrong {
  575. background: rgba(239, 68, 68, 0.3);
  576. }
  577. .feedback-text {
  578. font-size: 56rpx;
  579. font-weight: bold;
  580. color: #ffffff;
  581. padding: 24rpx 60rpx;
  582. border-radius: 24rpx;
  583. background: rgba(0, 0, 0, 0.35);
  584. }
  585. @keyframes feedbackFade {
  586. 0% {
  587. opacity: 0;
  588. }
  589. 20% {
  590. opacity: 1;
  591. }
  592. 80% {
  593. opacity: 1;
  594. }
  595. 100% {
  596. opacity: 0;
  597. }
  598. }
  599. /* ---------- 结果页 ---------- */
  600. .result {
  601. display: flex;
  602. flex-direction: column;
  603. align-items: center;
  604. width: 100%;
  605. padding-top: 100rpx;
  606. }
  607. .score-circle {
  608. width: 280rpx;
  609. height: 280rpx;
  610. border-radius: 50%;
  611. background: linear-gradient(135deg, #6366f1 0%, #818cf8 100%);
  612. display: flex;
  613. flex-direction: column;
  614. align-items: center;
  615. justify-content: center;
  616. box-shadow: 0 12rpx 40rpx rgba(99, 102, 241, 0.45);
  617. }
  618. .score-num {
  619. font-size: 96rpx;
  620. font-weight: bold;
  621. color: #ffffff;
  622. line-height: 1;
  623. }
  624. .score-unit {
  625. font-size: 28rpx;
  626. color: rgba(255, 255, 255, 0.8);
  627. margin-top: 8rpx;
  628. }
  629. .max-level {
  630. display: flex;
  631. flex-direction: row;
  632. align-items: center;
  633. margin-top: 60rpx;
  634. }
  635. .max-level-label {
  636. font-size: 30rpx;
  637. color: rgba(255, 255, 255, 0.6);
  638. margin-right: 16rpx;
  639. }
  640. .max-level-value {
  641. font-size: 40rpx;
  642. font-weight: bold;
  643. color: #ffffff;
  644. }
  645. .dimension-tag {
  646. margin-top: 24rpx;
  647. font-size: 26rpx;
  648. color: #a5b4fc;
  649. background: rgba(99, 102, 241, 0.15);
  650. border: 2rpx solid rgba(99, 102, 241, 0.4);
  651. border-radius: 24rpx;
  652. padding: 8rpx 28rpx;
  653. }
  654. .submit-btn {
  655. margin-top: 80rpx;
  656. }
  657. </style>