import React, { Component } from 'react'; import { Form, Input, Button, Radio, Checkbox, Select, message} from 'antd'; import { getDetail} from 'api/examPaper'; import { findIndustry} from 'api/question'; const { Option } = Select; interface optionsState { value: string; label: string; } // 定义单个试题的状态接口 interface QuestionState { id: string; serviceTypeId: string; questionTypes: string; questionContent: string; answerOptions: optionsState[]; answer: string | null; } // 定义组件的状态接口 interface States { id: string | undefined; industryDict: any; paperName : string | undefined; industryId : number | undefined; questionCount : number | undefined; totalScore : number | undefined; examDuration : string | undefined; durationType : number | undefined; paperContent : string | undefined; questions: QuestionState[]; } class ExamPaperView extends Component { formRef: any; constructor(props: any) { super(props); this.formRef = React.createRef(); this.state = { id: undefined, industryDict: undefined, paperName: undefined, industryId: undefined, questionCount: undefined, totalScore: undefined, examDuration: undefined, durationType: undefined, paperContent: undefined, questions: [], }; } // 初期 componentDidMount() { const id = sessionStorage.getItem('id'); sessionStorage.removeItem('id'); this.findIndustry(); this.getDetail(id); } // 监管行业 findIndustry() { findIndustry().then((res: any) => { if (res.data) { this.setState({ industryDict: res.data }); } }); } // 试卷详情 getDetail = (id: string | null) => { getDetail(id).then((res: any) => { if (res.data) { const newQuestions: QuestionState[] = res.data.data.map((questionData: any) => { const options = questionData.options.split(','); const answerOptions = options.map((option: any) => { const [value, label] = option.split(':'); return { value, label }; }); return { id: String(questionData.id), serviceTypeId: String(questionData.serviceTypeId), questionTypes: String(questionData.questionTypes), questionContent: String(questionData.questionContent), answerOptions: answerOptions, answer: String(questionData.answer) }; }); const paperName = res.data.head.paperName; const industryId = res.data.head.industryId; const questionCount = res.data.head.questionCount; const totalScore = res.data.head.totalScore; const examDuration = res.data.head.examDuration; const durationType = res.data.head.durationType; const paperContent = res.data.head.paperContent; this.setState({ questions: newQuestions, paperName, industryId, questionCount, totalScore, examDuration, durationType, paperContent, }); } }).catch(() => { message.error('获取题目详情失败,请重试'); }); }; findDurationTypeName = (durationType:number | undefined) => { switch (durationType) { case 1: return '分(min)'; case 2: return '时(h)'; default: return '未知'; } }; findIndustryName = (industryId:number | undefined) => { const { industryDict } = this.state; if (industryDict) { const industry = industryDict.find((item:any) => item.industryId === String(industryId)); return industry ? industry.industryName : '未知行业'; } return '未知行业'; }; render() { const { questions, paperName, industryId, questionCount, totalScore, examDuration, durationType, paperContent, } = this.state; // 页面跳转 const handleListQuestion = () => { this.props.history.push('/examPaperList'); }; return (

基本信息

{paperName}
{this.findIndustryName(industryId)} {questionCount} {totalScore} {examDuration} {this.findDurationTypeName(durationType)}
{paperContent}

试题详情

{questions.map((question, index) => (
{index + 1}. {question.questionContent} {question.questionTypes === '1'? ( {question.answerOptions.map((option) => ( {option.value}. {option.label} ))} ) : ( {question.answerOptions.map((option) => ( {option.value}. {option.label} ))} )}
))}
); } } export default ExamPaperView;