page.js 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  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 FileUpload from '@src/components/FileUpload';
  8. import { formatFormError, getMap } from '@src/services/Tools';
  9. import { asyncSMessage } from '@src/services/AsyncTools';
  10. // import { } from '../../../../Constant';
  11. // import { User } from '../../../stores/user';
  12. import { System } from '../../../stores/system';
  13. import { ContractKey } from '../../../../Constant';
  14. const ContractKeyMap = getMap(ContractKey, 'value', 'label');
  15. export default class extends Page {
  16. initData() {
  17. const { key } = this.params;
  18. const { form } = this.props;
  19. let handler;
  20. if (key) {
  21. handler = System.getContract({ key });
  22. } else {
  23. handler = Promise.resolve({});
  24. }
  25. handler
  26. .then(result => {
  27. form.setFieldsValue(result);
  28. this.setState({ data: result });
  29. });
  30. }
  31. submit() {
  32. const { form } = this.props;
  33. form.validateFields((err) => {
  34. if (!err) {
  35. const data = form.getFieldsValue();
  36. System.setContract(data).then(() => {
  37. asyncSMessage('保存成功');
  38. goBack();
  39. }).catch((e) => {
  40. if (e.result) form.setFields(formatFormError(data, e.result));
  41. });
  42. }
  43. });
  44. }
  45. renderBase() {
  46. const { getFieldDecorator } = this.props.form;
  47. const { data } = this.state;
  48. return <Block>
  49. <h1>协议基本信息</h1>
  50. <Form>
  51. {getFieldDecorator('key')(<input hidden />)}
  52. <Form.Item labelCol={{ span: 5 }} wrapperCol={{ span: 16 }} label='应用场景'>
  53. {ContractKeyMap[data.key] || ''}
  54. </Form.Item>
  55. <Form.Item labelCol={{ span: 5 }} wrapperCol={{ span: 16 }} label='协议标题'>
  56. {getFieldDecorator('title', {
  57. rules: [
  58. { required: true, message: '请输入名称' },
  59. ],
  60. })(
  61. <Input placeholder='请输入名称' />,
  62. )}
  63. </Form.Item>
  64. </Form>
  65. </Block>;
  66. }
  67. renderContent() {
  68. const { getFieldDecorator } = this.props.form;
  69. return <Block>
  70. <Form>
  71. <Form.Item label='正文'>
  72. {getFieldDecorator('content', {
  73. })(
  74. <Editor placeholder='输入内容' />,
  75. )}
  76. </Form.Item>
  77. </Form>
  78. </Block>;
  79. }
  80. renderView() {
  81. return <div flex>
  82. {this.renderBase()}
  83. {this.renderContent()}
  84. <Row type="flex" justify="center">
  85. <Col>
  86. <Button type="primary" onClick={() => {
  87. this.submit();
  88. }}>保存</Button>
  89. </Col>
  90. </Row>
  91. </div>;
  92. }
  93. }