page-answer-recruit.vue 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688
  1. <template>
  2. <div class="page-exam-question-box">
  3. <van-nav-bar title="答题" left-arrow @click-left="handleBackFun" />
  4. <!-- 题目卡片 -->
  5. <div class="exam-question-div">
  6. <div v-if="examQuestionList.length > 0" class="exam-question-card">
  7. <!-- 标题、分数、题页 -->
  8. <div class="exam-question-head">
  9. <div class="exam-question-head-left">
  10. <div class="exam-question-head-left-icon"></div>
  11. <div class="exam-question-head-left-txt">
  12. {{ examQuestionList[answerIndex].typeTxt || "undefind" }}
  13. </div>
  14. <van-tag
  15. plain
  16. type="primary"
  17. color="red"
  18. v-if="answerRecruitId === 'daily-questions'"
  19. >今日必答</van-tag
  20. >
  21. <!-- <span class="exam-question-head-left-txt"
  22. >{{ examQuestionList[answerIndex].grade || 1 }} 分</span
  23. > -->
  24. </div>
  25. <div class="exam-question-head-right">
  26. <span class="exam-question-head-right-now">{{
  27. answerIndex + 1
  28. }}</span>
  29. <span>/</span>
  30. <span>{{ examQuestionList.length }}</span>
  31. </div>
  32. </div>
  33. <!-- 描述 -->
  34. <div class="exam-question-describe">
  35. {{
  36. examQuestionList[answerIndex].questionContent ||
  37. examQuestionList[answerIndex].content
  38. }}
  39. </div>
  40. <!-- 答题列表 -->
  41. <!-- 单选题、多选题、判断题 的选项区域 -->
  42. <div
  43. v-if="
  44. examQuestionList[answerIndex].type === questionType.TrueOrFalse ||
  45. examQuestionList[answerIndex].type === questionType.singleChoice ||
  46. examQuestionList[answerIndex].type === questionType.multipleChoice
  47. "
  48. class="exam-question-options"
  49. >
  50. <div
  51. v-for="(item, index) in examQuestionList[answerIndex].answers"
  52. :key="index"
  53. :class="{
  54. 'exam-question-options-item': true,
  55. 'exam-question-options-item-checked': answerValue.includes(item),
  56. 'exam-question-options-false':
  57. answerValue.includes(item) && answerStatus === 2,
  58. }"
  59. @click="handleExamQuestionOptionsItemFun(item)"
  60. >
  61. {{ formatQuestionIndex(index) }}. {{ item }}
  62. </div>
  63. </div>
  64. <!-- 填空题的答题区域 -->
  65. <div
  66. v-if="examQuestionList[answerIndex].type === questionType.gapFilling"
  67. class="exam-question-gapFilling"
  68. :class="{ 'exam-question-gapFilling-false': answerStatus === 2 }"
  69. >
  70. <textarea
  71. v-for="(item, index) in examQuestionList[answerIndex]
  72. .questionInputNum"
  73. :key="index"
  74. :ref="'questionInputRef' + index"
  75. v-model="inputValue[index]"
  76. :disabled="[1, 2].includes(answerStatus)"
  77. maxlength="200"
  78. rows="1"
  79. @change="handleExamQuestionOptionsItemFun(inputValue, index)"
  80. />
  81. </div>
  82. <div
  83. v-if="answerRecruitId === 'daily-questions'"
  84. class="exam-question-button-box"
  85. >
  86. <div class="exam-question-single-button-box">
  87. <van-button
  88. v-if="this.answerIndex !== 0"
  89. class="exam-question-button"
  90. type="primary"
  91. color="#0088e9"
  92. @click="handleLastFun"
  93. >上一题</van-button
  94. >
  95. </div>
  96. <div class="exam-question-single-button-box">
  97. <van-button
  98. class="exam-question-button"
  99. type="primary"
  100. color="#0088e9"
  101. @click="handleSureFun"
  102. >确定</van-button
  103. >
  104. </div>
  105. <div class="exam-question-single-button-box">
  106. <van-button
  107. v-if="this.answerIndex < this.examQuestionList.length - 1"
  108. class="exam-question-button"
  109. type="primary"
  110. color="#0088e9"
  111. @click="handleNextFun"
  112. >下一题</van-button
  113. >
  114. </div>
  115. </div>
  116. <div v-else class="exam-question-button-box">
  117. <div class="exam-question-single-button-box"></div>
  118. <div class="exam-question-single-button-box">
  119. <van-button
  120. v-show="answerStatus === 0"
  121. class="exam-question-button"
  122. type="primary"
  123. color="#0088e9"
  124. @click="handleSureFun"
  125. >确定</van-button
  126. >
  127. </div>
  128. <div class="exam-question-single-button-box">
  129. <van-button
  130. v-show="answerStatus === 2"
  131. class="exam-question-button"
  132. type="primary"
  133. color="#0088e9"
  134. @click="handleNextFun"
  135. >下一题</van-button
  136. >
  137. </div>
  138. </div>
  139. </div>
  140. <div v-else class="exam-question-card">
  141. <div v-if="isInited" class="exam-question-card-nodata">暂无数据</div>
  142. <div
  143. v-else
  144. class="exam-question-card-nodata exam-question-card-loading"
  145. >
  146. 加载中...
  147. </div>
  148. </div>
  149. </div>
  150. <!-- 题目卡片 -->
  151. <div
  152. v-if="examQuestionList.length > 0"
  153. v-show="answerStatus === 2"
  154. class="exam-question-div"
  155. >
  156. <div class="exam-question-card">
  157. <div class="exam-question-result">
  158. 正确答案:{{
  159. formatQuestionFinalAnswerIndex(
  160. examQuestionList[answerIndex].finalAnswer
  161. )
  162. }}
  163. </div>
  164. <div class="exam-question-analysis">
  165. 答案解析:{{ this.examQuestionList[this.answerIndex].answerAnalysis }}
  166. </div>
  167. </div>
  168. </div>
  169. </div>
  170. </template>
  171. <script>
  172. import { Dialog } from "vant";
  173. import { mapState } from "vuex";
  174. export default {
  175. name: "page-exam-item-doing",
  176. components: {},
  177. data() {
  178. return {
  179. questionType: {
  180. singleChoice: "DanXuan", // 单选题
  181. multipleChoice: "DuoXuan", // 多选题
  182. gapFilling: "TianKong", // 填空题
  183. TrueOrFalse: "PanDuan" // 判断题
  184. }, // 试题类型
  185. examQuestionList: [], // 试题列表
  186. answerIndex: null, // 当前试题的下标索引
  187. answerValue: [], // 当前试题的所答
  188. inputValue: [], // 填空题时输入框的值
  189. isInited: false, // 是否已初始化完毕
  190. answerTime: {
  191. startTime: 0,
  192. endTime: 0
  193. }, // 答题的开始、结束时间
  194. answerStatus: 0 // 当前答题状态: 0未作答 1已做答且正确 2已做答但错误
  195. };
  196. },
  197. created() {
  198. this.initDataFun(); // 初始化数据信息
  199. },
  200. watch: {},
  201. computed: {
  202. ...mapState({
  203. answerRecruitId: state => state.answer.answerRecruitId,
  204. userInfo: state => state.user.userInfo,
  205. chooseEngneeringWork: state => state.user.chooseEngneeringWork
  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. }
  424. },
  425. // 方法:判断当前题目是否正确
  426. getAnswerItemResultFun(item) {
  427. // let isSure = false;
  428. // let sureNum = 0;
  429. // item.finalAnswer.forEach(it => {
  430. // item.userAnswer.forEach(answerItem => {
  431. // if (it === answerItem) {
  432. // sureNum++;
  433. // }
  434. // });
  435. // });
  436. // if (sureNum === item.finalAnswer.length) {
  437. // isSure = true;
  438. // }
  439. // return isSure;
  440. let isSure = false;
  441. let lastAnswer = [""];
  442. lastAnswer[0] = this.getUserAnswerSortFun(item);
  443. if (lastAnswer[0] === item.finalAnswer[0]) {
  444. isSure = true;
  445. }
  446. return isSure;
  447. },
  448. // 获取用户所选选项的升序编号答案
  449. getUserAnswerSortFun(item) {
  450. let newUnserAnswer = [];
  451. item.answers.forEach((itemOne, indexOne) => {
  452. item.userAnswer.forEach(itemTwo => {
  453. if (itemOne === itemTwo) {
  454. newUnserAnswer.push(this.formatQuestionIndex(indexOne));
  455. }
  456. });
  457. });
  458. newUnserAnswer.sort();
  459. return newUnserAnswer.join("");
  460. },
  461. // 操作:下一题
  462. handleNextFun() {
  463. let nextIndex = this.answerIndex + 1;
  464. if (nextIndex >= this.examQuestionList.length) {
  465. this.examsEndFun(); // 结束考试
  466. return;
  467. }
  468. this.handleExamQuestionItemFun(
  469. this.examQuestionList[nextIndex],
  470. nextIndex
  471. );
  472. },
  473. // 操作:上一题
  474. handleLastFun() {
  475. let nextIndex = this.answerIndex - 1;
  476. if (nextIndex >= this.examQuestionList.length) {
  477. this.examsEndFun(); // 结束考试
  478. return;
  479. }
  480. this.handleExamQuestionItemFun(
  481. this.examQuestionList[nextIndex],
  482. nextIndex
  483. );
  484. },
  485. // 方法:结束考试
  486. examsEndFun() {
  487. let curTime = new Date();
  488. this.answerTime.endTime = curTime; // 赋值开始时间
  489. this.$store.commit("toggleLoading", true);
  490. let grades = this.getUserExamAllPointsFun(); // 方法:计算成绩
  491. let answers = this.getUserExamAllAnswersFun(); // 方法:获取当前用户所有题目作答的答案
  492. let params = {
  493. userId: this.userInfo.userName,
  494. examId: this.answerRecruitId,
  495. points: grades, // 成绩:积分
  496. startTime: "", // this.answerTime.startTime,
  497. endTime: "", // this.answerTime.endTime
  498. userAnswers: answers
  499. };
  500. this.$_http
  501. .post(
  502. this.$pathParams(this.$_API.POST_JTXT_GET_EXAMS_END, {
  503. examId: this.answerRecruitId
  504. }),
  505. params
  506. )
  507. .then(res => {
  508. this.$store.commit("toggleLoading", false);
  509. this.$router.replace({
  510. name: "answerRecruitResult",
  511. params: {
  512. allQuestionsNum: this.examQuestionList.length,
  513. grades: grades
  514. }
  515. });
  516. })
  517. .catch(() => {
  518. this.$store.commit("toggleLoading", false);
  519. Dialog.close();
  520. Dialog({ message: "结束考试异常,请联系系统管理员" });
  521. this.$router.back();
  522. });
  523. },
  524. // 方法:计算成绩
  525. getUserExamAllPointsFun() {
  526. let grades = 0;
  527. this.examQuestionList.forEach(item => {
  528. let isSure = this.getAnswerItemResultFun(item);
  529. if (isSure) {
  530. grades++; // 默认每题一分
  531. }
  532. });
  533. return grades;
  534. },
  535. // 方法:获取题目的答案
  536. getUserExamAllAnswersFun() {
  537. let answers = [];
  538. this.examQuestionList.forEach((item, index) => {
  539. // answers[index] = item.userAnswer.join("||");
  540. answers[index] = this.getUserAnswerSortFun(item);
  541. });
  542. return answers;
  543. },
  544. // 操作:返回
  545. handleBackFun() {
  546. this.$router.back();
  547. }
  548. }
  549. };
  550. </script>
  551. <style lang="scss" scoped>
  552. @import "~@/styles/mixin";
  553. .page-exam-question-box {
  554. width: 100%;
  555. height: 100%;
  556. overflow-y: auto;
  557. overflow-x: hidden;
  558. font-size: 0.6rem;
  559. .exam-question-div {
  560. padding: 0.5rem 0.5rem;
  561. .exam-question-card {
  562. padding: 0.5rem 0.5rem;
  563. background-color: #fff;
  564. border-radius: 4px;
  565. box-shadow: 0px 3px 6px rgba(0, 0, 0, 0.16);
  566. .exam-question-head {
  567. display: flex;
  568. justify-content: space-between;
  569. align-items: center;
  570. flex-wrap: nowrap;
  571. border-bottom: 1px solid #0088e9;
  572. padding-bottom: 0.25rem;
  573. height: 1.25rem;
  574. font-size: 0.65rem;
  575. .exam-question-head-left {
  576. display: flex;
  577. align-items: center;
  578. flex-wrap: nowrap;
  579. .exam-question-head-left-icon {
  580. width: 0.25rem;
  581. height: 1rem;
  582. background-color: #0088e9;
  583. }
  584. .exam-question-head-left-txt {
  585. margin-left: 0.25rem;
  586. margin-right: 0.25rem;
  587. font-weight: bold;
  588. }
  589. }
  590. .exam-question-head-right {
  591. span {
  592. color: #666;
  593. }
  594. .exam-question-head-right-now {
  595. font-weight: bold;
  596. color: #000;
  597. }
  598. }
  599. }
  600. .exam-question-describe {
  601. padding: 0.5rem 0;
  602. font-size: 0.65rem;
  603. }
  604. .exam-question-options {
  605. .exam-question-options-item {
  606. width: 100%;
  607. padding: 0.5rem 0.25rem;
  608. background-color: #f3f3f3;
  609. margin-bottom: 0.5rem;
  610. border: 1px solid transparent;
  611. }
  612. .exam-question-options-item-checked {
  613. border-color: #0088e9;
  614. color: #0088e9;
  615. }
  616. .exam-question-options-false {
  617. border-color: red !important;
  618. background-color: #fedada !important;
  619. }
  620. }
  621. .exam-question-gapFilling {
  622. padding-top: 0.5rem;
  623. textarea {
  624. width: 100%;
  625. padding: 0.25rem 0.25rem;
  626. background-color: #f3f3f3;
  627. margin-bottom: 0.5rem;
  628. border: 1px solid transparent;
  629. &:active,
  630. &:focus {
  631. border-color: #0088e9;
  632. }
  633. }
  634. }
  635. .exam-question-gapFilling-false {
  636. textarea {
  637. border-color: red !important;
  638. background-color: #fedada !important;
  639. }
  640. }
  641. .exam-question-button-box {
  642. display: flex;
  643. flex-direction: row;
  644. justify-content: center;
  645. align-items: center;
  646. .exam-question-single-button-box {
  647. width: 30%;
  648. padding: 0.5rem 0.5rem;
  649. display: flex;
  650. flex-direction: row;
  651. justify-content: center;
  652. align-items: center;
  653. .exam-question-button {
  654. width: 6rem;
  655. height: auto;
  656. padding: 0.5rem 0.5rem;
  657. font-size: 0.65rem;
  658. margin-top: 0.75rem;
  659. }
  660. }
  661. }
  662. .exam-question-result {
  663. border-bottom: 1px solid #0088e9;
  664. padding-bottom: 0.25rem;
  665. font-size: 0.6rem;
  666. }
  667. .exam-question-analysis {
  668. padding-top: 0.25rem;
  669. }
  670. .exam-question-card-loading,
  671. .exam-question-card-nodata {
  672. width: 100%;
  673. height: 5rem;
  674. color: #333;
  675. font-size: 0.7rem;
  676. display: flex;
  677. justify-content: center;
  678. align-items: center;
  679. }
  680. .exam-question-card-loading {
  681. color: #0088e9;
  682. }
  683. }
  684. }
  685. }
  686. </style>