diff --git a/packages/examination/src/api/question/index.tsx b/packages/examination/src/api/question/index.tsx index d5cd82c..7fe783c 100644 --- a/packages/examination/src/api/question/index.tsx +++ b/packages/examination/src/api/question/index.tsx @@ -1,26 +1,19 @@ import axios from '../axios'; -/* -* 查询试题 -*/ -export function getList(num: number, page: number, obj:object){ - const data = { - num:num, - page:page, - industryId:obj['industryId'], - serviceTypeId:obj['serviceTypeId'], - questionContent:obj['questionContent'] - }; +/** + * 查询试题 + */ +export function getList(data: any) { return axios({ url: "/ex/question/getList", method: 'post', data: data - }) + }); } -/* -* 删除试题 -*/ +/** + * 删除试题 + */ export function delQuestion(ids: any) { return axios({ url: '/ex/question/delQuestion', @@ -29,44 +22,45 @@ export function delQuestion(ids: any) { }); } -/* -* 新增题目 -*/ -export function add(questionData: object) { +/** + * 添加试题 + */ +export function addQuestion(questionData: object) { return axios({ - url: "/ex/question/add", + url: "/ex/question/addQuestion", method: 'post', data: questionData }); } -/* -* 修改题目 -*/ -export function update(questionData: object) { +/** + * 修改试题 + */ +export function editQuestion(questionData: object) { return axios({ - url: "/ex/question/update", + url: "/ex/question/editQuestion", method: 'post', data: questionData }); } -/* -* 行业 -*/ -export function findIndustry() { +/** + * 获取题目详情 + */ +export function getQuestionDetail(id: string | null) { return axios({ - url: '/ex/question/findIndustry', + url: `/ex/question/getQuestionDetail?id=${id}`, method: 'get' - }) + }); } -/* -* 题目详情 -*/ -export function getDetail(id: string|null) { +/** + * 获取监管行业 + */ +export function findIndustry() { return axios({ - url: '/ex/question/getDetail?id=' + id, + url: '/ex/question/findIndustry', method: 'get' }); } + diff --git a/packages/examination/src/views/examPaper/examPaperAdd.tsx b/packages/examination/src/views/examPaper/examPaperAdd.tsx index 43f033d..6118261 100644 --- a/packages/examination/src/views/examPaper/examPaperAdd.tsx +++ b/packages/examination/src/views/examPaper/examPaperAdd.tsx @@ -243,7 +243,12 @@ class ExamPaperAdd extends Component { serviceTypeId, questionContent }; - getList(num, page, listQuery) + const data = { + ...listQuery, + num, + page + }; + getList(data) .then((res: any) => { if (res.data) { this.setState({ total: res.data.total }); diff --git a/packages/examination/src/views/question/questionAdd.tsx b/packages/examination/src/views/question/questionAdd.tsx index c9c7ddc..c0947cf 100644 --- a/packages/examination/src/views/question/questionAdd.tsx +++ b/packages/examination/src/views/question/questionAdd.tsx @@ -1,29 +1,16 @@ import React, { Component } from'react'; import { Form, Input, Button, Radio, Checkbox, Select, message } from 'antd'; import { dictionary } from "api/dict/index"; -import { add, findIndustry } from 'api/question'; +import { addQuestion, findIndustry } from 'api/question'; import * as XLSX from 'xlsx'; import { saveAs } from 'file-saver'; -import { CheckboxValueType } from 'antd/lib/checkbox/Group'; const { Option} = Select; -interface QuestionState { - questionTypes: string; - industryId: string; - serviceTypeId: string; - questionContent: string; - optionA: string; - optionB: string; - optionC: string; - optionD: string; - answer: string; -} - interface States { industryDict: any; serviceTypeDict: any; - questions: QuestionState[]; + formCount: number; } class QuestionAdd extends Component { @@ -36,28 +23,16 @@ class QuestionAdd extends Component { this.state = { industryDict: [], serviceTypeDict: [], - questions: [ - { - questionTypes: '1', - industryId: '', - serviceTypeId: '', - questionContent: '', - optionA: '', - optionB: '', - optionC: '', - optionD: '', - answer: '' - } - ] + formCount: 1 }; } componentDidMount() { - this.findDict(); + this.handleFindDict(); } // 字典 - findDict() { + handleFindDict() { // 监管行业 findIndustry() .then((res: any) => { @@ -82,97 +57,48 @@ class QuestionAdd extends Component { // 题型切换 handleQuestionTypeChange = (index: number, value: string) => { - this.setState((prevState) => { - const questions = [...prevState.questions]; - questions[index].questionTypes = value; - questions[index].answer = ''; - return { - questions - }; - }); - }; - - // 单选框Change - handleRadioChange = (index: number, value: string) => { - this.setState((prevState) => { - const questions = [...prevState.questions]; - questions[index].answer = value; - return { - questions - }; - }); - }; - - // 多选框Change - handleCheckboxChange = (index: number, values: CheckboxValueType[]) => { - this.setState((prevState) => { - const questions = [...prevState.questions]; - const stringValues = values.map((val) => String(val)); - stringValues.sort(); - questions[index].answer = stringValues.join(','); - return { - questions - }; - }); + const formValues = {}; + formValues[`questionTypes_${index}`] = value; + formValues[`answer_${index}`] = ''; + this.formRef.current.setFieldsValue(formValues); }; // 新增试题 - addNewQuestion = () => { + handleAddNewQuestion = () => { this.setState((prevState) => ({ - questions: [ - ...prevState.questions, - { - questionTypes: '1', - industryId: '', - serviceTypeId: '', - questionContent: '', - optionA: '', - optionB: '', - optionC: '', - optionD: '', - answer: '' - } - ] + formCount: prevState.formCount + 1, })); }; // 删除试题 - deleteQuestion = (index: number) => { - this.setState((prevState) => { - const questions = [...prevState.questions]; - questions.splice(index, 1); - return { - questions - }; - }); + handleDeleteQuestion = (index: number) => { + this.setState((prevState) => ({ + formCount: prevState.formCount - 1, + })); }; // 处理表单提交 handleSubmit = () => { this.formRef.current.validateFields().then((values: any) => { - const questions = this.state.questions.map((question, index) => { - const industryId = values[`industryId_${index}`]; - const serviceTypeId = values[`serviceTypeId_${index}`]; - const questionContent = values[`questionContent_${index}`]; - const optionA = values[`optionA_${index}`] - const optionB = values[`optionB_${index}`]; - const optionC = values[`optionC_${index}`]; - const optionD = values[`optionD_${index}`]; - return { - ...question, - industryId, - serviceTypeId, - questionContent, - optionA, - optionB, - optionC, - optionD, + const questions = []; + for (let i = 0; i < this.state.formCount; i++) { + const question = { + questionTypes: values[`questionTypes_${i}`], + industryId: values[`industryId_${i}`], + serviceTypeId: values[`serviceTypeId_${i}`], + questionContent: values[`questionContent_${i}`], + optionA: values[`optionA_${i}`], + optionB: values[`optionB_${i}`], + optionC: values[`optionC_${i}`], + optionD: values[`optionD_${i}`], + answer: values[`answer_${i}`], }; - }); + questions.push(question); + } - add(questions).then((res) => { - const count = res.data; - if (count > 0) { + addQuestion(questions).then((res) => { + const success = res['success']; + if (success) { message.success(`新增成功`); this.props.history.push('/questionList'); } else { @@ -181,12 +107,11 @@ class QuestionAdd extends Component { }).catch(() => { message.error('新增试题时发生错误,请检查'); }); - }).catch(() => { }); }; // 下载模板 - downloadTemplate = () => { + handleDownloadTemplate = () => { const headers = ['题型', '监管行业', 'AQ服务类型', '题干', '选项A', '选项B', '选项C', '选项D', '答案']; const ws = XLSX.utils.aoa_to_sheet([headers]); const wb = XLSX.utils.book_new(); @@ -207,37 +132,28 @@ class QuestionAdd extends Component { const worksheet = workbook.Sheets[firstSheetName]; const jsonData = XLSX.utils.sheet_to_json(worksheet, { header: 1 }); const rows = jsonData.slice(1); - const newQuestions = rows.map((row: any) => { - const question: QuestionState = { - questionTypes: String(row[0]), - industryId: String(row[1]), - serviceTypeId: String(row[2]), - questionContent: String(row[3]), - optionA: String(row[4]), - optionB: String(row[5]), - optionC: String(row[6]), - optionD: String(row[7]), - answer: String(row[8]) - }; - return question; + const formValues = {}; + rows.forEach((row: any, index) => { + formValues[`questionTypes_${index}`] = String(row[0]); + formValues[`industryId_${index}`] = String(row[1]); + formValues[`serviceTypeId_${index}`] = String(row[2]); + formValues[`questionContent_${index}`] = String(row[3]); + formValues[`optionA_${index}`] = String(row[4]); + formValues[`optionB_${index}`] = String(row[5]); + formValues[`optionC_${index}`] = String(row[6]); + formValues[`optionD_${index}`] = String(row[7]); + formValues[`answer_${index}`] = String(row[8]); }); - this.setState({ questions: newQuestions }, () => { - const formValues = {}; - newQuestions.forEach((question, index) => { - formValues[`questionTypes_${index}`] = question.questionTypes; - formValues[`industryId_${index}`] = question.industryId; - formValues[`serviceTypeId_${index}`] = question.serviceTypeId; - formValues[`questionContent_${index}`] = question.questionContent; - formValues[`answer_${index}`] = question.answer; - formValues[`optionA_${index}`] = question.optionA; - formValues[`optionB_${index}`] = question.optionB; - formValues[`optionC_${index}`] = question.optionC; - formValues[`optionD_${index}`] = question.optionD; - }); + this.setState({ formCount: rows.length }, () => { this.formRef.current.setFieldsValue(formValues); }); }; reader.readAsArrayBuffer(file); + + const input = this.fileInputRef.current; + if (input) { + input.value = null; + } } }; @@ -247,15 +163,14 @@ class QuestionAdd extends Component { }; render() { - const { industryDict, serviceTypeDict, questions } = this.state; - // 使用 this.props.history 进行页面跳转 + const { industryDict, serviceTypeDict, formCount } = this.state; const handleListQuestion = () => { this.props.history.push('/questionList'); }; return (
- + { onChange={this.handleFileChange} />
-
- {questions.map((question, index) => ( + + {Array.from({ length: formCount }, (_, index) => (
@@ -332,7 +244,7 @@ class QuestionAdd extends Component { {index > 0 && ( - )} + )}
@@ -350,87 +262,64 @@ class QuestionAdd extends Component {
- {question.questionTypes === '1' ? ( - - this.handleRadioChange(index, e.target.value)} - > -
- + + {this.formRef.current && this.formRef.current.getFieldValue(`questionTypes_${index}`) === '1' ? ( + +
A - - - - - + + + B + + + +
+
+ C + + + + D + + + +
+
+ ) : ( + +
+ A + + + B - +
- - C - + C - - - - D + + D - +
- -
- - ) : ( - this.handleCheckboxChange(index, values)} - > -
- - A - - - - - - B - - - - -
-
- - C - - - - - - D - - - - -
-
- )} + + )} +
))}
-
diff --git a/packages/examination/src/views/question/questionEdit.tsx b/packages/examination/src/views/question/questionEdit.tsx index c28cd13..ad3248f 100644 --- a/packages/examination/src/views/question/questionEdit.tsx +++ b/packages/examination/src/views/question/questionEdit.tsx @@ -1,211 +1,119 @@ import React, { Component } from'react'; import { Form, Input, Button, Radio, Checkbox, Select, message } from 'antd'; import { dictionary } from "api/dict/index"; -import {findIndustry, getDetail, update} from 'api/question'; +import { findIndustry, getQuestionDetail, editQuestion } from 'api/question'; const { Option } = Select; -interface QuestionState { - id: string|null; - questionTypes: string; - industryId: string; - serviceTypeId: string; - questionContent: string; - answerOptions: {[ - key: string]: string - }; - answer: string; -} - interface States { industryDict: any; serviceTypeDict: any; - isLoading: boolean; - question: QuestionState; + questionTypes: string; } class QuestionEdit extends Component { formRef: any; + constructor(props: any) { super(props); this.formRef = React.createRef(); this.state = { - industryDict: undefined, - serviceTypeDict: undefined, - isLoading: false, - question: { - id: '', - questionTypes: '1', - industryId: '', - serviceTypeId: '', - questionContent: '', - answerOptions: { A: '', B: '', C: '', D: '' }, - answer: '' - }, + industryDict: [], + serviceTypeDict: [], + questionTypes: '' }; } componentDidMount() { - this.findIndustry() - this.findDict(); - this.getDetail(); - } - - // 监管行业 - findIndustry() { - findIndustry().then((res: any) => { - if (res.data) { - this.setState({ industryDict: res.data }); - } - }); + this.handleFindDict(); + this.handleGetQuestionDetail(); } - // AQ服务类型 - findDict() { - dictionary('serviceTypeDict').then((res) => { - if (res.data) { - this.setState({ serviceTypeDict: res.data }); - } - }); + // 字典 + handleFindDict() { + findIndustry() + .then((res: any) => { + if (res.data) { + this.setState({ industryDict: res.data }); + } + }); + dictionary('serviceTypeDict') + .then((res) => { + if (res.data) { + this.setState({ serviceTypeDict: res.data }); + } + }); } - // 题目详情 - getDetail = () => { + // 试题详情 + handleGetQuestionDetail = () => { const id = sessionStorage.getItem('id'); sessionStorage.removeItem('id'); - getDetail(id).then((res: any) => { + getQuestionDetail(id).then((res: any) => { if (res.data) { - let answerOptions = {}; + //this.setState({ questionTypes: String(res.data.questionTypes) }); + const formValues = {}; + formValues['id'] = res.data.id; + formValues['questionTypes'] = String(res.data.questionTypes); + formValues['industryId'] = String(res.data.industryId); + formValues['serviceTypeId'] = String(res.data.serviceTypeId); + formValues['questionContent'] = res.data.questionContent; + formValues['answer'] = res.data.answer; const options = res.data.options.split(','); options.forEach((option: any) => { const [key, value] = option.split(':'); - answerOptions[key] = value; - }); - const question: QuestionState = { - id:id, - questionTypes: String(res.data.questionTypes), - industryId: String(res.data.industryId), - serviceTypeId: String(res.data.serviceTypeId), - questionContent: String(res.data.questionContent), - answerOptions: answerOptions, - answer: res.data.answer - }; - this.setState({ question: question }, () => { - const formValues = {}; - formValues['questionTypes'] = question.questionTypes; - formValues['industryId'] = question.industryId; - formValues['serviceTypeId'] = question.serviceTypeId; - formValues['questionContent'] = question.questionContent; - formValues['answer'] = question.answer; - formValues['answerOption_A'] = question.answerOptions.A; - formValues['answerOption_B'] = question.answerOptions.B; - formValues['answerOption_C'] = question.answerOptions.C; - formValues['answerOption_D'] = question.answerOptions.D; - this.formRef.current.setFieldsValue(formValues); + formValues[`option${key}`] = value; }); + this.formRef.current.setFieldsValue(formValues); } }).catch(() => { - message.error('获取题目详情失败,请重试'); + message.error('获取题目详情失败'); }); }; - // 题型 + // 题型切换 handleQuestionTypeChange = (value: string) => { this.setState((prevState) => { - prevState.question.questionTypes = value; - prevState.question.answer = ''; - return { - question: prevState.question - }; - }); - }; - - // 单选框 - handleRadioChange = (value: string) => { - this.setState((prevState) => { - prevState.question.answer = value; - return { - question: prevState.question - }; - }); - }; - - // 多选框 - handleCheckboxChange = (type: string, values: (string | number | boolean)[] | null) => { - const currentAnswer = this.state.question.answer; - const currentSelectedArray = currentAnswer? currentAnswer.split(',') : []; - if (!values || values.length === 0) { - const newSelectedArray = currentSelectedArray.filter(val => val!== type); - newSelectedArray.sort(); - this.setState((prevState) => { - prevState.question.answer = newSelectedArray.join(','); - return { - question: prevState.question - }; - }); - } else { - const stringValues = values.map((val) => String(val)); - const newSelectedArray = Array.from(new Set([...currentSelectedArray, ...stringValues])); - newSelectedArray.sort(); - this.setState((prevState) => { - prevState.question.answer = newSelectedArray.join(','); - return { - question: prevState.question - }; - }); - } - }; - - // 答案 - handleAnswerOptionChange = (option: string, value: string) => { - this.setState((prevState) => { - prevState.question.answerOptions[option] = value; return { - question: prevState.question + questionTypes: value }; }); + const formValues = {}; + formValues['answer'] = ''; + this.formRef.current.setFieldsValue(formValues); }; // 保存修改 - update = () => { - this.setState({ isLoading: true }); - this.formRef.current.validateFields().then((values: Record) => { - const { question } = this.state; - const updatedQuestion = { - ...question, - industryId: values['industryId'], - serviceTypeId: values['serviceTypeId'], - questionContent: values['questionContent'] - }; - this.setState({ question: updatedQuestion }); - update(updatedQuestion).then((res) => { - const isSuccess = res.data; - if (isSuccess) { + handleEditQuestion = () => { + this.formRef.current.validateFields().then((values: any) => { + if (Array.isArray(values.answer)) { + values.answer.sort(); + values.answer = values.answer.join(','); + } + editQuestion(values).then((res) => { + const success = res['success']; + if (success) { message.success('修改成功'); - this.setState({ isLoading: false }); this.props.history.push('/questionList'); } else { message.error('修改失败,请稍后重试'); - this.setState({ isLoading: false }); } }).catch(() => { message.error('修改时发生错误,请检查'); - this.setState({ isLoading: false }); }); }); }; + render() { - const { industryDict, serviceTypeDict, question, isLoading } = this.state; - const handleListQuestion = () => { - this.props.history.push('/questionList'); - }; + const { industryDict, serviceTypeDict, questionTypes } = this.state; return (
-
+
this.handleQuestionTypeChange(e.target.value)}> + onChange={(e) => this.handleQuestionTypeChange(e.target.value)} + > 单选题 多选题 @@ -215,24 +123,27 @@ class QuestionEdit extends Component { name="industryId" rules={[{ required: true, message: '请选择监管行业' }]} > - + {industryDict && industryDict.length > 0 ? ( + (() => { + let rows = []; + for (let i = 0; i < industryDict.length; i++) { + const item = industryDict[i]; + rows.push( + + ); + } + return rows; + })() + ) : ( + + )} { name="serviceTypeId" rules={[{ required: true, message: '请选择AQ服务类型' }]} > - + {serviceTypeDict && serviceTypeDict.length > 0 ? ( + (() => { + let rows = []; + for (let i = 0; i < serviceTypeDict.length; i++) { + const item = serviceTypeDict[i]; + rows.push( + + ); + } + return rows; + })() + ) : ( + + )}
@@ -273,128 +188,123 @@ class QuestionEdit extends Component { style={{ width: 1100, height: 60 }} /> - -
-
- {question.questionTypes === '1'? ( - this.handleRadioChange(e.target.value)} - > +
+ + {questionTypes === '1' ? ( + +
A - this.handleAnswerOptionChange('A', e.target.value)} - /> - - ) : ( - this.handleCheckboxChange('A', values,)}> - A - this.handleAnswerOptionChange('A', e.target.value)} - /> - - )} - {question.questionTypes === '1' ? ( - this.handleRadioChange( e.target.value)} - style={{ marginLeft: 20 }} - > + + + B - this.handleAnswerOptionChange('B', e.target.value)} - /> - - ) : ( - this.handleCheckboxChange('B',values)}> - B - this.handleAnswerOptionChange( 'B', e.target.value)} - /> - - )} -
-
- {question.questionTypes === '1'? ( - this.handleRadioChange( e.target.value)} - > + + + +
+
C - this.handleAnswerOptionChange('C', e.target.value)} - /> - - ) : ( - this.handleCheckboxChange('C', values)}> - C - this.handleAnswerOptionChange( 'C', e.target.value)} - /> - - )} - {question.questionTypes === '1'? ( - this.handleRadioChange( e.target.value)} - style={{ marginLeft: 20 }} - > + + + D - this.handleAnswerOptionChange('D', e.target.value)} - /> - - ) : ( - this.handleCheckboxChange('D',values)}> + + + +
+
+ ) : ( + +
+ A + + + + B + + + +
+
+ C + + + D - this.handleAnswerOptionChange('D', e.target.value)} - /> - - )} -
-
+ + + +
+ + )} + +
+ +
- -
diff --git a/packages/examination/src/views/question/questionList.tsx b/packages/examination/src/views/question/questionList.tsx index 87a96d7..87d1da4 100644 --- a/packages/examination/src/views/question/questionList.tsx +++ b/packages/examination/src/views/question/questionList.tsx @@ -1,4 +1,4 @@ -import React, { Component } from'react'; +import React, { Component } from 'react'; import { Form, Input, Button, Table, Select, message, Modal } from 'antd'; import { delQuestion, findIndustry, getList } from 'api/question'; import { dictionary } from "api/dict/index"; @@ -9,11 +9,6 @@ interface States { num: number; page: number; total: number; - listQuery: { - industryId: string; - serviceTypeId: string; - questionContent: string; - }; list: any[]; loading: boolean; industryDict: any; @@ -31,11 +26,6 @@ class QuestionList extends Component { num: 10, page: 1, total: 0, - listQuery: { - industryId: '', - serviceTypeId: '', - questionContent: '' - }, list: [], loading: false, industryDict: [], @@ -46,7 +36,7 @@ class QuestionList extends Component { componentDidMount() { this.findDict(); - this.getList(); + this.handlegetList(); } // 字典 @@ -61,6 +51,7 @@ class QuestionList extends Component { .catch(() => { message.error('获取监管行业字典数据失败,请稍后重试'); }); + // AQ服务类型 dictionary('serviceTypeDict') .then((res) => { @@ -74,10 +65,18 @@ class QuestionList extends Component { } // 查询 - getList() { + handlegetList() { this.setState({ loading: true }); - const { num, page, listQuery } = this.state; - getList(num, page, listQuery) + const values = this.formRef.current.getFieldsValue(); + const { num, page } = this.state; + + const data = { + ...values, + num, + page + }; + + getList(data) .then((res) => { this.setState({ list: res.data.data, @@ -96,13 +95,6 @@ class QuestionList extends Component { // 重置 handleReset = () => { this.formRef.current.resetFields(); - this.setState({ - listQuery: { - industryId: '', - serviceTypeId: '', - questionContent: '', - }, - }); }; // 删除问题 @@ -117,7 +109,7 @@ class QuestionList extends Component { const success = res['success']; if (success) { message.success('删除成功'); - this.getList(); + this.handlegetList(); } else { message.error('删除失败,请稍后重试'); } @@ -139,6 +131,7 @@ class QuestionList extends Component { message.warning('请选择要删除的问题').then(); return; } + Modal.confirm({ title: '确认删除', content: '你确定要删除这些选中的问题吗?', @@ -148,7 +141,7 @@ class QuestionList extends Component { const success = res['success']; if (success) { message.success('删除成功'); - this.getList(); + this.handlegetList(); } else { message.error('删除失败,请稍后重试'); } @@ -170,16 +163,10 @@ class QuestionList extends Component { }; render() { - const onFinish = (values: object) => { - const listQuery = { ...this.state.listQuery, ...values }; - this.setState({ listQuery }); - this.getList(); - }; - - const changePage = (current: number, pageSize?: number) => { + const changePage = (current: number, pageSize: number) => { setTimeout(() => { - this.setState({ page: current, num: pageSize || 20 }); - this.getList(); + this.setState({ page: current, num: pageSize }); + this.handlegetList(); }, 0); }; @@ -192,6 +179,7 @@ class QuestionList extends Component { page, num } = this.state; + const columns: any = [ { title: '序号', @@ -273,7 +261,6 @@ class QuestionList extends Component { ref={this.formRef} className="filter" layout="inline" - onFinish={onFinish} > { } + { } + { style={{ width: 240 }} /> + + - + +
{ +
+ { ); } } + export default QuestionList; \ No newline at end of file