|
|
|
@ -0,0 +1,311 @@
|
|
|
|
|
<template>
|
|
|
|
|
<view class="quiz-container">
|
|
|
|
|
<!-- 顶部栏 -->
|
|
|
|
|
<view class="top-bar">
|
|
|
|
|
<view class="question-info">
|
|
|
|
|
<text class="question-text">{{ currentQuestion.lx }} ({{ currentQuestion.score }} 分)</text>
|
|
|
|
|
</view>
|
|
|
|
|
<button class="answer-card-btn" @click="showAnswerCard">答题卡</button>
|
|
|
|
|
</view>
|
|
|
|
|
|
|
|
|
|
<!-- 动态渲染题目 -->
|
|
|
|
|
<view v-for="(question, index) in quizData" :key="question.id" class="question">
|
|
|
|
|
<view v-show="activeIndex === index">
|
|
|
|
|
<!-- 题目文本 -->
|
|
|
|
|
<text class="question-text">{{ question.text }}</text>
|
|
|
|
|
|
|
|
|
|
<!-- 选项:支持图片或文本 -->
|
|
|
|
|
<radio-group
|
|
|
|
|
v-if="question.type === 'single'"
|
|
|
|
|
:modelValue="userAnswers[question.id]"
|
|
|
|
|
@update:modelValue="handleRadioChange(question.id, $event)"
|
|
|
|
|
>
|
|
|
|
|
<label v-for="(option, i) in question.options" :key="i" class="option">
|
|
|
|
|
<radio :value="option.value" />
|
|
|
|
|
<image v-if="option.image" :src="option.image" mode="aspectFit" class="option-image" />
|
|
|
|
|
<text v-else>{{ option.text }}</text>
|
|
|
|
|
</label>
|
|
|
|
|
</radio-group>
|
|
|
|
|
|
|
|
|
|
<!-- 其他题型类似处理 -->
|
|
|
|
|
</view>
|
|
|
|
|
</view>
|
|
|
|
|
|
|
|
|
|
<!-- 答案反馈 -->
|
|
|
|
|
<view v-if="showResult" class="result">
|
|
|
|
|
<text v-for="(res, key) in results" :key="key" :style="{ color: res ? 'green' : 'red' }">
|
|
|
|
|
第 {{ key + 1 }} 题:{{ res ? '✅ 正确' : '❌ 错误' }}
|
|
|
|
|
</text>
|
|
|
|
|
</view>
|
|
|
|
|
|
|
|
|
|
<!-- 答题卡 -->
|
|
|
|
|
<view v-if="showAnswerCardFlag" class="answer-card">
|
|
|
|
|
<view v-for="(question, index) in quizData" :key="question.id" class="card-item" @click="goToQuestion(index)">
|
|
|
|
|
<text>第 {{ index + 1 }} 题</text>
|
|
|
|
|
<text :style="{ color: userAnswers[question.id] ? 'blue' : 'gray' }">
|
|
|
|
|
{{ userAnswers[question.id] || '未作答' }}
|
|
|
|
|
</text>
|
|
|
|
|
</view>
|
|
|
|
|
</view>
|
|
|
|
|
|
|
|
|
|
<!-- 得分与正确答案展示 -->
|
|
|
|
|
<view v-if="showResult" class="score-and-answer">
|
|
|
|
|
<text>本题得分:{{ score }}</text>
|
|
|
|
|
<text>正确答案:{{ correctAnswer }}</text>
|
|
|
|
|
</view>
|
|
|
|
|
|
|
|
|
|
<!-- 底部控制按钮 -->
|
|
|
|
|
<view class="bottom-controls">
|
|
|
|
|
<button @click="prevQuestion">上一题</button>
|
|
|
|
|
<button @click="submitAnswers" type="primary">提交答案</button>
|
|
|
|
|
<button @click="nextQuestion">下一题</button>
|
|
|
|
|
</view>
|
|
|
|
|
</view>
|
|
|
|
|
</template>
|
|
|
|
|
|
|
|
|
|
<script setup lang="ts">
|
|
|
|
|
import { ref, computed } from 'vue'
|
|
|
|
|
import {onLoad} from "@dcloudio/uni-app";
|
|
|
|
|
|
|
|
|
|
// 题目数据(模拟从接口获取)
|
|
|
|
|
const quizData = [
|
|
|
|
|
{
|
|
|
|
|
id: 1,
|
|
|
|
|
type: 'single',
|
|
|
|
|
lx: '单选题',
|
|
|
|
|
score: 5,
|
|
|
|
|
text: '计算由曲线 y=x?和直线y=x所围成的平面区域的面积,下列选项中正确的是()。',
|
|
|
|
|
options: [
|
|
|
|
|
{ value: 'A', text: '选项A' },
|
|
|
|
|
{ value: 'B', text: '选项B' },
|
|
|
|
|
{ value: 'C', text: '选项C' },
|
|
|
|
|
{ value: 'D', text: '选项D' }
|
|
|
|
|
],
|
|
|
|
|
answer: 'B'
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
id: 2,
|
|
|
|
|
type: 'single',
|
|
|
|
|
lx: '单选题',
|
|
|
|
|
score: 5,
|
|
|
|
|
text: '计算由曲线 y=x?和直线y=x所围成的平面区域的面积,下列选项中正确的是()。',
|
|
|
|
|
options: [
|
|
|
|
|
{ value: 'A', text: '选项A' },
|
|
|
|
|
{ value: 'B', text: 'B' },
|
|
|
|
|
{ value: 'C', text: '选项C' },
|
|
|
|
|
{ value: 'D', text: '选项D' }
|
|
|
|
|
],
|
|
|
|
|
answer: 'B'
|
|
|
|
|
}
|
|
|
|
|
]
|
|
|
|
|
|
|
|
|
|
// 当前激活的问题索引
|
|
|
|
|
const activeIndex = ref(0)
|
|
|
|
|
|
|
|
|
|
// 控制是否显示答案结果
|
|
|
|
|
const showResult = ref(false)
|
|
|
|
|
|
|
|
|
|
// 控制答题卡显示/隐藏
|
|
|
|
|
const showAnswerCardFlag = ref(false)
|
|
|
|
|
|
|
|
|
|
// 获取当前题目
|
|
|
|
|
const currentQuestion = computed(() => {
|
|
|
|
|
return quizData[activeIndex.value] || {}
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
// 存储用户答案,以题目 ID 为键
|
|
|
|
|
const userAnswers = ref<Record<number, any>>({})
|
|
|
|
|
|
|
|
|
|
// 存储每道题的结果(true/false)
|
|
|
|
|
const results = ref<boolean[]>([])
|
|
|
|
|
|
|
|
|
|
// 处理单选题选择变化
|
|
|
|
|
function handleRadioChange(id: number, value: string) {
|
|
|
|
|
userAnswers.value[id] = value
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 上一题按钮逻辑
|
|
|
|
|
function prevQuestion() {
|
|
|
|
|
if (activeIndex.value > 0) activeIndex.value--
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 下一题按钮逻辑
|
|
|
|
|
function nextQuestion() {
|
|
|
|
|
if (activeIndex.value < quizData.length - 1) activeIndex.value++
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 点击答题卡跳转到指定题目
|
|
|
|
|
function goToQuestion(index: number) {
|
|
|
|
|
activeIndex.value = index
|
|
|
|
|
showAnswerCardFlag.value = false // 关闭答题卡
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 提交答案并判断正误
|
|
|
|
|
function submitAnswers() {
|
|
|
|
|
// 重置当前题目的结果
|
|
|
|
|
results.value[activeIndex.value] = false
|
|
|
|
|
|
|
|
|
|
const currentQuestion = quizData[activeIndex.value]
|
|
|
|
|
const userAnswer = userAnswers.value[currentQuestion.id] ?? ''
|
|
|
|
|
const correctAnswer = String(currentQuestion.answer)
|
|
|
|
|
|
|
|
|
|
const normalizedUserAnswer = String(userAnswer).trim().toUpperCase()
|
|
|
|
|
const normalizedCorrectAnswer = correctAnswer.trim().toUpperCase()
|
|
|
|
|
|
|
|
|
|
const isCorrect = normalizedUserAnswer == normalizedCorrectAnswer
|
|
|
|
|
results.value[activeIndex.value] = isCorrect
|
|
|
|
|
showResult.value = true
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 计算当前题目得分
|
|
|
|
|
const score = computed(() => {
|
|
|
|
|
const currentResult = results.value[activeIndex.value]
|
|
|
|
|
return currentResult ? currentQuestion.value.score : 0
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
// 获取当前题目的正确答案
|
|
|
|
|
const correctAnswer = computed(() => {
|
|
|
|
|
const currentQuestion = quizData[activeIndex.value]
|
|
|
|
|
if (!currentQuestion) return ''
|
|
|
|
|
const correctOption = currentQuestion.options.find(
|
|
|
|
|
option => option.value === currentQuestion.answer
|
|
|
|
|
)
|
|
|
|
|
return correctOption ? correctOption.text : ''
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
// 显示或隐藏答题卡
|
|
|
|
|
function showAnswerCard() {
|
|
|
|
|
showAnswerCardFlag.value = !showAnswerCardFlag.value
|
|
|
|
|
}
|
|
|
|
|
onLoad((options) => {
|
|
|
|
|
console.log(options)
|
|
|
|
|
})
|
|
|
|
|
</script>
|
|
|
|
|
|
|
|
|
|
<style scoped>
|
|
|
|
|
.quiz-container {
|
|
|
|
|
padding: 5px;
|
|
|
|
|
position: relative;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* 顶部栏样式 */
|
|
|
|
|
.top-bar {
|
|
|
|
|
display: flex;
|
|
|
|
|
justify-content: space-between;
|
|
|
|
|
align-items: center;
|
|
|
|
|
padding: 3px;
|
|
|
|
|
border-bottom: 1px solid #e0e0e0;
|
|
|
|
|
background-color: #d97e7e;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
.question-text {
|
|
|
|
|
font-size: 16px;
|
|
|
|
|
color: #333;
|
|
|
|
|
font-weight: bold;
|
|
|
|
|
margin-left: 10px;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
.answer-card-btn {
|
|
|
|
|
background-color: #63a2cf;
|
|
|
|
|
border: 1px solid #a8d5ff;
|
|
|
|
|
color: #000;
|
|
|
|
|
padding: 3px 6px;
|
|
|
|
|
border-radius: 4px;
|
|
|
|
|
font-size: 12px;
|
|
|
|
|
background-color: #4490ca;
|
|
|
|
|
margin-left: auto;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* 新增左侧竖线装饰 */
|
|
|
|
|
.question-info::before {
|
|
|
|
|
content: '';
|
|
|
|
|
display: inline-block;
|
|
|
|
|
width: 4px;
|
|
|
|
|
height: 20px;
|
|
|
|
|
background-color: #007bff;
|
|
|
|
|
margin-right: 3px;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
.question {
|
|
|
|
|
margin-bottom: 20px;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
.question-text {
|
|
|
|
|
font-size: 16px;
|
|
|
|
|
margin-bottom: 10px;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
.option {
|
|
|
|
|
display: flex;
|
|
|
|
|
align-items: center;
|
|
|
|
|
margin: 5px 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
.option-image {
|
|
|
|
|
width: 50px;
|
|
|
|
|
height: 50px;
|
|
|
|
|
margin-left: 10px;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
.result {
|
|
|
|
|
margin-top: 20px;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
.answer-card {
|
|
|
|
|
display: flex;
|
|
|
|
|
flex-wrap: wrap;
|
|
|
|
|
gap: 10px;
|
|
|
|
|
margin-top: 20px;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
.card-item {
|
|
|
|
|
padding: 8px;
|
|
|
|
|
border: 1px solid #ccc;
|
|
|
|
|
border-radius: 4px;
|
|
|
|
|
cursor: pointer;
|
|
|
|
|
background-color: #0ef306;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
.score-and-answer {
|
|
|
|
|
margin-top: 20px;
|
|
|
|
|
background-color: #f0f0f0;
|
|
|
|
|
padding: 10px;
|
|
|
|
|
border-radius: 8px;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
.bottom-controls {
|
|
|
|
|
position: fixed;
|
|
|
|
|
bottom: 0;
|
|
|
|
|
left: 0;
|
|
|
|
|
right: 0;
|
|
|
|
|
background-color: #fff;
|
|
|
|
|
padding: 5px;
|
|
|
|
|
display: flex;
|
|
|
|
|
justify-content: space-between;
|
|
|
|
|
align-items: center;
|
|
|
|
|
box-shadow: 0 -2px 6px rgba(0, 0, 0, 0.1);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
.bottom-controls button {
|
|
|
|
|
padding: 5px 10px;
|
|
|
|
|
font-size: 14px;
|
|
|
|
|
border-radius: 4px;
|
|
|
|
|
background-color: #f0f0f0;
|
|
|
|
|
color: #333;
|
|
|
|
|
border: none;
|
|
|
|
|
transition: all 0.3s ease;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
.bottom-controls button:hover {
|
|
|
|
|
background-color: #e0e0e0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
.bottom-controls button[type="primary"] {
|
|
|
|
|
background-color: #2196f3;
|
|
|
|
|
color: #fff;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
.bottom-controls button[type="primary"]:hover {
|
|
|
|
|
background-color: #1976d2;
|
|
|
|
|
}
|
|
|
|
|
</style>
|