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