examQusetionCreateGapFilling.vue 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  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: 500px;"
  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. },
  25. ]"
  26. />
  27. </a-form-item>
  28. <div class="question-option">
  29. <span class="question-option-title">正确答案:</span>
  30. <span class="question-option-add" @click="questionOptionAddFun"
  31. >新增填空</span
  32. >
  33. </div>
  34. <div
  35. class="question-option-list-row"
  36. v-for="(item, index) in optionsList"
  37. :key="index"
  38. >
  39. <span
  40. v-if="optionsList.length > 1"
  41. class="question-option-list-row-delete"
  42. @click="questionOptionDeleteFun(index)"
  43. >删除</span
  44. >
  45. <a-form-item
  46. label=" "
  47. :label-col="labelCol"
  48. :wrapper-col="wrapperCol"
  49. >
  50. <a-input
  51. placeholder="填空需与试题一一对应"
  52. :maxLength="100"
  53. v-decorator="[
  54. `answers-${index}`,
  55. {
  56. rules: [{ required: true, message: '请输入填空答案!' }],
  57. },
  58. ]"
  59. />
  60. </a-form-item>
  61. </div>
  62. <a-form-item
  63. label="试题类型"
  64. :label-col="labelCol"
  65. :wrapper-col="wrapperCol"
  66. >
  67. <a-select
  68. v-decorator="[
  69. 'typeCondition',
  70. {
  71. rules: [
  72. {
  73. required: false,
  74. },
  75. ],
  76. initialValue: typeCondition,
  77. },
  78. ]"
  79. >
  80. <a-select-option
  81. v-for="(item, index) in typeConditionList"
  82. :key="index"
  83. :value="item.id"
  84. >
  85. {{ item.name }}
  86. </a-select-option>
  87. </a-select>
  88. </a-form-item>
  89. <a-form-item
  90. label="工种类别"
  91. :label-col="labelCol"
  92. :wrapper-col="wrapperCol"
  93. >
  94. <a-select
  95. v-decorator="[
  96. 'engineeringWorkChooseValue',
  97. {
  98. rules: [
  99. {
  100. required: false,
  101. },
  102. ],
  103. initialValue: engineeringWorkChooseValue,
  104. },
  105. ]"
  106. >
  107. <a-select-option
  108. v-for="(item, index) in engineeringWorkList"
  109. :key="index"
  110. :value="item.id"
  111. >
  112. {{ item.name }}
  113. </a-select-option>
  114. </a-select>
  115. </a-form-item>
  116. <a-form-item>
  117. <a-button
  118. class="qusetionSubmitBtn"
  119. type="primary"
  120. html-type="submit"
  121. >提交</a-button
  122. >
  123. </a-form-item>
  124. </a-form>
  125. </a-spin>
  126. </div>
  127. </div>
  128. </template>
  129. <script>
  130. import { formateUrlParams } from '@/filters';
  131. import { mapGetters } from 'vuex';
  132. export default {
  133. name: 'examQusetionCreateGapFilling',
  134. props: {},
  135. components: {},
  136. data() {
  137. return {
  138. loading: false, // 是否显示加载动画
  139. labelCol: { span: 6 }, // 表单行中label的占位
  140. wrapperCol: { span: 18 }, // 表单行中内容的占位
  141. aquestionAddForm: this.$form.createForm(this, {
  142. name: 'examQusetionAddDuoXuan',
  143. }),
  144. questionType: 'TianKong', // 试题类型:填空题
  145. optionsList: [{ value: '' }], // 选项列表
  146. typeConditionList: [], // 试题类型列表
  147. typeCondition: '', // 所选试题类型
  148. engineeringWorkList: [], // 工种数据列表
  149. engineeringWorkChooseValue: '', // 所选工种
  150. finalAnswerIndexs: undefined, // 正确答案下标
  151. };
  152. },
  153. created() {
  154. this.initDataFun(); // 初始化数据
  155. },
  156. mounted() {},
  157. beforeDestroy() {},
  158. watch: {},
  159. computed: {
  160. ...mapGetters([
  161. 'GET_ENGINEERING_WORK_LIST',
  162. 'GET_EXAM_QUESTION_TYPE_CONDITION',
  163. ]),
  164. },
  165. methods: {
  166. // 初始化数据
  167. initDataFun() {
  168. // 试题类型
  169. this.typeConditionList = [
  170. { name: '不限', id: '' },
  171. ...this.GET_EXAM_QUESTION_TYPE_CONDITION,
  172. ];
  173. // 工种类别
  174. this.engineeringWorkList = [
  175. { name: '不限', id: '' },
  176. ...this.GET_ENGINEERING_WORK_LIST,
  177. ];
  178. },
  179. // 操作:新增选项
  180. questionOptionAddFun() {
  181. this.optionsList.push({ value: '' });
  182. },
  183. // 操作:删除选项
  184. questionOptionDeleteFun(index) {
  185. this.optionsList.splice(index, 1);
  186. },
  187. // 操作:表单提交
  188. handleSubmitForm(e) {
  189. e.preventDefault();
  190. this.aquestionAddForm.validateFields((err, values) => {
  191. if (!err) {
  192. console.log(values);
  193. let engineerTypeInfo = this.formatEngineeringWorkChooseValue(
  194. values.engineeringWorkChooseValue
  195. ); // 获取工种信息
  196. // 需要拼接到请求地址后面的参数
  197. let urlParams = {
  198. categoryid: this.questionType, // 试题类别ID
  199. engineertypeid: values.engineeringWorkChooseValue, // 工种ID
  200. };
  201. // 接口Body参数
  202. let params = {
  203. content: values.content, // 内容
  204. finalAnswer: this.formatQuestionAnswersArr(values), // 正确答案
  205. engineerTypes: [engineerTypeInfo], // 工种信息集合
  206. type: this.questionType, // 试题类型ID
  207. answers: [], // 选项
  208. };
  209. this.httpQuestFun(urlParams, params);
  210. }
  211. });
  212. },
  213. // 获取工种信息
  214. formatEngineeringWorkChooseValue(id) {
  215. let seleteItem = {};
  216. for (let i = 0; i < this.engineeringWorkList.length; i++) {
  217. let item = this.engineeringWorkList[i];
  218. if (item.id === id) {
  219. seleteItem = item;
  220. break;
  221. }
  222. }
  223. return seleteItem;
  224. },
  225. // 把答案放入一个数组中
  226. formatQuestionAnswersArr(values) {
  227. let arr = [];
  228. for (let key in values) {
  229. if (key.includes('answers-')) {
  230. arr.push(values[key]);
  231. }
  232. }
  233. return arr;
  234. },
  235. // 表单提交请求
  236. httpQuestFun(urlParams, params) {
  237. this.loading = true;
  238. this.$_http
  239. .post(
  240. formateUrlParams(
  241. this.$_API.INTERFACE_POST_EXAMS_QUESTION_ADD,
  242. urlParams
  243. ),
  244. params
  245. )
  246. .then(() => {
  247. this.loading = false;
  248. this.$message.success('添加填空题成功');
  249. })
  250. .catch(() => {
  251. this.loading = false;
  252. });
  253. },
  254. },
  255. };
  256. </script>
  257. <style lang="less"></style>
  258. <style lang="less" scoped>
  259. @import '~@/styles/common/variable.less';
  260. </style>