1
0

page.js 13 KB

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