perception-test.vue 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459
  1. <template>
  2. <view class="test-container" :style="{ '--font-scale': fontScale }">
  3. <!-- 步骤一:测试说明 -->
  4. <view class="step-panel" v-if="step === 'instruction'">
  5. <view class="title-icon">👁️</view>
  6. <text class="test-title">感知觉测试</text>
  7. <text class="test-desc">屏幕上会出现 6 个相似的图案,其中有一个与众不同。</text>
  8. <text class="test-desc">请快速找出"不一样"的那个,点击它即可!</text>
  9. <text class="test-meta">共 8 题 · 每题限时 8 秒 · 超时自动进入下一题</text>
  10. <button class="btn-primary" @click="startTest">开始测试</button>
  11. </view>
  12. <!-- 步骤二:答题中 -->
  13. <view class="step-panel" v-else-if="step === 'playing'">
  14. <view class="progress-row">
  15. <text class="progress-text">第 {{ currentIndex + 1 }}/8 题</text>
  16. <text class="progress-text">答对 {{ correctCount }} 题</text>
  17. </view>
  18. <view class="timer-bar">
  19. <view class="timer-fill" :style="timerBarStyle"></view>
  20. </view>
  21. <text class="timer-text">{{ timerLeft }}秒</text>
  22. <text class="question-hint" v-if="currentQuestion">{{ currentQuestion.hint }}</text>
  23. <view class="grid-wrap">
  24. <view
  25. class="grid-cell"
  26. v-for="(item, index) in currentQuestion.items"
  27. :key="getItemKey(item, index)"
  28. :class="{
  29. 'cell-correct': lockInput && index === currentQuestion.oddIndex,
  30. 'cell-wrong': tappedIndex === index && index !== currentQuestion.oddIndex
  31. }"
  32. @click="handleTap(index)"
  33. >
  34. <text class="cell-emoji">{{ item }}</text>
  35. </view>
  36. </view>
  37. </view>
  38. <!-- 步骤三:结果 -->
  39. <view class="step-panel" v-else>
  40. <text class="result-title">测试完成</text>
  41. <view class="score-circle">
  42. <text class="score-num">{{ score }}</text>
  43. </view>
  44. <view class="score-row">
  45. <text class="score-label">答对 {{ correctCount }}/8 题</text>
  46. <text class="dimension-tag">感知觉</text>
  47. </view>
  48. <view class="result-tip" v-if="score >= 75">
  49. <text class="result-tip-text">感官敏锐,找不同又快又准!</text>
  50. </view>
  51. <view class="result-tip" v-else-if="score >= 50">
  52. <text class="result-tip-text">表现不错,再细心一点会更好!</text>
  53. </view>
  54. <view class="result-tip" v-else>
  55. <text class="result-tip-text">别灰心,多练几次会越来越快!</text>
  56. </view>
  57. <button class="btn-primary" @click="submitResult">提交成绩</button>
  58. <button class="btn-secondary" @click="retryTest">再测一次</button>
  59. </view>
  60. </view>
  61. </template>
  62. <script>
  63. import { submitCognitiveSelfTest } from '../../utils/api.js'
  64. // 题库:6 个相似图案,其中一个是"不同"的(颜色/形状变体)
  65. var QUESTION_BANK = [
  66. { items: ['🟦','🟦','🟦','🟦','🟦','🟥'], oddIndex: 5, hint: '找出不同的颜色' },
  67. { items: ['🟨','🟨','🟨','🟨','🟨','🟩'], oddIndex: 3, hint: '找出不同的颜色' },
  68. { items: ['🟪','🟪','🟪','🟪','🟪','🟦'], oddIndex: 4, hint: '找出不同的颜色' },
  69. { items: ['🟩','🟩','🟩','🟩','🟩','🟨'], oddIndex: 1, hint: '找出不同的颜色' },
  70. { items: ['🟥','🟥','🟥','🟥','🟥','🟪'], oddIndex: 2, hint: '找出不同的颜色' },
  71. { items: ['🟡','🟡','🟡','🟡','🟡','🟢'], oddIndex: 0, hint: '找出不同的颜色' },
  72. { items: ['⬛','⬛','⬛','⬛','⬛','⬜'], oddIndex: 5, hint: '找出不同的颜色' },
  73. { items: ['🔴','🔴','🔴','🔴','🔴','🟡'], oddIndex: 2, hint: '找出不同的颜色' },
  74. { items: ['🟨','🟨','🟨','🟨','🟨','🟥'], oddIndex: 0, hint: '找出不同的颜色' },
  75. { items: ['🟩','🟩','🟩','🟩','🟩','🟦'], oddIndex: 4, hint: '找出不同的颜色' },
  76. { items: ['🟪','🟪','🟪','🟪','🟪','⬛'], oddIndex: 1, hint: '找出不同的图案' },
  77. { items: ['⬜','⬜','⬜','⬜','⬜','⬛'], oddIndex: 3, hint: '找出不同的图案' },
  78. { items: ['🔴','🔴','🔴','🔴','🔴','🟥'], oddIndex: 5, hint: '找出不同的图案' },
  79. { items: ['🟥','🟥','🟥','🟥','🟥','🔴'], oddIndex: 2, hint: '找出不同的图案' },
  80. { items: ['🟡','🟡','🟡','🟡','🟡','🟢'], oddIndex: 1, hint: '找出不同的颜色' },
  81. { items: ['🟦','🟦','🟦','🟦','🟦','⬜'], oddIndex: 4, hint: '找出不同的图案' },
  82. { items: ['⬡','⬡','⬡','⬡','⬡','⬢'], oddIndex: 5, hint: '找出不同的形状' },
  83. { items: ['◈','◈','◈','◈','◈','◆'], oddIndex: 1, hint: '找出不同的形状' },
  84. { items: ['○','○','○','○','○','◇'], oddIndex: 3, hint: '找出不同的形状' },
  85. { items: ['◆','◆','◆','◆','◆','◈'], oddIndex: 0, hint: '找出不同的形状' },
  86. { items: ['⬢','⬢','⬢','⬢','⬢','⬡'], oddIndex: 2, hint: '找出不同的形状' },
  87. { items: ['◇','◇','◇','◇','◇','○'], oddIndex: 5, hint: '找出不同的形状' }
  88. ]
  89. import fontScaleMixin from '@/mixins/font-scale.js'
  90. export default {
  91. mixins: [fontScaleMixin],
  92. data() {
  93. return {
  94. step: 'instruction',
  95. memberId: '',
  96. roundQuestions: [],
  97. currentIndex: 0,
  98. currentQuestion: null,
  99. correctCount: 0,
  100. timerLeft: 8,
  101. timer: null,
  102. nextTimer: null,
  103. lockInput: false,
  104. tappedIndex: -1,
  105. score: 0
  106. }
  107. },
  108. computed: {
  109. timerPercent: function() {
  110. return Math.round(this.timerLeft / 8 * 100)
  111. },
  112. timerBarStyle: function() {
  113. return 'width:' + this.timerPercent + '%'
  114. }
  115. },
  116. onLoad(options) {
  117. if (options && options.memberId) {
  118. this.memberId = options.memberId
  119. }
  120. },
  121. onUnload() {
  122. this.stopTimer()
  123. if (this.nextTimer) {
  124. clearTimeout(this.nextTimer)
  125. this.nextTimer = null
  126. }
  127. },
  128. methods: {
  129. getItemKey: function(item, index) {
  130. return item + '-' + index
  131. },
  132. startTest: function() {
  133. this.correctCount = 0
  134. this.currentIndex = 0
  135. this.score = 0
  136. this.lockInput = false
  137. this.tappedIndex = -1
  138. // 洗牌后取前 8 题
  139. var bank = QUESTION_BANK.slice()
  140. for (var i = bank.length - 1; i > 0; i--) {
  141. var j = Math.floor(Math.random() * (i + 1))
  142. var tmp = bank[i]
  143. bank[i] = bank[j]
  144. bank[j] = tmp
  145. }
  146. this.roundQuestions = bank.slice(0, 8)
  147. this.step = 'playing'
  148. this.loadQuestion(0)
  149. },
  150. loadQuestion: function(index) {
  151. this.currentIndex = index
  152. this.currentQuestion = this.roundQuestions[index]
  153. this.tappedIndex = -1
  154. this.lockInput = false
  155. this.timerLeft = 8
  156. this.startTimer()
  157. },
  158. startTimer: function() {
  159. this.stopTimer()
  160. this.timer = setInterval(() => {
  161. this.timerLeft--
  162. if (this.timerLeft <= 0) {
  163. this.timerLeft = 0
  164. this.onTimeout()
  165. }
  166. }, 1000)
  167. },
  168. stopTimer: function() {
  169. if (this.timer) {
  170. clearInterval(this.timer)
  171. this.timer = null
  172. }
  173. },
  174. handleTap: function(index) {
  175. if (this.lockInput || !this.currentQuestion) return
  176. this.lockInput = true
  177. this.stopTimer()
  178. this.tappedIndex = index
  179. if (index === this.currentQuestion.oddIndex) {
  180. this.correctCount++
  181. }
  182. this.scheduleNext()
  183. },
  184. onTimeout: function() {
  185. if (this.lockInput) return
  186. this.lockInput = true
  187. this.stopTimer()
  188. this.tappedIndex = -1
  189. // 超时未作答,短暂高亮正确答案后进入下一题
  190. this.scheduleNext()
  191. },
  192. scheduleNext: function() {
  193. if (this.nextTimer) clearTimeout(this.nextTimer)
  194. this.nextTimer = setTimeout(() => {
  195. this.nextTimer = null
  196. this.advance()
  197. }, 700)
  198. },
  199. advance: function() {
  200. if (this.currentIndex + 1 >= this.roundQuestions.length) {
  201. this.finishTest()
  202. } else {
  203. this.loadQuestion(this.currentIndex + 1)
  204. }
  205. },
  206. finishTest: function() {
  207. this.stopTimer()
  208. this.score = Math.round(this.correctCount / this.roundQuestions.length * 100)
  209. this.step = 'result'
  210. },
  211. submitResult: function() {
  212. var memberId = this.memberId || uni.getStorageSync('currentChildId') || ''
  213. if (!memberId) {
  214. uni.showToast({ title: '请先选择家庭成员', icon: 'none' })
  215. return
  216. }
  217. submitCognitiveSelfTest(memberId, 'perceptionScore', this.score)
  218. .then((res) => {
  219. uni.showToast({ title: '提交成功', icon: 'success' })
  220. setTimeout(() => {
  221. uni.navigateBack()
  222. }, 1500)
  223. })
  224. .catch((err) => {
  225. console.error('提交感知觉测试成绩失败', err)
  226. uni.showToast({ title: '提交失败,请重试', icon: 'none' })
  227. })
  228. },
  229. retryTest: function() {
  230. this.startTest()
  231. }
  232. }
  233. }
  234. </script>
  235. <style scoped lang="scss">
  236. .test-container {
  237. min-height: 100vh;
  238. background: linear-gradient(135deg, $app-ink-night 0%, $app-ink-night-2 100%);
  239. padding: 60rpx 40rpx;
  240. }
  241. .step-panel {
  242. display: flex;
  243. flex-direction: column;
  244. align-items: center;
  245. }
  246. /* ===== 说明页 ===== */
  247. .title-icon {
  248. font-size: calc(80rpx * var(--font-scale, 1));
  249. margin-bottom: 24rpx;
  250. }
  251. .test-title {
  252. font-size: calc(48rpx * var(--font-scale, 1));
  253. font-weight: bold;
  254. color: $app-surface;
  255. margin-bottom: 32rpx;
  256. }
  257. .test-desc {
  258. font-size: calc(30rpx * var(--font-scale, 1));
  259. color: rgba(255,255,255,0.7);
  260. line-height: 1.7;
  261. text-align: center;
  262. }
  263. .test-meta {
  264. font-size: calc(24rpx * var(--font-scale, 1));
  265. color: rgba(255,255,255,0.45);
  266. margin-top: 24rpx;
  267. margin-bottom: 80rpx;
  268. text-align: center;
  269. }
  270. .btn-primary {
  271. background: linear-gradient(135deg, $flat-teal, #45B7AA);
  272. color: $app-surface;
  273. font-size: calc(34rpx * var(--font-scale, 1));
  274. font-weight: bold;
  275. width: 480rpx;
  276. height: 92rpx;
  277. line-height: 92rpx;
  278. border-radius: 24rpx;
  279. border: none;
  280. margin-top: 60rpx;
  281. padding: 0;
  282. }
  283. .btn-primary::after {
  284. border: none;
  285. }
  286. .btn-secondary {
  287. background: rgba(255,255,255,0.1);
  288. color: rgba(255,255,255,0.85);
  289. font-size: calc(32rpx * var(--font-scale, 1));
  290. width: 480rpx;
  291. height: 92rpx;
  292. line-height: 92rpx;
  293. border-radius: 24rpx;
  294. border: 2rpx solid rgba(255,255,255,0.25);
  295. margin-top: 32rpx;
  296. padding: 0;
  297. }
  298. .btn-secondary::after {
  299. border: none;
  300. }
  301. /* ===== 答题页 ===== */
  302. .progress-row {
  303. display: flex;
  304. justify-content: space-between;
  305. width: 100%;
  306. margin-bottom: 24rpx;
  307. }
  308. .progress-text {
  309. font-size: calc(28rpx * var(--font-scale, 1));
  310. color: rgba(255,255,255,0.7);
  311. }
  312. .timer-bar {
  313. width: 100%;
  314. height: 16rpx;
  315. background: rgba(255,255,255,0.12);
  316. border-radius: 8rpx;
  317. overflow: hidden;
  318. }
  319. .timer-fill {
  320. height: 100%;
  321. background: linear-gradient(90deg, $flat-teal, #45B7AA);
  322. border-radius: 8rpx;
  323. transition: width 0.9s linear;
  324. }
  325. .timer-text {
  326. display: block;
  327. font-size: calc(26rpx * var(--font-scale, 1));
  328. color: $flat-teal;
  329. margin-top: 12rpx;
  330. text-align: center;
  331. }
  332. .question-hint {
  333. display: block;
  334. font-size: calc(32rpx * var(--font-scale, 1));
  335. font-weight: bold;
  336. color: $app-surface;
  337. margin: 40rpx 0 32rpx;
  338. text-align: center;
  339. }
  340. .grid-wrap {
  341. display: flex;
  342. flex-wrap: wrap;
  343. justify-content: space-between;
  344. width: 100%;
  345. }
  346. .grid-cell {
  347. display: flex;
  348. align-items: center;
  349. justify-content: center;
  350. width: 30%;
  351. height: 190rpx;
  352. background: $game-ink;
  353. border-radius: 24rpx;
  354. border: 4rpx solid transparent;
  355. margin-bottom: 24rpx;
  356. box-sizing: border-box;
  357. }
  358. .grid-cell:active {
  359. background: #35406b;
  360. }
  361. .cell-emoji {
  362. font-size: calc(50rpx * var(--font-scale, 1));
  363. }
  364. .cell-correct {
  365. background: #2f4a4a;
  366. border-color: $flat-teal;
  367. }
  368. .cell-wrong {
  369. background: #4a2f35;
  370. border-color: $accent-coral;
  371. }
  372. /* ===== 结果页 ===== */
  373. .result-title {
  374. font-size: calc(40rpx * var(--font-scale, 1));
  375. font-weight: bold;
  376. color: $app-surface;
  377. margin-bottom: 48rpx;
  378. }
  379. .score-circle {
  380. display: flex;
  381. align-items: center;
  382. justify-content: center;
  383. width: 260rpx;
  384. height: 260rpx;
  385. border-radius: 50%;
  386. border: 10rpx solid $flat-teal;
  387. background: rgba(78,205,196,0.1);
  388. margin-bottom: 36rpx;
  389. }
  390. .score-num {
  391. font-size: calc(80rpx * var(--font-scale, 1));
  392. font-weight: bold;
  393. color: $flat-teal;
  394. }
  395. .score-row {
  396. display: flex;
  397. align-items: center;
  398. margin-bottom: 24rpx;
  399. }
  400. .score-label {
  401. font-size: calc(30rpx * var(--font-scale, 1));
  402. color: rgba(255,255,255,0.7);
  403. margin-right: 20rpx;
  404. }
  405. .dimension-tag {
  406. font-size: calc(24rpx * var(--font-scale, 1));
  407. color: $flat-teal;
  408. border: 2rpx solid $flat-teal;
  409. border-radius: 24rpx;
  410. padding: 6rpx 24rpx;
  411. }
  412. .result-tip {
  413. margin-bottom: 20rpx;
  414. }
  415. .result-tip-text {
  416. font-size: calc(28rpx * var(--font-scale, 1));
  417. color: rgba(255,255,255,0.6);
  418. }
  419. </style>