spatial-test.vue 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517
  1. <template>
  2. <view class="page">
  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 in currentQuestion.options"
  43. :key="getOptKey(opt)"
  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. export default {
  115. data() {
  116. return {
  117. memberId: '',
  118. step: 'instruction', // instruction | playing | result
  119. questions: [],
  120. currentIndex: 0,
  121. selected: '',
  122. answered: false,
  123. feedback: '', // '' | correct | wrong | timeout
  124. correctCount: 0,
  125. timeLeft: TOTAL_TIME,
  126. score: 0,
  127. submitting: false,
  128. timer: null,
  129. timeoutJump: null
  130. }
  131. },
  132. computed: {
  133. currentQuestion() {
  134. return this.questions[this.currentIndex] || {}
  135. },
  136. timerBarStyle() {
  137. return 'width:' + (this.timeLeft / TOTAL_TIME * 100) + '%'
  138. },
  139. feedbackText() {
  140. if (this.feedback === 'correct') return '回答正确'
  141. if (this.feedback === 'wrong') return '回答错误'
  142. if (this.feedback === 'timeout') return '时间到,未作答'
  143. return ''
  144. }
  145. },
  146. onLoad() {
  147. this.memberId = uni.getStorageSync('currentChildId') || ''
  148. },
  149. onUnload() {
  150. this.stopTimer()
  151. if (this.timeoutJump) {
  152. clearTimeout(this.timeoutJump)
  153. this.timeoutJump = null
  154. }
  155. },
  156. methods: {
  157. getOptKey(opt) {
  158. return this.currentIndex + '_' + opt
  159. },
  160. generateQuestions() {
  161. var shuffled = shuffleArray(QUESTION_BANK)
  162. return shuffled.slice(0, TOTAL_QUESTIONS)
  163. },
  164. startTest() {
  165. this.questions = this.generateQuestions()
  166. this.currentIndex = 0
  167. this.selected = ''
  168. this.answered = false
  169. this.feedback = ''
  170. this.correctCount = 0
  171. this.score = 0
  172. this.submitting = false
  173. this.step = 'playing'
  174. this.startTimer()
  175. },
  176. startTimer() {
  177. this.stopTimer()
  178. this.timeLeft = TOTAL_TIME
  179. this.timer = setInterval(() => {
  180. this.timeLeft -= 1
  181. if (this.timeLeft <= 0) {
  182. this.handleTimeout()
  183. }
  184. }, 1000)
  185. },
  186. stopTimer() {
  187. if (this.timer) {
  188. clearInterval(this.timer)
  189. this.timer = null
  190. }
  191. },
  192. handleOptionTap(opt) {
  193. if (this.answered) {
  194. return
  195. }
  196. this.answered = true
  197. this.selected = opt
  198. this.stopTimer()
  199. if (opt === this.currentQuestion.answer) {
  200. this.correctCount += 1
  201. this.feedback = 'correct'
  202. } else {
  203. this.feedback = 'wrong'
  204. }
  205. this.scheduleNext()
  206. },
  207. handleTimeout() {
  208. this.stopTimer()
  209. this.answered = true
  210. this.feedback = 'timeout'
  211. this.scheduleNext()
  212. },
  213. scheduleNext() {
  214. var self = this
  215. this.timeoutJump = setTimeout(function () {
  216. self.nextQuestion()
  217. }, 800)
  218. },
  219. nextQuestion() {
  220. if (this.timeoutJump) {
  221. clearTimeout(this.timeoutJump)
  222. this.timeoutJump = null
  223. }
  224. if (this.currentIndex >= this.questions.length - 1) {
  225. this.finishTest()
  226. return
  227. }
  228. this.currentIndex += 1
  229. this.selected = ''
  230. this.answered = false
  231. this.feedback = ''
  232. this.startTimer()
  233. },
  234. finishTest() {
  235. this.stopTimer()
  236. this.score = Math.round(this.correctCount / this.questions.length * 100)
  237. this.step = 'result'
  238. },
  239. submitScore() {
  240. if (this.submitting) {
  241. return
  242. }
  243. this.submitting = true
  244. var memberId = this.memberId || uni.getStorageSync('currentChildId') || ''
  245. submitCognitiveSelfTest(memberId, 'spatialScore', this.score)
  246. .then(() => {
  247. uni.showToast({ title: '提交成功', icon: 'success' })
  248. setTimeout(() => {
  249. uni.navigateBack()
  250. }, 1500)
  251. })
  252. .catch(() => {
  253. this.submitting = false
  254. uni.showToast({ title: '提交失败,请重试', icon: 'none' })
  255. })
  256. },
  257. restartTest() {
  258. this.startTest()
  259. }
  260. }
  261. }
  262. </script>
  263. <style scoped>
  264. .page {
  265. min-height: 100vh;
  266. background: linear-gradient(135deg, #1a1a2e 0%, #16213e 100%);
  267. padding: 60rpx 40rpx;
  268. box-sizing: border-box;
  269. }
  270. .container {
  271. display: flex;
  272. flex-direction: column;
  273. align-items: center;
  274. }
  275. /* --- 引导页 --- */
  276. .hero-icon {
  277. width: 160rpx;
  278. height: 160rpx;
  279. border-radius: 50%;
  280. background: rgba(59, 130, 246, 0.15);
  281. border: 2rpx solid rgba(59, 130, 246, 0.5);
  282. display: flex;
  283. align-items: center;
  284. justify-content: center;
  285. margin-top: 100rpx;
  286. margin-bottom: 40rpx;
  287. }
  288. .hero-symbol {
  289. font-size: 80rpx;
  290. color: #3B82F6;
  291. line-height: 1;
  292. }
  293. .hero-title {
  294. font-size: 44rpx;
  295. font-weight: bold;
  296. color: #ffffff;
  297. margin-bottom: 24rpx;
  298. }
  299. .hero-desc {
  300. font-size: 28rpx;
  301. color: rgba(255, 255, 255, 0.7);
  302. line-height: 1.7;
  303. text-align: center;
  304. margin-bottom: 48rpx;
  305. }
  306. .rule-list {
  307. width: 100%;
  308. background: rgba(255, 255, 255, 0.06);
  309. border-radius: 20rpx;
  310. padding: 30rpx;
  311. margin-bottom: 80rpx;
  312. box-sizing: border-box;
  313. }
  314. .rule-item {
  315. display: flex;
  316. align-items: center;
  317. margin-bottom: 20rpx;
  318. }
  319. .rule-item:last-child {
  320. margin-bottom: 0;
  321. }
  322. .rule-dot {
  323. width: 12rpx;
  324. height: 12rpx;
  325. border-radius: 50%;
  326. background: #3B82F6;
  327. margin-right: 16rpx;
  328. }
  329. .rule-text {
  330. font-size: 26rpx;
  331. color: rgba(255, 255, 255, 0.8);
  332. }
  333. /* --- 答题页 --- */
  334. .progress-row {
  335. width: 100%;
  336. display: flex;
  337. justify-content: space-between;
  338. align-items: center;
  339. margin-bottom: 20rpx;
  340. }
  341. .progress-text {
  342. font-size: 28rpx;
  343. color: rgba(255, 255, 255, 0.85);
  344. }
  345. .timer-text {
  346. font-size: 28rpx;
  347. color: #60A5FA;
  348. font-weight: bold;
  349. }
  350. .timer-bar {
  351. width: 100%;
  352. height: 12rpx;
  353. background: rgba(255, 255, 255, 0.1);
  354. border-radius: 6rpx;
  355. overflow: hidden;
  356. margin-bottom: 80rpx;
  357. }
  358. .timer-bar-fill {
  359. height: 100%;
  360. background: linear-gradient(90deg, #3B82F6, #60A5FA);
  361. border-radius: 6rpx;
  362. transition: width 1s linear;
  363. }
  364. .question-box {
  365. width: 100%;
  366. background: rgba(255, 255, 255, 0.06);
  367. border-radius: 24rpx;
  368. padding: 50rpx 0;
  369. display: flex;
  370. flex-direction: column;
  371. align-items: center;
  372. margin-bottom: 60rpx;
  373. }
  374. .direction-label {
  375. font-size: 24rpx;
  376. color: rgba(255, 255, 255, 0.5);
  377. margin-bottom: 20rpx;
  378. }
  379. .direction-arrow {
  380. font-size: 100rpx;
  381. color: #ffffff;
  382. line-height: 1.2;
  383. margin-bottom: 20rpx;
  384. }
  385. .rotation-tip {
  386. font-size: 30rpx;
  387. color: #93C5FD;
  388. }
  389. .options-wrap {
  390. width: 100%;
  391. display: flex;
  392. flex-wrap: wrap;
  393. justify-content: space-between;
  394. }
  395. .option-btn {
  396. width: 300rpx;
  397. height: 220rpx;
  398. background: rgba(255, 255, 255, 0.08);
  399. border: 2rpx solid rgba(255, 255, 255, 0.15);
  400. border-radius: 24rpx;
  401. display: flex;
  402. align-items: center;
  403. justify-content: center;
  404. margin-bottom: 30rpx;
  405. box-sizing: border-box;
  406. }
  407. .option-selected {
  408. border-color: #3B82F6;
  409. background: rgba(59, 130, 246, 0.2);
  410. }
  411. .option-arrow {
  412. font-size: 72rpx;
  413. color: #ffffff;
  414. }
  415. .feedback-row {
  416. margin-top: 10rpx;
  417. }
  418. .feedback-text {
  419. font-size: 30rpx;
  420. }
  421. .feedback-correct {
  422. color: #34D399;
  423. }
  424. .feedback-wrong {
  425. color: #F87171;
  426. }
  427. .feedback-timeout {
  428. color: rgba(255, 255, 255, 0.5);
  429. }
  430. /* --- 结果页 --- */
  431. .result-title {
  432. font-size: 36rpx;
  433. color: #ffffff;
  434. margin-top: 60rpx;
  435. margin-bottom: 20rpx;
  436. }
  437. .score-circle {
  438. width: 280rpx;
  439. height: 280rpx;
  440. border-radius: 50%;
  441. border: 10rpx solid #3B82F6;
  442. display: flex;
  443. flex-direction: column;
  444. align-items: center;
  445. justify-content: center;
  446. margin: 40rpx 0;
  447. }
  448. .score-number {
  449. font-size: 80rpx;
  450. font-weight: bold;
  451. color: #ffffff;
  452. line-height: 1.1;
  453. }
  454. .score-unit {
  455. font-size: 28rpx;
  456. color: rgba(255, 255, 255, 0.6);
  457. }
  458. .score-detail {
  459. font-size: 28rpx;
  460. color: rgba(255, 255, 255, 0.8);
  461. margin-bottom: 30rpx;
  462. }
  463. .dimension-tag {
  464. padding: 10rpx 32rpx;
  465. background: rgba(59, 130, 246, 0.15);
  466. border: 2rpx solid rgba(59, 130, 246, 0.5);
  467. border-radius: 40rpx;
  468. margin-bottom: 80rpx;
  469. }
  470. .dimension-name {
  471. font-size: 26rpx;
  472. color: #93C5FD;
  473. }
  474. /* --- 按钮 --- */
  475. .btn {
  476. width: 100%;
  477. height: 96rpx;
  478. line-height: 96rpx;
  479. border-radius: 48rpx;
  480. font-size: 32rpx;
  481. text-align: center;
  482. margin-bottom: 30rpx;
  483. padding: 0;
  484. }
  485. .btn::after {
  486. border: none;
  487. }
  488. .btn-primary {
  489. background: #3B82F6;
  490. color: #ffffff;
  491. }
  492. .btn-secondary {
  493. background: transparent;
  494. border: 2rpx solid #3B82F6;
  495. color: #3B82F6;
  496. box-sizing: border-box;
  497. }
  498. </style>