1
0

page.js 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  1. import React from 'react';
  2. import { Button, Tabs, Row, Col, Form, Input } from 'antd';
  3. import './index.less';
  4. import Page from '@src/containers/Page';
  5. import Block from '@src/components/Block';
  6. import ActionLayout from '@src/layouts/ActionLayout';
  7. import TableLayout from '@src/layouts/TableLayout';
  8. import { formatFormError, formatDate } from '@src/services/Tools';
  9. import { asyncSMessage, asyncForm } from '@src/services/AsyncTools';
  10. import { System } from '../../../stores/system';
  11. export default class extends Page {
  12. init() {
  13. this.itemList = [{
  14. key: 'id',
  15. type: 'hidden',
  16. }, {
  17. key: 'title',
  18. type: 'input',
  19. name: '标题',
  20. }, {
  21. key: 'content',
  22. type: 'textarea',
  23. name: '内容',
  24. }, {
  25. key: 'link',
  26. type: 'input',
  27. name: '链接地址',
  28. }, {
  29. key: 'sendTime',
  30. type: 'date',
  31. name: '发送时间',
  32. }];
  33. this.columns = [{
  34. title: '消息标题',
  35. dataIndex: 'title',
  36. }, {
  37. title: '消息内容',
  38. dataIndex: 'content',
  39. }, {
  40. title: '发送时间',
  41. dataIndex: 'sendTime',
  42. render: (text) => {
  43. return formatDate(text);
  44. },
  45. }, {
  46. title: '操作',
  47. dataIndex: 'handler',
  48. render: (text, record) => {
  49. return <div className="table-button">
  50. {(
  51. <a onClick={() => {
  52. this.editAction(record);
  53. }}>编辑</a>
  54. )}
  55. </div>;
  56. },
  57. }];
  58. this.actionList = [{
  59. key: 'add',
  60. name: '增加',
  61. }];
  62. this.state.tab = 'template';
  63. }
  64. initData() {
  65. this.refresh(this.state.tab);
  66. }
  67. refresh(tab) {
  68. if (tab === 'template') {
  69. return this.refreshTemplate();
  70. }
  71. if (tab === 'custom') {
  72. return this.refreshCustom();
  73. }
  74. return Promise.reject();
  75. }
  76. refreshTemplate() {
  77. return System.getMessageTemplate().then(result => {
  78. this.setState({ template: result || {} });
  79. });
  80. }
  81. refreshCustom() {
  82. return System.listMessage().then((result) => {
  83. this.setState({ list: result.list, total: result.total });
  84. });
  85. }
  86. submit(tab) {
  87. let handler;
  88. if (tab === 'template') {
  89. handler = this.submitTemplate();
  90. }
  91. handler.then(() => {
  92. asyncSMessage('保存成功');
  93. });
  94. }
  95. submitTemplate() {
  96. const { form } = this.props;
  97. form.validateFields((err) => {
  98. if (!err) {
  99. const data = form.getFieldsValue();
  100. System.setMessageTemplate(data)
  101. .then(() => {
  102. this.setState(data);
  103. asyncSMessage('保存成功');
  104. }).catch((e) => {
  105. form.setFields(formatFormError(data, e.result));
  106. });
  107. }
  108. });
  109. }
  110. submitCustom() {
  111. const { exercise } = this.state;
  112. return System.setExerciseTime(exercise);
  113. }
  114. addAction() {
  115. asyncForm('创建消息', this.itemList, {}, data => {
  116. return System.addMessage(data).then(() => {
  117. asyncSMessage('添加成功!');
  118. this.refresh('custom');
  119. });
  120. });
  121. }
  122. editAction(row) {
  123. asyncForm('编辑消息', this.itemList, row, data => {
  124. return System.editMessage(data).then(() => {
  125. asyncSMessage('编辑成功!');
  126. this.refresh('custom');
  127. });
  128. });
  129. }
  130. renderTemplate() {
  131. const { getFieldDecorator } = this.props.form;
  132. return <div>
  133. <Block>
  134. <h1>购买消息</h1>
  135. <Form>
  136. <Form.Item help='可插入自定义字段' />
  137. <Form.Item>
  138. {getFieldDecorator('pay.content', {
  139. rules: [
  140. { required: false, message: '请输入购买消息内容' },
  141. ],
  142. })(
  143. <Input.TextArea />,
  144. )}
  145. </Form.Item>
  146. <Form.Item label='链接地址'>
  147. {getFieldDecorator('pay.link')(
  148. <Input placeholder='请输入跳转地址' />,
  149. )}
  150. </Form.Item>
  151. </Form>
  152. </Block>
  153. <Block>
  154. <h1>换库消息</h1>
  155. <Form>
  156. <Form.Item help='可插入自定义字段' />
  157. <Form.Item>
  158. {getFieldDecorator('library.content', {
  159. rules: [
  160. { required: false, message: '请输入换库消息内容' },
  161. ],
  162. })(
  163. <Input.TextArea />,
  164. )}
  165. </Form.Item>
  166. <Form.Item label='链接地址'>
  167. {getFieldDecorator('library.link')(
  168. <Input placeholder='请输入跳转地址' />,
  169. )}
  170. </Form.Item>
  171. </Form>
  172. </Block>
  173. <Block>
  174. <h1>课程消息</h1>
  175. <Form>
  176. <Form.Item help='可插入自定义字段' />
  177. <Form.Item>
  178. {getFieldDecorator('course.content', {
  179. rules: [
  180. { required: false, message: '请输入课程消息内容' },
  181. ],
  182. })(
  183. <Input.TextArea />,
  184. )}
  185. </Form.Item>
  186. <Form.Item label='链接地址'>
  187. {getFieldDecorator('course.link')(
  188. <Input placeholder='请输入跳转地址' />,
  189. )}
  190. </Form.Item>
  191. </Form>
  192. </Block>
  193. </div>;
  194. }
  195. renderCustom() {
  196. return <Block flex>
  197. <ActionLayout
  198. itemList={this.actionList}
  199. selectedKeys={this.state.selectedKeys}
  200. onAction={key => this.onAction(key)}
  201. />
  202. <TableLayout
  203. columns={this.columns}
  204. list={this.state.list}
  205. pagination={false}
  206. loading={this.props.core.loading}
  207. />
  208. </Block>;
  209. }
  210. renderView() {
  211. const { tab } = this.state;
  212. return <div>
  213. <Tabs activeKey={tab} onChange={(value) => {
  214. this.setState({ tab: value, selectedKeys: [], checkedKeys: [] });
  215. this.refresh(value);
  216. }}>
  217. <Tabs.TabPane tab="模版消息" key="template">
  218. {this.renderTemplate()}
  219. </Tabs.TabPane>
  220. <Tabs.TabPane tab="自定义消息" key="custom">
  221. {this.renderCustom()}
  222. </Tabs.TabPane>
  223. </Tabs>
  224. {tab !== 'custom' && <Row type="flex" justify="center">
  225. <Col>
  226. <Button type="primary" onClick={() => {
  227. this.submit(tab);
  228. }}>保存</Button>
  229. </Col>
  230. </Row>}
  231. </div>;
  232. }
  233. }