page.js 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  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 { MessageCategory } from '../../../../Constant';
  14. const MessageCategoryMap = getMap(MessageCategory, 'value', 'label');
  15. const MessageCategoryParamsMap = getMap(MessageCategory, 'value', 'params');
  16. export default class extends Page {
  17. initData() {
  18. const { id } = this.params;
  19. const { form } = this.props;
  20. let handler;
  21. if (id) {
  22. handler = System.getMessage({ id });
  23. } else {
  24. handler = Promise.resolve({ messageMethod: 'inside', messageCategory: 'custom' });
  25. }
  26. handler
  27. .then(result => {
  28. form.setFieldsValue(result);
  29. this.setState({ data: result });
  30. });
  31. }
  32. submit() {
  33. const { form } = this.props;
  34. form.validateFields((err) => {
  35. if (!err) {
  36. const data = form.getFieldsValue();
  37. System.setContract(data).then(() => {
  38. asyncSMessage('保存成功');
  39. goBack();
  40. }).catch((e) => {
  41. if (e.result) form.setFields(formatFormError(data, e.result));
  42. });
  43. }
  44. });
  45. }
  46. renderBase() {
  47. const { getFieldDecorator } = this.props.form;
  48. const { data } = this.state;
  49. return <Block>
  50. <h1>{data.messageMethod === 'email' && '邮件模版'}{data.messageMethod === 'inside' && '站内信模版'}</h1>
  51. <Form>
  52. {getFieldDecorator('id')(<input hidden />)}
  53. <Form.Item labelCol={{ span: 5 }} wrapperCol={{ span: 16 }} label='触发场景'>
  54. {MessageCategoryMap[data.messageCategory] || ''}
  55. </Form.Item>
  56. <Form.Item labelCol={{ span: 5 }} wrapperCol={{ span: 16 }} label='消息标题'>
  57. {getFieldDecorator('title', {
  58. rules: [
  59. { required: true, message: '请输入名称' },
  60. ],
  61. })(
  62. <Input placeholder='请输入名称' />,
  63. )}
  64. </Form.Item>
  65. {data.messageMethod === 'inside' && <Form.Item labelCol={{ span: 5 }} wrapperCol={{ span: 16 }} label='跳转链接'>
  66. {getFieldDecorator('link', {
  67. rules: [
  68. { required: true, message: '请输入链接' },
  69. ],
  70. })(
  71. <Input placeholder='请输入链接' />,
  72. )}
  73. </Form.Item>}
  74. </Form>
  75. </Block>;
  76. }
  77. renderContent() {
  78. const { getFieldDecorator } = this.props.form;
  79. const { data } = this.state;
  80. return <Block>
  81. <Form>
  82. <Form.Item label='正文'>
  83. 可插入自定义字段:{(MessageCategoryParamsMap[data.messageCategory] || []).map(row => `{${row}}`).join(', ')}
  84. {getFieldDecorator('content', {
  85. })(
  86. <Editor placeholder='输入内容' />,
  87. )}
  88. </Form.Item>
  89. </Form>
  90. </Block>;
  91. }
  92. renderView() {
  93. return <div flex>
  94. {this.renderBase()}
  95. {this.renderContent()}
  96. <Row type="flex" justify="center">
  97. <Col>
  98. <Button type="primary" onClick={() => {
  99. this.submit();
  100. }}>保存</Button>
  101. </Col>
  102. </Row>
  103. </div>;
  104. }
  105. }