examQusetionCreateTrueOrFalse.vue 8.0 KB

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