page.js 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  1. import React from 'react';
  2. import { Form, Input, InputNumber, Tabs, Switch, Checkbox, Row, Col, Button } from 'antd';
  3. import './index.less';
  4. import Page from '@src/containers/Page';
  5. import Block from '@src/components/Block';
  6. import Editor from '@src/components/Editor';
  7. import Select from '@src/components/Select';
  8. // import FileUpload from '@src/components/FileUpload';
  9. import { formatFormError } from '@src/services/Tools';
  10. import { asyncSMessage } from '@src/services/AsyncTools';
  11. import { SentenceOption } from '../../../../Constant';
  12. import { Preview } from '../../../stores/preview';
  13. import { Exercise } from '../../../stores/exercise';
  14. import { Sentence } from '../../../stores/sentence';
  15. import config from './index';
  16. export default class extends Page {
  17. constructor(props) {
  18. super(props);
  19. const { id } = this.params;
  20. if (id) {
  21. config.title = '编辑长难句题目';
  22. } else {
  23. config.title = '添加长难句题目';
  24. }
  25. }
  26. init() {
  27. Exercise.allStruct().then(result => {
  28. result = result.filter(row => row.level === 2).map(row => { row.title = `${row.titleZh}/${row.titleEn}`; row.value = row.id; return row; });
  29. this.setState({ exercise: result });
  30. });
  31. }
  32. initData() {
  33. const { id } = this.params;
  34. const { form } = this.props;
  35. let handler;
  36. if (id) {
  37. handler = Preview.get({ id });
  38. } else {
  39. handler = Promise.resolve({ no: 1 });
  40. }
  41. handler
  42. .then(result => {
  43. form.setFieldsValue(result);
  44. this.setState({ data: result });
  45. });
  46. }
  47. submit() {
  48. const { form } = this.props;
  49. form.validateFields((err) => {
  50. if (!err) {
  51. const data = form.getFieldsValue();
  52. data.isTrail = data.isTrail ? 1 : 0;
  53. data.isCustomer = data.isCustomer ? 1 : 0;
  54. data.question.description = data.question.stem.replace(/<[^>]+>/g, '');
  55. let handler;
  56. if (data.id) {
  57. handler = Sentence.editQuestion(data);
  58. } else {
  59. handler = Sentence.addQuestion(data);
  60. }
  61. handler.then(() => {
  62. asyncSMessage('保存成功');
  63. }).catch((e) => {
  64. if (e.result) form.setFields(formatFormError(data, e.result));
  65. });
  66. }
  67. });
  68. }
  69. renderTitle() {
  70. const { id } = this.params;
  71. const { data } = this.state;
  72. const { getFieldDecorator } = this.props.form;
  73. return <Block>
  74. <Form>
  75. {getFieldDecorator('id')(<input hidden />)}
  76. <Form.Item labelCol={{ span: 5 }} wrapperCol={{ span: 16 }} label='题目序号' help='设定后,不允许修改'>
  77. {id && (data.isSystem ? '是' : '否')}
  78. {!id && getFieldDecorator('no', {
  79. rules: [
  80. { required: true, message: '请输入序号' },
  81. // {
  82. // validator: (rule, value, callback) => {
  83. // if (this.partList.indexOf(value) >= 0) callback('该part已被使用');
  84. // else callback();
  85. // callback();
  86. // },
  87. // },
  88. ],
  89. })(
  90. <InputNumber min={1} precision={0} formatter={(v) => parseInt(v, 10) || 1} />,
  91. )}
  92. </Form.Item>
  93. {/* 不允许修改组卷逻辑 */}
  94. <Form.Item labelCol={{ span: 5 }} wrapperCol={{ span: 16 }} label='自定义库' help='打开后,不自动组卷,用于预习作业,设定后,不允许修改'>
  95. {id && (data.isSystem ? '是' : '否')}
  96. {!id && getFieldDecorator('isCustomer', {
  97. valuePropName: 'checked',
  98. })(
  99. <Switch checkedChildren='on' unCheckedChildren='off' />,
  100. )}
  101. </Form.Item>
  102. <Form.Item labelCol={{ span: 5 }} wrapperCol={{ span: 16 }} label='名称'>
  103. {getFieldDecorator('title', {
  104. rules: [
  105. { required: true, message: '请输入名称' },
  106. ],
  107. })(
  108. <Input placeholder='请输入' />,
  109. )}
  110. </Form.Item>
  111. <Form.Item labelCol={{ span: 5 }} wrapperCol={{ span: 16 }} label='开放试用'>
  112. {getFieldDecorator('isTrail', {
  113. valuePropName: 'checked',
  114. })(
  115. <Switch checkedChildren='on' unCheckedChildren='off' />,
  116. )}
  117. </Form.Item>
  118. </Form>
  119. </Block>;
  120. }
  121. renderBase() {
  122. const { getFieldDecorator } = this.props.form;
  123. return <Block flex>
  124. <h1>题干信息</h1>
  125. <Form>
  126. {getFieldDecorator('question.id')(<input hidden />)}
  127. <Form.Item>
  128. {getFieldDecorator('question.stem', {
  129. })(
  130. <Editor placeholder='请输入内容' />,
  131. )}
  132. </Form.Item>
  133. </Form>
  134. </Block>;
  135. }
  136. renderAnswer() {
  137. const { getFieldDecorator } = this.props.form;
  138. return <Block flex>
  139. <h1>题目答案</h1>
  140. <table boarder cellSpacing className='answer'>
  141. <tr>
  142. <td>主语</td>
  143. <td>
  144. <Form.Item>
  145. {getFieldDecorator('question.answer.subject')(
  146. <Select mode='tags' maxTagCount={200} notFoundContent={null} tokenSeparators={[',', ',']} />,
  147. )}
  148. </Form.Item>
  149. </td>
  150. </tr>
  151. <tr>
  152. <td>谓语</td>
  153. <td><Form.Item>
  154. {getFieldDecorator('question.answer.predicate')(
  155. <Select mode='tags' maxTagCount={200} notFoundContent={null} tokenSeparators={[',', ',']} />,
  156. )}
  157. </Form.Item></td>
  158. </tr>
  159. <tr>
  160. <td>宾语</td>
  161. <td><Form.Item>
  162. {getFieldDecorator('question.answer.object')(
  163. <Select mode='tags' maxTagCount={200} notFoundContent={null} tokenSeparators={[',', ',']} />,
  164. )}
  165. </Form.Item></td>
  166. </tr>
  167. </table>
  168. </Block>;
  169. }
  170. renderOption() {
  171. const { getFieldDecorator } = this.props.form;
  172. return <Block flex>
  173. <h1>选项信息(长难句)</h1>
  174. <Form>
  175. <Form.Item>
  176. {getFieldDecorator('question.answer.options')(
  177. <Checkbox.Group options={SentenceOption} />,
  178. )}
  179. </Form.Item>
  180. </Form>
  181. </Block>;
  182. }
  183. renderQX() {
  184. const { getFieldDecorator } = this.props.form;
  185. return <Block flex>
  186. <Form>
  187. <Form.Item label='千行解析'>
  188. {getFieldDecorator('question.qxContent', {
  189. })(
  190. <Editor placeholder='输入内容' />,
  191. )}
  192. </Form.Item>
  193. </Form>
  194. </Block>;
  195. }
  196. renderChinese() {
  197. const { getFieldDecorator } = this.props.form;
  198. return <Block flex>
  199. <Form>
  200. <Form.Item label='中文解析'>
  201. {getFieldDecorator('chinese', {
  202. })(
  203. <Editor placeholder='输入内容' />,
  204. )}
  205. </Form.Item>
  206. </Form>
  207. </Block>;
  208. }
  209. renderTab() {
  210. return <Tabs activeKey='sentence' onChange={(tab) => {
  211. switch (tab) {
  212. case 'textbook':
  213. linkTo('/subject/textbook/question');
  214. break;
  215. case 'base':
  216. linkTo('/subject/question');
  217. break;
  218. default:
  219. }
  220. }}>
  221. <Tabs.TabPane key='base' tab='考试题型' />
  222. <Tabs.TabPane key='sentence' tab='长难句' />
  223. <Tabs.TabPane key='textbook' tab='数学机经' />
  224. </Tabs>;
  225. }
  226. renderView() {
  227. return <div flex >
  228. {this.renderTab()}
  229. {this.renderTitle()}
  230. {this.renderBase()}
  231. {this.renderAnswer()}
  232. {this.renderOption()}
  233. {this.renderQX()}
  234. {this.renderChinese()}
  235. <Row type="flex" justify="center">
  236. <Col>
  237. <Button type="primary" onClick={() => {
  238. this.submit();
  239. }}>保存</Button>
  240. </Col>
  241. </Row>
  242. </div>;
  243. }
  244. }