import React from 'react'; import { Tabs, Form, Tag, InputNumber, Radio, Row, Col, Checkbox, Icon, Input, Button, List, Cascader, Switch } from 'antd'; import './index.less'; import DragList from '@src/components/DragList'; import Editor from '@src/components/Editor'; import Page from '@src/containers/Page'; import Block from '@src/components/Block'; import Select from '@src/components/Select'; import { getMap, formatFormError, formatTreeData, generateSearch } from '@src/services/Tools'; import { asyncSMessage, asyncGet } from '@src/services/AsyncTools'; import { QuestionType, QuestionDifficult, QuestionStyleType, QuestionRadioDirection } from '../../../../Constant'; import QuestionNoList from '../../../components/QuestionNoList'; import { System } from '../../../stores/system'; import { Question } from '../../../stores/question'; import { Examination } from '../../../stores/examination'; import { Exercise } from '../../../stores/exercise'; const QuestionStyleTypeMap = getMap(QuestionStyleType, 'value', 'label'); export default class extends Page { constructor(props) { super(props); this.placeList = []; this.placeSetting = null; this.uuid = []; this.examinationStructMap = {}; this.exerciseStructMap = {}; } init() { Promise.all([ Exercise.allStruct().then(result => { result = result.filter(row => row.isExamination); this.exerciseStructMap = getMap(result, 'id'); return { value: 'exercise', key: 'exercise', label: '练习', title: '练习', children: formatTreeData(result.map(row => { row.title = `${row.titleZh}/${row.titleEn}`; return row; }), 'id', 'title', 'parentId') }; }), Examination.allStruct().then(result => { this.examinationStructMap = getMap(result, 'id'); return { value: 'examination', key: 'examination', label: '模考', title: '模考', children: formatTreeData(result.map(row => { row.title = `${row.titleZh}/${row.titleEn}`; return row; }), 'id', 'title', 'parentId') }; }), ]).then(result => { this.setState({ moduleStructData: result }); }); } initData() { const { id } = this.params; const { form } = this.props; let handler; if (id) { handler = Question.get({ id }).then(result => { result.content = result.content || { questions: [], steps: [] }; result.keyword = result.keyword || []; return result; }); } else { handler = Promise.resolve({ content: { number: 1, type: 'single', typeset: 'one', questions: [], steps: [] } }); } handler.then(result => { this.uuid[0] = -1; let type = result.content.type || 'single'; if (type === 'inline') type = 'single'; result.content.questions.forEach((row, index) => { const keys = []; this.uuid[index] = 0; if (row.select && row.select.length > 0) { for (; this.uuid[index] < row.select.length; this.uuid[index] += 1) { keys.push(this.uuid[index]); form.getFieldDecorator(`content.questions[${index}].select[${this.uuid}]`); form.getFieldDecorator(`answer.questions[${index}].${type}[${this.uuid}]`); } } form.getFieldDecorator(`content.questions[${index}].keys`); row.keys = keys; return row; }); (result.content.steps || []).forEach((row, index) => { form.getFieldDecorator(`content.steps[${index}].title`); form.getFieldDecorator(`content.steps[${index}].stem`); }); const { row, col } = result.content.table; for (let i = 0; i < col; i += 1) { console.log(`content.table.header[${i}]`); form.getFieldDecorator(`content.table.header[${i}]`); for (let j = 0; j < row; j += 1) { console.log(`content.table.data[${j}][${i}]`); form.getFieldDecorator(`content.table.data[${j}][${i}]`); } } form.getFieldDecorator('content.step'); form.getFieldDecorator('content.number'); form.getFieldDecorator('content.table.row'); form.getFieldDecorator('content.table.col'); form.getFieldDecorator('stem'); form.getFieldDecorator('difficult'); form.getFieldDecorator('questionType'); form.getFieldDecorator('place'); form.getFieldDecorator('id'); form.getFieldDecorator('questionNoIds'); form.getFieldDecorator('relationQuestion'); form.setFieldsValue(result); return result; }) .then((result) => { this.setState({ associationContentQuestionNos: [], realtionQuestionNos: [] }); if (result.questionNoIds && result.questionNoIds.length > 0) { Question.listNo({ ids: result.questionNoIds }).then(list => { this.setState({ questionNos: list }); return list; }).then(rr => { // 阅读关联题目: 只有一个id if (rr.length === 1 && rr[0].relationQuestion && rr[0].relationQuestion.length > 0) { form.getFieldDecorator('rcType'); form.setFieldsValue({ rcType: true }); this.bindRelationQuestion(rr[0].relationQuestion); } else { this.bindRelationQuestion(); } return null; }); } else { this.bindRelationQuestion(); } this.bindAssociationContent(result.associationContent); return null; }); } refreshPlace(type) { let handler = null; if (this.placeSetting) { handler = Promise.resolve(this.placeSetting); } else { handler = System.getPlace(); } handler.then(result => { this.placeSetting = result; this.placeList = result[type] || []; this.setState({ placeList: this.placeList }); }); } addNo() { const { form } = this.props; form.validateFields(['moduleStruct', 'questionNo'], (err) => { if (!err) { const data = form.getFieldsValue(['id', 'moduleStruct', 'questionNo']); data.moduleStruct = data.moduleStruct.map(row => row); data.module = data.moduleStruct.shift(); const node = data.moduleStruct[data.moduleStruct.length - 1]; let nodeString; switch (data.module) { case 'exercise': nodeString = this.exerciseStructMap[node].titleEn; break; case 'examination': nodeString = this.examinationStructMap[node].titleEn; break; default: } data.questionId = data.id || 0; delete data.id; data.no = data.questionNo; data.title = `${nodeString}-${data.no}`; delete data.questionNo; Question.addNo(data).then((result) => { const { questionNos = [] } = this.state; questionNos.push(result); this.setState({ questionNos }); form.setFieldsValue({ moduleStruct: [], questionNo: '', questionNoIds: questionNos.map(row => row.id) }); asyncSMessage('保存成功'); }).catch((e) => { if (e.result) form.setFields(formatFormError(data, e.result)); }); } }); } removeNo(noId) { let { questionNos } = this.state; questionNos = questionNos.filter(row => row.id !== noId); Question.delNo({ id: noId }).then(() => { this.setState({ questionNos }); }); } changeType(type) { const { getFieldValue, setFieldsValue, getFieldDecorator } = this.props.form; const number = getFieldValue('content.number'); // const keys = []; this.uuid = []; for (let index = 0; index < Number(number); index += 1) { this.uuid[index] = 0; switch (type) { case 'double': getFieldDecorator(`content.questions[${index}].direction`); setFieldsValue({ [`content.questions[${index}].direction`]: 'landscape' }); break; default: } } } removeQuestion(index, k) { const { form } = this.props; const keys = form.getFieldValue(`content.questions[${index}].keys`); if (keys.length === 1) { return; } form.setFieldsValue({ [`content.questions[${index}].keys`]: keys.filter(key => key !== k), }); } addQuestion(index) { const { form } = this.props; const keys = form.getFieldValue(`content.questions[${index}].keys`) || []; if (!this.uuid[index]) { this.uuid[index] = 1; } else { this.uuid[index] += 1; } const nextKeys = keys.concat(this.uuid[index]); form.setFieldsValue({ [`content.questions[${index}].keys`]: nextKeys, }); } orderQuestion(index, oldIndex, newIndex) { const { form } = this.props; const keys = form.getFieldValue(`content.questions[${index}].keys`) || []; const tmp = keys[oldIndex]; keys[oldIndex] = keys[newIndex]; keys[newIndex] = tmp; form.setFieldsValue({ [`content.questions[${index}].keys`]: keys, }); } changeQuestion(index, k, value) { const { form } = this.props; let answer = form.getFieldValue(`answer.questions[${index}].single`) || []; answer = answer.map(() => !value); answer[k] = !!value; form.setFieldsValue({ [`answer.questions[${index}].single`]: answer }); } changeDouble(index, k, o, value) { const { form } = this.props; const direction = form.getFieldValue(`content.questions[${index}].direction`); let answer = form.getFieldValue(`answer.questions[${index}].double`) || []; switch (direction) { case 'landscape': answer[k] = answer[k].map(() => !value); if (o >= 0) { answer[k][o] = !!value; } break; case 'portrait': answer = answer.map((row) => { row[o] = !value; return row; }); if (o >= 0) { answer[k][o] = !!value; } break; default: } form.setFieldsValue({ [`answer.questions[${index}].double`]: answer }); } submit() { const { form } = this.props; const fields = ['id', 'questionType', 'place', 'difficult', 'content', 'answer', 'stem', 'keyword', 'questionNoIds', 'officialContent', 'qxContent', 'associationContent']; form.validateFields(fields, (err) => { if (!err) { const data = form.getFieldsValue(fields); let handler; data.associationContent = this.state.associationContentQuestionNos.map(row => row.id); data.stem = data.stem || ''; data.description = (data.stem || '').replace(/<[^>]+>/g, ''); data.qxContent = data.qxContent || ''; data.officialContent = data.officialContent || ''; data.content = data.content || {}; data.content.questions = (data.content.questions || []).map(row => { delete row.keys; row.select = (row.select || []).filter(r => r); return row; }); data.answer = data.answer || {}; data.answer.questions = (data.answer.questions || []).map(row => { if (row.single) row.single = row.single.filter(r => r); if (row.double) row.double = row.double.filter(r => r); return row; }); data.relationQuestion = this.state.realtionQuestionNos.map(row => row.id); if (!data.id) { handler = Question.add(data); } else { handler = Question.edit(data); } handler.then((result) => { asyncSMessage('保存成功'); if (data.id) { linkTo(`/subject/question/${data.id}`); } else { linkTo(`/subject/question/${result.id}`); } }).catch((e) => { if (e.result) form.setFields(formatFormError(data, e.result)); }); } }); } searchStem() { const { form } = this.props; const content = form.getFieldValue('stem').replace(/<[^>]+>/g, ''); Question.searchStem({ content }) .then(result => { if (result.list.length > 0) { asyncGet(() => import('../../../components/SimilarQuestionNo'), { title: '找到可匹配题目', questionNos: result.list, modal: true }, (questionNo) => { this.inited = false; linkTo(`/subject/question/${questionNo.questionId}`); return Promise.resolve(); }); } else { asyncSMessage('无可匹配题目'); } }); } bindAssociationContent(associationContent) { generateSearch('associationContent', { mode: 'multiple' }, this, (search) => { return this.searchQuestion(search); }, (row) => { return { title: row.title, value: row.id, }; }, associationContent, null); if (associationContent) this.listQuestion(associationContent, 'ids', 'associationContentQuestionNos'); } bindRelationQuestion(relationQuestion) { generateSearch('relationQuestion', { mode: 'multiple' }, this, (search) => { return this.searchQuestion(search); }, (row) => { return { title: row.title, value: row.id, }; }, relationQuestion, null); if (relationQuestion) this.listQuestion(relationQuestion, 'ids', 'realtionQuestionNos'); } listQuestion(values, field, targetField) { if (!values || values.length === 0) { this.setState({ [targetField]: [] }); return; } this.setState({ [`${targetField}Loading`]: true }); Question.listNo({ [field]: values }).then(result => { const map = getMap(result, 'id'); const questionNos = values.map(row => map[row]).filter(row => row); this.setState({ [targetField]: questionNos, [`${targetField}Loading`]: false }); }); } searchQuestion(params) { return Question.searchNo(params); } renderBase() { const { getFieldDecorator } = this.props.form; return

题干信息

{getFieldDecorator('id')()} {getFieldDecorator('stem', { })( { const range = quill.getSelection(true); quill.insertText(range.index, '#table#'); }, select: (quill) => { const range = quill.getSelection(true); quill.insertText(range.index, '#select#'); }, }, }, }} placeholder='请输入内容' onUpload={(file) => System.uploadImage(file)} />, )} {getFieldDecorator('content.step', { normalize: (value, preValue) => { if (value === undefined || value === '' || value === null) return preValue; if (Math.abs(value - preValue) > 1) return preValue; return value; }, })( , )} 行 {getFieldDecorator('content.table.row')( , )} 列 {getFieldDecorator('content.table.col')( , )} {this.renderTable()}
; } renderTable() { const { getFieldDecorator, getFieldValue } = this.props.form; const row = Number(getFieldValue('content.table.row')); const col = Number(getFieldValue('content.table.col')); const table = []; if (row === 0 || col === 0) return table; const span = 100 / col; const colums = []; for (let i = 0; i < col; i += 1) { colums.push(
{getFieldDecorator(`content.table.header[${i}]`)()}
); } table.push({colums}); for (let index = 0; index < row; index += 1) { const cols = []; for (let i = 0; i < col; i += 1) { cols.push(
{getFieldDecorator(`content.table.data[${index}][${i}]`)()}
); } table.push({cols}); } return table; } renderStep() { const { getFieldDecorator, getFieldValue } = this.props.form; const number = getFieldValue('content.step'); const result = []; for (let index = 0; index < Number(number); index += 1) { result.push(

递进层次 {index + 1}

{getFieldDecorator(`content.steps[${index}].title`, { rules: [ { required: true, message: '请输入标题' }, ], })( , )} {getFieldDecorator(`content.steps[${index}].stem`, { })( , )}
); } return result; } renderIdentity() { const { questionNos = [] } = this.state; const { getFieldDecorator } = this.props.form; return

题目身份

{getFieldDecorator('questionNoIds', { rules: [{ required: true, message: '添加关联题目ID', }], })()} {questionNos.map((no, index) => { return { this.removeNo(no.id); }}> {no.title} ; })} {getFieldDecorator('moduleStruct', { rules: [{ required: true, message: '选择题目编号关系', }], })( { this.setState({ moduleStruct: value }); }} placeholder='选择' options={this.state.moduleStructData} />)} {getFieldDecorator('questionNo', { rules: [{ required: true, message: '输入编号', }], })( { this.setState({ questionNo: value }); }} />)} {getFieldDecorator('keyword')( { this.refreshPlace(v); }} />, )} {getFieldDecorator('place', { rules: [ { required: true, message: '请选择考点' }, ], })( , )} {/* 阅读题,并且只有一个id才能关联 */} {isRc && {getFieldDecorator('rcType', { valuePropName: 'checked', })( , )} } {rcType && {getFieldDecorator('relationQuestion', { rules: [{ required: true, message: '请输入关联题目', }], })( { this.changeType(type); }} />, )} {getFieldDecorator('content.number', { normalize: (value, preValue) => { if (value === undefined || value === '' || value === null) return preValue; if (Math.abs(value - preValue) > 1) return preValue; return value; }, })( , )} {getFieldDecorator('content.typeset')( 单排 双排 , )}
; } renderSelect() { const { getFieldDecorator, getFieldValue } = this.props.form; const number = getFieldValue('content.number'); const type = getFieldValue('content.type'); const result = []; let handler = null; switch (type) { case 'single': handler = (index) => this.renderSelectSingle(index); break; case 'double': handler = (index) => this.renderSelectDouble(index); break; case 'inline': handler = (index) => this.renderSelectInline(index); break; default: } for (let index = 0; index < Number(number); index += 1) { result.push(

选项信息({QuestionStyleTypeMap[type]})

{type !== 'inline' && ( {getFieldDecorator(`content.questions[${index}].description`, { })( , )} )} {handler(index)}
); } return result; } renderSelectSingle(index) { const { getFieldDecorator, getFieldValue } = this.props.form; getFieldDecorator(`content.questions[${index}].keys`); const keys = getFieldValue(`content.questions[${index}].keys`) || []; return [ { this.orderQuestion(index, oldIndex, newIndex); }} renderItem={(k) => ( ]}> {getFieldDecorator(`answer.questions[${index}].single[${k}]`, { valuePropName: 'checked', })( { this.changeQuestion(index, k, e.target.checked); }} />, )} )} />, , ]; } renderSelectDouble(index) { const { getFieldDecorator, getFieldValue } = this.props.form; getFieldDecorator(`content.questions[${index}].keys`); const keys = getFieldValue(`content.questions[${index}].keys`) || []; return [ {getFieldDecorator(`content.questions[${index}].direction`)( , )} {getFieldDecorator(`content.questions[${index}].second`)( , )} , { this.orderQuestion(index, oldIndex, newIndex); }} renderItem={(k) => ( ]}> {getFieldDecorator(`answer.questions[${index}].double[${k}][0]`, { valuePropName: 'checked', })( { this.changeDouble(index, k, 0, e.target.checkedue); }} />, )} {getFieldDecorator(`answer.questions[${index}].double[${k}][1]`, { valuePropName: 'checked', })( { this.changeDouble(index, k, 1, e.target.checked); }} />, )} )} />, , ]; } renderSelectInline(index) { return this.renderSelectSingle(index); } renderOffical() { const { getFieldDecorator } = this.props.form; return
{getFieldDecorator('officalContent', { })( , )}
; } renderQX() { const { getFieldDecorator } = this.props.form; return
{getFieldDecorator('qxContent', { })( , )}
; } renderAssociation() { const { getFieldDecorator, setFieldsValue } = this.props.form; return

题源联想

{getFieldDecorator('associationContent')(