speed-test.vue 13 KB

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