import React from 'react'; import PropTypes from 'prop-types'; import { Layout, Menu, Icon, Avatar, Dropdown, Tag, message, Spin } from 'antd'; import DocumentTitle from 'react-document-title'; import { connect } from 'dva'; import { Link, routerRedux } from 'dva/router'; import moment from 'moment'; import groupBy from 'lodash/groupBy'; import styles from './BasicLayout.less'; import HeaderSearch from '../components/HeaderSearch'; import NoticeIcon from '../components/NoticeIcon'; import GlobalFooter from '../components/GlobalFooter'; import { getNavData } from '../common/nav'; const { Header, Sider, Content } = Layout; const { SubMenu } = Menu; class BasicLayout extends React.PureComponent { static childContextTypes = { routes: PropTypes.array, params: PropTypes.object, } constructor(props) { super(props); // 把一级 Layout 的 children 作为菜单项 this.menus = getNavData().reduce((arr, current) => arr.concat(current.children), []); this.state = { openKeys: this.getDefaultCollapsedSubMenus(props), }; } getChildContext() { const { routes, params } = this.props; return { routes, params }; } componentDidMount() { this.props.dispatch({ type: 'user/fetchCurrent', }); } onCollapse = (collapsed) => { this.props.dispatch({ type: 'global/changeLayoutCollapsed', payload: collapsed, }); } onMenuClick = ({ key }) => { if (key === 'logout') { this.props.dispatch(routerRedux.push('/user/login')); } } getDefaultCollapsedSubMenus(props) { const currentMenuSelectedKeys = [...this.getCurrentMenuSelectedKeys(props)]; currentMenuSelectedKeys.splice(-1, 1); return currentMenuSelectedKeys; } getCurrentMenuSelectedKeys(props) { const { location: { pathname } } = props || this.props; const keys = pathname.split('/').slice(1); if (keys.length === 1 && keys[0] === '') { return [this.menus[0].key]; } return keys; } getNavMenuItems(menusData, parentPath = '') { if (!menusData) { return []; } return menusData.map((item) => { if (!item.name) { return null; } const itemPath = `${parentPath}/${item.path || ''}`.replace(/\/+/g, '/'); if (item.children && item.children.some(child => child.name)) { return ( {item.name} ) : item.name } key={item.key || item.path} > {this.getNavMenuItems(item.children, itemPath)} ); } return ( {item.icon && } {item.name} ); }); } getPageTitle() { const { routes } = this.props; for (let i = routes.length - 1; i >= 0; i -= 1) { if (routes[i].breadcrumbName) { return `${routes[i].breadcrumbName} - Ant Design Pro`; } } return 'Ant Design Pro'; } getNoticeData() { const { notices = [] } = this.props; if (notices.length === 0) { return {}; } const newNotices = notices.map((notice) => { const newNotice = { ...notice }; if (newNotice.datetime) { newNotice.datetime = moment(notice.datetime).fromNow(); } // transform id to item key if (newNotice.id) { newNotice.key = newNotice.id; } if (newNotice.extra && newNotice.status) { const color = ({ processing: 'blue', urgent: 'red', doing: 'yellow', })[newNotice.status]; newNotice.extra = {newNotice.extra}; } return newNotice; }); return groupBy(newNotices, 'type'); } handleOpenChange = (openKeys) => { const latestOpenKey = openKeys.find(key => this.state.openKeys.indexOf(key) === -1); this.setState({ openKeys: latestOpenKey ? [latestOpenKey] : [], }); } toggle = () => { const { collapsed } = this.props; this.props.dispatch({ type: 'global/changeLayoutCollapsed', payload: !collapsed, }); } handleNoticeClear = (type) => { message.success(`清空了${type}`); this.props.dispatch({ type: 'global/clearNotices', payload: type, }); } handleNoticeVisibleChange = (visible) => { if (visible) { this.props.dispatch({ type: 'global/fetchNotices', }); } } render() { const { children, currentUser, collapsed, fetchingNotices } = this.props; const menu = ( 个人中心 设置 退出登录 ); const noticeData = this.getNoticeData(); // Don't show popup menu when it is been collapsed const menuProps = collapsed ? {} : { openKeys: this.state.openKeys, }; return (
logo

Ant Design Pro

{this.getNavMenuItems(this.menus)}
{ console.log('input', value); // eslint-disable-line }} onPressEnter={(value) => { console.log('enter', value); // eslint-disable-line }} /> { console.log(item, tabProps); // eslint-disable-line }} onClear={this.handleNoticeClear} onPopupVisibleChange={this.handleNoticeVisibleChange} loading={fetchingNotices} popupAlign={{ offset: [20, -16] }} > {currentUser.name ? ( {currentUser.name} ) : }
{children} Copyright 2017 蚂蚁金服体验技术部出品} />
); } } export default connect(state => ({ currentUser: state.user.currentUser, collapsed: state.global.collapsed, fetchingNotices: state.global.fetchingNotices, notices: state.global.notices, }))(BasicLayout);