page.js 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406
  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. else asyncSMessage(e.message, 'error');
  171. });
  172. }
  173. });
  174. }
  175. renderAttr() {
  176. const { id } = this.params;
  177. const { getFieldDecorator } = this.props.form;
  178. return <Block flex>
  179. <h1>基本信息</h1>
  180. <Form>
  181. {getFieldDecorator('id')(<input hidden />)}
  182. <Form.Item labelCol={{ span: 5 }} wrapperCol={{ span: 16 }} label='题型' help='设定后,不允许修改'>
  183. {getFieldDecorator('question.questionType', {
  184. rules: [
  185. { required: true, message: '请选择题型' },
  186. ],
  187. })(
  188. <Select disabled={id} select={TextbookType} placeholder='请选择题型' onChange={(v) => {
  189. this.refreshPlace(v);
  190. }} />,
  191. )}
  192. </Form.Item>
  193. <Form.Item labelCol={{ span: 5 }} wrapperCol={{ span: 16 }} label='换库表' help='设定后,不允许修改'>
  194. {getFieldDecorator('libraryId', {
  195. rules: [
  196. { required: true, message: '请选择换库表' },
  197. ],
  198. })(
  199. <Select disabled={id} {...this.state.libraryId} placeholder='请选择换库表' onChange={(v) => {
  200. this.refreshNo(v);
  201. }} />,
  202. )}
  203. </Form.Item>
  204. <Form.Item labelCol={{ span: 5 }} wrapperCol={{ span: 16 }} label='题目序号'>
  205. {getFieldDecorator('no')(
  206. <Input disabled />,
  207. )}
  208. </Form.Item>
  209. <Form.Item labelCol={{ span: 5 }} wrapperCol={{ span: 16 }} label='考点'>
  210. {getFieldDecorator('question.place', {
  211. rules: [
  212. { required: true, message: '请选择考点' },
  213. ],
  214. })(
  215. <Select select={this.state.placeList} placeholder='请选择考点' />,
  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='选项问题说明' onUpload={(file) => System.uploadImage(file)} />,
  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('question.qxContent', {
  342. })(
  343. <Editor placeholder='输入内容' onUpload={(file) => System.uploadImage(file)} />,
  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. }