question-card.vue 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383
  1. <!--
  2. * @description:
  3. -->
  4. <template>
  5. <div>
  6. <div class="type">
  7. <div class="bg">
  8. <div v-if="answerList[currentQuestion]" class="question-type">
  9. {{ answerList[currentQuestion].quType }}
  10. </div>
  11. </div>
  12. <div class="bg1">
  13. <div class="question-type">
  14. 第{{ currentQuestion + 1 }}题
  15. <span>/共{{ answerList.length }}题</span>
  16. </div>
  17. </div>
  18. </div>
  19. <div class="question">
  20. <template>
  21. <div v-if="answerList[currentQuestion]" class="question-title">{{ answerList[currentQuestion].title }}</div>
  22. <div class="question-answer">
  23. <div class="question-answer-item">
  24. <div v-if="answerList[currentQuestion] && answerList[currentQuestion].answer"
  25. v-for="(el, idx) in answerList[currentQuestion].answer"
  26. :class="{
  27. active: isOptionSelected(currentQuestion, idx),
  28. }"
  29. class="question-answer-item-title" @click="chooseAnswer(el, currentQuestion, idx)">
  30. <span class="num">{{ getOptionLabel(idx) }}</span>
  31. {{ el.content }}
  32. </div>
  33. </div>
  34. </div>
  35. </template>
  36. <div class="choose-question">
  37. <div v-if="currentQuestion > 0" class="btn line" @click="preQuestion">上一题</div>
  38. <div v-if="currentQuestion < answerList.length - 1" class="btn" @click="nextQuestion">下一题</div>
  39. </div>
  40. <div class="time">倒计时 {{ formattedTime }}</div>
  41. <div class="tips"><span>无需答题,直接交卷</span></div>
  42. <div class="btn line none" @click="submit">提交答卷</div>
  43. </div>
  44. </div>
  45. </template>
  46. <script>
  47. export default {
  48. name: "QuestionCard",
  49. props: {
  50. answerList: {
  51. type: Array,
  52. default: () => []
  53. },
  54. exTime: { // 新增
  55. type: Number,
  56. default: 0
  57. }
  58. },
  59. data() {
  60. return {
  61. timeLeft: 0, // 45 分钟,单位秒
  62. timer: null,
  63. activeIndex: [], // 改为存储数组,支持多选
  64. currentQuestion: 0
  65. }
  66. },
  67. computed: {
  68. formattedTime() {
  69. const minutes = Math.floor(this.timeLeft / 60);
  70. const seconds = this.timeLeft % 60;
  71. return `${String(minutes).padStart(2, '0')}:${String(seconds).padStart(2, '0')}`;
  72. },
  73. // 判断当前题目是否为多选题
  74. isMultipleChoice() {
  75. const currentQuestion = this.answerList[this.currentQuestion];
  76. return currentQuestion && currentQuestion.quType &&
  77. (currentQuestion.quType.includes('多选') ||
  78. currentQuestion.quType.includes('multiple') ||
  79. currentQuestion.quType === '2'); // 假设2表示多选题
  80. }
  81. },
  82. mounted() {
  83. this.timeLeft = this.exTime*60 || 0;
  84. this.startCountdown();
  85. },
  86. beforeDestroy() {
  87. if (this.timer) {
  88. clearInterval(this.timer);
  89. }
  90. },
  91. methods: {
  92. startCountdown() {
  93. this.timer = setInterval(() => {
  94. if (this.timeLeft > 0) {
  95. this.timeLeft--;
  96. } else {
  97. clearInterval(this.timer);
  98. this.$emit("timeout"); // 可选:通知父组件时间到
  99. uni.showToast({ title: '测试时间结束', icon: 'none' });
  100. }
  101. }, 1000);
  102. },
  103. getOptionLabel(index) {
  104. return String.fromCharCode(65 + index); // 65 是 'A' 的 Unicode 编码
  105. },
  106. // 判断选项是否被选中
  107. isOptionSelected(questionIndex, optionIndex) {
  108. const selectedOptions = this.activeIndex[questionIndex];
  109. if (!selectedOptions) return false;
  110. if (Array.isArray(selectedOptions)) {
  111. return selectedOptions.some(option => option.eroConnNo === optionIndex);
  112. } else {
  113. return selectedOptions.eroConnNo === optionIndex;
  114. }
  115. },
  116. // 选择答案(支持多选)
  117. chooseAnswer(el, questionIndex, optionIndex) {
  118. const currentQuestion = this.answerList[questionIndex];
  119. const isMultiple = currentQuestion && currentQuestion.quType &&
  120. (currentQuestion.quType.includes('多选') ||
  121. currentQuestion.quType.includes('multiple') ||
  122. currentQuestion.quType === '2');
  123. if (isMultiple) {
  124. // 多选题逻辑
  125. let selectedOptions = this.activeIndex[questionIndex] || [];
  126. // 如果selectedOptions不是数组(可能是旧数据),转换为数组
  127. if (!Array.isArray(selectedOptions)) {
  128. selectedOptions = selectedOptions.eroConnNo !== undefined ? [selectedOptions] : [];
  129. }
  130. // 检查是否已经选中
  131. const existingIndex = selectedOptions.findIndex(option => option.eroConnNo === optionIndex);
  132. if (existingIndex > -1) {
  133. // 如果已经选中,移除
  134. selectedOptions.splice(existingIndex, 1);
  135. } else {
  136. // 如果未选中,添加
  137. selectedOptions.push({
  138. "quId": el.quId,
  139. "eroConnNo": optionIndex,
  140. "eroSort": optionIndex // 使用optionIndex作为eroSort
  141. });
  142. }
  143. this.$set(this.activeIndex, questionIndex, selectedOptions);
  144. } else {
  145. // 单选题逻辑(保持原有逻辑)
  146. this.$set(this.activeIndex, questionIndex, {
  147. "quId": el.quId,
  148. "eroConnNo": optionIndex,
  149. "eroSort": optionIndex // 使用optionIndex作为eroSort
  150. });
  151. }
  152. this.$emit("chooseAnswer", this.activeIndex);
  153. },
  154. submit() {
  155. const leftTimeSec = this.exTime*60 - this.timeLeft; // 剩余秒数
  156. this.$emit("submit",leftTimeSec)
  157. },
  158. preQuestion() {
  159. console.log(this.currentQuestion)
  160. this.currentQuestion = this.currentQuestion === 0 ? 0 : this.currentQuestion - 1;
  161. this.$emit("currentQuestion", this.currentQuestion)
  162. },
  163. nextQuestion(idx) {
  164. this.currentQuestion = this.currentQuestion + 1
  165. this.$emit("currentQuestion", this.currentQuestion)
  166. },
  167. selectQuestion(idx) {
  168. this.currentQuestion = idx; // 更新当前题目索引
  169. this.$emit("currentQuestion", this.currentQuestion); // 向父组件传递新的题目索引
  170. },
  171. getExamTime(exTime) {
  172. this.timeLeft = exTime;
  173. }
  174. }
  175. }
  176. </script>
  177. <style lang="scss" scoped>
  178. .type {
  179. position: relative;
  180. height: 120rpx;
  181. padding: 0 42rpx;
  182. overflow: hidden;
  183. .bg {
  184. position: absolute;
  185. left: 0;
  186. width: 60%;
  187. height: 100%;
  188. background: linear-gradient(-130deg, transparent 80rpx, #D2EBFA 0, #D2EBFA 0);
  189. border-radius: 56rpx 256rpx 0 0;
  190. z-index: 1;
  191. padding: 40rpx 0 0 28rpx;
  192. }
  193. .bg1 {
  194. position: absolute;
  195. width: 50%;
  196. top: 20rpx;
  197. right: 0;
  198. height: 100%;
  199. background: linear-gradient(180deg, #4583F0 0%, #4883F6 100%);
  200. border-radius: 56rpx 56rpx 0 0;
  201. padding: 40rpx 0 0 42rpx;
  202. .question-type {
  203. padding-right: 28rpx;
  204. font-weight: bold;
  205. font-size: 32rpx;
  206. color: #FFFFFF;
  207. line-height: 40rpx;
  208. text-align: right;
  209. span {
  210. font-size: 26rpx;
  211. color: rgba(255, 255, 255, 0.6);
  212. line-height: 32rpx;
  213. }
  214. }
  215. }
  216. .question-type {
  217. font-size: 40rpx;
  218. line-height: 48rpx;
  219. color: #496CF4;
  220. span {
  221. margin-left: 14rpx;
  222. font-size: 26rpx;
  223. color: #6896F6;
  224. line-height: 30rpx;
  225. }
  226. }
  227. }
  228. .question {
  229. padding: 56rpx 48rpx;
  230. background: #FFFFFF;
  231. border-radius: 0 32rpx 32rpx 32rpx;
  232. box-shadow: 0 8rpx 20rpx 0 rgba(0, 0, 0, 0.06);
  233. .question-title {
  234. font-weight: 400;
  235. font-size: 32rpx;
  236. color: #3D3D3D;
  237. line-height: 38rpx;
  238. }
  239. .question-answer-item-title {
  240. display: flex;
  241. align-items: center;
  242. background: #F1F7FE;
  243. border-radius: 80rpx;
  244. padding: 16rpx 48rpx 16rpx 16rpx;
  245. font-size: 28rpx;
  246. color: #3D3D3D;
  247. line-height: 34rpx;
  248. margin-top: 36rpx;
  249. border: 2rpx solid rgba(255, 255, 255, 0.5);
  250. position: relative;
  251. .num {
  252. display: inline-flex;
  253. align-items: center;
  254. justify-content: center;
  255. width: 64rpx;
  256. height: 64rpx;
  257. background: linear-gradient(135deg, #6E9BFC 0%, #496CF4 100%);
  258. box-shadow: 4rpx 4rpx 16rpx 0 rgba(76, 112, 246, 0.3);
  259. border-radius: 80rpx;
  260. margin-right: 24rpx;
  261. font-size: 28rpx;
  262. color: #FFFFFF;
  263. }
  264. &.active {
  265. background: #F1F7FE;
  266. box-shadow: 0 0 20rpx 0 rgba(76, 112, 246, 0.6);
  267. border-radius: 80rpx;
  268. border-color: rgba(76, 112, 246, 0.5);
  269. &:after {
  270. content: "";
  271. position: absolute;
  272. right: 48rpx;
  273. display: inline-flex;
  274. width: 48rpx;
  275. height: 48rpx;
  276. background: url("../../../static/images/dui.png") no-repeat;
  277. background-size: 100%;
  278. }
  279. }
  280. }
  281. }
  282. .choose-question {
  283. display: flex;
  284. gap: 32rpx;
  285. padding-top: 32rpx;
  286. .btn {
  287. flex: 1;
  288. }
  289. }
  290. .btn,
  291. .time {
  292. margin: 36rpx 0;
  293. border-radius: 80rpx;
  294. font-weight: bold;
  295. font-size: 32rpx;
  296. color: #FFFFFF;
  297. text-align: center;
  298. &.none {
  299. margin-bottom: 0;
  300. }
  301. }
  302. .btn {
  303. border: 2px solid #fff;
  304. padding: 18rpx 0;
  305. background: linear-gradient(135deg, #6E9BFC 0%, #496CF4 100%);
  306. &.line {
  307. background: inherit;
  308. border-color: #496CF4;
  309. color: #496CF4;
  310. }
  311. }
  312. .time {
  313. margin: 32rpx 0;
  314. color: #333333;
  315. }
  316. .tips {
  317. font-weight: 400;
  318. font-size: 24rpx;
  319. color: #999999;
  320. text-align: center;
  321. position: relative;
  322. &:after {
  323. content: "";
  324. display: inline-block;
  325. width: 100%;
  326. position: absolute;
  327. top: 50%;
  328. left: 0;
  329. border-bottom: 2rpx dashed #E5E5E5;
  330. transform: translateY(-50%);
  331. }
  332. span {
  333. position: relative;
  334. padding: 0 8rpx;
  335. background: #fff;
  336. z-index: 2;
  337. }
  338. }
  339. </style>