page.js 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. import React from 'react';
  2. import { Form, Input, Button, Row, Col } 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 Select from '@src/components/Select';
  8. // import FileUpload from '@src/components/FileUpload';
  9. import { formatFormError, getMap } from '@src/services/Tools';
  10. import { asyncSMessage } from '@src/services/AsyncTools';
  11. import { Ready } from '../../../stores/ready';
  12. import { System } from '../../../stores/system';
  13. export default class extends Page {
  14. init() {
  15. Ready.getReadyRead().then(result => {
  16. return this.refreshStruct(result);
  17. });
  18. }
  19. initData() {
  20. const { id } = this.params;
  21. const { form } = this.props;
  22. let handler;
  23. if (id) {
  24. handler = Ready.getRead({ id });
  25. } else {
  26. handler = Promise.resolve({});
  27. }
  28. handler
  29. .then(result => {
  30. form.setFieldsValue(result);
  31. });
  32. }
  33. refreshStruct(result) {
  34. result = result || {};
  35. result.plates = (result.plates || []).map((row, index) => { row.value = index + 1; return row; });
  36. this.structMap = getMap(result.plates, 'value', 'plate');
  37. this.setState({
  38. struct: result,
  39. plates: result.plates.map((row) => {
  40. row.value = `${row.value}`;
  41. row.label = row.plate;
  42. row.title = '';
  43. return row;
  44. }),
  45. });
  46. }
  47. submit() {
  48. const { form } = this.props;
  49. form.validateFields((err) => {
  50. if (!err) {
  51. const data = form.getFieldsValue();
  52. let handler;
  53. if (data.id) {
  54. handler = Ready.editRead(data);
  55. } else {
  56. handler = Ready.addRead(data);
  57. }
  58. handler.then(() => {
  59. asyncSMessage('保存成功');
  60. goBack();
  61. }).catch((e) => {
  62. if (e.result) form.setFields(formatFormError(data, e.result));
  63. });
  64. }
  65. });
  66. }
  67. renderBase() {
  68. const { getFieldDecorator } = this.props.form;
  69. return <Block>
  70. <Form>
  71. {getFieldDecorator('id')(<input hidden />)}
  72. <Form.Item labelCol={{ span: 5 }} wrapperCol={{ span: 16 }} label='板块'>
  73. {getFieldDecorator('plate', {
  74. rules: [
  75. { required: true, message: '请选择' },
  76. ],
  77. })(
  78. <Select select={this.state.plates} placeholder='请选择' />,
  79. )}
  80. </Form.Item>
  81. <Form.Item labelCol={{ span: 5 }} wrapperCol={{ span: 16 }} label='文章标题'>
  82. {getFieldDecorator('title', {
  83. rules: [
  84. { required: true, message: '请输入标题' },
  85. ],
  86. })(
  87. <Input placeholder='请输入标题' />,
  88. )}
  89. </Form.Item>
  90. <Form.Item label='正文'>
  91. {getFieldDecorator('content', {
  92. })(
  93. <Editor placeholder='请输入内容' onUpload={(file) => System.uploadImage(file)} />,
  94. )}
  95. </Form.Item>
  96. </Form>
  97. </Block>;
  98. }
  99. renderView() {
  100. return <div flex>
  101. {this.renderBase()}
  102. <Row type="flex" justify="center">
  103. <Col>
  104. <Button type="primary" onClick={() => {
  105. this.submit();
  106. }}>保存</Button>
  107. </Col>
  108. </Row>
  109. </div>;
  110. }
  111. }