spatial-test.vue 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516
  1. <template>
  2. <view class="page" :style="{ '--font-scale': fontScale }">
  3. <!-- 开始引导 -->
  4. <view v-if="step === 'instruction'" class="container">
  5. <view class="hero-icon">
  6. <text class="hero-symbol">↻</text>
  7. </view>
  8. <text class="hero-title">空间思维测试</text>
  9. <text class="hero-desc">观察箭头的初始方向,想象它按提示旋转后的样子,从四个选项中选出正确的方向。</text>
  10. <view class="rule-list">
  11. <view class="rule-item">
  12. <view class="rule-dot"></view>
  13. <text class="rule-text">共 8 题,每题限时 10 秒</text>
  14. </view>
  15. <view class="rule-item">
  16. <view class="rule-dot"></view>
  17. <text class="rule-text">超时未作答按答错处理</text>
  18. </view>
  19. <view class="rule-item">
  20. <view class="rule-dot"></view>
  21. <text class="rule-text">答题结束后自动计算得分</text>
  22. </view>
  23. </view>
  24. <button class="btn btn-primary" @tap="startTest">开始测试</button>
  25. </view>
  26. <!-- 答题中 -->
  27. <view v-else-if="step === 'playing'" class="container">
  28. <view class="progress-row">
  29. <text class="progress-text">第 {{ currentIndex + 1 }}/{{ questions.length }} 题</text>
  30. <text class="timer-text">{{ timeLeft }}s</text>
  31. </view>
  32. <view class="timer-bar">
  33. <view class="timer-bar-fill" :style="timerBarStyle"></view>
  34. </view>
  35. <view class="question-box">
  36. <text class="direction-label">初始方向</text>
  37. <text class="direction-arrow">{{ currentQuestion.direction }}</text>
  38. <text class="rotation-tip">顺时针旋转 {{ currentQuestion.rotation }}°</text>
  39. </view>
  40. <view class="options-wrap">
  41. <view
  42. v-for="(opt, oi) in currentQuestion.options"
  43. :key="oi"
  44. class="option-btn"
  45. :class="selected === opt ? 'option-selected' : ''"
  46. @tap="handleOptionTap(opt)"
  47. >
  48. <text class="option-arrow">{{ opt }}</text>
  49. </view>
  50. </view>
  51. <view v-if="feedback" class="feedback-row">
  52. <text class="feedback-text" :class="'feedback-' + feedback">{{ feedbackText }}</text>
  53. </view>
  54. </view>
  55. <!-- 结果 -->
  56. <view v-else class="container">
  57. <text class="result-title">测试完成</text>
  58. <view class="score-circle">
  59. <text class="score-number">{{ score }}</text>
  60. <text class="score-unit">分</text>
  61. </view>
  62. <text class="score-detail">答对 {{ correctCount }}/{{ questions.length }} 题</text>
  63. <view class="dimension-tag">
  64. <text class="dimension-name">空间思维</text>
  65. </view>
  66. <button class="btn btn-primary" :disabled="submitting" @tap="submitScore">提交成绩</button>
  67. <button class="btn btn-secondary" @tap="restartTest">再测一次</button>
  68. </view>
  69. </view>
  70. </template>
  71. <script>
  72. import { submitCognitiveSelfTest } from '../../utils/api.js'
  73. const TOTAL_TIME = 10
  74. const TOTAL_QUESTIONS = 8
  75. const DIRECTIONS = ['↑', '→', '↓', '←']
  76. // 顺时针旋转 90° 的方向映射
  77. const ROTATE_CW = { '↑': '→', '→': '↓', '↓': '←', '←': '↑' }
  78. function shuffleArray(arr) {
  79. var a = arr.slice()
  80. var i = a.length - 1
  81. while (i > 0) {
  82. var j = Math.floor(Math.random() * (i + 1))
  83. var tmp = a[i]
  84. a[i] = a[j]
  85. a[j] = tmp
  86. i -= 1
  87. }
  88. return a
  89. }
  90. function rotateDirection(direction, degrees) {
  91. var result = direction
  92. var times = degrees / 90
  93. for (var i = 0; i < times; i++) {
  94. result = ROTATE_CW[result]
  95. }
  96. return result
  97. }
  98. function buildQuestionBank() {
  99. var bank = []
  100. var rotations = [90, 180, 270]
  101. for (var d = 0; d < DIRECTIONS.length; d++) {
  102. var dir = DIRECTIONS[d]
  103. for (var r = 0; r < rotations.length; r++) {
  104. var rot = rotations[r]
  105. var answer = rotateDirection(dir, rot)
  106. // 每个 方向×角度 组合生成 2 题,共 24 题
  107. bank.push({ direction: dir, rotation: rot, answer: answer, options: shuffleArray(DIRECTIONS) })
  108. bank.push({ direction: dir, rotation: rot, answer: answer, options: shuffleArray(DIRECTIONS) })
  109. }
  110. }
  111. return bank
  112. }
  113. const QUESTION_BANK = buildQuestionBank()
  114. import fontScaleMixin from '@/mixins/font-scale.js'
  115. export default {
  116. mixins: [fontScaleMixin],
  117. data() {
  118. return {
  119. memberId: '',
  120. step: 'instruction', // instruction | playing | result
  121. questions: [],
  122. currentIndex: 0,
  123. selected: '',
  124. answered: false,
  125. feedback: '', // '' | correct | wrong | timeout
  126. correctCount: 0,
  127. timeLeft: TOTAL_TIME,
  128. score: 0,
  129. submitting: false,
  130. timer: null,
  131. timeoutJump: null
  132. }
  133. },
  134. computed: {
  135. currentQuestion() {
  136. return this.questions[this.currentIndex] || {}
  137. },
  138. timerBarStyle() {
  139. return 'width:' + (this.timeLeft / TOTAL_TIME * 100) + '%'
  140. },
  141. feedbackText() {
  142. if (this.feedback === 'correct') return '回答正确'
  143. if (this.feedback === 'wrong') return '回答错误'
  144. if (this.feedback === 'timeout') return '时间到,未作答'
  145. return ''
  146. }
  147. },
  148. onLoad() {
  149. this.memberId = uni.getStorageSync('currentChildId') || ''
  150. },
  151. onUnload() {
  152. this.stopTimer()
  153. if (this.timeoutJump) {
  154. clearTimeout(this.timeoutJump)
  155. this.timeoutJump = null
  156. }
  157. },
  158. methods: {
  159. generateQuestions() {
  160. var shuffled = shuffleArray(QUESTION_BANK)
  161. return shuffled.slice(0, TOTAL_QUESTIONS)
  162. },
  163. startTest() {
  164. this.questions = this.generateQuestions()
  165. this.currentIndex = 0
  166. this.selected = ''
  167. this.answered = false
  168. this.feedback = ''
  169. this.correctCount = 0
  170. this.score = 0
  171. this.submitting = false
  172. this.step = 'playing'
  173. this.startTimer()
  174. },
  175. startTimer() {
  176. this.stopTimer()
  177. this.timeLeft = TOTAL_TIME
  178. this.timer = setInterval(() => {
  179. this.timeLeft -= 1
  180. if (this.timeLeft <= 0) {
  181. this.handleTimeout()
  182. }
  183. }, 1000)
  184. },
  185. stopTimer() {
  186. if (this.timer) {
  187. clearInterval(this.timer)
  188. this.timer = null
  189. }
  190. },
  191. handleOptionTap(opt) {
  192. if (this.answered) {
  193. return
  194. }
  195. this.answered = true
  196. this.selected = opt
  197. this.stopTimer()
  198. if (opt === this.currentQuestion.answer) {
  199. this.correctCount += 1
  200. this.feedback = 'correct'
  201. } else {
  202. this.feedback = 'wrong'
  203. }
  204. this.scheduleNext()
  205. },
  206. handleTimeout() {
  207. this.stopTimer()
  208. this.answered = true
  209. this.feedback = 'timeout'
  210. this.scheduleNext()
  211. },
  212. scheduleNext() {
  213. var self = this
  214. this.timeoutJump = setTimeout(function () {
  215. self.nextQuestion()
  216. }, 800)
  217. },
  218. nextQuestion() {
  219. if (this.timeoutJump) {
  220. clearTimeout(this.timeoutJump)
  221. this.timeoutJump = null
  222. }
  223. if (this.currentIndex >= this.questions.length - 1) {
  224. this.finishTest()
  225. return
  226. }
  227. this.currentIndex += 1
  228. this.selected = ''
  229. this.answered = false
  230. this.feedback = ''
  231. this.startTimer()
  232. },
  233. finishTest() {
  234. this.stopTimer()
  235. this.score = Math.round(this.correctCount / this.questions.length * 100)
  236. this.step = 'result'
  237. },
  238. submitScore() {
  239. if (this.submitting) {
  240. return
  241. }
  242. this.submitting = true
  243. var memberId = this.memberId || uni.getStorageSync('currentChildId') || ''
  244. submitCognitiveSelfTest(memberId, 'spatialScore', this.score)
  245. .then(() => {
  246. uni.showToast({ title: '提交成功', icon: 'success' })
  247. setTimeout(() => {
  248. uni.navigateBack()
  249. }, 1500)
  250. })
  251. .catch(() => {
  252. this.submitting = false
  253. uni.showToast({ title: '提交失败,请重试', icon: 'none' })
  254. })
  255. },
  256. restartTest() {
  257. this.startTest()
  258. }
  259. }
  260. }
  261. </script>
  262. <style scoped lang="scss">
  263. .page {
  264. min-height: 100vh;
  265. background: linear-gradient(135deg, $app-ink-night 0%, $app-ink-night-2 100%);
  266. padding: 60rpx 40rpx;
  267. box-sizing: border-box;
  268. }
  269. .container {
  270. display: flex;
  271. flex-direction: column;
  272. align-items: center;
  273. }
  274. /* --- 引导页 --- */
  275. .hero-icon {
  276. width: 160rpx;
  277. height: 160rpx;
  278. border-radius: 50%;
  279. background: rgba(59, 130, 246, 0.15);
  280. border: 2rpx solid rgba(59, 130, 246, 0.5);
  281. display: flex;
  282. align-items: center;
  283. justify-content: center;
  284. margin-top: 100rpx;
  285. margin-bottom: 40rpx;
  286. }
  287. .hero-symbol {
  288. font-size: calc(80rpx * var(--font-scale, 1));
  289. color: $status-info;
  290. line-height: 1;
  291. }
  292. .hero-title {
  293. font-size: calc(44rpx * var(--font-scale, 1));
  294. font-weight: bold;
  295. color: $app-surface;
  296. margin-bottom: 24rpx;
  297. }
  298. .hero-desc {
  299. font-size: calc(28rpx * var(--font-scale, 1));
  300. color: rgba(255, 255, 255, 0.7);
  301. line-height: 1.7;
  302. text-align: center;
  303. margin-bottom: 48rpx;
  304. }
  305. .rule-list {
  306. width: 100%;
  307. background: rgba(255, 255, 255, 0.06);
  308. border-radius: 24rpx;
  309. padding: $uni-space-lg;
  310. margin-bottom: 80rpx;
  311. box-sizing: border-box;
  312. }
  313. .rule-item {
  314. display: flex;
  315. align-items: center;
  316. margin-bottom: 20rpx;
  317. }
  318. .rule-item:last-child {
  319. margin-bottom: 0;
  320. }
  321. .rule-dot {
  322. width: 12rpx;
  323. height: 12rpx;
  324. border-radius: 50%;
  325. background: $status-info;
  326. margin-right: 16rpx;
  327. }
  328. .rule-text {
  329. font-size: calc(26rpx * var(--font-scale, 1));
  330. color: rgba(255, 255, 255, 0.8);
  331. }
  332. /* --- 答题页 --- */
  333. .progress-row {
  334. width: 100%;
  335. display: flex;
  336. justify-content: space-between;
  337. align-items: center;
  338. margin-bottom: 20rpx;
  339. }
  340. .progress-text {
  341. font-size: calc(28rpx * var(--font-scale, 1));
  342. color: rgba(255, 255, 255, 0.85);
  343. }
  344. .timer-text {
  345. font-size: calc(28rpx * var(--font-scale, 1));
  346. color: #60A5FA;
  347. font-weight: bold;
  348. }
  349. .timer-bar {
  350. width: 100%;
  351. height: 12rpx;
  352. background: rgba(255, 255, 255, 0.1);
  353. border-radius: 8rpx;
  354. overflow: hidden;
  355. margin-bottom: 80rpx;
  356. }
  357. .timer-bar-fill {
  358. height: 100%;
  359. background: linear-gradient(90deg, $status-info, #60A5FA);
  360. border-radius: 8rpx;
  361. transition: width 1s linear;
  362. }
  363. .question-box {
  364. width: 100%;
  365. background: rgba(255, 255, 255, 0.06);
  366. border-radius: 24rpx;
  367. padding: 50rpx 0;
  368. display: flex;
  369. flex-direction: column;
  370. align-items: center;
  371. margin-bottom: 60rpx;
  372. }
  373. .direction-label {
  374. font-size: calc(24rpx * var(--font-scale, 1));
  375. color: rgba(255, 255, 255, 0.5);
  376. margin-bottom: 20rpx;
  377. }
  378. .direction-arrow {
  379. font-size: calc(100rpx * var(--font-scale, 1));
  380. color: $app-surface;
  381. line-height: 1.2;
  382. margin-bottom: 20rpx;
  383. }
  384. .rotation-tip {
  385. font-size: calc(30rpx * var(--font-scale, 1));
  386. color: $tint-blue-300;
  387. }
  388. .options-wrap {
  389. width: 100%;
  390. display: flex;
  391. flex-wrap: wrap;
  392. justify-content: space-between;
  393. }
  394. .option-btn {
  395. width: 300rpx;
  396. height: 220rpx;
  397. background: rgba(255, 255, 255, 0.08);
  398. border: 2rpx solid rgba(255, 255, 255, 0.15);
  399. border-radius: 24rpx;
  400. display: flex;
  401. align-items: center;
  402. justify-content: center;
  403. margin-bottom: $uni-space-lg;
  404. box-sizing: border-box;
  405. }
  406. .option-selected {
  407. border-color: $status-info;
  408. background: rgba(59, 130, 246, 0.2);
  409. }
  410. .option-arrow {
  411. font-size: calc(72rpx * var(--font-scale, 1));
  412. color: $app-surface;
  413. }
  414. .feedback-row {
  415. margin-top: 10rpx;
  416. }
  417. .feedback-text {
  418. font-size: calc(30rpx * var(--font-scale, 1));
  419. }
  420. .feedback-correct {
  421. color: $status-success-light-3;
  422. }
  423. .feedback-wrong {
  424. color: $status-danger-light;
  425. }
  426. .feedback-timeout {
  427. color: rgba(255, 255, 255, 0.5);
  428. }
  429. /* --- 结果页 --- */
  430. .result-title {
  431. font-size: calc(36rpx * var(--font-scale, 1));
  432. color: $app-surface;
  433. margin-top: 60rpx;
  434. margin-bottom: 20rpx;
  435. }
  436. .score-circle {
  437. width: 280rpx;
  438. height: 280rpx;
  439. border-radius: 50%;
  440. border: 10rpx solid $status-info;
  441. display: flex;
  442. flex-direction: column;
  443. align-items: center;
  444. justify-content: center;
  445. margin: 40rpx 0;
  446. }
  447. .score-number {
  448. font-size: calc(80rpx * var(--font-scale, 1));
  449. font-weight: bold;
  450. color: $app-surface;
  451. line-height: 1.1;
  452. }
  453. .score-unit {
  454. font-size: calc(28rpx * var(--font-scale, 1));
  455. color: rgba(255, 255, 255, 0.6);
  456. }
  457. .score-detail {
  458. font-size: calc(28rpx * var(--font-scale, 1));
  459. color: rgba(255, 255, 255, 0.8);
  460. margin-bottom: $uni-space-lg;
  461. }
  462. .dimension-tag {
  463. padding: 10rpx 32rpx;
  464. background: rgba(59, 130, 246, 0.15);
  465. border: 2rpx solid rgba(59, 130, 246, 0.5);
  466. border-radius: 24rpx;
  467. margin-bottom: 80rpx;
  468. }
  469. .dimension-name {
  470. font-size: calc(26rpx * var(--font-scale, 1));
  471. color: $tint-blue-300;
  472. }
  473. /* --- 按钮 --- */
  474. .btn {
  475. width: 100%;
  476. height: 96rpx;
  477. line-height: 96rpx;
  478. border-radius: 24rpx;
  479. font-size: calc(32rpx * var(--font-scale, 1));
  480. text-align: center;
  481. margin-bottom: $uni-space-lg;
  482. padding: 0;
  483. }
  484. .btn::after {
  485. border: none;
  486. }
  487. .btn-primary {
  488. background: $status-info;
  489. color: $app-surface;
  490. }
  491. .btn-secondary {
  492. background: transparent;
  493. border: 2rpx solid $status-info;
  494. color: $status-info;
  495. box-sizing: border-box;
  496. }
  497. </style>