ExamCard.vue 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776
  1. <template>
  2. <el-container v-if="show">
  3. <el-header style="margin-top: 60px">
  4. <el-row>
  5. <el-col :span="18" :offset="3" style="border-bottom: 1px solid #f5f5f5">
  6. <span class="startExam">开始考试</span>
  7. <span class="examTitle">距离考试结束还有:</span>
  8. <span style="color: red;font-size: 18px;">{{ duration | timeFormat }}</span>
  9. <el-button
  10. type="warning"
  11. round
  12. style="background-color: #ffd550;float: right;color: black;font-weight: 800"
  13. @click="uploadExamToAdmin"
  14. >提交试卷
  15. </el-button>
  16. </el-col>
  17. </el-row>
  18. </el-header>
  19. <el-main>
  20. <el-row>
  21. <el-col :span="18" :offset="3">
  22. <el-col :span="16">
  23. <el-card style="min-height: 500px">
  24. <!-- 题目信息 -->
  25. <div>
  26. <span v-if="questionList[curIndex].questionType === 1">【单选题】</span>
  27. <span v-else-if="questionList[curIndex].questionType === 2">【多选题】</span>
  28. <span v-else-if="questionList[curIndex].questionType === 3">【不定项选择题】</span>
  29. <span v-else-if="questionList[curIndex].questionType === 4">【判断题】</span>
  30. <span v-else-if="questionList[curIndex].questionType === 5">【填空题】</span>
  31. <span v-else-if="questionList[curIndex].questionType === 6">【问答题】</span>
  32. <span v-else-if="questionList[curIndex].questionType === 7">【理解题】</span>
  33. <span v-else>【综合题】</span>
  34. <br>
  35. <br>
  36. <i class="num">{{ curIndex + 1 }}</i>
  37. <span>{{ questionList[curIndex].questionContent }}:</span>
  38. </div>
  39. <!--题目中的配图-->
  40. <img
  41. v-for="url in questionList[curIndex].images"
  42. :src="url"
  43. title="点击查看大图"
  44. alt="题目图片"
  45. style="width: 100px;height: 100px;cursor: pointer"
  46. @click="showBigImg(url)"
  47. >
  48. <!-- 单选和判断题候选答案列表 -->
  49. <div
  50. v-show="questionList[curIndex].questionType === 1
  51. || questionList[curIndex].questionType === 4"
  52. style="margin-top: 25px"
  53. >
  54. <div class="el-radio-group">
  55. <label
  56. v-for="(item,index) in questionList[curIndex].answer"
  57. :class="index === userAnswer[curIndex] ? 'active' : ''"
  58. @click="checkSingleAnswer(index)"
  59. >
  60. <span>{{ optionName[index] + '、' + item.answer }}</span>
  61. <img
  62. v-for="i2 in item.images"
  63. v-if="item.images !== null"
  64. style="position: absolute;left:100%;top:50%;transform: translateY(-50%);width: 40px;height: 40px;float: right;cursor: pointer;"
  65. title="点击查看大图"
  66. :src="i2"
  67. alt=""
  68. @mouseover="showBigImg(i2)"
  69. >
  70. </label>
  71. </div>
  72. </div>
  73. <!-- 多选和不定项选择题的候选答案列表 -->
  74. <div
  75. v-show="questionList[curIndex].questionType === 2
  76. || questionList[curIndex].questionType === 3"
  77. style="margin-top: 25px"
  78. >
  79. <div class="el-radio-group">
  80. <label
  81. v-for="(item,index) in questionList[curIndex].answer"
  82. :class="(userAnswer[curIndex]+'').indexOf(index+'') !== -1? 'active' : ''"
  83. @click="selectedMultipleAnswer(index)"
  84. >
  85. <span>{{ optionName[index] + '、' + item.answer }}</span>
  86. <img
  87. v-for="i2 in item.images"
  88. v-if="item.images !== null"
  89. style="position: absolute;left:100%;top:50%;transform: translateY(-50%);
  90. width: 40px;height: 40px;float: right;cursor: pointer;"
  91. title="点击查看大图"
  92. :src="i2"
  93. alt=""
  94. @mouseover="showBigImg(i2)"
  95. >
  96. </label>
  97. </div>
  98. </div>
  99. <!-- 填空题和问答题的回答区 -->
  100. <div
  101. v-show="questionList[curIndex].questionType === 5
  102. || questionList[curIndex].questionType === 6"
  103. style="margin-top: 25px"
  104. >
  105. <el-input
  106. v-model="userAnswer[curIndex]"
  107. type="textarea"
  108. :rows="8"
  109. placeholder="请输入答案"
  110. />
  111. </div>
  112. <!-- 综合题的回答区 -->
  113. <div
  114. v-show="questionList[curIndex].questionType === 8"
  115. style="margin-top: 25px"
  116. >
  117. <el-input
  118. v-model="userAnswer[curIndex]"
  119. type="textarea"
  120. :rows="8"
  121. placeholder="请输入答案"
  122. />
  123. </div>
  124. <!-- 上一题和下一题按钮 -->
  125. <div style="margin-top: 25px">
  126. <el-button type="primary" icon="el-icon-back" :disabled="curIndex<1" @click="curIndex--">上一题</el-button>
  127. <el-button
  128. type="primary"
  129. icon="el-icon-right"
  130. :disabled="curIndex>=questionList.length-1"
  131. @click="curIndex++"
  132. >下一题
  133. </el-button>
  134. </div>
  135. </el-card>
  136. </el-col>
  137. <!-- 答题卡 -->
  138. <el-col :span="7" :offset="1">
  139. <el-card>
  140. <div>
  141. <p style="font-size: 18px;">答题卡</p>
  142. <div style="margin-top: 25px">
  143. <span
  144. style="background-color: rgb(238,238,238);padding: 5px 10px 5px 10px;margin-left: 15px"
  145. >未作答</span>
  146. <span
  147. style="background-color: rgb(87,148,247);color: white;padding: 5px 10px 5px 10px;margin-left: 15px"
  148. >已作答</span>
  149. </div>
  150. </div>
  151. <!-- 单选的答题卡 -->
  152. <div style="margin-top: 25px">
  153. <p style="font-size: 18px;">单选题</p>
  154. <el-button
  155. v-for="item in questionList.length"
  156. v-show="questionList[item-1].questionType === 1"
  157. :key="item"
  158. style="margin-top: 10px;margin-left: 15px"
  159. size="mini"
  160. :class="questionList[item-1].questionType === 1 && userAnswer[item-1] !== undefined ?
  161. 'done' : userAnswer[item-1] === undefined ? curIndex === (item-1) ? 'orange' : 'noAnswer' : 'noAnswer'"
  162. @click="curIndex = item-1"
  163. >{{ item }}
  164. </el-button>
  165. </div>
  166. <!-- 多选的答题卡 -->
  167. <div style="margin-top: 25px">
  168. <p style="font-size: 18px;">多选题</p>
  169. <el-button
  170. v-for="item in questionList.length"
  171. v-show="questionList[item-1].questionType === 2"
  172. :key="item"
  173. style="margin-top: 10px;margin-left: 15px"
  174. size="mini"
  175. :class="questionList[item-1].questionType === 2 && userAnswer[item-1] !== undefined ?
  176. 'done' : userAnswer[item-1] === undefined ? curIndex === (item-1) ? 'orange' : 'noAnswer' : 'noAnswer'"
  177. @click="curIndex = item-1"
  178. >{{ item }}
  179. </el-button>
  180. </div>
  181. <!-- 不定项选择题的答题卡 -->
  182. <div style="margin-top: 25px">
  183. <p style="font-size: 18px;">多选题</p>
  184. <el-button
  185. v-for="item in questionList.length"
  186. v-show="questionList[item-1].questionType === 3"
  187. :key="item"
  188. style="margin-top: 10px;margin-left: 15px"
  189. size="mini"
  190. :class="questionList[item-1].questionType === 3 && userAnswer[item-1] !== undefined ?
  191. 'done' : userAnswer[item-1] === undefined ? curIndex === (item-1) ? 'orange' : 'noAnswer' : 'noAnswer'"
  192. @click="curIndex = item-1"
  193. >{{ item }}
  194. </el-button>
  195. </div>
  196. <!-- 判断题的答题卡 -->
  197. <div style="margin-top: 25px">
  198. <p style="font-size: 18px;">判断题</p>
  199. <el-button
  200. v-for="item in questionList.length"
  201. v-show="questionList[item-1].questionType === 4"
  202. :key="item"
  203. style="margin-top: 10px;margin-left: 15px"
  204. size="mini"
  205. :class="questionList[item-1].questionType === 4 && userAnswer[item-1] !== undefined ?
  206. 'done' : userAnswer[item-1] === undefined ? curIndex === (item-1) ? 'orange' : 'noAnswer' : 'noAnswer'"
  207. @click="curIndex = item-1"
  208. >{{ item }}
  209. </el-button>
  210. </div>
  211. <!-- 填空题的答题卡 -->
  212. <div style="margin-top: 25px">
  213. <p style="font-size: 18px;">填空题</p>
  214. <el-button
  215. v-for="item in questionList.length"
  216. v-show="questionList[item-1].questionType === 5"
  217. :key="item"
  218. style="margin-top: 10px;margin-left: 15px"
  219. size="mini"
  220. :class="questionList[item-1].questionType === 5 && userAnswer[item-1] !== undefined ?
  221. 'done' : userAnswer[item-1] === undefined ? curIndex === (item-1) ? 'orange' : 'noAnswer' : 'noAnswer'"
  222. @click="curIndex = item-1"
  223. >{{ item }}
  224. </el-button>
  225. </div>
  226. <!-- 问答题的答题卡 -->
  227. <div style="margin-top: 25px">
  228. <p style="font-size: 18px;">问答题</p>
  229. <el-button
  230. v-for="item in questionList.length"
  231. v-show="questionList[item-1].questionType === 6"
  232. :key="item"
  233. style="margin-top: 10px;margin-left: 15px"
  234. size="mini"
  235. :class="questionList[item-1].questionType === 6 && userAnswer[item-1] !== undefined ?
  236. 'done' : userAnswer[item-1] === undefined ? curIndex === (item-1) ? 'orange' : 'noAnswer' : 'noAnswer'"
  237. @click="curIndex = item-1"
  238. >{{ item }}
  239. </el-button>
  240. </div>
  241. <!-- 综合题的答题卡 -->
  242. <div style="margin-top: 25px">
  243. <p style="font-size: 18px;">综合题</p>
  244. <el-button
  245. v-for="item in questionList.length"
  246. v-show="questionList[item-1].questionType === 8"
  247. :key="item"
  248. style="margin-top: 10px;margin-left: 15px"
  249. size="mini"
  250. :class="questionList[item-1].questionType === 8 && userAnswer[item-1] !== undefined ?
  251. 'done' : userAnswer[item-1] === undefined ? curIndex === (item-1) ? 'orange' : 'noAnswer' : 'noAnswer'"
  252. @click="curIndex = item-1"
  253. >{{ item }}
  254. </el-button>
  255. </div>
  256. </el-card>
  257. </el-col>
  258. </el-col>
  259. </el-row>
  260. <video
  261. id="video"
  262. muted="muted"
  263. style="float:right;position: fixed;top: 80%;left: 85%"
  264. width="200px"
  265. height="200px"
  266. autoplay="autoplay"
  267. />
  268. <canvas id="canvas" hidden width="200px" height="200px" />
  269. </el-main>
  270. <!--图片回显-->
  271. <el-dialog :visible.sync="bigImgDialog" @close="bigImgDialog = false">
  272. <img style="width: 100%" :src="bigImgUrl">
  273. </el-dialog>
  274. </el-container>
  275. </template>
  276. <script>
  277. import { submitExamPaper, getExamInfoById, getQuestionByPaperId } from '@/api/exam'
  278. export default {
  279. name: 'ExamCard',
  280. filters: {
  281. ellipsis(value) {
  282. if (!value) return ''
  283. const max = 50
  284. if (value.length > max) {
  285. return value.slice(0, max) + '...'
  286. }
  287. return value
  288. },
  289. timeFormat(time) {
  290. // 分钟
  291. var minute = time / 60
  292. var minutes = parseInt(minute)
  293. if (minutes < 10) {
  294. minutes = '0' + minutes
  295. }
  296. // 秒
  297. var second = time % 60
  298. var seconds = Math.round(second)
  299. if (seconds < 10) {
  300. seconds = '0' + seconds
  301. }
  302. return `${minutes}:${seconds}`
  303. }
  304. },
  305. data() {
  306. return {
  307. // 当前考试的信息
  308. examInfo: {},
  309. // 当前的考试题目
  310. questionList: [
  311. {
  312. questionType: ''
  313. }
  314. ],
  315. // 当前题目的索引值
  316. curIndex: 0,
  317. // 控制大图的对话框
  318. bigImgDialog: false,
  319. // 当前要展示的大图的url
  320. bigImgUrl: '',
  321. // 用户选择的答案
  322. userAnswer: [],
  323. // 页面数据加载
  324. loading: {},
  325. // 页面绘制是否开始
  326. show: false,
  327. // 答案的选项名abcd数据
  328. optionName: ['A', 'B', 'C', 'D', 'E', 'F', 'G'],
  329. // 考试总时长
  330. duration: 0,
  331. // 摄像头对象
  332. mediaStreamTrack: null,
  333. // 诚信照片的url
  334. takePhotoUrl: [],
  335. // 摄像头是否开启
  336. cameraOn: false
  337. }
  338. },
  339. watch: {
  340. // 监控考试的剩余时间
  341. async duration(newVal) {
  342. const examDuration = {
  343. duration: newVal,
  344. timestamp: Date.now()
  345. }
  346. localStorage.setItem('examDuration' + this.examInfo.examId, JSON.stringify(examDuration))
  347. // 摄像头数据
  348. const constraints = {
  349. video: {
  350. width: 200,
  351. height: 200
  352. },
  353. audio: false
  354. }
  355. // 通过调用摄像头判断用户是否中途关闭摄像头
  356. /* const promise = navigator.mediaDevices.getUserMedia(constraints)
  357. promise.catch((back) => {
  358. this.cameraOn = false
  359. })*/
  360. if (!this.cameraOn) { // 如果摄像头未开启,就再次调用开启
  361. this.getCamera()
  362. }
  363. // 考试时间结束, 自动提交试卷
  364. if (newVal < 1) {
  365. /* if (this.cameraOn) {
  366. // 结束的时候拍照上传一张
  367. await this.takePhoto()
  368. this.closeCamera()
  369. }*/
  370. this.submitUserPayload(true)
  371. }
  372. }
  373. },
  374. created() {
  375. this.getExamInfo()
  376. // 页面数据加载的等待状态栏
  377. this.loading = this.$loading({
  378. body: true,
  379. lock: true,
  380. text: '数据拼命加载中,(*╹▽╹*)',
  381. spinner: 'el-icon-loading'
  382. })
  383. // 开启摄像头
  384. window.onload = () => {
  385. setTimeout(() => {
  386. this.getCamera()
  387. }, 1000)
  388. // 生成3次时间点截图
  389. const times = []
  390. for (let i = 0; i < 2; i++) {
  391. times.push(Math.ceil(Math.random() * this.duration * 1000))
  392. }
  393. times.push(10000)
  394. // 一次考试最多3次随机的诚信截图
  395. times.forEach(item => {
  396. window.setTimeout(() => {
  397. this.takePhoto()
  398. }, item)
  399. })
  400. }
  401. },
  402. mounted() {
  403. // 关闭浏览器窗口的时候移除 localstorage的时长
  404. var userAgent = navigator.userAgent // 取得浏览器的userAgent字符串
  405. var isOpera = userAgent.indexOf('Opera') > -1 // 判断是否Opera浏览器
  406. var isIE = userAgent.indexOf('compatible') > -1 && userAgent.indexOf('MSIE') > -1 && !isOpera // 判断是否IE浏览器
  407. var isIE11 = userAgent.indexOf('rv:11.0') > -1 // 判断是否是IE11浏览器
  408. var isEdge = userAgent.indexOf('Edge') > -1 && !isIE // 判断是否IE的Edge浏览器
  409. if (!isIE && !isEdge && !isIE11) { // 兼容chrome和firefox
  410. var _beforeUnload_time = 0; var _gap_time = 0
  411. var is_fireFox = navigator.userAgent.indexOf('Firefox') > -1// 是否是火狐浏览器
  412. window.onunload = function() {
  413. _gap_time = new Date().getTime() - _beforeUnload_time
  414. if (_gap_time <= 5) {
  415. localStorage.removeItem('examDuration' + this.examInfo.examId)
  416. } else { // 谷歌浏览器刷新
  417. }
  418. }
  419. window.onbeforeunload = function() {
  420. _beforeUnload_time = new Date().getTime()
  421. if (is_fireFox) { // 火狐关闭执行
  422. } else { // 火狐浏览器刷新
  423. }
  424. }
  425. }
  426. },
  427. methods: {
  428. // 查询当前考试的信息
  429. getExamInfo() {
  430. const paperId = this.$route.params.paperId
  431. getExamInfoById(paperId).then((resp) => {
  432. if (resp.code === 0) {
  433. this.examInfo = resp.data
  434. // 设置定时(秒)
  435. try {
  436. const examDuration = JSON.parse(localStorage.getItem('examDuration' + this.examInfo.examId) || '{}')
  437. if (examDuration.duration === 0 || Date.now() >= (examDuration.timestamp ||
  438. Date.now()) + (examDuration.duration * 1000 || Date.now())) {
  439. localStorage.removeItem('examDuration' + this.examInfo.examId)
  440. }
  441. this.duration = Math.min(JSON.parse(
  442. localStorage.getItem('examDuration' + this.examInfo.examId) || '{}').duration ||
  443. this.examInfo.examDuration * 60, this.examInfo.examDuration * 60)
  444. } catch (e) {
  445. localStorage.removeItem('examDuration' + this.examInfo.examId)
  446. }
  447. // 考试剩余时间定时器
  448. this.timer = window.setInterval(() => {
  449. if (this.duration > 0) this.duration--
  450. }, 1000)
  451. var paperId = this.examInfo.examId
  452. this.getQuestionInfo1(paperId)
  453. }
  454. })
  455. },
  456. // 查询考试的题目信息
  457. async getQuestionInfo1(paperId) {
  458. await getQuestionByPaperId(paperId).then(resp => {
  459. if (resp.code === 0) {
  460. this.questionList = resp.data || []
  461. // 重置问题的顺序 单选 多选 判断 简答
  462. this.questionList = this.questionList.sort(function(a, b) {
  463. return a.questionType - b.questionType
  464. })
  465. }
  466. })
  467. this.loading.close()
  468. this.show = true
  469. },
  470. // 点击展示高清大图
  471. showBigImg(url) {
  472. console.log('show big')
  473. console.log(url)
  474. this.bigImgUrl = url
  475. this.bigImgDialog = true
  476. },
  477. // ****************************************************************************************************************
  478. // 检验单选题的用户选择的答案
  479. checkSingleAnswer(index) {
  480. this.$set(this.userAnswer, this.curIndex, index)
  481. },
  482. // 多选题用户的答案选中
  483. selectedMultipleAnswer(index) {
  484. if (this.userAnswer[this.curIndex] === undefined) { // 当前是多选的第一个答案
  485. this.$set(this.userAnswer, this.curIndex, index)
  486. } else if (String(this.userAnswer[this.curIndex]).split(',').includes(index + '')) { // 取消选中
  487. const newArr = []
  488. String(this.userAnswer[this.curIndex]).split(',').forEach(item => {
  489. if (item !== '' + index) newArr.push(item)
  490. })
  491. if (newArr.length === 0) {
  492. this.$set(this.userAnswer, this.curIndex, undefined)
  493. } else {
  494. this.$set(this.userAnswer, this.curIndex, newArr.join(','))
  495. // 答案格式化顺序DBAC -> ABCD
  496. this.userAnswer[this.curIndex] = String(this.userAnswer[this.curIndex]).split(',').sort(function(a, b) {
  497. return a - b
  498. }).join(',')
  499. }
  500. } else if (!((this.userAnswer[this.curIndex] + '').split(',').includes(index + ''))) { // 第n个答案
  501. this.$set(this.userAnswer, this.curIndex, this.userAnswer[this.curIndex] += ',' + index)
  502. // 答案格式化顺序DBAC -> ABCD
  503. this.userAnswer[this.curIndex] = String(this.userAnswer[this.curIndex]).split(',').sort(function(a, b) {
  504. return a - b
  505. }).join(',')
  506. }
  507. },
  508. // ****************************************************************************************************************
  509. // 调用摄像头
  510. getCamera() {
  511. const constraints = {
  512. video: {
  513. width: 200,
  514. height: 200
  515. },
  516. audio: false
  517. }
  518. // 获得video摄像头
  519. const video = document.getElementById('video')
  520. /* const promise = navigator.mediaDevices.getUserMedia(constraints)
  521. promise.then((mediaStream) => {
  522. this.mediaStreamTrack = typeof mediaStream.stop === 'function' ? mediaStream : mediaStream.getTracks()[1]
  523. video.srcObject = mediaStream
  524. video.play()
  525. this.cameraOn = true
  526. }).catch((back) => {
  527. this.$message({
  528. duration: 1500,
  529. message: '请开启摄像头权限o(╥﹏╥)o!',
  530. type: 'error'
  531. })
  532. })*/
  533. },
  534. // 拍照
  535. async takePhoto() {
  536. if (this.cameraOn) { // 摄像头是否开启 开启了才执行上传信用图片
  537. // 获得Canvas对象
  538. const video = document.getElementById('video')
  539. const canvas = document.getElementById('canvas')
  540. const ctx = canvas.getContext('2d')
  541. ctx.drawImage(video, 0, 0, 200, 200)
  542. // toDataURL --- 可传入'image/png'---默认, 'image/jpeg'
  543. const img = document.getElementById('canvas').toDataURL()
  544. // 构造post的form表单
  545. const formData = new FormData()
  546. // convertBase64UrlToBlob函数是将base64编码转换为Blob
  547. formData.append('file', this.base64ToFile(img, 'examTakePhoto.png'))
  548. // 上传阿里云OSS
  549. /* await ossUtils.uploadImage(formData).then((resp) => {
  550. if (resp.code === 0) this.takePhotoUrl.push(resp.data)
  551. })*/
  552. }
  553. },
  554. // 关闭摄像头
  555. closeCamera() {
  556. const stream = document.getElementById('video').srcObject
  557. const tracks = stream.getTracks()
  558. tracks.forEach(function(track) {
  559. track.stop()
  560. })
  561. document.getElementById('video').srcObject = null
  562. },
  563. // 将摄像头截图的base64串转化为file提交后台
  564. base64ToFile(urlData, fileName) {
  565. const arr = urlData.split(',')
  566. const mime = arr[0].match(/:(.*?);/)[1]
  567. const bytes = atob(arr[1]) // 解码base64
  568. let n = bytes.length
  569. const ia = new Uint8Array(n)
  570. while (n--) {
  571. ia[n] = bytes.charCodeAt(n)
  572. }
  573. return new File([ia], fileName, { type: mime })
  574. },
  575. // ****************************************************************************************************************
  576. // 上传用户考试信息进入后台
  577. async uploadExamToAdmin() {
  578. console.log(this.userAnswer)
  579. if (this.cameraOn) await this.takePhoto()// 结束的时候拍照上传一张
  580. // 正则
  581. var reg = new RegExp('-', 'g')
  582. // 去掉用户输入的非法分割符号(-),保证后端接受数据处理不报错
  583. this.userAnswer.forEach((item, index) => {
  584. // 简答题答案处理
  585. if (this.questionList[index].questionType === 5 ||
  586. this.questionList[index].questionType === 6 ||
  587. this.questionList[index].questionType === 7) {
  588. this.userAnswer[index] = item.replace(reg, ' ')
  589. }
  590. })
  591. // 标记题目是否全部做完
  592. let flag = true
  593. for (let i = 0; i < this.userAnswer.length; i++) { // 检测用户是否题目全部做完
  594. if (this.userAnswer[i] === undefined) {
  595. flag = false
  596. }
  597. }
  598. // 如果用户所有答案的数组长度小于题目长度,这个时候也要将标志位置为false
  599. if (this.userAnswer.length < this.questionList.length) {
  600. flag = false
  601. }
  602. // 题目未做完
  603. if (!flag) {
  604. this.$confirm('当前试题暂未做完, 是否继续提交o(╥﹏╥)o ?', 'Tips', {
  605. confirmButtonText: '确定',
  606. cancelButtonText: '取消',
  607. type: 'warning'
  608. }).then(() => {
  609. this.submitUserPayload(false)
  610. }).catch(() => {
  611. this.$notify({
  612. title: 'Tips',
  613. message: '继续加油! *^▽^*',
  614. type: 'success',
  615. duration: 2000
  616. })
  617. })
  618. } else { // 当前题目做完了
  619. if (this.cameraOn) {
  620. // 结束的时候拍照上传一张
  621. await this.takePhoto()
  622. this.closeCamera()
  623. }
  624. this.submitUserPayload(false)
  625. }
  626. },
  627. submitUserPayload(autoSubmit) {
  628. var msg = '考试结束了捏 *^▽^*'
  629. if (autoSubmit) {
  630. msg = '考试时间结束,已为您自动提交 *^▽^*'
  631. }
  632. const userPayload = {}
  633. userPayload.questionIds = []
  634. userPayload.userAnswers = this.userAnswer.join('-')
  635. this.questionList.forEach((item, index) => {
  636. userPayload.questionIds.push(item.questionId)
  637. // 当前数据不完整,用户回答不完整(我们自动补充空答案,防止业务出错)
  638. if (index > this.userAnswer.length) {
  639. userPayload.userAnswers += ' -'
  640. }
  641. })
  642. // 如果所有题目全部未答
  643. if (userPayload.userAnswers === '') {
  644. this.questionList.forEach(item => {
  645. userPayload.userAnswers += ' -'
  646. })
  647. userPayload.userAnswers.split(0, userPayload.userAnswers.length - 1)
  648. }
  649. userPayload.examId = parseInt(this.$route.params.paperId)
  650. userPayload.questionIds = userPayload.questionIds.join(',')
  651. userPayload.creditImgUrl = this.takePhotoUrl.join(',')
  652. submitExamPaper(userPayload).then((resp) => {
  653. if (resp.code === 0) {
  654. this.$notify({
  655. title: 'Tips',
  656. message: msg,
  657. type: 'success',
  658. duration: 2000
  659. })
  660. this.$router.push('/exam/result/' + resp.data)
  661. }
  662. })
  663. }
  664. }
  665. }
  666. </script>
  667. <style lang="scss" scoped>
  668. * {
  669. font-weight: 800;
  670. }
  671. .el-container {
  672. width: 100%;
  673. height: 100%;
  674. }
  675. .startExam {
  676. color: #160f58;
  677. border-bottom: 4px solid #ffd550;
  678. font-size: 18px;
  679. font-weight: 700;
  680. padding-bottom: 10px
  681. }
  682. .examTitle {
  683. font-size: 18px;
  684. color: #cbcacf;
  685. margin-left: 20px;
  686. font-weight: 700;
  687. }
  688. .el-radio-group label {
  689. display: block;
  690. width: 400px;
  691. padding: 48px 20px 10px 20px;
  692. border-radius: 4px;
  693. border: 1px solid #dcdfe6;
  694. margin-bottom: 10px;
  695. cursor: pointer;
  696. position: relative;
  697. span {
  698. position: absolute;
  699. top: 50%;
  700. transform: translateY(-50%);
  701. font-size: 16px;
  702. }
  703. }
  704. .el-radio-group label:hover {
  705. background-color: rgb(245, 247, 250);
  706. }
  707. /*当前选中的答案*/
  708. .active {
  709. border: 1px solid #1f90ff !important;
  710. opacity: .5;
  711. }
  712. /*做过的题目的高亮颜色*/
  713. .done {
  714. background-color: rgb(87, 148, 247);
  715. }
  716. /*未做题目的高亮颜色*/
  717. .noAnswer {
  718. background-color: rgb(238, 238, 238);
  719. }
  720. /*当前在做的题目高亮的颜色*/
  721. .orange {
  722. background-color: rgb(255, 213, 80);
  723. }
  724. .num {
  725. display: inline-block;
  726. background: url('../../assets/img/examTitle.png') no-repeat 95%;
  727. background-size: contain;
  728. height: 37px;
  729. width: 37px;
  730. line-height: 30px;
  731. color: #fff;
  732. font-size: 20px;
  733. text-align: center;
  734. margin-right: 15px;
  735. }
  736. </style>