examQusetionCreateMultiple.vue 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345
  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. },
  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 > 2"
  41. class="question-option-list-row-delete"
  42. @click="questionOptionDeleteFun(index)"
  43. >删除</span
  44. >
  45. <a-form-item
  46. :label="index | formatQuestionIndex"
  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. mode="multiple"
  69. v-decorator="[
  70. 'finalAnswerIndexs',
  71. {
  72. rules: [{ required: true, message: '请选择答案!' }],
  73. initialValue: finalAnswerIndexs,
  74. },
  75. ]"
  76. >
  77. <a-select-option
  78. v-for="(item, index) in optionsList"
  79. :key="index"
  80. :value="index"
  81. >
  82. {{ index | formatQuestionIndex }}
  83. </a-select-option>
  84. </a-select>
  85. </a-form-item>
  86. <div class="create-select-div">
  87. <span>试题类型:</span>
  88. <div class="create-select-row">
  89. <div class="create-select-item">
  90. <span>父类</span>
  91. <a-select
  92. v-model="typeConditionParentValue"
  93. @change="selectTypeConditionParent"
  94. >
  95. <a-select-option
  96. v-for="(item, index) in typeConditionParentList"
  97. :key="index"
  98. :value="item.id"
  99. >
  100. {{ item.name }}
  101. </a-select-option>
  102. </a-select>
  103. </div>
  104. <div class="create-select-item">
  105. <span>子类</span>
  106. <a-select v-model="typeConditionChildrenValue">
  107. <a-select-option
  108. v-for="(item, index) in typeConditionChildrenList"
  109. :key="index"
  110. :value="item.id"
  111. >
  112. {{ item.name }}
  113. </a-select-option>
  114. </a-select>
  115. </div>
  116. </div>
  117. </div>
  118. <a-form-item
  119. label="工种类别"
  120. :label-col="labelCol"
  121. :wrapper-col="wrapperCol"
  122. >
  123. <a-select
  124. v-decorator="[
  125. 'engineeringWorkChooseValue',
  126. {
  127. rules: [
  128. {
  129. required: false,
  130. },
  131. ],
  132. initialValue: engineeringWorkChooseValue,
  133. },
  134. ]"
  135. >
  136. <a-select-option
  137. v-for="(item, index) in engineeringWorkList"
  138. :key="index"
  139. :value="item.id"
  140. >
  141. {{ item.name }}
  142. </a-select-option>
  143. </a-select>
  144. </a-form-item>
  145. <a-form-item>
  146. <a-button
  147. class="qusetionSubmitBtn"
  148. type="primary"
  149. html-type="submit"
  150. >提交</a-button
  151. >
  152. </a-form-item>
  153. </a-form>
  154. </a-spin>
  155. </div>
  156. <div class="company-info">
  157. <span>
  158. copyright © 浮游科技有限公司出品
  159. </span>
  160. </div>
  161. </div>
  162. </template>
  163. <script>
  164. import {
  165. formatQuestionIndex,
  166. formateUrlParams,
  167. formatePathParams,
  168. } from '@/filters';
  169. import { mapGetters } from 'vuex';
  170. export default {
  171. name: 'examQusetionCreateMultiple',
  172. props: {},
  173. components: {},
  174. data() {
  175. return {
  176. loading: false, // 是否显示加载动画
  177. labelCol: { span: 6 }, // 表单行中label的占位
  178. wrapperCol: { span: 18 }, // 表单行中内容的占位
  179. aquestionAddForm: this.$form.createForm(this, {
  180. name: 'examQusetionAddDuoXuan',
  181. }),
  182. questionType: 'DuoXuan', // 试题类型:多选题
  183. optionsList: [{ value: '' }, { value: '' }], // 选项列表
  184. typeConditionParentList: [], // 试题类型-父类列表
  185. typeConditionParentValue: '', // 父类所选值
  186. typeConditionChildrenList: [], // 试题类型-子类列表
  187. typeConditionChildrenValue: '', // 子类所选值
  188. engineeringWorkList: [], // 工种数据列表
  189. engineeringWorkChooseValue: '', // 所选工种
  190. finalAnswerIndexs: undefined, // 正确答案下标
  191. };
  192. },
  193. created() {
  194. this.initDataFun(); // 初始化数据
  195. },
  196. mounted() {},
  197. beforeDestroy() {},
  198. watch: {},
  199. computed: {
  200. ...mapGetters([
  201. 'GET_ENGINEERING_WORK_LIST',
  202. 'GET_EXAM_QUESTION_TYPE_CONDITION_PARENT',
  203. ]),
  204. },
  205. methods: {
  206. // 初始化数据
  207. initDataFun() {
  208. // 试题类型
  209. this.typeConditionParentList = [
  210. ...this.GET_EXAM_QUESTION_TYPE_CONDITION_PARENT,
  211. ];
  212. this.typeConditionParentValue = this.typeConditionParentList[0].id;
  213. // 工种类别
  214. this.engineeringWorkList = [
  215. { name: '不限', id: '' },
  216. ...this.GET_ENGINEERING_WORK_LIST,
  217. ];
  218. this.getTableChildrenListFun(); // 查询:试题类型列表-子类
  219. },
  220. // 查询:试题类型列表-子类
  221. getTableChildrenListFun() {
  222. this.loading = true;
  223. let params = {
  224. categoryId: this.typeConditionParentValue,
  225. };
  226. this.$_http
  227. .get(
  228. formatePathParams(
  229. this.$_API.INTERFACE_GET_EXAMS_QUESTION_TYPE_CONDITION_CHILDRENS,
  230. params
  231. )
  232. )
  233. .then((res) => {
  234. this.typeConditionChildrenList = res.data;
  235. this.typeConditionChildrenValue = this.typeConditionChildrenList[0].id;
  236. this.loading = false;
  237. })
  238. .catch(() => {
  239. this.loading = false;
  240. });
  241. },
  242. // 操作:新增选项
  243. questionOptionAddFun() {
  244. this.optionsList.push({ value: '' });
  245. },
  246. // 操作:删除选项
  247. questionOptionDeleteFun(index) {
  248. this.optionsList.splice(index, 1);
  249. },
  250. // 操作:选择了父类
  251. selectTypeConditionParent() {
  252. this.getTableChildrenListFun(); // 查询:试题类型列表-子类
  253. },
  254. // 操作:表单提交
  255. handleSubmitForm(e) {
  256. e.preventDefault();
  257. this.aquestionAddForm.validateFields((err, values) => {
  258. if (!err) {
  259. // let engineerTypeInfo = this.formatEngineeringWorkChooseValue(
  260. // values.engineeringWorkChooseValue
  261. // ); // 获取工种信息
  262. // 需要拼接到请求地址后面的参数
  263. let urlParams = {
  264. categoryid: this.typeConditionChildrenValue, // 试题类型ID
  265. engineertypeid: values.engineeringWorkChooseValue, // 工种ID
  266. };
  267. // 接口Body参数
  268. let params = {
  269. content: values.content, // 内容
  270. finalAnswer: this.formatQuestionFinalAnswerArr(
  271. values.finalAnswerIndexs
  272. ), // 正确答案
  273. // engineerTypes: [engineerTypeInfo], // 工种信息集合
  274. type: this.questionType, // 试题类型ID
  275. answers: this.formatQuestionAnswersArr(values), // 选项
  276. };
  277. this.httpQuestFun(urlParams, params);
  278. }
  279. });
  280. },
  281. // 获取工种信息
  282. formatEngineeringWorkChooseValue(id) {
  283. let seleteItem = {};
  284. for (let i = 0; i < this.engineeringWorkList.length; i++) {
  285. let item = this.engineeringWorkList[i];
  286. if (item.id === id) {
  287. seleteItem = item;
  288. break;
  289. }
  290. }
  291. return seleteItem;
  292. },
  293. // 把答案下标转换为大写英文字母放入一个数组中
  294. formatQuestionFinalAnswerArr(indexs) {
  295. let arr = [];
  296. indexs.forEach((item) => {
  297. arr.push(formatQuestionIndex(item));
  298. });
  299. arr.sort();
  300. let str = '';
  301. arr.forEach((item) => {
  302. str = str + item;
  303. });
  304. return [str];
  305. },
  306. // 把选项放入一个数组中
  307. formatQuestionAnswersArr(values) {
  308. let arr = [];
  309. for (let key in values) {
  310. if (key.includes('answers-')) {
  311. arr.push(values[key]);
  312. }
  313. }
  314. return arr;
  315. },
  316. // 表单提交请求
  317. httpQuestFun(urlParams, params) {
  318. this.loading = true;
  319. this.$_http
  320. .post(
  321. formateUrlParams(
  322. this.$_API.INTERFACE_POST_EXAMS_QUESTION_ADD,
  323. urlParams
  324. ),
  325. params
  326. )
  327. .then(() => {
  328. this.loading = false;
  329. this.$message.success('添加多选题成功');
  330. })
  331. .catch(() => {
  332. this.loading = false;
  333. });
  334. },
  335. },
  336. };
  337. </script>
  338. <style lang="less"></style>
  339. <style lang="less" scoped>
  340. @import '~@/styles/common/variable.less';
  341. </style>