memory-test.vue 17 KB

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