page.js 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. import React from 'react';
  2. import { Tabs } from 'antd';
  3. import './index.less';
  4. import Page from '@src/containers/Page';
  5. import Block from '@src/components/Block';
  6. import ShowImage from '@src/components/ShowImage';
  7. import ActionLayout from '@src/layouts/ActionLayout';
  8. import TableLayout from '@src/layouts/TableLayout';
  9. // import { formatDate } from '@src/services/Tools';
  10. import { asyncSMessage, asyncForm, asyncDelConfirm } from '@src/services/AsyncTools';
  11. import { Ready } from '../../../stores/ready';
  12. import { System } from '../../../stores/system';
  13. export default class extends Page {
  14. init() {
  15. this.itemList = [{
  16. key: 'id',
  17. type: 'hidden',
  18. }, {
  19. key: 'title',
  20. type: 'input',
  21. name: '标题',
  22. }, {
  23. key: 'content',
  24. type: 'textarea',
  25. name: '内容',
  26. }, {
  27. key: 'cover',
  28. type: 'image',
  29. name: '封面图片',
  30. onUpload: ({ file }) => {
  31. return System.uploadImage(file).then(result => { return result; });
  32. },
  33. }];
  34. this.columns = [{
  35. title: '资料封面',
  36. dataIndex: 'cover',
  37. render: (text) => {
  38. return <ShowImage src={text} />;
  39. },
  40. }, {
  41. title: '资料标题',
  42. dataIndex: 'title',
  43. }, {
  44. title: '内容',
  45. dataIndex: 'content',
  46. }, {
  47. title: '操作',
  48. dataIndex: 'handler',
  49. render: (text, record) => {
  50. return <div className="table-button">
  51. {(
  52. <a onClick={() => {
  53. this.editAction(record);
  54. }}>编辑</a>
  55. )}
  56. {(
  57. <a onClick={() => {
  58. this.deleteAction(record);
  59. }}>删除</a>
  60. )}
  61. </div>;
  62. },
  63. }];
  64. this.actionList = [{
  65. key: 'add',
  66. name: '增加',
  67. }];
  68. }
  69. initData() {
  70. this.refreshTab(this.state.search.tab || 'official');
  71. }
  72. refreshTab(tab) {
  73. const { search } = this.state;
  74. search.tab = tab;
  75. this.setState({ search });
  76. if (tab === 'official') {
  77. return this.refreshOfficial();
  78. }
  79. if (tab === 'unofficial') {
  80. return this.refreshUnofficial();
  81. }
  82. return Promise.reject();
  83. }
  84. refreshOfficial() {
  85. return Ready.listData({ isOfficial: true }).then((result) => {
  86. this.setState({ list: result.list, total: result.total });
  87. });
  88. }
  89. refreshUnofficial() {
  90. return Ready.listData({ isOfficial: false }).then((result) => {
  91. this.setState({ list: result.list, total: result.total });
  92. });
  93. }
  94. addAction() {
  95. asyncForm('创建资料', this.itemList, {}, data => {
  96. data.isOfficial = this.state.search.tab === 'official' ? 1 : 0;
  97. return Ready.addData(data).then(() => {
  98. asyncSMessage('添加成功!');
  99. this.refresh();
  100. });
  101. });
  102. }
  103. editAction(row) {
  104. asyncForm('编辑资料', this.itemList, row, data => {
  105. return Ready.editData(data).then(() => {
  106. asyncSMessage('编辑成功!');
  107. this.refresh();
  108. });
  109. });
  110. }
  111. deleteAction(row) {
  112. asyncDelConfirm('删除确认', '是否删除选中?', () => {
  113. const handler = Ready.delData({ id: row.id });
  114. return handler.then(() => {
  115. asyncSMessage('删除成功!');
  116. this.refresh();
  117. });
  118. });
  119. }
  120. renderOfficial() {
  121. return <Block flex>
  122. <ActionLayout
  123. itemList={this.actionList}
  124. selectedKeys={this.state.selectedKeys}
  125. onAction={key => this.onAction(key)}
  126. />
  127. <TableLayout
  128. columns={this.tableSort(this.columns)}
  129. list={this.state.list}
  130. pagination={false}
  131. loading={this.props.core.loading}
  132. />
  133. </Block>;
  134. }
  135. renderUnofficial() {
  136. return this.renderOfficial();
  137. }
  138. renderView() {
  139. const { search } = this.state;
  140. const { tab } = search;
  141. return <div>
  142. <Tabs activeKey={tab || 'official'} onChange={(value) => {
  143. this.search({ tab: value });
  144. }}>
  145. <Tabs.TabPane tab="官方资料" key="official">
  146. {this.renderOfficial()}
  147. </Tabs.TabPane>
  148. <Tabs.TabPane tab="非官方资料" key="unofficial">
  149. {this.renderUnofficial()}
  150. </Tabs.TabPane>
  151. </Tabs>
  152. </div>;
  153. }
  154. }