page.js 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  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. const { form } = this.props;
  78. return System.getMessageTemplate().then(result => {
  79. form.setFieldsValue(result);
  80. this.setState({ template: result || {} });
  81. });
  82. }
  83. refreshCustom() {
  84. return System.listMessage().then((result) => {
  85. this.setState({ list: result.list, total: result.total });
  86. });
  87. }
  88. submit(tab) {
  89. if (tab === 'template') {
  90. this.submitTemplate();
  91. }
  92. }
  93. submitTemplate() {
  94. const { form } = this.props;
  95. form.validateFields((err) => {
  96. if (!err) {
  97. const data = form.getFieldsValue();
  98. System.setMessageTemplate(data)
  99. .then(() => {
  100. this.setState(data);
  101. asyncSMessage('保存成功');
  102. }).catch((e) => {
  103. form.setFields(formatFormError(data, e.result));
  104. });
  105. }
  106. });
  107. }
  108. submitCustom() {
  109. const { exercise } = this.state;
  110. return System.setExerciseTime(exercise);
  111. }
  112. addAction() {
  113. asyncForm('创建消息', this.itemList, {}, data => {
  114. return System.addMessage(data).then(() => {
  115. asyncSMessage('添加成功!');
  116. this.refresh('custom');
  117. });
  118. });
  119. }
  120. editAction(row) {
  121. asyncForm('编辑消息', this.itemList, row, data => {
  122. return System.editMessage(data).then(() => {
  123. asyncSMessage('编辑成功!');
  124. this.refresh('custom');
  125. });
  126. });
  127. }
  128. renderTemplate() {
  129. const { getFieldDecorator } = this.props.form;
  130. return <div>
  131. <Block>
  132. <h1>购买消息</h1>
  133. <Form>
  134. <Form.Item help='可插入自定义字段' />
  135. <Form.Item>
  136. {getFieldDecorator('pay.content', {
  137. rules: [
  138. { required: false, message: '请输入购买消息内容' },
  139. ],
  140. })(
  141. <Input.TextArea />,
  142. )}
  143. </Form.Item>
  144. <Form.Item label='链接地址'>
  145. {getFieldDecorator('pay.link')(
  146. <Input placeholder='请输入跳转地址' />,
  147. )}
  148. </Form.Item>
  149. </Form>
  150. </Block>
  151. <Block>
  152. <h1>换库消息</h1>
  153. <Form>
  154. <Form.Item help='可插入自定义字段' />
  155. <Form.Item>
  156. {getFieldDecorator('library.content', {
  157. rules: [
  158. { required: false, message: '请输入换库消息内容' },
  159. ],
  160. })(
  161. <Input.TextArea />,
  162. )}
  163. </Form.Item>
  164. <Form.Item label='链接地址'>
  165. {getFieldDecorator('library.link')(
  166. <Input placeholder='请输入跳转地址' />,
  167. )}
  168. </Form.Item>
  169. </Form>
  170. </Block>
  171. <Block>
  172. <h1>课程消息</h1>
  173. <Form>
  174. <Form.Item help='可插入自定义字段' />
  175. <Form.Item>
  176. {getFieldDecorator('course.content', {
  177. rules: [
  178. { required: false, message: '请输入课程消息内容' },
  179. ],
  180. })(
  181. <Input.TextArea />,
  182. )}
  183. </Form.Item>
  184. <Form.Item label='链接地址'>
  185. {getFieldDecorator('course.link')(
  186. <Input placeholder='请输入跳转地址' />,
  187. )}
  188. </Form.Item>
  189. </Form>
  190. </Block>
  191. </div>;
  192. }
  193. renderCustom() {
  194. return <Block flex>
  195. <ActionLayout
  196. itemList={this.actionList}
  197. selectedKeys={this.state.selectedKeys}
  198. onAction={key => this.onAction(key)}
  199. />
  200. <TableLayout
  201. columns={this.tableSort(this.columns)}
  202. list={this.state.list}
  203. pagination={false}
  204. loading={this.props.core.loading}
  205. />
  206. </Block>;
  207. }
  208. renderView() {
  209. const { tab } = this.state;
  210. return <div>
  211. <Tabs activeKey={tab} onChange={(value) => {
  212. this.setState({ tab: value, selectedKeys: [], checkedKeys: [] });
  213. this.refresh(value);
  214. }}>
  215. <Tabs.TabPane tab="模版消息" key="template">
  216. {this.renderTemplate()}
  217. </Tabs.TabPane>
  218. <Tabs.TabPane tab="自定义消息" key="custom">
  219. {this.renderCustom()}
  220. </Tabs.TabPane>
  221. </Tabs>
  222. {tab !== 'custom' && <Row type="flex" justify="center">
  223. <Col>
  224. <Button type="primary" onClick={() => {
  225. this.submit(tab);
  226. }}>保存</Button>
  227. </Col>
  228. </Row>}
  229. </div>;
  230. }
  231. }