Page.js 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. import React, { Component } from 'react';
  2. import * as querystring from 'querystring';
  3. export default class extends Component {
  4. constructor(props) {
  5. super(props);
  6. const { config, core, match } = this.props;
  7. this.inited = false;
  8. this.loaded = 0;
  9. this.params = match.params;
  10. const state = {
  11. page: {
  12. current: core.query.page ? Number(core.query.page) : 1,
  13. pageSize: core.query.number ? Number(core.query.number) : 20,
  14. showQuickJumper: true,
  15. showSizeChanger: true,
  16. showTotal: total => `总数: ${total}`,
  17. total: 0,
  18. },
  19. search: Object.assign({ page: 1, number: 20 }, core.query),
  20. list: [],
  21. data: {},
  22. selectedKeys: [],
  23. selectedRows: [],
  24. tab: '',
  25. };
  26. this.state = Object.assign(state, this.initState());
  27. this.init();
  28. this.changePageInfo(config);
  29. }
  30. changePageInfo(config) {
  31. if (config.title) window.document.title = config.title;
  32. try {
  33. const metaList = document.documentElement.getElementsByTagName('meta');
  34. for (let i = 0; i < metaList.length; i += 1) {
  35. if (metaList[i].name === 'description' && config.description) {
  36. metaList[i].setAttribute('content', config.description);
  37. } else if (metaList[i].name === 'keyword' && config.keyword) {
  38. metaList[i].setAttribute('content', config.keyword);
  39. }
  40. }
  41. } catch (e) {
  42. console.log(e);
  43. }
  44. }
  45. componentWillMount() {
  46. if (!this.props.core.isCallBack || this.props.config.repeat) this.initData();
  47. this.inited = true;
  48. this.inPage();
  49. }
  50. componentWillUpdate() {
  51. if (this.inited) {
  52. this.updateData();
  53. } else {
  54. if (!this.props.core.isCallBack || this.props.config.repeat) this.initData();
  55. this.inited = true;
  56. }
  57. }
  58. componentWillUnmount() {
  59. this.outPage();
  60. }
  61. init() { }
  62. initState() {
  63. return {};
  64. }
  65. restart() {
  66. /**
  67. * 下次更新重新初始化页面数据
  68. */
  69. this.inited = false;
  70. }
  71. initData() {
  72. /**
  73. * 初始化数据
  74. */
  75. }
  76. updateData() {
  77. /**
  78. * 更新数据
  79. */
  80. }
  81. inPage() {
  82. /**
  83. * 进入页面
  84. */
  85. }
  86. outPage() {
  87. /**
  88. * 离开页面
  89. */
  90. }
  91. onAction(key) {
  92. if (this[`${key}Action`]) this[`${key}Action`]();
  93. }
  94. tabChange(key) {
  95. if (this[`${key}Tab`]) this[`${key}Tab`]();
  96. this.setState({ tab: key });
  97. }
  98. setTableData(list, total) {
  99. const { page } = this.state;
  100. page.total = total;
  101. this.setState({ list, page, selectedKeys: [] });
  102. }
  103. tableSelect(selectedKeys, selectedRows) {
  104. this.setState({ selectedKeys, selectedRows });
  105. }
  106. tableChange(pagination, filters, sorter = {}) {
  107. this.search({
  108. page: pagination.current,
  109. number: pagination.pageSize,
  110. order: sorter.field,
  111. direction: sorter.order === 'ascend' ? 'asc' : 'desc',
  112. });
  113. }
  114. search(data) {
  115. this.refreshQuery(Object.assign(this.state.search, data));
  116. }
  117. refresh() {
  118. this.restart();
  119. this.refreshQuery(this.state.search);
  120. }
  121. refreshQuery(query) {
  122. // this.inited = false;
  123. replaceLink(`${this.props.location.pathname}?${querystring.stringify(query)}`);
  124. }
  125. render() {
  126. const { config } = this.props;
  127. return (
  128. <div id={config.key} className="page">
  129. {this.renderView()}
  130. </div>
  131. );
  132. }
  133. renderView() {
  134. return <div />;
  135. }
  136. open(item = {}, modal = 'detail') {
  137. this.setState({
  138. [modal]: item,
  139. });
  140. }
  141. close(refresh, modal = 'detail') {
  142. this.setState({
  143. [modal]: null,
  144. });
  145. if (refresh) this.refresh();
  146. }
  147. }