speed-test.vue 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575
  1. <template>
  2. <view class="page-container" :style="{ '--font-scale': fontScale }">
  3. <!-- 说明页 -->
  4. <view v-if="step === 'instruction'" class="step-wrap">
  5. <text class="title">加工速度测试</text>
  6. <text class="subtitle">符号匹配 · 反应速度</text>
  7. <view class="desc-card">
  8. <text class="desc-title">测试说明</text>
  9. <text class="desc-line">1. 先记住出现的符号</text>
  10. <text class="desc-line">2. 从 4 个候选中点出相同的符号</text>
  11. <text class="desc-line">3. 越快越准,得分越高</text>
  12. <text class="desc-line">共 10 轮,每轮限时 5 秒</text>
  13. </view>
  14. <button class="btn-primary" @click="startTest">开始测试</button>
  15. </view>
  16. <!-- 测试中 -->
  17. <view v-if="step === 'playing'" class="step-wrap">
  18. <view class="round-bar">
  19. <text class="round-text">第 {{ roundIndex }}/10</text>
  20. <view class="timer-track">
  21. <view class="timer-fill" :style="'width:' + timerPercent + '%'"></view>
  22. </view>
  23. </view>
  24. <!-- 记忆阶段:展示目标符号 -->
  25. <view v-if="phase === 'show'" class="show-area">
  26. <text class="phase-hint">记住这个符号</text>
  27. <view class="symbol-box" :style="'width:' + symbolBoxSize + 'px;height:' + symbolBoxSize + 'px'">
  28. <text class="symbol-big" :style="'font-size:' + symbolFontSize + 'px'">{{ currentTarget }}</text>
  29. </view>
  30. </view>
  31. <!-- 选择阶段:4 选 1 -->
  32. <view v-else class="choice-area">
  33. <text class="phase-hint">找出刚才的符号</text>
  34. <view class="choice-grid">
  35. <view
  36. v-for="(sym, idx) in currentOptions"
  37. :key="idx"
  38. :class="'choice-cell' + (selIndex === idx ? (roundCorrect ? ' cell-hit' : ' cell-miss') : '')"
  39. :style="'width:' + cellSize + 'px;height:' + cellSize + 'px'"
  40. @click="handleChoice(sym)"
  41. >
  42. <text class="symbol-opt" :style="'font-size:' + symbolFontSize + 'px'">{{ sym }}</text>
  43. </view>
  44. </view>
  45. </view>
  46. </view>
  47. <!-- 结果页 -->
  48. <view v-if="step === 'result'" class="step-wrap">
  49. <view class="score-circle">
  50. <text class="score-num">{{ score }}</text>
  51. <text class="score-label">分</text>
  52. </view>
  53. <view class="result-stats">
  54. <view class="stat-item">
  55. <text class="stat-value">{{ correctCount }}/10</text>
  56. <text class="stat-label">答对</text>
  57. </view>
  58. <view class="stat-item">
  59. <text class="stat-value">{{ avgTimeText }}</text>
  60. <text class="stat-label">平均反应</text>
  61. </view>
  62. <view class="stat-item">
  63. <text class="stat-value">+{{ speedBonus }}</text>
  64. <text class="stat-label">速度加分</text>
  65. </view>
  66. </view>
  67. <view class="dimension-tag">加工速度</view>
  68. <button class="btn-primary" :disabled="submitting" @click="submitScore">
  69. {{ submitting ? '提交中...' : '提交成绩' }}
  70. </button>
  71. <button class="btn-secondary" @click="startTest">再测一次</button>
  72. </view>
  73. </view>
  74. </template>
  75. <script>
  76. import { submitCognitiveSelfTest } from '../../utils/api.js'
  77. import fontScaleMixin from '@/mixins/font-scale.js'
  78. export default {
  79. mixins: [fontScaleMixin],
  80. data() {
  81. return {
  82. SYMBOLS: ['★', '●', '■', '▲', '◆', '◇', '○', '△'],
  83. SYMBOL_PAIRS: [['★', '◆'], ['●', '○'], ['■', '◇'], ['▲', '△']],
  84. step: 'instruction',
  85. phase: 'show',
  86. roundIndex: 1,
  87. currentTarget: '',
  88. currentOptions: [],
  89. roundStartMs: 0,
  90. roundElapsed: 0,
  91. timerPercent: 0,
  92. locked: false,
  93. selIndex: -1,
  94. roundCorrect: false,
  95. results: [],
  96. correctCount: 0,
  97. avgTimeText: '0.00s',
  98. speedBonus: 0,
  99. score: 0,
  100. submitting: false,
  101. memberId: '',
  102. showTimer: null,
  103. roundTimer: null,
  104. windowWidth: 375,
  105. symbolFontSize: 75,
  106. symbolBoxSize: 150,
  107. cellSize: 112
  108. }
  109. },
  110. onLoad(options) {
  111. var win = uni.getWindowInfo()
  112. this.windowWidth = (win && win.windowWidth) || 375
  113. this.symbolFontSize = Math.round(this.windowWidth * 0.2)
  114. this.symbolBoxSize = Math.round(this.windowWidth * 0.4)
  115. this.cellSize = Math.round(this.windowWidth * 0.3)
  116. this.memberId = (options && options.memberId) || uni.getStorageSync('currentChildId') || ''
  117. },
  118. onUnload() {
  119. this.clearTimers()
  120. },
  121. methods: {
  122. clearTimers() {
  123. if (this.showTimer) {
  124. clearTimeout(this.showTimer)
  125. this.showTimer = null
  126. }
  127. if (this.roundTimer) {
  128. clearInterval(this.roundTimer)
  129. this.roundTimer = null
  130. }
  131. },
  132. startTest() {
  133. this.clearTimers()
  134. this.results = []
  135. this.roundIndex = 1
  136. this.submitting = false
  137. this.selIndex = -1
  138. this.timerPercent = 0
  139. this.step = 'playing'
  140. this.startRound()
  141. },
  142. // 生成一轮:从符号对中随机取目标,3 个干扰项 + 配对符号
  143. generateRound() {
  144. var pair = this.SYMBOL_PAIRS[Math.floor(Math.random() * this.SYMBOL_PAIRS.length)]
  145. var target = pair[Math.floor(Math.random() * 2)]
  146. var pairMate = target === pair[0] ? pair[1] : pair[0]
  147. var others = []
  148. for (var i = 0; i < this.SYMBOLS.length; i++) {
  149. if (this.SYMBOLS[i] !== target && this.SYMBOLS[i] !== pairMate) {
  150. others.push(this.SYMBOLS[i])
  151. }
  152. }
  153. this.shuffle(others)
  154. var options = [target, pairMate, others[0], others[1]]
  155. this.shuffle(options)
  156. return { target: target, options: options }
  157. },
  158. shuffle(arr) {
  159. for (var i = arr.length - 1; i > 0; i--) {
  160. var j = Math.floor(Math.random() * (i + 1))
  161. var tmp = arr[i]
  162. arr[i] = arr[j]
  163. arr[j] = tmp
  164. }
  165. },
  166. startRound() {
  167. this.clearTimers()
  168. this.phase = 'show'
  169. this.locked = false
  170. this.selIndex = -1
  171. this.roundElapsed = 0
  172. this.timerPercent = 0
  173. var round = this.generateRound()
  174. this.currentTarget = round.target
  175. this.currentOptions = round.options
  176. // 记忆阶段:展示 1 秒后进入选择
  177. var that = this
  178. this.showTimer = setTimeout(function () {
  179. that.phase = 'choice'
  180. that.roundStartMs = Date.now()
  181. that.roundTimer = setInterval(function () {
  182. that.tickRound()
  183. }, 100)
  184. }, 1000)
  185. },
  186. tickRound() {
  187. this.roundElapsed += 100
  188. this.timerPercent = Math.min(this.roundElapsed / 5000 * 100, 100)
  189. if (this.roundElapsed >= 5000) {
  190. // 超时未作答,记错
  191. this.finishRound()
  192. }
  193. },
  194. handleChoice(sym) {
  195. if (this.phase !== 'choice' || this.locked) return
  196. this.locked = true
  197. var isCorrect = sym === this.currentTarget
  198. this.selIndex = this.currentOptions.indexOf(sym)
  199. this.roundCorrect = isCorrect
  200. this.results.push({
  201. correct: isCorrect,
  202. timeMs: Date.now() - this.roundStartMs
  203. })
  204. if (this.roundTimer) {
  205. clearInterval(this.roundTimer)
  206. this.roundTimer = null
  207. }
  208. var that = this
  209. setTimeout(function () {
  210. that.nextRound()
  211. }, 450)
  212. },
  213. // 超时:timeMs 记 0(不计入平均反应时间)
  214. finishRound() {
  215. if (this.locked) return
  216. this.locked = true
  217. this.results.push({ correct: false, timeMs: 0 })
  218. if (this.roundTimer) {
  219. clearInterval(this.roundTimer)
  220. this.roundTimer = null
  221. }
  222. var that = this
  223. setTimeout(function () {
  224. that.nextRound()
  225. }, 300)
  226. },
  227. nextRound() {
  228. if (this.roundIndex >= 10) {
  229. this.finishTest()
  230. } else {
  231. this.roundIndex++
  232. this.startRound()
  233. }
  234. },
  235. finishTest() {
  236. this.clearTimers()
  237. this.step = 'result'
  238. this.computeResult()
  239. },
  240. computeResult() {
  241. var correct = 0
  242. var totalMs = 0
  243. var answered = 0
  244. for (var i = 0; i < this.results.length; i++) {
  245. var r = this.results[i]
  246. if (r.correct) correct++
  247. if (r.timeMs > 0) {
  248. totalMs += r.timeMs
  249. answered++
  250. }
  251. }
  252. this.correctCount = correct
  253. var avgMs = answered > 0 ? totalMs / answered : 5000
  254. this.avgTimeText = (avgMs / 1000).toFixed(2) + 's'
  255. var avgSec = avgMs / 1000
  256. var bonus = 5
  257. if (avgSec < 1) {
  258. bonus = 20
  259. } else if (avgSec < 2) {
  260. bonus = 17
  261. } else if (avgSec < 3) {
  262. bonus = 14
  263. } else if (avgSec < 4) {
  264. bonus = 10
  265. }
  266. this.speedBonus = bonus
  267. var total = Math.round(correct / 10 * 80 + bonus)
  268. this.score = Math.min(Math.max(total, 0), 100)
  269. },
  270. submitScore() {
  271. if (this.submitting) return
  272. var memberId = this.memberId || uni.getStorageSync('currentChildId') || ''
  273. if (!memberId) {
  274. uni.showToast({ title: '请先选择家庭成员', icon: 'none' })
  275. return
  276. }
  277. this.submitting = true
  278. var that = this
  279. submitCognitiveSelfTest(memberId, 'processingSpeedScore', this.score)
  280. .then(function () {
  281. uni.showToast({ title: '提交成功', icon: 'success' })
  282. setTimeout(function () {
  283. uni.navigateBack()
  284. }, 800)
  285. })
  286. .catch(function () {
  287. that.submitting = false
  288. uni.showToast({ title: '提交失败,请重试', icon: 'none' })
  289. })
  290. }
  291. }
  292. }
  293. </script>
  294. <style scoped lang="scss">
  295. .page-container {
  296. min-height: 100vh;
  297. background: linear-gradient(135deg, $app-ink-night 0%, $app-ink-night-2 100%);
  298. padding: 40rpx 50rpx;
  299. }
  300. .step-wrap {
  301. display: flex;
  302. flex-direction: column;
  303. align-items: center;
  304. }
  305. .title {
  306. font-size: calc($uni-font-size-3xl * var(--font-scale, 1));
  307. font-weight: bold;
  308. color: $app-surface;
  309. margin-top: 40rpx;
  310. }
  311. .subtitle {
  312. font-size: calc(28rpx * var(--font-scale, 1));
  313. color: rgba(255, 255, 255, 0.5);
  314. margin-top: 16rpx;
  315. }
  316. .desc-card {
  317. width: 100%;
  318. background: rgba(255, 255, 255, 0.06);
  319. border-radius: 24rpx;
  320. padding: 40rpx;
  321. margin: 60rpx 0;
  322. }
  323. .desc-title {
  324. display: block;
  325. font-size: calc(32rpx * var(--font-scale, 1));
  326. font-weight: bold;
  327. color: $brand-primary;
  328. margin-bottom: 24rpx;
  329. }
  330. .desc-line {
  331. display: block;
  332. font-size: calc(28rpx * var(--font-scale, 1));
  333. color: rgba(255, 255, 255, 0.85);
  334. line-height: 1.9;
  335. }
  336. /* 按钮 */
  337. .btn-primary {
  338. width: 70%;
  339. background: linear-gradient(135deg, $brand-primary, $brand-primary-light);
  340. color: $app-surface;
  341. font-size: calc(34rpx * var(--font-scale, 1));
  342. border-radius: 24rpx;
  343. border: none;
  344. margin-top: 20rpx;
  345. }
  346. .btn-primary::after {
  347. border: none;
  348. }
  349. .btn-primary[disabled] {
  350. opacity: 0.6;
  351. }
  352. .btn-secondary {
  353. width: 70%;
  354. background: transparent;
  355. color: $brand-primary;
  356. font-size: calc(32rpx * var(--font-scale, 1));
  357. border-radius: 24rpx;
  358. border: 2rpx solid $brand-primary;
  359. margin-top: $uni-space-lg;
  360. }
  361. .btn-secondary::after {
  362. border: none;
  363. }
  364. /* 测试中 */
  365. .round-bar {
  366. width: 100%;
  367. display: flex;
  368. flex-direction: column;
  369. align-items: center;
  370. margin-top: $uni-space-lg;
  371. }
  372. .round-text {
  373. font-size: calc(34rpx * var(--font-scale, 1));
  374. font-weight: bold;
  375. color: $app-surface;
  376. margin-bottom: 20rpx;
  377. }
  378. .timer-track {
  379. width: 100%;
  380. height: 10rpx;
  381. background: rgba(255, 255, 255, 0.12);
  382. border-radius: 8rpx;
  383. overflow: hidden;
  384. }
  385. .timer-fill {
  386. height: 100%;
  387. border-radius: 8rpx;
  388. background: linear-gradient(90deg, $brand-primary, $brand-primary-light);
  389. transition: width 0.1s linear;
  390. }
  391. .phase-hint {
  392. font-size: calc(30rpx * var(--font-scale, 1));
  393. color: rgba(255, 255, 255, 0.6);
  394. margin: 70rpx 0 40rpx;
  395. }
  396. /* 记忆阶段 */
  397. .show-area {
  398. display: flex;
  399. flex-direction: column;
  400. align-items: center;
  401. width: 100%;
  402. }
  403. .symbol-box {
  404. display: flex;
  405. align-items: center;
  406. justify-content: center;
  407. background: rgba(255, 255, 255, 0.08);
  408. border: 2rpx solid rgba(249, 115, 22, 0.4);
  409. border-radius: 24rpx;
  410. box-shadow: 0 0 40rpx rgba(249, 115, 22, 0.15);
  411. }
  412. .symbol-big {
  413. color: $app-surface;
  414. font-weight: bold;
  415. line-height: 1;
  416. }
  417. /* 选择阶段 */
  418. .choice-area {
  419. display: flex;
  420. flex-direction: column;
  421. align-items: center;
  422. width: 100%;
  423. }
  424. .choice-grid {
  425. width: 100%;
  426. display: flex;
  427. flex-wrap: wrap;
  428. justify-content: space-between;
  429. }
  430. .choice-cell {
  431. display: flex;
  432. align-items: center;
  433. justify-content: center;
  434. background: $game-ink;
  435. border: 2rpx solid transparent;
  436. border-radius: 24rpx;
  437. margin-bottom: $uni-space-lg;
  438. }
  439. .choice-cell:active {
  440. background: #36365c;
  441. }
  442. .choice-cell.cell-hit {
  443. background: #1e3a2f;
  444. border-color: $status-success-2;
  445. box-shadow: 0 0 30rpx rgba(34, 197, 94, 0.3);
  446. }
  447. .choice-cell.cell-miss {
  448. background: #3a1f22;
  449. border-color: $status-danger;
  450. box-shadow: 0 0 30rpx rgba(239, 68, 68, 0.3);
  451. }
  452. .symbol-opt {
  453. color: $app-surface;
  454. font-weight: bold;
  455. line-height: 1;
  456. }
  457. /* 结果页 */
  458. .score-circle {
  459. width: 280rpx;
  460. height: 280rpx;
  461. border-radius: 50%;
  462. background: linear-gradient(135deg, $brand-primary, $brand-primary-light);
  463. display: flex;
  464. flex-direction: column;
  465. align-items: center;
  466. justify-content: center;
  467. margin: 60rpx 0 50rpx;
  468. box-shadow: 0 0 60rpx rgba(249, 115, 22, 0.35);
  469. }
  470. .score-num {
  471. font-size: calc($uni-font-size-5xl * var(--font-scale, 1));
  472. font-weight: bold;
  473. color: $app-surface;
  474. line-height: 1;
  475. }
  476. .score-label {
  477. font-size: calc(28rpx * var(--font-scale, 1));
  478. color: rgba(255, 255, 255, 0.9);
  479. margin-top: 8rpx;
  480. }
  481. .result-stats {
  482. width: 100%;
  483. display: flex;
  484. justify-content: space-around;
  485. margin-bottom: 40rpx;
  486. }
  487. .stat-item {
  488. display: flex;
  489. flex-direction: column;
  490. align-items: center;
  491. }
  492. .stat-value {
  493. font-size: calc(40rpx * var(--font-scale, 1));
  494. font-weight: bold;
  495. color: $app-surface;
  496. }
  497. .stat-label {
  498. font-size: calc(26rpx * var(--font-scale, 1));
  499. color: rgba(255, 255, 255, 0.5);
  500. margin-top: 10rpx;
  501. }
  502. .dimension-tag {
  503. font-size: calc(28rpx * var(--font-scale, 1));
  504. color: $brand-primary;
  505. background: rgba(249, 115, 22, 0.12);
  506. border: 2rpx solid rgba(249, 115, 22, 0.4);
  507. border-radius: 24rpx;
  508. padding: 12rpx 40rpx;
  509. margin-bottom: 50rpx;
  510. }
  511. </style>