through-question-doing.vue 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688
  1. <template>
  2. <div class="page-through-question-box">
  3. <van-nav-bar title="闯关答题" left-arrow @click-left="handleBackFun" />
  4. <!-- 题目卡片 -->
  5. <div v-if="answerThroughNumber" class="through-question-div">
  6. <div class="through-question-card">
  7. <div class="through-question-number">第{{ answerThroughNumber }}关</div>
  8. </div>
  9. </div>
  10. <!-- 题目卡片 -->
  11. <div class="through-question-div">
  12. <div v-if="examQuestionList.length > 0" class="through-question-card">
  13. <!-- 标题、分数、题页 -->
  14. <div class="through-question-head">
  15. <div class="through-question-head-left">
  16. <div class="through-question-head-left-icon"></div>
  17. <div class="through-question-head-left-txt">
  18. {{ examQuestionList[answerIndex].typeTxt || "undefind" }}
  19. </div>
  20. </div>
  21. <div class="through-question-head-right">
  22. <span class="through-question-head-right-now">{{
  23. answerIndex + 1
  24. }}</span>
  25. <span>/</span>
  26. <span>{{ examQuestionList.length }}</span>
  27. </div>
  28. </div>
  29. <!-- 描述 -->
  30. <div class="through-question-describe">
  31. {{
  32. examQuestionList[answerIndex].questionContent ||
  33. examQuestionList[answerIndex].content
  34. }}
  35. </div>
  36. <!-- 答题列表 -->
  37. <!-- 单选题、多选题、判断题 的选项区域 -->
  38. <div
  39. v-if="
  40. examQuestionList[answerIndex].type === questionType.TrueOrFalse ||
  41. examQuestionList[answerIndex].type ===
  42. questionType.singleChoice ||
  43. examQuestionList[answerIndex].type === questionType.multipleChoice
  44. "
  45. class="through-question-options"
  46. >
  47. <div
  48. v-for="(item, index) in examQuestionList[answerIndex].answers"
  49. :key="index"
  50. :class="{
  51. 'through-question-options-item': true,
  52. 'through-question-options-item-checked': answerValue.includes(
  53. item
  54. ),
  55. 'through-question-options-false':
  56. answerValue.includes(item) && answerStatus === 2
  57. }"
  58. @click="handleExamQuestionOptionsItemFun(item)"
  59. >
  60. {{ formatQuestionIndex(index) }}. {{ item }}
  61. </div>
  62. </div>
  63. <!-- 填空题的答题区域 -->
  64. <div
  65. v-if="examQuestionList[answerIndex].type === questionType.gapFilling"
  66. class="through-question-gapFilling"
  67. :class="{ 'through-question-gapFilling-false': answerStatus === 2 }"
  68. >
  69. <textarea
  70. v-for="(item, index) in examQuestionList[answerIndex]
  71. .questionInputNum"
  72. :key="index"
  73. :ref="'questionInputRef' + index"
  74. v-model="inputValue[index]"
  75. :disabled="[1, 2].includes(answerStatus)"
  76. maxlength="200"
  77. rows="1"
  78. @change="handleExamQuestionOptionsItemFun(inputValue, index)"
  79. />
  80. </div>
  81. <div
  82. v-if="answerRecruitId === 'daily-questions'"
  83. class="through-question-button-box"
  84. >
  85. <div class="through-question-single-button-box"></div>
  86. <div class="through-question-single-button-box">
  87. <van-button
  88. class="through-question-button"
  89. type="primary"
  90. color="#0088e9"
  91. :disabled="answerValue.length === 0"
  92. @click="handleSureFun"
  93. >确定</van-button
  94. >
  95. </div>
  96. <div class="through-question-single-button-box">
  97. <van-button
  98. v-show="examQuestionList[answerIndex].userAnswer.length"
  99. class="through-question-button"
  100. type="primary"
  101. color="#0088e9"
  102. @click="handleNextFun"
  103. >{{
  104. this.examQuestionList.length - 1 > this.answerIndex
  105. ? "下一题"
  106. : "完成"
  107. }}</van-button
  108. >
  109. </div>
  110. </div>
  111. <div v-else class="through-question-button-box">
  112. <div class="through-question-single-button-box">
  113. <van-button
  114. v-show="answerStatus === 0"
  115. class="through-question-button"
  116. type="primary"
  117. color="#0088e9"
  118. :disabled="answerValue.length === 0"
  119. @click="handleSureFun"
  120. >确定</van-button
  121. >
  122. </div>
  123. <div class="through-question-single-button-box">
  124. <van-button
  125. v-show="answerStatus === 2"
  126. class="through-question-button"
  127. type="primary"
  128. color="#0088e9"
  129. @click="handleNextFun"
  130. >{{
  131. this.examQuestionList.length - 1 > this.answerIndex
  132. ? "下一题"
  133. : "完成"
  134. }}</van-button
  135. >
  136. </div>
  137. </div>
  138. </div>
  139. <div v-else class="through-question-card">
  140. <div v-if="isInited" class="through-question-card-nodata">暂无数据</div>
  141. <div
  142. v-else
  143. class="through-question-card-nodata through-question-card-loading"
  144. >
  145. 加载中...
  146. </div>
  147. </div>
  148. </div>
  149. <!-- 题目卡片 -->
  150. <div
  151. v-if="examQuestionList.length > 0"
  152. v-show="answerStatus === 2"
  153. class="through-question-div"
  154. >
  155. <div class="through-question-card">
  156. <div class="through-question-result">
  157. 正确答案:{{
  158. formatQuestionFinalAnswerIndex(
  159. examQuestionList[answerIndex].finalAnswer
  160. )
  161. }}
  162. </div>
  163. <div class="through-question-analysis">
  164. 答案解析:{{ this.examQuestionList[this.answerIndex].answerAnalysis }}
  165. </div>
  166. </div>
  167. </div>
  168. </div>
  169. </template>
  170. <script>
  171. import { Dialog } from "vant";
  172. import { mapState } from "vuex";
  173. export default {
  174. name: "through-question-doing",
  175. components: {},
  176. data() {
  177. return {
  178. questionType: {
  179. singleChoice: "DanXuan", // 单选题
  180. multipleChoice: "DuoXuan", // 多选题
  181. gapFilling: "TianKong", // 填空题
  182. TrueOrFalse: "PanDuan" // 判断题
  183. }, // 试题类型
  184. examQuestionList: [], // 试题列表
  185. answerIndex: null, // 当前试题的下标索引
  186. answerValue: [], // 当前试题的所答
  187. inputValue: [], // 填空题时输入框的值
  188. isInited: false, // 是否已初始化完毕
  189. answerTime: {
  190. startTime: 0,
  191. endTime: 0
  192. }, // 答题的开始、结束时间
  193. answerStatus: 0 // 当前答题状态: 0未作答 1已做答且正确 2已做答但错误 3已完成当前的所有题目作答
  194. };
  195. },
  196. created() {
  197. this.initDataFun(); // 初始化数据信息
  198. },
  199. watch: {},
  200. computed: {
  201. ...mapState({
  202. answerRecruitId: state => state.answer.answerRecruitId,
  203. userInfo: state => state.user.userInfo,
  204. chooseEngneeringWork: state => state.user.chooseEngneeringWork,
  205. answerThroughNumber: state => state.answer.answerThroughNumber
  206. })
  207. },
  208. methods: {
  209. // 初始化数据信息
  210. initDataFun() {
  211. this.examStartFun(); // 方法:开始考试
  212. },
  213. // 方法:开始考试
  214. examStartFun() {
  215. if (!this.answerRecruitId) {
  216. Dialog.alert({
  217. message: "试题信息有误,请重新进入当前页面",
  218. theme: "round-button"
  219. }).then(() => {
  220. this.$router.back();
  221. });
  222. return;
  223. }
  224. this.$store.commit("toggleLoading", true);
  225. let params = {
  226. type: this.chooseEngneeringWork.value
  227. };
  228. this.$_http
  229. .post(
  230. this.$pathParams(this.$_API.POST_JTXT_GET_EXAMS_START, {
  231. examId: this.answerRecruitId
  232. }),
  233. this.userInfo,
  234. { params }
  235. )
  236. .then(res => {
  237. if (res.data) {
  238. let httpResultData = [];
  239. res.data.forEach((item, index) => {
  240. this.getExamQuestionsListFun(
  241. item,
  242. httpResultData,
  243. index === res.data.length - 1
  244. );
  245. });
  246. }
  247. })
  248. .catch(() => {
  249. this.$store.commit("toggleLoading", false);
  250. });
  251. },
  252. // 查询:试题信息
  253. async getExamQuestionsListFun(questionId, httpResultData, isLast) {
  254. this.$_http
  255. .get(
  256. this.$pathParams(this.$_API.GET_JTXT_GET_EXAMS_ONE_QUESTIONS_LIST, {
  257. questionId: questionId
  258. })
  259. )
  260. .then(res => {
  261. let resData = { ...res.data };
  262. httpResultData.push(this.setPersonDataFun(resData));
  263. if (isLast) {
  264. // 临时解决多个异步请求进行的问题
  265. this.examQuestionList = httpResultData;
  266. this.handleExamQuestionItemFun(this.examQuestionList[0], 0); // 设置第一题开始
  267. let curTime = new Date();
  268. this.answerTime.startTime = curTime; // 赋值开始时间
  269. this.isInited = true;
  270. this.$store.commit("toggleLoading", false);
  271. }
  272. })
  273. .catch(() => {
  274. this.$store.commit("toggleLoading", false);
  275. });
  276. },
  277. // 方法:过滤试题的类型,添加用户作答的字段
  278. setPersonDataFun(httpResultDataItme) {
  279. let results = this.formatQuestionType(
  280. httpResultDataItme.type,
  281. httpResultDataItme.content
  282. );
  283. httpResultDataItme.typeTxt = results.typeTxt;
  284. httpResultDataItme.questionContent = results.questionContent;
  285. httpResultDataItme.questionInputNum = results.questionInputNum;
  286. httpResultDataItme.userAnswer = [];
  287. return httpResultDataItme;
  288. },
  289. // 方法:过滤试题的类型、填空题的题目、填空题的回答框个数
  290. formatQuestionType(type, content) {
  291. let typeTxt = "";
  292. let questionContent = "";
  293. let questionInputNum = 0;
  294. switch (type) {
  295. case this.questionType.singleChoice:
  296. typeTxt = "单选题";
  297. break;
  298. case this.questionType.multipleChoice:
  299. typeTxt = "多选题";
  300. break;
  301. case this.questionType.gapFilling:
  302. typeTxt = "填空题";
  303. questionInputNum = this.getPlaceholderCount(content);
  304. questionContent = content.replace("$PH$", "()");
  305. break;
  306. case this.questionType.TrueOrFalse:
  307. typeTxt = "判断题";
  308. break;
  309. default:
  310. break;
  311. }
  312. return {
  313. typeTxt: typeTxt,
  314. questionContent: questionContent,
  315. questionInputNum: questionInputNum
  316. };
  317. },
  318. // 方法:统计字符串中包含的特定字符个数
  319. getPlaceholderCount(strSource) {
  320. // 统计字符串中包含{}或{xxXX}的个数
  321. let countNum = 0;
  322. strSource.replace("$PH$", function(m, i) {
  323. // m为找到的{xx}元素、i为索引
  324. countNum++;
  325. });
  326. return countNum;
  327. },
  328. // 方法:过滤正确答案的选项的编号
  329. formatQuestionFinalAnswerIndex(finalAnswer) {
  330. let txt = "";
  331. switch (this.examQuestionList[this.answerIndex].type) {
  332. case this.questionType.singleChoice:
  333. case this.questionType.multipleChoice:
  334. case this.questionType.TrueOrFalse:
  335. finalAnswer.forEach((item, index) => {
  336. txt = txt + item;
  337. });
  338. break;
  339. case this.questionType.gapFilling:
  340. finalAnswer.forEach((item, index) => {
  341. txt = txt + (index > 0 ? "、" : "") + item;
  342. });
  343. break;
  344. default:
  345. break;
  346. }
  347. return txt;
  348. },
  349. // 操作:点击了某个题序
  350. handleExamQuestionItemFun(item, index) {
  351. if (this.answerIndex === index || !this.examQuestionList.length) {
  352. return;
  353. }
  354. this.answerStatus = 0;
  355. this.answerIndex = index;
  356. switch (item.type) {
  357. case this.questionType.singleChoice:
  358. case this.questionType.multipleChoice:
  359. case this.questionType.TrueOrFalse:
  360. this.answerValue = [...new Set(item.userAnswer)];
  361. break;
  362. case this.questionType.gapFilling:
  363. this.answerValue = [...new Set(item.userAnswer)];
  364. this.inputValue = this.answerValue || [];
  365. break;
  366. default:
  367. break;
  368. }
  369. },
  370. // 操作:作答了某个选项
  371. handleExamQuestionOptionsItemFun(value, index) {
  372. // 答错了的不允许修改
  373. if (this.answerStatus === 2) {
  374. return;
  375. }
  376. let answerQuestionType = this.examQuestionList[this.answerIndex].type;
  377. switch (answerQuestionType) {
  378. // 判断题、单选题
  379. case this.questionType.TrueOrFalse:
  380. case this.questionType.singleChoice:
  381. this.answerValue = [value];
  382. break;
  383. // 多选题
  384. case this.questionType.multipleChoice:
  385. // 遍历所答的数组,看是否有重复的
  386. let repetitiveIndex = null;
  387. for (let i = 0; i < this.answerValue.length; i++) {
  388. let it = this.answerValue[i];
  389. if (value === it) {
  390. repetitiveIndex = i;
  391. break;
  392. }
  393. }
  394. // 如果有重复的就移除
  395. if (repetitiveIndex !== null) {
  396. this.answerValue.splice(repetitiveIndex, 1);
  397. } else {
  398. this.answerValue.push(value);
  399. }
  400. break;
  401. // 填空题
  402. case this.questionType.gapFilling:
  403. this.$refs["questionInputRef" + index][0].blur(); // 清除input的焦点
  404. this.answerValue = value;
  405. break;
  406. default:
  407. break;
  408. }
  409. },
  410. // 操作:确定
  411. handleSureFun() {
  412. if (!this.examQuestionList.length) {
  413. return;
  414. }
  415. this.examQuestionList[this.answerIndex].userAnswer = this.answerValue;
  416. this.answerStatus = this.getAnswerItemResultFun(
  417. this.examQuestionList[this.answerIndex]
  418. )
  419. ? 1
  420. : 2;
  421. if (this.answerStatus === 1) {
  422. this.handleNextFun();
  423. } else if (this.answerStatus === 2) {
  424. Dialog.alert({
  425. message: "本题答错,闯关失败",
  426. theme: "round-button"
  427. }).then(() => {
  428. this.$router.back();
  429. });
  430. }
  431. },
  432. // 方法:判断当前题目是否正确
  433. getAnswerItemResultFun(item) {
  434. // let isSure = false;
  435. // let sureNum = 0;
  436. // item.finalAnswer.forEach(it => {
  437. // item.userAnswer.forEach(answerItem => {
  438. // if (it === answerItem) {
  439. // sureNum++;
  440. // }
  441. // });
  442. // });
  443. // if (sureNum === item.finalAnswer.length) {
  444. // isSure = true;
  445. // }
  446. // return isSure;
  447. let isSure = false;
  448. let lastAnswer = [""];
  449. lastAnswer[0] = this.getUserAnswerSortFun(item);
  450. if (lastAnswer[0] === item.finalAnswer[0]) {
  451. isSure = true;
  452. }
  453. return isSure;
  454. },
  455. // 获取用户所选选项的升序编号答案
  456. getUserAnswerSortFun(item) {
  457. let newUnserAnswer = [];
  458. item.answers.forEach((itemOne, indexOne) => {
  459. item.userAnswer.forEach(itemTwo => {
  460. if (itemOne === itemTwo) {
  461. newUnserAnswer.push(this.formatQuestionIndex(indexOne));
  462. }
  463. });
  464. });
  465. newUnserAnswer.sort();
  466. return newUnserAnswer.join("");
  467. },
  468. // 操作:下一题
  469. handleNextFun() {
  470. let nextIndex = this.answerIndex + 1;
  471. if (nextIndex >= this.examQuestionList.length) {
  472. this.examsEndFun(); // 结束考试
  473. return;
  474. }
  475. this.handleExamQuestionItemFun(
  476. this.examQuestionList[nextIndex],
  477. nextIndex
  478. );
  479. },
  480. // 方法:结束考试
  481. examsEndFun() {
  482. let curTime = new Date();
  483. this.answerTime.endTime = curTime; // 赋值开始时间
  484. this.$store.commit("toggleLoading", true);
  485. let grades = this.getUserExamAllPointsFun(); // 方法:计算成绩
  486. let answers = this.getUserExamAllAnswersFun(); // 方法:获取当前用户所有题目作答的答案
  487. let params = {
  488. userId: this.userInfo.userName,
  489. examId: this.answerRecruitId,
  490. points: grades, // 成绩:积分
  491. startTime: "", // this.answerTime.startTime,
  492. endTime: "", // this.answerTime.endTime
  493. userAnswers: answers
  494. };
  495. this.$_http
  496. .post(
  497. this.$pathParams(this.$_API.POST_JTXT_GET_EXAMS_END, {
  498. examId: this.answerRecruitId
  499. }),
  500. params
  501. )
  502. .then(res => {
  503. this.$store.commit("toggleLoading", false);
  504. this.$router.replace({
  505. name: "throughQuestionEnd",
  506. params: {
  507. allQuestionsNum: this.examQuestionList.length,
  508. grades: grades
  509. }
  510. });
  511. })
  512. .catch(() => {
  513. this.$store.commit("toggleLoading", false);
  514. Dialog.close();
  515. Dialog({ message: "结束考试异常,请联系系统管理员" });
  516. this.$router.back();
  517. });
  518. },
  519. // 方法:计算成绩
  520. getUserExamAllPointsFun() {
  521. let grades = 0;
  522. this.examQuestionList.forEach(item => {
  523. let isSure = this.getAnswerItemResultFun(item);
  524. if (isSure) {
  525. grades++; // 默认每题一分
  526. }
  527. });
  528. return grades;
  529. },
  530. // 方法:获取题目的答案
  531. getUserExamAllAnswersFun() {
  532. let answers = [];
  533. this.examQuestionList.forEach((item, index) => {
  534. // answers[index] = item.userAnswer.join("||");
  535. answers[index] = this.getUserAnswerSortFun(item);
  536. });
  537. return answers;
  538. },
  539. // 操作:返回
  540. handleBackFun() {
  541. this.$router.back();
  542. }
  543. }
  544. };
  545. </script>
  546. <style lang="scss" scoped>
  547. @import "~@/styles/mixin";
  548. .page-through-question-box {
  549. width: 100%;
  550. height: 100%;
  551. overflow-y: auto;
  552. overflow-x: hidden;
  553. font-size: 0.6rem;
  554. .through-question-div {
  555. padding: 0.5rem 0.5rem;
  556. .through-question-card {
  557. padding: 0.5rem 0.5rem;
  558. background-color: #fff;
  559. border-radius: 4px;
  560. box-shadow: 0px 3px 6px rgba(0, 0, 0, 0.16);
  561. .through-question-head {
  562. display: flex;
  563. justify-content: space-between;
  564. align-items: center;
  565. flex-wrap: nowrap;
  566. border-bottom: 1px solid #0088e9;
  567. padding-bottom: 0.25rem;
  568. height: 1.25rem;
  569. font-size: 0.65rem;
  570. .through-question-head-left {
  571. display: flex;
  572. align-items: center;
  573. flex-wrap: nowrap;
  574. .through-question-head-left-icon {
  575. width: 0.25rem;
  576. height: 1rem;
  577. background-color: #0088e9;
  578. }
  579. .through-question-head-left-txt {
  580. margin-left: 0.25rem;
  581. margin-right: 0.25rem;
  582. font-weight: bold;
  583. }
  584. }
  585. .through-question-head-right {
  586. span {
  587. color: #666;
  588. }
  589. .through-question-head-right-now {
  590. font-weight: bold;
  591. color: #000;
  592. }
  593. }
  594. }
  595. .through-question-describe {
  596. padding: 0.5rem 0;
  597. font-size: 0.65rem;
  598. }
  599. .through-question-options {
  600. .through-question-options-item {
  601. width: 100%;
  602. padding: 0.5rem 0.25rem;
  603. background-color: #f3f3f3;
  604. margin-bottom: 0.5rem;
  605. border: 1px solid transparent;
  606. }
  607. .through-question-options-item-checked {
  608. border-color: #0088e9;
  609. color: #0088e9;
  610. }
  611. .through-question-options-false {
  612. border-color: red !important;
  613. background-color: #fedada !important;
  614. }
  615. }
  616. .through-question-gapFilling {
  617. padding-top: 0.5rem;
  618. textarea {
  619. width: 100%;
  620. padding: 0.25rem 0.25rem;
  621. background-color: #f3f3f3;
  622. margin-bottom: 0.5rem;
  623. border: 1px solid transparent;
  624. &:active,
  625. &:focus {
  626. border-color: #0088e9;
  627. }
  628. }
  629. }
  630. .through-question-gapFilling-false {
  631. textarea {
  632. border-color: red !important;
  633. background-color: #fedada !important;
  634. }
  635. }
  636. .through-question-button-box {
  637. display: flex;
  638. flex-direction: row;
  639. justify-content: center;
  640. align-items: center;
  641. .through-question-single-button-box {
  642. width: 30%;
  643. padding: 0.5rem 0.5rem;
  644. display: flex;
  645. flex-direction: row;
  646. justify-content: center;
  647. align-items: center;
  648. .through-question-button {
  649. width: 6rem;
  650. height: auto;
  651. padding: 0.5rem 0.5rem;
  652. font-size: 0.65rem;
  653. margin-top: 0.75rem;
  654. }
  655. }
  656. }
  657. .through-question-result {
  658. border-bottom: 1px solid #0088e9;
  659. padding-bottom: 0.25rem;
  660. font-size: 0.6rem;
  661. }
  662. .through-question-analysis {
  663. padding-top: 0.25rem;
  664. }
  665. .through-question-card-loading,
  666. .through-question-card-nodata {
  667. width: 100%;
  668. height: 5rem;
  669. color: #333;
  670. font-size: 0.7rem;
  671. display: flex;
  672. justify-content: center;
  673. align-items: center;
  674. }
  675. .through-question-card-loading {
  676. color: #0088e9;
  677. }
  678. .through-question-number {
  679. padding: 0.25rem 0;
  680. font-size: 0.7rem;
  681. text-align: center;
  682. }
  683. }
  684. }
  685. }
  686. </style>