page.js 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. import React from 'react';
  2. import { Link } from 'react-router-dom';
  3. import { Button, Switch, Modal } from 'antd';
  4. import './index.less';
  5. import Page from '@src/containers/Page';
  6. import Block from '@src/components/Block';
  7. import EditTableCell from '@src/components/EditTableCell';
  8. // import FilterLayout from '@src/layouts/FilterLayout';
  9. import ActionLayout from '@src/layouts/ActionLayout';
  10. import TableLayout from '@src/layouts/TableLayout';
  11. import { formatDate, getMap } from '@src/services/Tools';
  12. import { asyncSMessage, asyncDelConfirm } from '@src/services/AsyncTools';
  13. // import { SwitchSelect } from '../../../../Constant';
  14. import { Ready } from '../../../stores/ready';
  15. export default class extends Page {
  16. init() {
  17. this.structMap = {};
  18. this.actionList = [{
  19. key: 'add',
  20. type: 'primary',
  21. name: '创建',
  22. render: (item) => {
  23. return <Link to='/ready/read/detail'><Button>{item.name}</Button></Link>;
  24. },
  25. }, {
  26. key: 'struct',
  27. name: '管理板块',
  28. }];
  29. this.columns = [{
  30. title: '板块',
  31. dataIndex: 'plate',
  32. render: (text) => {
  33. return this.structMap[text] || '';
  34. },
  35. }, {
  36. title: '文章标题',
  37. dataIndex: 'title',
  38. }, {
  39. title: '更新时间',
  40. dataIndex: 'updateTime',
  41. render: (text) => {
  42. return formatDate(text);
  43. },
  44. }, {
  45. title: '操作',
  46. dataIndex: 'handler',
  47. render: (text, record) => {
  48. return <div className="table-button">
  49. {<Link to={`/ready/read/detail/${record.id}`}>编辑</Link>}
  50. {<a onClick={() => {
  51. this.deleteAction(record);
  52. }}>删除</a>}
  53. </div>;
  54. },
  55. }];
  56. this.structColumns = [{
  57. title: '板块名称',
  58. dataIndex: 'plate',
  59. render: (text, record, index) => {
  60. return <EditTableCell value={text} onChange={(v) => {
  61. this.changeStruct(index, 'plate', v);
  62. }} />;
  63. },
  64. }, {
  65. title: '增加跳转',
  66. dataIndex: 'jump',
  67. render: (text, record, index) => {
  68. return <Switch onChange={(value) => {
  69. this.changeStruct(index, 'jump', value ? 1 : 0);
  70. }} checked={!!text} />;
  71. },
  72. }, {
  73. title: '跳转地址',
  74. dataIndex: 'link',
  75. render: (text, record, index) => {
  76. return <EditTableCell value={text} onChange={(v) => {
  77. this.changeStruct(index, 'link', v);
  78. }} />;
  79. },
  80. }, {
  81. title: '按钮名称',
  82. dataIndex: 'title',
  83. render: (text, record, index) => {
  84. return <EditTableCell value={text} onChange={(v) => {
  85. this.changeStruct(index, 'title', v);
  86. }} />;
  87. },
  88. }];
  89. Ready.getReadyRead().then(result => {
  90. return this.refreshStruct(result);
  91. }).then(() => {
  92. this.initData();
  93. });
  94. }
  95. initData() {
  96. Ready.listRead(this.state.search).then(result => {
  97. this.setTableData(result.list, result.total);
  98. });
  99. }
  100. refreshStruct(result) {
  101. result = result || {};
  102. result.plates = (result.plates || []).map((row, index) => { row.value = index + 1; return row; });
  103. this.structMap = getMap(result.plates, 'value', 'plate');
  104. this.setState({ struct: result });
  105. }
  106. structAction() {
  107. const { struct = {} } = this.state;
  108. this.open(struct);
  109. }
  110. changeStruct(index, field, value, other) {
  111. const { detail } = this.state;
  112. if (other !== undefined) {
  113. detail.plates.forEach((row) => {
  114. row[field] = other;
  115. });
  116. }
  117. detail.plates[index][field] = value;
  118. this.setState({ detail });
  119. }
  120. submitStruct() {
  121. const { detail } = this.state;
  122. Ready.setReadyRead(detail).then(() => {
  123. asyncSMessage('保存成功');
  124. this.close(false, 'detail');
  125. return this.refreshStruct(detail);
  126. }).then(() => {
  127. return this.initData();
  128. });
  129. }
  130. deleteAction(row) {
  131. asyncDelConfirm('删除确认', '是否删除选中?', () => {
  132. const handler = Ready.delRead({ id: row.id });
  133. return handler.then(() => {
  134. asyncSMessage('删除成功!');
  135. this.refresh();
  136. });
  137. });
  138. }
  139. renderView() {
  140. return <Block flex>
  141. {/* <FilterLayout
  142. show
  143. itemList={this.filterForm}
  144. data={this.state.search}
  145. onChange={data => {
  146. this.search(data);
  147. }} /> */}
  148. <ActionLayout
  149. itemList={this.actionList}
  150. selectedKeys={this.state.selectedKeys}
  151. onAction={key => this.onAction(key)}
  152. />
  153. <TableLayout
  154. columns={this.tableSort(this.columns)}
  155. list={this.state.list}
  156. pagination={this.state.page}
  157. loading={this.props.core.loading}
  158. onChange={(pagination, filters, sorter) => this.tableChange(pagination, filters, sorter)}
  159. onSelect={(keys, rows) => this.tableSelect(keys, rows)}
  160. selectedKeys={this.state.selectedKeys}
  161. />
  162. {this.state.detail && <Modal visible closable title='编辑章节' onCancel={() => {
  163. this.close(false, 'detail');
  164. }} onOk={() => {
  165. this.submitStruct();
  166. }}>
  167. <TableLayout
  168. rowKey={'plate'}
  169. columns={this.structColumns}
  170. list={this.state.detail.plates || []}
  171. pagination={false}
  172. />
  173. <Button onClick={() => {
  174. const { detail } = this.state;
  175. detail.plates.push({});
  176. this.setState({ detail });
  177. }}>增加板块</Button></Modal>}
  178. </Block>;
  179. }
  180. }