examQusetionCreateGapFilling.vue 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387
  1. <template>
  2. <div class="app-container">
  3. <div class="common-card exam-question-create-body">
  4. <a-spin :spinning="loading">
  5. <a-form
  6. :form="aquestionAddForm"
  7. @submit="handleSubmitForm"
  8. style="width: 515px;"
  9. :loading="loading"
  10. >
  11. <a-form-item
  12. label="试题内容"
  13. :label-col="labelCol"
  14. :wrapper-col="wrapperCol"
  15. >
  16. <a-textarea
  17. placeholder="试题内容"
  18. :auto-size="{ minRows: 3, maxRows: 5 }"
  19. :maxLength="500"
  20. v-decorator="[
  21. 'content',
  22. {
  23. rules: [{ required: true, message: '请输入试题内容!' }],
  24. initialValue: examQuestionDetail.content,
  25. },
  26. ]"
  27. />
  28. </a-form-item>
  29. <div class="question-option">
  30. <span class="question-option-title">正确答案:</span>
  31. <span class="question-option-add" @click="questionOptionAddFun"
  32. >新增填空</span
  33. >
  34. </div>
  35. <div
  36. class="question-option-list-row"
  37. v-for="(item, index) in optionsList"
  38. :key="index"
  39. >
  40. <span
  41. v-if="optionsList.length > 1"
  42. class="question-option-list-row-delete"
  43. @click="questionOptionDeleteFun(index)"
  44. >删除</span
  45. >
  46. <a-form-item
  47. label=" "
  48. :label-col="labelCol"
  49. :wrapper-col="wrapperCol"
  50. >
  51. <a-input
  52. placeholder="填空需与试题一一对应"
  53. :maxLength="100"
  54. v-decorator="[
  55. `answers-${index}`,
  56. {
  57. rules: [{ required: true, message: '请输入填空答案!' }],
  58. initialValue: item.value,
  59. },
  60. ]"
  61. />
  62. </a-form-item>
  63. </div>
  64. <div class="create-select-div">
  65. <span>试题类型:</span>
  66. <div class="create-select-row">
  67. <div class="create-select-item">
  68. <span>父类:</span>
  69. <a-select
  70. v-model="typeConditionParentValue"
  71. @change="selectTypeConditionParent"
  72. >
  73. <a-select-option
  74. v-for="(item, index) in typeConditionParentList"
  75. :key="index"
  76. :value="item.id"
  77. >
  78. {{ item.name }}
  79. </a-select-option>
  80. </a-select>
  81. </div>
  82. <div class="create-select-item">
  83. <span>子类:</span>
  84. <a-select
  85. v-model="typeConditionChildrenValue"
  86. :disabled="!typeConditionParentValue"
  87. >
  88. <a-select-option
  89. v-for="(item, index) in typeConditionChildrenList"
  90. :key="index"
  91. :value="item.id"
  92. >
  93. {{ item.name }}
  94. </a-select-option>
  95. </a-select>
  96. </div>
  97. </div>
  98. </div>
  99. <a-form-item
  100. label="工种类别"
  101. :label-col="labelCol"
  102. :wrapper-col="wrapperCol"
  103. >
  104. <a-select
  105. v-decorator="[
  106. 'engineeringWorkChooseValue',
  107. {
  108. rules: [
  109. {
  110. required: false,
  111. },
  112. ],
  113. initialValue: engineeringWorkChooseValue,
  114. },
  115. ]"
  116. >
  117. <a-select-option
  118. v-for="(item, index) in engineeringWorkList"
  119. :key="index"
  120. :value="item.id"
  121. >
  122. {{ item.name }}
  123. </a-select-option>
  124. </a-select>
  125. </a-form-item>
  126. <a-form-item>
  127. <a-button
  128. class="qusetionSubmitBtn"
  129. type="primary"
  130. html-type="submit"
  131. >提交</a-button
  132. >
  133. </a-form-item>
  134. </a-form>
  135. </a-spin>
  136. </div>
  137. <div class="company-info">
  138. <span>
  139. copyright © 浮游科技有限公司出品
  140. </span>
  141. </div>
  142. </div>
  143. </template>
  144. <script>
  145. import {
  146. // formatQuestionLetter,
  147. formateUrlParams,
  148. formatePathParams,
  149. } from '@/filters';
  150. import { mapGetters } from 'vuex';
  151. export default {
  152. name: 'examQusetionCreateGapFilling',
  153. props: {},
  154. components: {},
  155. data() {
  156. return {
  157. loading: false, // 是否显示加载动画
  158. labelCol: { span: 6 }, // 表单行中label的占位
  159. wrapperCol: { span: 18 }, // 表单行中内容的占位
  160. aquestionAddForm: this.$form.createForm(this, {
  161. name: 'examQusetionAddDuoXuan',
  162. }),
  163. questionType: 'TianKong', // 试题类型:填空题
  164. optionsList: [{ value: '' }], // 选项列表
  165. typeConditionParentList: [], // 试题类型-父类列表
  166. typeConditionParentValue: '', // 父类所选值
  167. typeConditionChildrenList: [], // 试题类型-子类列表
  168. typeConditionChildrenValue: '', // 子类所选值
  169. engineeringWorkList: [], // 工种数据列表
  170. engineeringWorkChooseValue: '', // 所选工种
  171. finalAnswerIndexs: undefined, // 正确答案下标
  172. examQuestionDetail: {
  173. id: '', // key
  174. content: '', // 内容
  175. finalAnswer: [], // 正确答案
  176. engineerTypes: [], // 信息集合:工种
  177. type: '', // 试题类型ID
  178. answers: [], // 选项
  179. rootQuestionCategory: '', // 试题类型-父类
  180. questionCategory: '', // 试题类型-子类
  181. }, // 题目的信息
  182. };
  183. },
  184. created() {
  185. this.initDataFun(); // 初始化数据
  186. },
  187. mounted() {},
  188. beforeDestroy() {},
  189. watch: {},
  190. computed: {
  191. ...mapGetters([
  192. 'GET_ENGINEERING_WORK_LIST',
  193. 'GET_EXAM_QUESTION_TYPE_CONDITION_PARENT',
  194. ]),
  195. },
  196. methods: {
  197. // 初始化数据
  198. initDataFun() {
  199. // 试题类型
  200. this.typeConditionParentList = [
  201. ...this.GET_EXAM_QUESTION_TYPE_CONDITION_PARENT,
  202. ];
  203. // 工种类别
  204. this.engineeringWorkList = [
  205. { name: '不限', id: '' },
  206. ...this.GET_ENGINEERING_WORK_LIST,
  207. ];
  208. if (this.$route.query.id) {
  209. this.getExamQuestionDetailFun(this.$route.query.id); // 查询:试题的详情
  210. } else {
  211. this.typeConditionParentValue = this.typeConditionParentList[0].id; // 所选试题类型-父类
  212. this.getTableChildrenListFun(true); // 查询:试题类型列表-子类
  213. }
  214. },
  215. // 查询:试题的详情
  216. getExamQuestionDetailFun(id) {
  217. if (!id) {
  218. return;
  219. }
  220. this.loading = true;
  221. let params = {
  222. questionId: id,
  223. };
  224. this.$_http
  225. .get(
  226. formatePathParams(
  227. this.$_API.INTERFACE_GET_EXAMS_QUESTION_DETAIL,
  228. params
  229. )
  230. )
  231. .then((res) => {
  232. this.examQuestionDetail = { ...res.data };
  233. // 选项
  234. this.optionsList = this.examQuestionDetail.finalAnswer.map((item) => {
  235. return { value: item };
  236. });
  237. // 正确答案
  238. // this.finalAnswerIndexs = formatQuestionLetter(
  239. // this.examQuestionDetail.finalAnswer[0]
  240. // )[0];
  241. // 试题类型-父类
  242. this.typeConditionParentValue =
  243. this.examQuestionDetail.rootQuestionCategory.id || '';
  244. this.getTableChildrenListFun(); // 查询:试题类型列表-子类
  245. // 试题类型-子类
  246. this.typeConditionChildrenValue =
  247. this.examQuestionDetail.questionCategory.id || '';
  248. // 工种
  249. this.engineeringWorkChooseValue = this.examQuestionDetail.engineerTypes[0];
  250. this.loading = false;
  251. })
  252. .catch(() => {
  253. this.loading = false;
  254. });
  255. },
  256. // 查询:试题类型列表-子类
  257. getTableChildrenListFun(isNeedReset) {
  258. this.loading = true;
  259. let params = {
  260. categoryId: this.typeConditionParentValue,
  261. };
  262. this.$_http
  263. .get(
  264. formatePathParams(
  265. this.$_API.INTERFACE_GET_EXAMS_QUESTION_TYPE_CONDITION_CHILDRENS,
  266. params
  267. )
  268. )
  269. .then((res) => {
  270. this.typeConditionChildrenList = res.data;
  271. if (isNeedReset) {
  272. this.typeConditionChildrenValue = this.typeConditionChildrenList[0].id;
  273. }
  274. this.loading = false;
  275. })
  276. .catch(() => {
  277. this.loading = false;
  278. });
  279. },
  280. // 操作:新增选项
  281. questionOptionAddFun() {
  282. this.optionsList.push({ value: '' });
  283. },
  284. // 操作:删除选项
  285. questionOptionDeleteFun(index) {
  286. this.optionsList.splice(index, 1);
  287. },
  288. // 操作:选择了父类
  289. selectTypeConditionParent() {
  290. if (this.typeConditionParentValue) {
  291. this.getTableChildrenListFun(true); // 查询:试题类型列表-子类
  292. } else {
  293. this.typeConditionChildrenList = [];
  294. this.typeConditionChildrenValue = '';
  295. }
  296. },
  297. // 操作:表单提交
  298. handleSubmitForm(e) {
  299. e.preventDefault();
  300. this.aquestionAddForm.validateFields((err, values) => {
  301. if (!err) {
  302. // 需要拼接到请求地址后面的参数
  303. let urlParams = {
  304. categoryid: this.typeConditionChildrenValue, // 试题类型ID
  305. engineertypeid: values.engineeringWorkChooseValue, // 工种ID
  306. };
  307. // 接口Body参数
  308. let params = {
  309. content: values.content, // 内容
  310. finalAnswer: this.formatQuestionAnswersArr(values), // 正确答案
  311. engineerTypes: [values.engineeringWorkChooseValue], // 工种信息集合
  312. type: this.questionType, // 试题类型ID
  313. answers: [], // 选项
  314. };
  315. if (this.examQuestionDetail.id) {
  316. params.id = this.examQuestionDetail.id;
  317. let pathParams = {
  318. questionId: this.examQuestionDetail.id,
  319. };
  320. this.httpQuestEditFun(urlParams, params, pathParams); // 编辑
  321. } else {
  322. this.httpQuestAddFun(urlParams, params); // 新建
  323. }
  324. }
  325. });
  326. },
  327. // 把答案放入一个数组中
  328. formatQuestionAnswersArr(values) {
  329. let arr = [];
  330. for (let key in values) {
  331. if (key.includes('answers-')) {
  332. arr.push(values[key]);
  333. }
  334. }
  335. return arr;
  336. },
  337. // 新建
  338. httpQuestAddFun(urlParams, params) {
  339. this.loading = true;
  340. this.$_http
  341. .post(
  342. formateUrlParams(
  343. this.$_API.INTERFACE_POST_EXAMS_QUESTION_ADD,
  344. urlParams
  345. ),
  346. params
  347. )
  348. .then(() => {
  349. this.loading = false;
  350. this.$message.success('添加填空题成功');
  351. })
  352. .catch(() => {
  353. this.loading = false;
  354. });
  355. },
  356. // 编辑
  357. httpQuestEditFun(urlParams, params, pathParams) {
  358. this.loading = true;
  359. this.$_http
  360. .put(
  361. formatePathParams(
  362. formateUrlParams(
  363. this.$_API.INTERFACE_POST_EXAMS_QUESTION_EDIT,
  364. urlParams
  365. ),
  366. pathParams
  367. ),
  368. params
  369. )
  370. .then(() => {
  371. this.loading = false;
  372. this.$message.success('编辑填空题成功');
  373. })
  374. .catch(() => {
  375. this.loading = false;
  376. });
  377. },
  378. },
  379. };
  380. </script>
  381. <style lang="less"></style>
  382. <style lang="less" scoped>
  383. @import '~@/styles/common/variable.less';
  384. </style>