addTopic.vue 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673
  1. <!--新增题目-->
  2. <template>
  3. <div class="addTopic">
  4. <p class="title-p">
  5. {{title}}-{{pageType == 1? "单选题": pageType == 2? "多选题": pageType == 3? "判断题": ""}}
  6. </p>
  7. <el-form ref="form" :model="form" :rules="rules" label-width="80px">
  8. <!-- <el-form-item label="学科类别:" prop="code">
  9. <el-select v-model="form.code" @change="subClassChange" placeholder="请选择学科类别">
  10. <el-option
  11. v-for="item in subClassOptions"
  12. :key="item.dictValue"
  13. :label="item.dictLabel"
  14. :value="item.dictValue"
  15. >
  16. </el-option>
  17. </el-select>
  18. </el-form-item>-->
  19. <el-form-item label="题目分类:" prop="classifyIds">
  20. <el-select
  21. v-model="form.classifyIds"
  22. placeholder="请选择题目分类"
  23. >
  24. <el-option
  25. v-for="item in classifys"
  26. :key="item.id"
  27. :label="item.title"
  28. :value="item.id"
  29. >
  30. </el-option>
  31. </el-select>
  32. </el-form-item>
  33. <el-form-item label="难度级别:" prop="level">
  34. <el-select v-model="form.level" placeholder="请选择难度级别">
  35. <el-option
  36. v-for="item in levels"
  37. :key="item.type"
  38. :label="item.value"
  39. :value="item.type"
  40. >
  41. </el-option>
  42. </el-select>
  43. </el-form-item>
  44. <!-- <el-form-item label="归属题库:" prop="repoIds">
  45. <el-select
  46. v-model="form.repoIds"
  47. multiple
  48. placeholder="请选择归属题库"
  49. >
  50. <el-option
  51. v-for="item in repoOption"
  52. :key="item.id"
  53. :label="item.title"
  54. :value="item.id"
  55. >
  56. </el-option>
  57. </el-select>
  58. </el-form-item>-->
  59. <!-- <el-form-item label="专业:" prop="majorIds">
  60. <el-select
  61. v-model="form.majorIds"
  62. multiple
  63. placeholder="请选择专业"
  64. >
  65. <el-option
  66. v-for="item in majors"
  67. :key="item.id"
  68. :label="item.title"
  69. :value="item.id"
  70. >
  71. </el-option>
  72. </el-select>
  73. </el-form-item>-->
  74. <!--<el-form-item label="适用范围:" prop="repoIds">-->
  75. <!--<el-select-->
  76. <!--v-model="form.scopeType"-->
  77. <!--placeholder="请选择适用范围"-->
  78. <!--&gt;-->
  79. <!--<el-option-->
  80. <!--v-for="item in scopeTypes"-->
  81. <!--:key="item.id"-->
  82. <!--:label="item.value"-->
  83. <!--:value="item.type"-->
  84. <!--&gt;-->
  85. <!--</el-option>-->
  86. <!--</el-select>-->
  87. <!--</el-form-item>-->
  88. <el-form-item label="题目名称:" prop="content">
  89. <el-input
  90. v-model="form.content"
  91. placeholder="请输入题目名称"
  92. clearable
  93. size="small"
  94. maxLength="1500"
  95. />
  96. </el-form-item>
  97. <el-form-item
  98. label="选项设置:"
  99. class="options-box"
  100. v-if="pageType == 1 || pageType == 2"
  101. >
  102. <el-form-item
  103. :label="
  104. index == 0
  105. ? '选项A'
  106. : index == 1
  107. ? '选项B'
  108. : index == 2
  109. ? '选项C'
  110. : index == 3
  111. ? '选项D'
  112. : index == 4
  113. ? '选项E'
  114. : index == 5
  115. ? '选项F'
  116. : index == 6
  117. ? '选项G'
  118. : index == 7
  119. ? '选项H'
  120. : ''
  121. "
  122. v-for="(item, index) in form.answerList"
  123. :key="index"
  124. class="options-min-box"
  125. >
  126. <el-input
  127. v-model="item.content"
  128. placeholder="请输入选项内容"
  129. clearable
  130. size="small"
  131. maxLength="1500"
  132. />
  133. <i class="el-icon-remove del-i-box" @click="delOptions(index)"></i>
  134. </el-form-item>
  135. <p class="add-button" @click="addOptions">
  136. <i class="el-icon-plus"></i>添加
  137. </p>
  138. </el-form-item>
  139. <el-form-item label="正确答案:">
  140. <template>
  141. <el-radio
  142. v-model="radio"
  143. :label="index"
  144. v-for="(item, index) in form.answerList"
  145. :key="index"
  146. v-if="pageType == 1"
  147. >{{
  148. index == 0
  149. ? "选项A"
  150. : index == 1
  151. ? "选项B"
  152. : index == 2
  153. ? "选项C"
  154. : index == 3
  155. ? "选项D"
  156. : index == 4
  157. ? "选项E"
  158. : index == 5
  159. ? "选项F"
  160. : index == 6
  161. ? "选项G"
  162. : index == 7
  163. ? "选项H"
  164. : ""
  165. }}</el-radio
  166. >
  167. <el-checkbox
  168. v-model="radioList"
  169. :label="index"
  170. v-for="(item, index) in form.answerList"
  171. :key="index"
  172. v-if="pageType == 2"
  173. >{{
  174. index == 0
  175. ? "选项A"
  176. : index == 1
  177. ? "选项B"
  178. : index == 2
  179. ? "选项C"
  180. : index == 3
  181. ? "选项D"
  182. : index == 4
  183. ? "选项E"
  184. : index == 5
  185. ? "选项F"
  186. : index == 6
  187. ? "选项G"
  188. : index == 7
  189. ? "选项H"
  190. : ""
  191. }}</el-checkbox
  192. >
  193. <el-radio v-model="radio" :label="index" v-if="pageType == 3" v-for="(item,index) in form.answerList" :key="index"
  194. >{{item.content}}</el-radio
  195. >
  196. </template>
  197. </el-form-item>
  198. <el-form-item label="答案解析:" prop="analysis">
  199. <el-input
  200. v-model="form.analysis"
  201. placeholder="请输入答案解析"
  202. clearable
  203. size="small"
  204. maxLength="1500"
  205. />
  206. </el-form-item>
  207. </el-form>
  208. <div class="button-box">
  209. <el-button @click="leftButtonClick">取 消</el-button>
  210. <el-button type="primary" @click="rightButtonClick">确 定</el-button>
  211. </div>
  212. </div>
  213. </template>
  214. <script>
  215. import { Message } from "element-ui";
  216. import { optionRepo } from "@/api/exam/repo";
  217. import { list_option } from "@/api/exam/el_classify";
  218. import { getQu,updateQu,addQu } from "@/api/exam/qu";
  219. export default {
  220. props: {
  221. pageType: {},
  222. quId:{},
  223. title:"",
  224. },
  225. name: "addTopic",
  226. data() {
  227. return {
  228. scopeTypes: [
  229. {
  230. type: 1,
  231. value: "安全准入考试",
  232. },
  233. {
  234. type: 2,
  235. value: "负面清单考试",
  236. },
  237. {
  238. type: 3,
  239. value: "黑名单考试",
  240. },
  241. {
  242. type: 4,
  243. value: "模拟考试",
  244. },
  245. ],
  246. levels: [
  247. {
  248. type: 1,
  249. value: "中等",
  250. },
  251. {
  252. type: 2,
  253. value: "较难",
  254. },
  255. {
  256. type: 3,
  257. value: "难",
  258. },
  259. ],
  260. quTypes: [
  261. {
  262. type: 1,
  263. value: "单选题",
  264. },
  265. {
  266. type: 2,
  267. value: "多选题",
  268. },
  269. {
  270. type: 3,
  271. value: "判断题",
  272. },
  273. ],
  274. form: {
  275. code: "",//学科类别
  276. level: "",//难度级别
  277. repoIds:[],//归属题库
  278. content: "",//名称
  279. answerList:[],//选项
  280. analysis:"",//解析
  281. classifyIds: "",//题目分类
  282. majorIds: "",//专业
  283. },
  284. radio:"",
  285. radioList:[],
  286. rules: {
  287. classifyIds: [
  288. { required: true, trigger: "blur", message: "请选择题目分类" },
  289. ],
  290. level: [
  291. { required: true, trigger: "blur", message: "请选择难度级别" },
  292. ],
  293. // repoIds: [
  294. // { required: true, trigger: "blur", message: "请选择归属题库" },
  295. // ],
  296. content: [
  297. { required: true, trigger: "blur", message: "请输入题目名称" },
  298. { required: true, message: "请输入题目名称", validator: this.spaceJudgment, trigger: "blur" }
  299. // { min: 2, max: 60, message: '题目名称长度必须介于 2 和 60 之间', trigger: 'blur' }
  300. ],
  301. // analysis: [
  302. // { required: true, trigger: "blur", message: "请输入答案解析" },
  303. // { min: 2, max: 200, message: '答案解析长度必须介于 2 和 200 之间', trigger: 'blur' }
  304. // ],
  305. },
  306. // 学科字典
  307. subClassOptions: [],
  308. // 归属题库
  309. repoOption: [],
  310. // 题目分类
  311. classifys: [],
  312. //专业
  313. majors: []
  314. };
  315. },
  316. created() {
  317. if(this.quId){
  318. // this.form = {...this.quData};
  319. this.getInfo(this.quId);
  320. this.title = '编辑题目'
  321. }else{
  322. this.title = '新增题目'
  323. }
  324. if(this.pageType==3){
  325. this.form.answerList=[
  326. {
  327. content:"正确",
  328. isRight:0
  329. },
  330. {
  331. content:"错误",
  332. isRight:0
  333. },
  334. ]
  335. }
  336. // this.getDicts("subject_class").then((response) => {
  337. // this.subClassOptions = response.data;
  338. // });
  339. // this.getRepoOption();
  340. this.getClassifys();
  341. this.getMajors();
  342. },
  343. methods: {
  344. subClassChange(e){
  345. this.getRepoOption(e);
  346. },
  347. getInfo(id){
  348. let self = this;
  349. getQu(id).then(response => {
  350. this.form = response.data;
  351. this.form.code = String(response.data.code);
  352. this.form.classifyIds = Number(this.form.classifyIds);
  353. // let ids = [];
  354. /* if(this.form.majorIds){
  355. this.form.majorIds = this.form.majorIds.split(",")
  356. for (let i = 0; i < this.form.majorIds.length; i++) {
  357. ids.push(parseInt(this.form.majorIds[i]))
  358. }
  359. this.form.majorIds = ids;
  360. } */
  361. // ids = [];
  362. // if(this.form.classifyIds){
  363. // this.form.classifyIds = this.form.classifyIds.split(",")
  364. // for (let i = 0; i < this.form.classifyIds.length; i++) {
  365. // ids.push(parseInt(this.form.classifyIds[i]))
  366. // }
  367. // this.form.classifyIds = ids;
  368. // }
  369. // self.getRepoOption(this.form.code)
  370. //判断题
  371. if(this.pageType==3||this.pageType==1){
  372. for(let i=0;i<response.data.answerList.length;i++){
  373. if(response.data.answerList[i].isRight==1){
  374. self.radio = i;
  375. }
  376. }
  377. }else if(this.pageType==2){
  378. for(let i=0;i<response.data.answerList.length;i++){
  379. if(response.data.answerList[i].isRight==1){
  380. self.radioList.push(i);
  381. }
  382. }
  383. }
  384. // this.form.level = String(response.data.level);
  385. });
  386. },
  387. /** 查询题目分类 */
  388. getClassifys(){
  389. list_option({}).then(response => {
  390. this.classifys = response.data;
  391. });
  392. },
  393. /** 查询专业分类 */
  394. getMajors(){
  395. // TODO 等专业接口 出来替换
  396. //list_option({}).then(response => {
  397. this.majors = [
  398. {
  399. id: 1,
  400. title: "化学类"
  401. },
  402. {
  403. id: 2,
  404. title: "物理类"
  405. }
  406. ]
  407. //});
  408. },
  409. /** 查询题库 */
  410. getRepoOption(code) {
  411. optionRepo({"code": code}).then(response => {
  412. this.repoOption = response.data;
  413. });
  414. },
  415. StringFuncition(index) {
  416. let text = String(index);
  417. return String(index + 1);
  418. },
  419. //添加选项
  420. addOptions() {
  421. if (this.form.answerList.length > 7) {
  422. Message({
  423. message: "无法添加更多选项",
  424. type: "warning",
  425. });
  426. return;
  427. }
  428. let obj = {
  429. content: "",
  430. };
  431. if (this.pageType == 2) {
  432. obj.checked = false;
  433. }
  434. this.form.answerList.push(obj);
  435. },
  436. //删除选项
  437. delOptions(index) {
  438. let self = this;
  439. this.form.answerList.splice(index, 1);
  440. if (this.pageType==2){
  441. for (let i=0;i<self.radioList.length;i++){
  442. if(!self.form.answerList[self.radioList[i]]){
  443. self.radioList.splice(i, 1);
  444. }
  445. }
  446. } else if(this.pageType==1){
  447. if(!self.form.answerList[self.radio]){
  448. self.radio="";
  449. }
  450. }
  451. },
  452. leftButtonClick() {
  453. this.$parent.offAddTopic();
  454. },
  455. rightButtonClick() {
  456. let self = this;
  457. this.$refs.form.validate(valid => {
  458. if (valid) {
  459. this.loading = true;
  460. // 数组转换
  461. // this.form.majorIds = this.form.majorIds.join(",")
  462. // this.form.classifyIds = this.form.classifyIds.join(",")
  463. if(this.form.id){
  464. //修改
  465. let newData = {...this.form};
  466. if(this.pageType==1||this.pageType==3){
  467. if(!self.radio){
  468. if (self.radio!==0){
  469. Message({
  470. message: "请选择答案",
  471. type: "warning",
  472. });
  473. return
  474. }
  475. }
  476. for(let i=0;i<newData.answerList.length;i++){
  477. if(i==this.radio){
  478. newData.answerList[i].isRight = 1;
  479. }else{
  480. newData.answerList[i].isRight = 0;
  481. }
  482. }
  483. }else if(this.pageType==2){
  484. if(!newData.answerList[0]){
  485. Message({
  486. message: "请添加选项",
  487. type: "warning",
  488. });
  489. return
  490. }
  491. if(!self.radioList[0]){
  492. if(self.radioList[0]!==0){
  493. Message({
  494. message: "请选择答案",
  495. type: "warning",
  496. });
  497. return
  498. }
  499. }
  500. let num = 0 ;
  501. for(let i=0;i<newData.answerList.length;i++){
  502. if(!newData.answerList[i].content){
  503. num++
  504. }
  505. newData.answerList[i].isRight = 0;
  506. }
  507. if(num>0){
  508. Message({
  509. message: "请填写选项内容",
  510. type: "warning",
  511. });
  512. return
  513. }
  514. for(let i=0;i<self.radioList.length;i++){
  515. newData.answerList[self.radioList[i]].isRight = 1;
  516. }
  517. }
  518. updateQu(newData).then(response => {
  519. this.loading = false;
  520. Message({
  521. message: "编辑成功",
  522. type: "success",
  523. });
  524. this.$parent.refreshData();
  525. });
  526. }else{
  527. //新建
  528. let newData = {...this.form};
  529. newData.quType = Number(this.pageType);
  530. if(this.pageType==1||this.pageType==3){
  531. //单选-判断
  532. if(!self.radio){
  533. if (self.radio!==0){
  534. Message({
  535. message: "请选择答案",
  536. type: "warning",
  537. });
  538. return
  539. }
  540. }
  541. for(let i=0;i<newData.answerList.length;i++){
  542. if(i==this.radio){
  543. newData.answerList[i].isRight = 1;
  544. }else{
  545. newData.answerList[i].isRight = 0;
  546. }
  547. }
  548. }else if(this.pageType==2){
  549. //多选
  550. if(!newData.answerList[0]){
  551. Message({
  552. message: "请添加选项",
  553. type: "warning",
  554. });
  555. return
  556. }
  557. if(!self.radioList[0]){
  558. if(self.radioList[0]!==0){
  559. Message({
  560. message: "请选择答案",
  561. type: "warning",
  562. });
  563. return
  564. }
  565. }
  566. let num = 0 ;
  567. for(let i=0;i<newData.answerList.length;i++){
  568. if(!newData.answerList[i].content){
  569. num++
  570. }
  571. newData.answerList[i].isRight = 0;
  572. }
  573. if(num>0){
  574. Message({
  575. message: "请填写选项内容",
  576. type: "warning",
  577. });
  578. return
  579. }
  580. for(let i=0;i<self.radioList.length;i++){
  581. newData.answerList[self.radioList[i]].isRight = 1;
  582. }
  583. }
  584. addQu(newData).then(response => {
  585. this.loading = false;
  586. Message({
  587. message: "新增成功",
  588. type: "success",
  589. });
  590. this.$parent.refreshData();
  591. });
  592. }
  593. }
  594. })
  595. },
  596. },
  597. };
  598. </script>
  599. <style lang="scss" scoped>
  600. .addTopic {
  601. height: 100%;
  602. width: 100%;
  603. padding: 0 15px;
  604. .title-p {
  605. line-height: 60px;
  606. color: #0045af;
  607. font-size: 18px;
  608. margin: -20px 0 20px;
  609. border-bottom: 1px solid #e0e0e0;
  610. }
  611. .del-i-box {
  612. font-size: 20px;
  613. color: #999;
  614. cursor: pointer;
  615. margin-left: 10px;
  616. }
  617. .options-box {
  618. .options-min-box {
  619. margin-top: 20px;
  620. }
  621. .options-min-box:nth-child(1) {
  622. margin-top: 0;
  623. }
  624. }
  625. .add-button {
  626. margin-top: 20px;
  627. width: 365px;
  628. height: 40px;
  629. line-height: 40px;
  630. border: 1px solid #e0e0e0;
  631. border-radius: 6px;
  632. font-size: 12px;
  633. color: #999;
  634. text-align: center;
  635. margin-left: 100px;
  636. cursor: pointer;
  637. i {
  638. margin-right: 10px;
  639. }
  640. }
  641. .button-box {
  642. width: 220px;
  643. display: flex;
  644. margin-left: 196px;
  645. p {
  646. cursor: pointer;
  647. width: 100px;
  648. height: 40px;
  649. line-height: 40px;
  650. border-radius: 6px;
  651. font-size: 14px;
  652. text-align: center;
  653. }
  654. p:nth-child(1) {
  655. background: #e0e0e0;
  656. color: #333333;
  657. margin-right: 20px;
  658. }
  659. p:nth-child(2) {
  660. background: #0045af;
  661. color: #ffffff;
  662. }
  663. }
  664. }
  665. </style>