page.js 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328
  1. import React from 'react';
  2. import { Form, Button, Row, Col, Icon, Input, InputNumber, Upload } from 'antd';
  3. import './index.less';
  4. import Editor from '@src/components/Editor';
  5. import Page from '@src/containers/Page';
  6. import Block from '@src/components/Block';
  7. import Radio from '@src/components/Radio';
  8. import TreeSelect from '@src/components/TreeSelect';
  9. // import EditTableCell from '@src/components/EditTableCell';
  10. // import ActionLayout from '@src/layouts/ActionLayout';
  11. // import TableLayout from '@src/layouts/TableLayout';
  12. import { formatFormError, formatTreeData, getMap } from '@src/services/Tools';
  13. import { asyncSMessage } from '@src/services/AsyncTools';
  14. import { SwitchSelect, DataType } from '../../../../Constant';
  15. // import { User } from '../../../stores/user';
  16. import { Exercise } from '../../../stores/exercise';
  17. import { Course } from '../../../stores/course';
  18. import { System } from '../../../stores/system';
  19. export default class extends Page {
  20. init() {
  21. this.exerciseMap = {};
  22. Exercise.dataStruct().then((result) => {
  23. const list = result.map(row => { row.title = `${row.titleZh}`; row.value = row.id; return row; });
  24. const tree = formatTreeData(list, 'id', 'title', 'parentId');
  25. this.exerciseMap = getMap(result.map(row => {
  26. row.title = `${row.titleZh}`;
  27. row.value = row.id;
  28. return row;
  29. }), 'id');
  30. this.setState({ exercise: tree });
  31. });
  32. }
  33. initData() {
  34. const { id } = this.params;
  35. let handler;
  36. if (id) {
  37. handler = Course.getData({ id });
  38. } else {
  39. handler = Promise.resolve({ structId: '' });
  40. }
  41. handler
  42. .then(result => {
  43. const { setFieldsValue } = this.props.form;
  44. result.structId = `${result.structId || ' '}`;
  45. setFieldsValue(result);
  46. this.setState({ data: result });
  47. });
  48. }
  49. submit() {
  50. const { form } = this.props;
  51. form.validateFields((err, values) => {
  52. console.log(values);
  53. if (!err) {
  54. const data = form.getFieldsValue();
  55. data.parentStructId = this.exerciseMap[data.structId] ? this.exerciseMap[data.structId].parentId : 0;
  56. data.isSentence = this.exerciseMap[data.structId] ? (this.exerciseMap[data.structId].extend === 'sentence' ? 1 : 0) : 0;
  57. let handler;
  58. if (data.id) {
  59. handler = Course.editData(data);
  60. } else {
  61. data.courseModule = module;
  62. handler = Course.addData(data);
  63. }
  64. handler.then(() => {
  65. asyncSMessage('保存成功');
  66. }).catch((e) => {
  67. if (e.result) form.setFields(formatFormError(data, e.result));
  68. });
  69. }
  70. });
  71. }
  72. renderBase() {
  73. const { exercise, data } = this.state;
  74. const { getFieldDecorator } = this.props.form;
  75. const structIdConfig = {
  76. rules: [{
  77. required: true, message: '请选择学科',
  78. }],
  79. };
  80. if (data.structId) {
  81. structIdConfig.initialValue = data.structId;
  82. }
  83. return <Block>
  84. <h1>基本信息</h1>
  85. <Form>
  86. {getFieldDecorator('id')(<input hidden />)}
  87. {exercise && data && <Form.Item key={data.structId} labelCol={{ span: 5 }} wrapperCol={{ span: 16 }} label='学科'>
  88. {getFieldDecorator('structId', structIdConfig)(
  89. <TreeSelect treeData={exercise} />,
  90. )}
  91. </Form.Item>}
  92. <Form.Item labelCol={{ span: 5 }} wrapperCol={{ span: 16 }} label='资料形式'>
  93. {getFieldDecorator('dataType', {
  94. rules: [{
  95. required: true, message: '请选择类型',
  96. }],
  97. })(
  98. <Radio select={DataType} />,
  99. )}
  100. </Form.Item>
  101. <Form.Item labelCol={{ span: 5 }} wrapperCol={{ span: 16 }} label='适合新手'>
  102. {getFieldDecorator('isNovice', {
  103. rules: [{
  104. required: true, message: '请选择',
  105. }],
  106. })(
  107. <Radio select={SwitchSelect} />,
  108. )}
  109. </Form.Item>
  110. <Form.Item labelCol={{ span: 5 }} wrapperCol={{ span: 16 }} label='原创资料'>
  111. {getFieldDecorator('isOriginal', {
  112. rules: [{
  113. required: true, message: '请选择',
  114. }],
  115. })(
  116. <Radio select={SwitchSelect} />,
  117. )}
  118. </Form.Item>
  119. <Form.Item labelCol={{ span: 5 }} wrapperCol={{ span: 16 }} label='资料名称'>
  120. {getFieldDecorator('title', {
  121. rules: [{
  122. required: true, message: '请输入名称',
  123. }],
  124. })(
  125. <Input placeholder='请输入' />,
  126. )}
  127. </Form.Item>
  128. <Form.Item labelCol={{ span: 5 }} wrapperCol={{ span: 16 }} label='价格'>
  129. {getFieldDecorator('price', {
  130. rules: [{
  131. required: true, message: '请输入价格',
  132. }],
  133. })(
  134. <InputNumber placeholder='请输入' min={0} />,
  135. )}
  136. </Form.Item>
  137. <Form.Item labelCol={{ span: 5 }} wrapperCol={{ span: 16 }} label='页数'>
  138. {getFieldDecorator('pages', {
  139. rules: [{
  140. required: true, message: '请输入页数',
  141. }],
  142. })(
  143. <InputNumber placeholder='请输入' min={0} />,
  144. )}
  145. </Form.Item>
  146. <Form.Item labelCol={{ span: 5 }} wrapperCol={{ span: 16 }} label='链接地址'>
  147. {getFieldDecorator('link', {
  148. rules: [{
  149. required: true, message: '请输入地址',
  150. }],
  151. })(
  152. <Input placeholder='请输入' />,
  153. )}
  154. </Form.Item>
  155. </Form>
  156. </Block>;
  157. }
  158. renderInfo() {
  159. const { getFieldDecorator, setFieldsValue, getFieldValue } = this.props.form;
  160. const cover = getFieldValue('cover');
  161. return <Block>
  162. <h1>宣传信息</h1>
  163. <Form>
  164. <Form.Item labelCol={{ span: 5 }} wrapperCol={{ span: 16 }} label='资料封面'>
  165. {getFieldDecorator('cover', {
  166. rules: [
  167. { required: true, message: '上传图片' },
  168. ],
  169. })(
  170. <Upload
  171. listType="picture-card"
  172. showUploadList={false}
  173. beforeUpload={(file) => System.uploadImage(file).then((result) => {
  174. setFieldsValue({ cover: result.url });
  175. return Promise.reject();
  176. })}
  177. >
  178. {cover ? <img src={cover} alt="avatar" /> : <div>
  179. <Icon type={this.state.loading ? 'loading' : 'plus'} />
  180. <div className="ant-upload-text">Upload</div>
  181. </div>}
  182. </Upload>,
  183. )}
  184. </Form.Item>
  185. <Form.Item labelCol={{ span: 5 }} wrapperCol={{ span: 16 }} label='摘要介绍'>
  186. {getFieldDecorator('description', {
  187. rules: [{
  188. required: true, message: '请输入摘要',
  189. }],
  190. })(
  191. <Input placeholder='请输入' />,
  192. )}
  193. </Form.Item>
  194. {/* <Form.Item labelCol={{ span: 5 }} wrapperCol={{ span: 16 }} label='试用页码'>
  195. <Row>
  196. <Col span={3}>
  197. <Form.Item>
  198. {getFieldDecorator('trailStart')(
  199. <InputNumber min={1} precision={0} formatter={(v) => parseInt(v, 10) || 1} />,
  200. )}
  201. </Form.Item>
  202. </Col>
  203. <Col span={1}>至</Col>
  204. <Col span={3}>
  205. <Form.Item>
  206. {getFieldDecorator('trailEnd')(
  207. <InputNumber min={1} precision={0} formatter={(v) => parseInt(v, 10) || 1} />,
  208. )}
  209. </Form.Item>
  210. </Col>
  211. </Row>
  212. </Form.Item> */}
  213. </Form>
  214. </Block>;
  215. }
  216. renderFile() {
  217. const { getFieldDecorator, setFieldsValue, getFieldValue } = this.props.form;
  218. const resource = getFieldValue('resource');
  219. const trailResource = getFieldValue('trailResource');
  220. return <Block>
  221. <h1>资料文件</h1>
  222. <Form.Item labelCol={{ span: 5 }} wrapperCol={{ span: 16 }} label='正式版本'>
  223. {resource && <a href={resource} target="_blank">访问</a>}
  224. {getFieldDecorator('resource', {
  225. rules: [
  226. { required: true, message: '上传文件' },
  227. ],
  228. })(
  229. <Upload
  230. showUploadList={false}
  231. beforeUpload={(file) => System.uploadImage(file).then((result) => {
  232. setFieldsValue({ resource: result.url });
  233. return Promise.reject();
  234. })}
  235. >
  236. <Button>上传文件</Button>
  237. </Upload>,
  238. )}
  239. </Form.Item>
  240. <Form.Item labelCol={{ span: 5 }} wrapperCol={{ span: 16 }} label='试用版本'>
  241. {trailResource && <a href={trailResource} target="_blank">访问</a>}
  242. {getFieldDecorator('trailResource', {
  243. rules: [
  244. { required: true, message: '上传文件' },
  245. ],
  246. })(
  247. <Upload
  248. showUploadList={false}
  249. beforeUpload={(file) => System.uploadImage(file).then((result) => {
  250. setFieldsValue({ trailResource: result.url });
  251. return Promise.reject();
  252. })}
  253. >
  254. <Button>上传文件</Button>
  255. </Upload>,
  256. )}
  257. </Form.Item>
  258. </Block>;
  259. }
  260. renderContent() {
  261. const { getFieldDecorator } = this.props.form;
  262. return <Block flex>
  263. <Form>
  264. <Form.Item label='资料介绍'>
  265. {getFieldDecorator('content', {
  266. })(
  267. <Editor placeholder='输入内容' />,
  268. )}
  269. </Form.Item>
  270. </Form>
  271. </Block>;
  272. }
  273. renderAuthor() {
  274. const { getFieldDecorator } = this.props.form;
  275. return <Block flex>
  276. <Form>
  277. <Form.Item label='作者介绍'>
  278. {getFieldDecorator('authorContent', {
  279. })(
  280. <Editor placeholder='输入内容' />,
  281. )}
  282. </Form.Item>
  283. </Form>
  284. </Block>;
  285. }
  286. renderMethod() {
  287. const { getFieldDecorator } = this.props.form;
  288. return <Block flex>
  289. <Form>
  290. <Form.Item label='获取方式'>
  291. {getFieldDecorator('methodContent', {
  292. })(
  293. <Editor placeholder='输入内容' />,
  294. )}
  295. </Form.Item>
  296. </Form>
  297. </Block>;
  298. }
  299. renderView() {
  300. return <div flex>
  301. {this.renderBase()}
  302. {this.renderInfo()}
  303. {this.renderFile()}
  304. {this.renderContent()}
  305. {this.renderAuthor()}
  306. {this.renderMethod()}
  307. <Row type="flex" justify="center">
  308. <Col>
  309. <Button type="primary" onClick={() => {
  310. this.submit();
  311. }}>保存</Button>
  312. </Col>
  313. </Row>
  314. </div>;
  315. }
  316. }