speed-test.vue 13 KB

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