import React, { Component } from 'react'; import * as querystring from 'querystring'; export default class extends Component { constructor(props) { super(props); const { config, core, match } = this.props; this.inited = false; this.loaded = 0; this.params = match.params; const state = { page: { current: core.query.page ? Number(core.query.page) : 1, pageSize: core.query.number ? Number(core.query.number) : 20, showQuickJumper: true, showSizeChanger: true, showTotal: total => `总数: ${total}`, total: 0, }, search: Object.assign({ page: 1, number: 20 }, core.query), list: [], data: {}, selectedKeys: [], selectedRows: [], tab: '', }; this.state = Object.assign(state, this.initState()); this.init(); this.changePageInfo(config); } changePageInfo(config) { if (config.title) window.document.title = config.title; try { const metaList = document.documentElement.getElementsByTagName('meta'); for (let i = 0; i < metaList.length; i += 1) { if (metaList[i].name === 'description' && config.description) { metaList[i].setAttribute('content', config.description); } else if (metaList[i].name === 'keyword' && config.keyword) { metaList[i].setAttribute('content', config.keyword); } } } catch (e) { console.log(e); } } componentWillMount() { if (!this.props.core.isCallBack || this.props.config.repeat) this.initData(); this.inited = true; this.inPage(); } componentWillUpdate() { if (this.inited) { this.updateData(); } else { if (!this.props.core.isCallBack || this.props.config.repeat) this.initData(); this.inited = true; } } componentWillUnmount() { this.outPage(); } init() { } initState() { return {}; } restart() { /** * 下次更新重新初始化页面数据 */ this.inited = false; } initData() { /** * 初始化数据 */ } updateData() { /** * 更新数据 */ } inPage() { /** * 进入页面 */ } outPage() { /** * 离开页面 */ } onAction(key) { if (this[`${key}Action`]) this[`${key}Action`](); } tabChange(key) { if (this[`${key}Tab`]) this[`${key}Tab`](); this.setState({ tab: key }); } setTableData(list, total) { const { page } = this.state; page.total = total; this.setState({ list, page, selectedKeys: [] }); } tableSelect(selectedKeys, selectedRows) { this.setState({ selectedKeys, selectedRows }); } tableChange(pagination, filters, sorter = {}) { this.search({ page: pagination.current, number: pagination.pageSize, order: sorter.field, direction: sorter.order === 'ascend' ? 'asc' : 'desc', }); } search(data) { this.refreshQuery(Object.assign(this.state.search, data)); } refresh() { this.restart(); this.refreshQuery(this.state.search); } refreshQuery(query) { // this.inited = false; replaceLink(`${this.props.location.pathname}?${querystring.stringify(query)}`); } render() { const { config } = this.props; return (
{this.renderView()}
); } renderView() { return
; } open(item = {}, modal = 'detail') { this.setState({ [modal]: item, }); } close(refresh, modal = 'detail') { this.setState({ [modal]: null, }); if (refresh) this.refresh(); } }