spatial-test.vue 12 KB

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