page.js 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  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, asyncForm } 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, 'YYYY-MM-DD HH:mm:ss');
  43. },
  44. }, {
  45. title: '操作',
  46. dataIndex: 'handler',
  47. render: (text, record) => {
  48. return <div className="table-button">
  49. {<a onClick={() => {
  50. this.editAction(record);
  51. }}>编辑</a>}
  52. {<a onClick={() => {
  53. this.deleteAction(record);
  54. }}>删除</a>}
  55. </div>;
  56. },
  57. }];
  58. this.itemList = [{
  59. key: 'id',
  60. type: 'hidden',
  61. }, {
  62. key: 'plate',
  63. type: 'select',
  64. name: '板块',
  65. select: [],
  66. required: true,
  67. }, {
  68. key: 'title',
  69. type: 'input',
  70. name: '标题',
  71. required: true,
  72. }, {
  73. key: 'link',
  74. type: 'input',
  75. name: '链接',
  76. required: true,
  77. }];
  78. this.structColumns = [{
  79. title: '板块名称',
  80. dataIndex: 'plate',
  81. render: (text, record, index) => {
  82. return <EditTableCell value={text} onChange={(v) => {
  83. this.changeStruct(index, 'plate', v);
  84. }} />;
  85. },
  86. }, {
  87. title: '增加跳转',
  88. dataIndex: 'jump',
  89. render: (text, record, index) => {
  90. return <Switch onChange={(value) => {
  91. this.changeStruct(index, 'jump', value ? 1 : 0);
  92. }} checked={!!text} />;
  93. },
  94. }, {
  95. title: '跳转地址',
  96. dataIndex: 'link',
  97. render: (text, record, index) => {
  98. return <EditTableCell value={text} onChange={(v) => {
  99. this.changeStruct(index, 'link', v);
  100. }} />;
  101. },
  102. }, {
  103. title: '按钮名称',
  104. dataIndex: 'text',
  105. render: (text, record, index) => {
  106. return <EditTableCell value={text} onChange={(v) => {
  107. this.changeStruct(index, 'text', v);
  108. }} />;
  109. },
  110. }];
  111. Ready.getReadyRead().then(result => {
  112. return this.refreshStruct(result);
  113. }).then(() => {
  114. this.initData();
  115. });
  116. }
  117. initData() {
  118. Ready.listRead(this.state.search).then(result => {
  119. this.setTableData(result.list, result.total);
  120. });
  121. }
  122. refreshStruct(result) {
  123. result = result || {};
  124. result.plates = (result.plates || []).map((row, index) => {
  125. row.title = row.plate;
  126. row.value = `${index + 1}`;
  127. return row;
  128. });
  129. this.structMap = getMap(result.plates, 'value', 'plate');
  130. this.setState({ struct: result });
  131. }
  132. structAction() {
  133. const { struct = {} } = this.state;
  134. this.open(struct);
  135. }
  136. changeStruct(index, field, value, other) {
  137. const { detail } = this.state;
  138. if (other !== undefined) {
  139. detail.plates.forEach((row) => {
  140. row[field] = other;
  141. });
  142. }
  143. detail.plates[index][field] = value;
  144. this.setState({ detail });
  145. }
  146. submitStruct() {
  147. const { detail } = this.state;
  148. Ready.setReadyRead(detail).then(() => {
  149. asyncSMessage('保存成功');
  150. this.close(false, 'detail');
  151. return this.refreshStruct(detail);
  152. }).then(() => {
  153. return this.initData();
  154. });
  155. }
  156. addAction() {
  157. this.itemList[1].select = this.state.struct.plates;
  158. asyncForm('创建', this.itemList, {}, data => {
  159. return Ready.addRead(data).then(() => {
  160. asyncSMessage('添加成功!');
  161. this.refresh();
  162. });
  163. }).then(component => {
  164. this.formF = component;
  165. });
  166. }
  167. editAction(row) {
  168. this.itemList[1].select = this.state.struct.plates;
  169. asyncForm('编辑', this.itemList, row, data => {
  170. return Ready.editRead(data).then(() => {
  171. asyncSMessage('编辑成功!');
  172. this.refresh();
  173. });
  174. }).then(component => {
  175. this.formF = component;
  176. });
  177. }
  178. deleteAction(row) {
  179. asyncDelConfirm('删除确认', '是否删除选中?', () => {
  180. const handler = Ready.delRead({ id: row.id });
  181. return handler.then(() => {
  182. asyncSMessage('删除成功!');
  183. this.refresh();
  184. });
  185. });
  186. }
  187. renderView() {
  188. return <Block flex>
  189. {/* <FilterLayout
  190. show
  191. itemList={this.filterForm}
  192. data={this.state.search}
  193. onChange={data => {
  194. data.page = 1;
  195. this.search(data);
  196. }} /> */}
  197. <ActionLayout
  198. itemList={this.actionList}
  199. selectedKeys={this.state.selectedKeys}
  200. onAction={key => this.onAction(key)}
  201. />
  202. <TableLayout
  203. columns={this.tableSort(this.columns)}
  204. list={this.state.list}
  205. pagination={this.state.page}
  206. loading={this.props.core.loading}
  207. onChange={(pagination, filters, sorter) => this.tableChange(pagination, filters, sorter)}
  208. onSelect={(keys, rows) => this.tableSelect(keys, rows)}
  209. selectedKeys={this.state.selectedKeys}
  210. />
  211. {this.state.detail && <Modal visible closable title='编辑章节' onCancel={() => {
  212. this.close(false, 'detail');
  213. }} onOk={() => {
  214. this.submitStruct();
  215. }}>
  216. <TableLayout
  217. rowKey={'plate'}
  218. columns={this.structColumns}
  219. list={this.state.detail.plates || []}
  220. pagination={false}
  221. />
  222. <Button onClick={() => {
  223. const { detail } = this.state;
  224. detail.plates.push({});
  225. this.setState({ detail });
  226. }}>增加板块</Button></Modal>}
  227. </Block>;
  228. }
  229. }