page.js 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405
  1. import React from 'react';
  2. import { Form, Input, Tabs, Row, Col, Button, List, Icon, Checkbox } from 'antd';
  3. import './index.less';
  4. import DragList from '@src/components/DragList';
  5. import Editor from '@src/components/Editor';
  6. import Page from '@src/containers/Page';
  7. import Block from '@src/components/Block';
  8. import Select from '@src/components/Select';
  9. // import FileUpload from '@src/components/FileUpload';
  10. import { formatFormError, generateSearch, getMap } from '@src/services/Tools';
  11. import { asyncSMessage } from '@src/services/AsyncTools';
  12. import { TextbookType, QuestionStyleType } from '../../../../Constant';
  13. import { Textbook } from '../../../stores/textbook';
  14. import { System } from '../../../stores/system';
  15. import config from './index';
  16. const QuestionStyleTypeMap = getMap(QuestionStyleType, 'value', 'label');
  17. export default class extends Page {
  18. constructor(props) {
  19. super(props);
  20. this.placeList = [];
  21. this.placeSetting = null;
  22. this.uuid = [];
  23. const { id } = this.params;
  24. if (id) {
  25. config.title = '编辑机经题目';
  26. } else {
  27. config.title = '添加机经题目';
  28. }
  29. }
  30. init() {
  31. }
  32. initData() {
  33. const { id } = this.params;
  34. const { form } = this.props;
  35. let handler;
  36. if (id) {
  37. handler = Textbook.getQuestion({ id });
  38. } else {
  39. let { libraryId } = this.state.search;
  40. libraryId = Number(libraryId || 0);
  41. handler = Textbook.getNextQuestion({ libraryId }).then(result => {
  42. return { libraryId: libraryId || '', no: result, question: { content: { number: 1, type: 'single', typeset: 'one', questions: [], steps: [] } } };
  43. });
  44. }
  45. handler
  46. .then(result => {
  47. const { questionNoIds } = result;
  48. result.time = [result.startTime, result.endTime];
  49. this.uuid[0] = -1;
  50. result.question.content.questions.forEach((row, index) => {
  51. const keys = [];
  52. this.uuid[index] = 0;
  53. if (row.select && row.select.length > 0) {
  54. for (; this.uuid[index] < row.select.length; this.uuid[index] += 1) {
  55. keys.push(this.uuid[index]);
  56. form.getFieldDecorator(`question.content.questions[${index}].select[${this.uuid}]`);
  57. form.getFieldDecorator(`question.content.questions[${index}].answer[${this.uuid}]`);
  58. }
  59. const answerMap = {};
  60. row.answer.forEach(r => {
  61. answerMap[r] = true;
  62. });
  63. row.answer = row.select.map(r => answerMap[r] || false);
  64. } else if (row.answer && row.answer.length > 0) {
  65. for (; this.uuid[index] < row.answer.length; this.uuid[index] += 1) {
  66. keys.push(this.uuid);
  67. form.getFieldDecorator(`question.content.questions[${index}].answer[${this.uuid}]`);
  68. }
  69. }
  70. form.getFieldDecorator(`question.content.questions[${index}].keys`);
  71. row.keys = keys;
  72. return row;
  73. });
  74. result.question.content.steps.forEach((row, index) => {
  75. form.getFieldDecorator(`question.content.steps[${index}].title`);
  76. form.getFieldDecorator(`question.content.steps[${index}].stem`);
  77. });
  78. form.getFieldDecorator('question.content.step');
  79. form.getFieldDecorator('question.content.number');
  80. form.getFieldDecorator('question.content.type');
  81. form.getFieldDecorator('question.content.typeset');
  82. form.getFieldDecorator('libraryId');
  83. form.getFieldDecorator('no');
  84. form.getFieldDecorator('question.questionType');
  85. form.getFieldDecorator('question.place');
  86. form.getFieldDecorator('question.id');
  87. form.getFieldDecorator('id');
  88. form.setFieldsValue(result);
  89. generateSearch('libraryId', {}, this, (search) => {
  90. return Textbook.listLibrary(search);
  91. }, (row) => {
  92. return {
  93. title: `${row.startDate} - ${row.endDate || '至今'}`,
  94. value: row.id,
  95. };
  96. }, result.libraryId, null);
  97. this.setState({ questionNoIds });
  98. });
  99. }
  100. refreshPlace(type) {
  101. let handler = null;
  102. if (this.placeSetting) {
  103. handler = Promise.resolve(this.placeSetting);
  104. } else {
  105. handler = System.getPlace();
  106. }
  107. return handler.then(result => {
  108. this.placeSetting = result;
  109. this.placeList = result[type] || [];
  110. });
  111. }
  112. refreshNo(libraryId) {
  113. return Textbook.getNextQuestion({ libraryId }).then(result => {
  114. this.props.form.setFieldsValue({ no: result });
  115. });
  116. }
  117. removeQuestion(index, k) {
  118. const { form } = this.props;
  119. const keys = form.getFieldValue(`question.content.questions[${index}].keys`);
  120. if (keys.length === 1) {
  121. return;
  122. }
  123. form.setFieldsValue({
  124. [`question.content.questions[${index}].keys`]: keys.filter(key => key !== k),
  125. });
  126. }
  127. addQuestion(index) {
  128. const { form } = this.props;
  129. const keys = form.getFieldValue(`question.content.questions[${index}].keys`) || [];
  130. this.uuid[index] += 1;
  131. const nextKeys = keys.concat(this.uuid[index]);
  132. form.setFieldsValue({
  133. [`question.content.questions[${index}].keys`]: nextKeys,
  134. });
  135. }
  136. orderQuestion(index, oldIndex, newIndex) {
  137. const { form } = this.props;
  138. const keys = form.getFieldValue(`question.content.questions[${index}].keys`) || [];
  139. const tmp = keys[oldIndex];
  140. keys[oldIndex] = keys[newIndex];
  141. keys[newIndex] = tmp;
  142. form.setFieldsValue({
  143. [`question.content.questions[${index}].keys`]: keys,
  144. });
  145. }
  146. changeQuestion(index, k, value) {
  147. const { form } = this.props;
  148. let answer = form.getFieldValue(`question.content.answer[${index}].single`) || [];
  149. answer = answer.map(() => !value);
  150. answer[k] = !!value;
  151. form.setFieldsValue({ [`question.content.answer[${index}].single`]: answer });
  152. }
  153. submit() {
  154. const { form } = this.props;
  155. form.validateFields((err) => {
  156. if (!err) {
  157. const data = form.getFieldsValue();
  158. [data.startTime, data.endTime] = data.time;
  159. let handler;
  160. if (data.id) {
  161. handler = Textbook.editQuestion(data);
  162. } else {
  163. handler = Textbook.addQuestion(data);
  164. }
  165. handler.then(() => {
  166. asyncSMessage('保存成功');
  167. }).catch((e) => {
  168. if (e.result) form.setFields(formatFormError(data, e.result));
  169. });
  170. }
  171. });
  172. }
  173. renderAttr() {
  174. const { id } = this.params;
  175. const { getFieldDecorator } = this.props.form;
  176. return <Block flex>
  177. <h1>基本信息</h1>
  178. <Form>
  179. {getFieldDecorator('id')(<input hidden />)}
  180. <Form.Item labelCol={{ span: 5 }} wrapperCol={{ span: 16 }} label='题型' help='设定后,不允许修改'>
  181. {getFieldDecorator('question.questionType', {
  182. rules: [
  183. { required: true, message: '请选择题型' },
  184. ],
  185. })(
  186. <Select disabled={id} select={TextbookType} placeholder='请选择题型' onChange={(v) => {
  187. this.refreshPlace(v);
  188. }} />,
  189. )}
  190. </Form.Item>
  191. <Form.Item labelCol={{ span: 5 }} wrapperCol={{ span: 16 }} label='换库表' help='设定后,不允许修改'>
  192. {getFieldDecorator('libraryId', {
  193. rules: [
  194. { required: true, message: '请选择换库表' },
  195. ],
  196. })(
  197. <Select disabled={id} {...this.state.libraryId} placeholder='请选择换库表' onChange={(v) => {
  198. this.refreshNo(v);
  199. }} />,
  200. )}
  201. </Form.Item>
  202. <Form.Item labelCol={{ span: 5 }} wrapperCol={{ span: 16 }} label='题目序号'>
  203. {getFieldDecorator('no')(
  204. <Input disabled />,
  205. )}
  206. </Form.Item>
  207. <Form.Item labelCol={{ span: 5 }} wrapperCol={{ span: 16 }} label='考点'>
  208. {getFieldDecorator('question.place', {
  209. rules: [
  210. { required: true, message: '请选择考点' },
  211. ],
  212. })(
  213. <Select select={this.placeList} placeholder='请选择考点' onChange={(v) => {
  214. this.refreshPart(v);
  215. }} />,
  216. )}
  217. </Form.Item>
  218. </Form>
  219. </Block>;
  220. }
  221. renderBase() {
  222. const { getFieldDecorator } = this.props.form;
  223. return <Block flex>
  224. <h1>题干信息</h1>
  225. <Form>
  226. {getFieldDecorator('question.id')(<input hidden />)}
  227. {getFieldDecorator('question.stem', {
  228. })(
  229. <Editor modules={{
  230. toolbar: {
  231. container: [
  232. ['image'],
  233. [{ header: '1' }, { header: '2' }],
  234. ['bold', 'underline', 'blockquote'],
  235. [{ list: 'ordered' }, { list: 'bullet' }, { indent: '-1' }, { indent: '+1' }],
  236. ],
  237. handlers: {
  238. },
  239. },
  240. }} placeholder='请输入内容' onUpload={(file) => System.uploadImage(file)} />,
  241. )}
  242. {getFieldDecorator('question.content.type')(<input hidden />)}
  243. {getFieldDecorator('question.content.number')(<input hidden />)}
  244. {getFieldDecorator('question.content.typeset')(<input hidden />)}
  245. </Form>
  246. </Block>;
  247. }
  248. renderSelect() {
  249. const { getFieldDecorator, getFieldValue } = this.props.form;
  250. const number = getFieldValue('question.content.number');
  251. const type = getFieldValue('question.content.type');
  252. const result = [];
  253. let handler = null;
  254. switch (type) {
  255. case 'single':
  256. handler = (index) => this.renderSelectSingle(index);
  257. break;
  258. default:
  259. }
  260. for (let index = 0; index < Number(number); index += 1) {
  261. result.push(<Block flex className={type}>
  262. <h1>选项信息({QuestionStyleTypeMap[type]})</h1>
  263. <Form>
  264. {type !== 'inline' && (
  265. <Form.Item>
  266. {getFieldDecorator(`question.content.questions[${index}].description`, {
  267. })(
  268. <Editor placeholder='选项问题说明' />,
  269. )}
  270. </Form.Item>
  271. )}
  272. {handler(index)}
  273. </Form>
  274. </Block>);
  275. }
  276. return result;
  277. }
  278. renderSelectSingle(index) {
  279. const { getFieldDecorator, getFieldValue } = this.props.form;
  280. getFieldDecorator(`question.content.questions[${index}].keys`);
  281. const keys = getFieldValue(`question.content.questions[${index}].keys`) || [];
  282. return [
  283. <DragList
  284. loading={false}
  285. dataSource={keys || []}
  286. handle={'.icon'}
  287. onMove={(oldIndex, newIndex) => {
  288. this.orderQuestion(index, oldIndex, newIndex);
  289. }}
  290. renderItem={(k) => (
  291. <List.Item actions={[<Icon type='bars' className='icon' />]}>
  292. <Row key={k} style={{ width: '100%' }}>
  293. <Col span={1}>
  294. {getFieldDecorator(`question.answer.questions[${index}].single[${k}]`, {
  295. valuePropName: 'checked',
  296. })(
  297. <Checkbox onChange={(value) => {
  298. this.changeQuestion(index, k, value);
  299. }} />,
  300. )}
  301. </Col>
  302. <Col span={23}>
  303. <Form.Item
  304. key={k}
  305. hidden
  306. >
  307. {getFieldDecorator(`question.content.questions[${index}].select[${k}]`, {
  308. rules: [{
  309. required: true,
  310. whitespace: true,
  311. message: '请填写选项信息',
  312. }],
  313. })(
  314. <Input />,
  315. )}
  316. {keys.length > 1 ? (
  317. <Icon
  318. type='minus-circle-o'
  319. disabled={keys.length === 1}
  320. onClick={() => this.removeQuestion(index, k)}
  321. />
  322. ) : null}
  323. </Form.Item>
  324. </Col>
  325. </Row>
  326. </List.Item>
  327. )}
  328. />,
  329. <Form.Item>
  330. <Button type='dashed' onClick={() => this.addQuestion(index)} >
  331. <Icon type='plus' /> 新增
  332. </Button>
  333. </Form.Item>,
  334. ];
  335. }
  336. renderQX() {
  337. const { getFieldDecorator } = this.props.form;
  338. return <Block flex>
  339. <Form>
  340. <Form.Item label='千行解析'>
  341. {getFieldDecorator('qxContent', {
  342. })(
  343. <Editor placeholder='输入内容' />,
  344. )}
  345. </Form.Item>
  346. </Form>
  347. </Block>;
  348. }
  349. renderTab() {
  350. return <Tabs activeKey='textbook' onChange={(tab) => {
  351. switch (tab) {
  352. case 'sentence':
  353. linkTo('/subject/sentence/question');
  354. break;
  355. case 'base':
  356. linkTo('/subject/question');
  357. break;
  358. default:
  359. }
  360. }}>
  361. <Tabs.TabPane key='base' tab='考试题型' />
  362. <Tabs.TabPane key='sentence' tab='长难句' />
  363. <Tabs.TabPane key='textbook' tab='数学机经' />
  364. </Tabs>;
  365. }
  366. renderView() {
  367. return <div flex >
  368. {this.renderTab()}
  369. {this.renderAttr()}
  370. {this.renderBase()}
  371. {this.renderSelect()}
  372. {this.renderQX()}
  373. <Row type="flex" justify="center">
  374. <Col>
  375. <Button type="primary" onClick={() => {
  376. this.submit();
  377. }}>保存</Button>
  378. </Col>
  379. </Row>
  380. </div>;
  381. }
  382. }