test.vue 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646
  1. <template>
  2. <el-dialog
  3. v-loading="loading"
  4. :title="title"
  5. :visible.sync="dialogVisible"
  6. :close-on-click-modal="false"
  7. :close-on-press-escape="false"
  8. :show-close="false"
  9. append-to-body
  10. fullscreen
  11. class="dialog test-dialog"
  12. top="0"
  13. @open="loadData"
  14. @close="closeDialog"
  15. >
  16. <div class="container">
  17. <div class="question">
  18. <div
  19. v-for="(item, index) in questionList"
  20. v-show="showIndex === index + 1"
  21. :key="index"
  22. class="question-item"
  23. >
  24. <div class="type">{{ item.questionType }}</div>
  25. <div class="stem">
  26. <span>{{ `【${index + 1}】${item.stem}` }}</span>
  27. <el-tag type="info" size="small">{{ `${item.score}分` }}</el-tag>
  28. </div>
  29. <div v-if="item.img && item.img.length" class="img">
  30. <ibps-image
  31. v-model="item.img"
  32. height="100"
  33. width="100"
  34. accept=".jpg,.jpeg,.png,.gif,.bmp,.webp"
  35. download
  36. disabled
  37. />
  38. </div>
  39. <div class="answer">
  40. <el-radio-group v-if="item.questionType === '单选题'" v-model="item.answer" @change="goNext">
  41. <el-radio
  42. v-for="(o, i) in item.options"
  43. :key="`${index}${i}`"
  44. :label="o.label"
  45. >{{ `${o.label}.${o.value}` }}</el-radio>
  46. </el-radio-group>
  47. <el-checkbox-group v-else-if="item.questionType === '多选题'" v-model="item.answer" @change="changeOptions">
  48. <el-checkbox
  49. v-for="(o, i) in item.options"
  50. :key="`${index}${i}`"
  51. :label="o.label"
  52. >{{ `${o.label}.${o.value}` }}</el-checkbox>
  53. </el-checkbox-group>
  54. <el-radio-group v-else-if="item.questionType === '判断题'" v-model="item.answer" @change="goNext">
  55. <el-radio-button label="√">√</el-radio-button>
  56. <el-radio-button label="×">×</el-radio-button>
  57. </el-radio-group>
  58. <template v-else-if="item.questionType === '填空题'">
  59. <el-input
  60. v-for="(o, i) in item.options"
  61. :key="`${index}${i}`"
  62. v-model="o.answer"
  63. type="text"
  64. placeholder="请输入您的答案"
  65. />
  66. </template>
  67. <el-input
  68. v-else-if="item.questionType === '简答题'"
  69. v-model="item.answer"
  70. type="textarea"
  71. :autosize="{ minRows: 4, maxRows: 8}"
  72. placeholder="请输入您的答案"
  73. />
  74. </div>
  75. </div>
  76. </div>
  77. <div class="question-link">
  78. <el-progress
  79. type="line"
  80. class="progress"
  81. :text-inside="true"
  82. :stroke-width="20"
  83. :percentage="getProgress()"
  84. />
  85. <div
  86. v-for="(item, index) in questionList"
  87. :key="index"
  88. class="link-item"
  89. :class="setClassName(item, index)"
  90. @click="showIndex = index + 1"
  91. >{{ index + 1 }}</div>
  92. </div>
  93. </div>
  94. <div class="tips">
  95. <div>温馨提示:最下方题序方块无背景颜色时,表示该题还未作答;<br>题序方块背景颜色为<span style="background: #409EFF;">蓝色</span>时,表示题序对应题母为当前显示的题目;<br>题序方块背景颜色为<span style="background: #E6A23C;">黄色</span>时,表示该题已作答但是未完全答完(如填空题);<br>题序方块背景颜色为<span style="background: #67C23A;">绿色</span>时,表示该题已作答完成。</div>
  96. </div>
  97. <div slot="title" class="custom-title">
  98. <div class="dialog-title">{{ title }}</div>
  99. <div class="countdown">
  100. <div class="desc">⏲距离考试结束还有:{{ formattedCountdown }}</div>
  101. <div class="countdown-tips">超时将自动提交, 请注意考试时间!</div>
  102. </div>
  103. </div>
  104. <div slot="footer" class="el-dialog--center">
  105. <ibps-toolbar
  106. :actions="toolbars"
  107. @action-event="handleActionEvent"
  108. />
  109. </div>
  110. </el-dialog>
  111. </template>
  112. <script>
  113. // import Watermark from '@/layout/header-aside/components/header-message/watermark/watermark-cont'
  114. import { round } from 'lodash'
  115. export default {
  116. components: {
  117. IbpsImage: () => import('@/business/platform/file/image')
  118. },
  119. props: {
  120. visible: {
  121. type: Boolean,
  122. default: false
  123. },
  124. bankId: {
  125. type: String,
  126. default: ''
  127. },
  128. id: {
  129. type: String,
  130. default: ''
  131. },
  132. examData: {
  133. type: Object,
  134. default: () => {}
  135. }
  136. },
  137. data () {
  138. const { userId } = this.$store.getters || {}
  139. const { duration } = this.examData || {}
  140. const countdown = duration === '不限' ? 0 : parseInt(duration / 1000)
  141. return {
  142. countdown,
  143. title: this.examData.examName || '参加考试',
  144. dialogVisible: this.visible,
  145. loading: false,
  146. toolbars: [
  147. {
  148. key: 'prev',
  149. icon: 'ibps-icon-chevron-left',
  150. label: '上一题',
  151. disabled: () => {
  152. return this.showIndex === 1
  153. }
  154. },
  155. {
  156. key: 'next',
  157. icon: 'ibps-icon-chevron-right',
  158. label: '下一题',
  159. disabled: () => {
  160. return this.showIndex === this.questionList.length
  161. }
  162. },
  163. {
  164. key: 'submit',
  165. icon: 'ibps-icon-send',
  166. label: '交卷',
  167. hidden: () => {
  168. return this.readonly
  169. }
  170. },
  171. { key: 'cancel', label: '退出' }
  172. ],
  173. questionList: [],
  174. showIndex: 1,
  175. userId,
  176. countdownNotify: false
  177. }
  178. },
  179. computed: {
  180. formData () {
  181. return this.data
  182. },
  183. formattedCountdown () {
  184. const h = this.formatNum(parseInt(this.countdown / 60 / 60))
  185. const m = this.formatNum(parseInt(this.countdown / 60 % 60))
  186. const s = this.formatNum(parseInt(this.countdown % 60))
  187. return `${h}时${m}分${s}妙`
  188. }
  189. },
  190. watch: {
  191. visible: {
  192. handler (val, oldVal) {
  193. this.dialogVisible = this.visible
  194. }
  195. // immediate: true
  196. },
  197. showIndex: {
  198. handler (val, oldVal) {
  199. const temp = this.questionList[oldVal - 1]
  200. if (['填空题'].includes(temp.questionType)) {
  201. temp.answer = temp.options.map(item => item.answer)
  202. }
  203. }
  204. }
  205. },
  206. mounted () {
  207. this.loadData()
  208. this.startCountdown()
  209. // Watermark.set(`123`, `123123`)
  210. // 监听键盘事件
  211. window.addEventListener('keyup', this.handleKeyPress)
  212. window.addEventListener('beforeunload', this.handleBeforeUnload)
  213. },
  214. beforeDestroy () {
  215. window.removeEventListener('keyup', this.handleKeyPress)
  216. window.removeEventListener('beforeunload', this.handleBeforeUnload)
  217. clearInterval(this.timer)
  218. // Watermark.set('', '')
  219. },
  220. methods: {
  221. // 获取题库数据
  222. async loadData () {
  223. if (!this.bankId) {
  224. this.$message.error('获取题目信息失败,请重试!')
  225. this.closeDialog()
  226. return
  227. }
  228. this.questionList = await this.getQuestionData()
  229. // console.log(this.questionList)
  230. },
  231. startCountdown () {
  232. const timer = setInterval(() => {
  233. if (this.countdown > 0) {
  234. this.countdown--
  235. // 判断是否到达指定时间
  236. if (this.countdown <= 60 && !this.countdownNotify) {
  237. this.$notify({
  238. title: '温馨提示',
  239. message: `距离自动提交还有${this.countdown}秒,如有未完成的试题,请尽快填写答案!`,
  240. type: 'warning'
  241. })
  242. this.countdownNotify = true
  243. }
  244. if (this.countdown === 0) {
  245. this.submitForm(this.dealFormData())
  246. clearInterval(timer)
  247. }
  248. }
  249. }, 1000)
  250. },
  251. handleActionEvent ({ key }) {
  252. switch (key) {
  253. case 'prev':
  254. this.showIndex--
  255. break
  256. case 'next':
  257. this.showIndex++
  258. break
  259. case 'submit':
  260. this.handleSubmit()
  261. break
  262. case 'cancel':
  263. this.handleCancel()
  264. break
  265. default:
  266. break
  267. }
  268. },
  269. formatNum (num) {
  270. return num < 10 ? `0${num}` : num
  271. },
  272. getQuestionData () {
  273. this.loading = true
  274. const sql = `select id_ as questionId, ti_gan_ as stem, ti_xing_ as questionType, fu_tu_ as img, xuan_xiang_lei_xi as optionType, da_an_ as options, xuan_xiang_shu_ as optionsLength, fen_zhi_ as score, ping_fen_fang_shi as rateType, ping_fen_ren_ as rater, zheng_que_da_an_ as rightKey from t_questions where parent_id_ = '${this.bankId}' and zhuang_tai_ = '启用' order by field(ti_xing_, '单选题', '多选题', '判断题', '填空题', '简答题')`
  275. return new Promise((resolve, reject) => {
  276. this.$common.request('sql', sql).then(res => {
  277. const { data = [] } = res.variables || {}
  278. if (!data.length) {
  279. this.$message.error('获取题目信息失败!')
  280. this.closeDialog()
  281. return
  282. }
  283. data.map(item => {
  284. if (item.options) {
  285. const temp = JSON.parse(item.options) || {}
  286. const o = []
  287. Object.keys(temp).forEach(key => {
  288. o.push({
  289. label: key,
  290. value: temp[key]
  291. })
  292. })
  293. item.options = o
  294. } else {
  295. item.options = Array.from({ length: item.optionsLength }, (v, k) => {
  296. return { answer: null }
  297. })
  298. }
  299. item.img = item.img ? JSON.parse(item.img) : ''
  300. item.answer = item.questionType === '多选题' ? [] : null
  301. })
  302. this.loading = false
  303. resolve(data)
  304. }).catch(error => {
  305. this.loading = false
  306. reject(error)
  307. })
  308. })
  309. },
  310. getProgress () {
  311. const finished = []
  312. this.questionList.forEach(item => {
  313. if (item.questionType === '填空题') {
  314. finished.push(item.answer && !item.answer.some(i => i === '' || i === null))
  315. } else if (item.questionType === '多选题') {
  316. finished.push(item.answer.some(i => i) && true)
  317. } else {
  318. finished.push(item.answer && true)
  319. }
  320. })
  321. // const finished = this.questionList.filter(item => (item.questionType !== '填空题' && item.answer) || (item.questionType === '填空题' && item.answer && !item.answer.some(i => i === '' || i === null))).length
  322. const finishedCount = finished.filter(i => i).length
  323. const progress = round(parseFloat(finishedCount / this.questionList.length * 100), 2)
  324. return progress ? progress > 100 ? 100 : progress : 0
  325. },
  326. changeOptions (val) {
  327. this.questionList[this.showIndex - 1].answer = val
  328. },
  329. setClassName (item, index) {
  330. const result = []
  331. if (item.answer) {
  332. // 填空题且未填写所有答案
  333. if (['填空题'].includes(item.questionType) && (item.options.every(i => !i.answer))) {
  334. result.push('')
  335. } else if (['多选题'].includes(item.questionType) && !item.answer.length) {
  336. result.push('')
  337. } else if (item.questionType === '填空题' && (item.options.some(i => !i.answer))) {
  338. result.push('incomplete')
  339. } else {
  340. result.push('finished')
  341. }
  342. }
  343. if (this.showIndex === index + 1) {
  344. result.push('active')
  345. }
  346. return result.join(' ')
  347. },
  348. handleKeyPress (event) {
  349. if (event.keyCode === 37 || event.key === 'ArrowLeft') {
  350. if (this.showIndex === 1) {
  351. this.$message.warning('已经是第一题了!')
  352. return
  353. }
  354. this.showIndex--
  355. } else if (event.keyCode === 39 || event.key === 'ArrowRight') {
  356. if (this.showIndex === this.questionList.length) {
  357. this.$message.warning('已经是最后一题了!')
  358. return
  359. }
  360. this.showIndex++
  361. } else if (event.keyCode === 27 || event.key === 'Esc') {
  362. this.handleCancel()
  363. }
  364. },
  365. handleBeforeUnload (event) {
  366. const confirmationMessage = '离开将自动提交当前数据,确定要离开吗?';
  367. (event || window.event).returnValue = confirmationMessage
  368. window.addEventListener('unload', this.submitDataAfterConfirmation)
  369. return confirmationMessage
  370. },
  371. submitDataAfterConfirmation () {
  372. this.submitData(this.dealFormData())
  373. // 移除事件监听器,避免重复触发提交逻辑
  374. window.removeEventListener('unload', this.submitDataAfterConfirmation)
  375. },
  376. goNext () {
  377. if (this.showIndex === this.questionList.length) {
  378. return
  379. }
  380. this.showIndex++
  381. },
  382. getScore ({ questionType, answer, rightKey, score }) {
  383. if (questionType === '多选题') {
  384. const temp = rightKey.split(',')
  385. return answer.length === temp.length && answer.every(i => temp.includes(i)) ? score : 0
  386. } else {
  387. return answer === rightKey ? score : 0
  388. }
  389. },
  390. handleSubmit () {
  391. let incompleteList = []
  392. this.questionList.forEach((item, index) => {
  393. if (item.questionType === '填空题') {
  394. const t = item.answer && !item.answer.some(i => i === '' || i === null)
  395. incompleteList.push(!t ? index + 1 : '')
  396. } else if (item.questionType === '多选题') {
  397. const t = item.answer && item.answer.length
  398. incompleteList.push(!t ? index + 1 : '')
  399. } else {
  400. incompleteList.push(!item.answer ? index + 1 : '')
  401. }
  402. })
  403. incompleteList = incompleteList.filter(i => i)
  404. const tip = incompleteList.length ? `还有第${incompleteList.join('、')}题未作答,您确定要直接交卷吗?` : '您已完成作答,确定要交卷吗?'
  405. this.$confirm(tip, '提示', {
  406. type: incompleteList.length ? 'warning' : 'info',
  407. confirmButtonText: '确定',
  408. cancelButtonText: '取消',
  409. showClose: false,
  410. closeOnClickModal: false
  411. }).then(() => {
  412. this.submitForm(this.dealFormData())
  413. }).catch(() => {})
  414. },
  415. dealFormData () {
  416. const submitData = []
  417. const time = this.$common.getDateNow(19)
  418. this.questionList.forEach((item, index) => {
  419. const autoType = ['单选题', '多选题', '判断题'].includes(item.questionType)
  420. const multipleType = ['多选题', '填空题'].includes(item.questionType)
  421. const selectType = ['多选题', '单选题'].includes(item.questionType)
  422. submitData.push({
  423. parent_id_: this.id,
  424. ti_mu_id_: item.questionId,
  425. ti_gan_: item.stem,
  426. ti_xing_: item.questionType,
  427. fen_zhi_: item.score,
  428. fu_tu_: item.img,
  429. xuan_xiang_lei_xi: item.optionType,
  430. xuan_xiang_: selectType ? JSON.stringify(item.options) : '',
  431. can_kao_da_an_: item.rightKey,
  432. ping_fen_fang_shi: item.rateType,
  433. ping_fen_ren_: item.rater,
  434. hui_da_: multipleType && item.answer ? JSON.stringify(item.answer) : item.answer,
  435. shi_fou_yi_yue_: autoType ? '是' : '否',
  436. ping_yue_shi_jian: autoType ? time : '',
  437. de_fen_: autoType ? this.getScore(item) : '',
  438. jie_xi_: ''
  439. })
  440. })
  441. return submitData
  442. },
  443. submitForm (data) {
  444. const addParams = {
  445. tableName: 't_examination_detail',
  446. paramWhere: data
  447. }
  448. const isAllReviewed = !data.some(i => i.shi_fou_yi_yue_ === '否')
  449. const updateParams = {
  450. tableName: 't_examination',
  451. updList: [
  452. {
  453. where: {
  454. id_: this.id
  455. },
  456. param: {
  457. jie_shu_shi_jian_: this.$common.getDateNow(19),
  458. ti_ku_zong_fen_: data.reduce((sum, item) => sum + parseFloat(item.fen_zhi_), 0),
  459. zhuang_tai_: isAllReviewed ? '已完成' : '已交卷',
  460. de_fen_: isAllReviewed ? data.reduce((sum, item) => sum + parseFloat(item.de_fen_), 0) : ''
  461. // sheng_yu_shi_chan: ''
  462. }
  463. }
  464. ]
  465. }
  466. console.log(addParams, updateParams)
  467. this.$common.request('add', addParams).then(() => {
  468. this.$common.request('update', updateParams).then(() => {
  469. this.$message.success('提交成功!')
  470. this.closeDialog()
  471. })
  472. })
  473. },
  474. handleCancel () {
  475. this.$confirm('中途退出将自动提交考试数据,是否确认操作?', '提示', {
  476. confirmButtonText: '确定',
  477. cancelButtonText: '取消',
  478. type: 'warning',
  479. showClose: false,
  480. closeOnClickModal: false,
  481. closeOnPressEscape: false
  482. }).then(() => {
  483. this.submitForm(this.dealFormData())
  484. })
  485. },
  486. hilarity () {
  487. this.$notify({
  488. title: '提示',
  489. message: '考试时间已到,即将自动交卷,请勿关闭页面防止数据丢失!',
  490. duration: 0
  491. })
  492. },
  493. // 关闭当前窗口
  494. closeDialog () {
  495. this.$emit('close', false)
  496. }
  497. }
  498. }
  499. </script>
  500. <style lang="scss" scoped>
  501. .test-dialog {
  502. ::v-deep {
  503. .el-dialog__body {
  504. height: calc(100vh - 55px);
  505. width: 1080px;
  506. margin: 0 auto;
  507. box-shadow: 0 2px 12px 0 rgba(0, 0, 0, 0.1);
  508. .el-radio, .el-checkbox {
  509. display: block;
  510. margin-bottom: 10px;
  511. }
  512. .el-radio__label, .el-checkbox__label {
  513. font-size: 16px;
  514. white-space: normal;
  515. line-height: 1.5;
  516. }
  517. .el-input {
  518. margin-bottom: 10px;
  519. }
  520. .el-tag {
  521. margin-right: 0;
  522. }
  523. .list-group {
  524. display: inline-block;
  525. width: 100%;
  526. }
  527. }
  528. .el-dialog__header {
  529. text-align: center;
  530. }
  531. }
  532. .container {
  533. position: relative;
  534. height: calc(100vh - 160px);
  535. padding: 20px;
  536. .question {
  537. font-size: 16px;
  538. .question-item {
  539. margin-bottom: 10px;
  540. .type {
  541. font-size: 20px;
  542. font-weight: 600;
  543. margin-bottom: 20px;
  544. }
  545. .stem {
  546. display: flex;
  547. margin-bottom: 20px;
  548. align-items: flex-start;
  549. > span:first-child {
  550. line-height: 1.5;
  551. margin-right: 10px;
  552. }
  553. }
  554. .img {
  555. position: relative;
  556. width: 100%;
  557. margin-bottom: 10px;
  558. }
  559. }
  560. }
  561. .question-link {
  562. display: flex;
  563. flex-wrap: wrap;
  564. justify-content: flex-start;
  565. position: absolute;
  566. width: calc(100% - 40px);
  567. bottom: 20px;
  568. .progress {
  569. width: 100%;
  570. margin-bottom: 10px;
  571. ::v-deep .el-progress-bar__innerText {
  572. color: #fff !important;
  573. }
  574. }
  575. .link-item {
  576. width: 32px;
  577. height: 32px;
  578. text-align: center;
  579. line-height: 32px;
  580. border: 1px solid #DCDFE6;
  581. margin: 3px;
  582. border-radius: 2px;
  583. cursor: pointer;
  584. &:hover {
  585. background-color: #F5F7FA;
  586. }
  587. }
  588. $button-themes: (
  589. active: (#409EFF, #fff, #409EFF),
  590. incomplete: (#E6A23C, #fff, #E6A23C),
  591. finished: (#67C23A, #fff, #67C23A)
  592. );
  593. @each $theme, $colors in $button-themes {
  594. .#{$theme} {
  595. background-color: nth($colors, 1);
  596. color: nth($colors, 2);
  597. border-color: nth($colors, 3);
  598. &:hover {
  599. background-color: lighten(nth($colors, 1), 10%);
  600. border-color: lighten(nth($colors, 1), 10%);
  601. }
  602. }
  603. }
  604. }
  605. }
  606. .tips {
  607. position: fixed;
  608. font-size: 12px;
  609. right: calc(1100px + (100% - 1080px) / 2);
  610. top: 50%;
  611. width: auto;
  612. max-width: 200px;
  613. transform: translate(0, -50%);
  614. background-color: rgba(0, 0, 0, .6);
  615. color: #DCDFE6;
  616. margin-left: 20px;
  617. padding: 10px;
  618. border-radius: 5px;
  619. > div {
  620. line-height: 20px;
  621. > span {
  622. border-radius: 2px;
  623. padding: 2px 5px;
  624. margin: 0 2px;
  625. }
  626. }
  627. }
  628. .custom-title {
  629. display: flex;
  630. position: relative;
  631. justify-content: center;
  632. .dialog-title {
  633. font-size: 18px;
  634. line-height: 24px;
  635. }
  636. .countdown {
  637. position: absolute;
  638. // width: 200px;
  639. right: 0;
  640. top: -10px;
  641. }
  642. }
  643. }
  644. </style>