import React, { Component } from 'react'; import PropTypes from 'prop-types'; import queryString from 'query-string'; import { connect } from 'dva'; import { routerRedux } from 'dva/router'; import { Card } from 'antd'; import TableList from './table'; import ModalForm from './modal'; import Search from './search'; import PageHeaderLayout from '../../layouts/PageHeaderLayout'; @connect(state => ({ cmsUser: state.cmsUser })) export default class CMSUser extends Component { static propTypes = { cmsUser: PropTypes.object, location: PropTypes.object, dispatch: PropTypes.func, }; render() { const { location, dispatch, cmsUser } = this.props; location.query = queryString.parse(location.search); const { query, pathname } = location; const { field, keyword, ...filters } = query; const { list, listLoading, pagination, currentItem, itemLoading, modalVisible, modalType } = cmsUser; // 把携带的参数中空值项删除 Object.keys(filters).map(key => { filters[key] ? null : delete filters[key] }); // 如果搜索内容不为空则添加进filters中 if (field && keyword) { filters.field = field; filters.keyword = keyword; } const modalProps = { item: modalType === 'create' ? {} : currentItem, visible: modalVisible, width: 550, maskClosable: false, title: `${modalType === 'create' ? '添加CMS用户' : '编辑CMS用户'}`, wrapClassName: 'vertical-center-modal', onOk (data) { dispatch({ type: `cmsUser/${modalType}`, payload: data, }); }, onCancel () { dispatch({ type: 'cmsUser/hideModal', }); }, }; const searchProps = { field, keyword, onSearch: (payload) => { if (!payload.keyword.length) { delete payload.field; delete payload.keyword; } dispatch(routerRedux.push({ pathname, search: queryString.stringify({ ...payload }) })); }, onAdd: () => { dispatch({ type: 'cmsUser/showModal', payload: { modalType: 'create', }, }); } }; const listProps = { location, pagination, dataSource: list, loading: listLoading, curStatus: filters.status, onChange: (pagination, filterArgs) => { const getValue = obj => Object.keys(obj).map(key => obj[key]).join(','); const tableFilters = Object.keys(filterArgs).reduce((obj, key) => { const newObj = { ...obj }; newObj[key] = getValue(filterArgs[key]); return newObj; }, {}); const data = { ...filters, ...tableFilters }; Object.keys(data).map(key => data[key] ? null : delete data[key]); dispatch(routerRedux.push({ pathname, search: queryString.stringify({ ...data, pageNo: pagination.current, pageSize: pagination.pageSize, }), })); }, // 编辑 onEditItem: (item) => { dispatch({ type: 'cmsUser/showModal', payload: { modalType: 'update', currentItem: item, }, }) }, // 封禁 onDeleteItem: (id) => { dispatch({ type: 'cmsUser/delete', payload: id, callback: () => { dispatch( routerRedux.push({ pathname, search: queryString.stringify(filters), }) ); } }); }, // 解禁 onRecoverItem: (payload) => { dispatch({ type: 'cmsUser/recover', payload, callback: () => { dispatch( routerRedux.push({ pathname, search: queryString.stringify(filters), }) ); } }); } }; return ( {modalVisible && } ); } }