SystemMenu.js 4.5 KB
import React, { PropTypes } from 'react';
import { connect } from 'react-redux';
import { Button, Tabs, Menu } from 'antd';
import cx from 'classnames';
import s from './systemMenuLess.les';
import { doLogin, sendQuickLoginSmsCode } from '../redux/actions/authed';
import { clearServiceContractsList, loadServiceContractsList } from '../redux/actions/custome';
import { loadUnreadMessage, finishAllMessages, finishNotification } from '../redux/actions/notification'
import { providerMenus, menuLinkMap, menuIconMap, getMenuKeyByPath } from '../utils/sysMenuUtil';
import CustomScrollbars from './widget/CustomScrollbars';
const SubMenu = Menu.SubMenu;


class SystemMenu extends React.Component {
	constructor(props) {
		super(props);
		this.changeMenu = this.changeMenu.bind(this);
		this.onCollapse = this.onCollapse.bind(this);
		this.changeOpenSubMenu = this.changeOpenSubMenu.bind(this);
		this.state = {
			collapsed: false,
			mode: 'inline',
			active: '',
			defaultSelectedKeys: '',
			defaultOpenKeys: '',
			imageUrl: '',
			userImage: '',
			logoImage: ''
		}
	}
	componentDidMount() {
		const { location } = this.props;
		const defaultSelectedKeys = getMenuKeyByPath(location.pathname);
		this.setState({
			defaultSelectedKeys
		})
	}
	componentWillReceiveProps(nextProps) {
	}
	componentWillUnmount() {
	}
	changeOpenSubMenu(openKeys) {
		if (openKeys.length > 0) {
			this.setState({
				defaultOpenKeys: openKeys[1]
			})
		} else {
			this.setState({
				defaultOpenKeys: []
			})
		}

	}
	onCollapse(collapsed) {
		this.setState({
			collapsed,
			mode: collapsed ? 'vertical' : 'inline',
		});
	}
	changeMenu(menu) {
		const { item, key, selectedKeys } = menu;
		const { dispatch } = this.props;
		// dispatch(clearServiceContractsList()); 
		document.location.href = menuLinkMap[key];
		this.setState({
			defaultSelectedKeys: key
		})
		dispatch(loadUnreadMessage())
	}
	render() {
		let { menus, userInfo, height, width } = this.props;
		const { user = {}, tenant = {} } = userInfo;
		const { collapsed, active, imageUrl, userImage, logoImage, defaultSelectedKeys, defaultOpenKeys } = this.state;
		const renderSubMenu = (subMenus) => {
			return subMenus.map((subMenu, i) => {
				return (
					<Menu.Item key={subMenu.module}>
						<span className="nav-text">{subMenu.display_name}{subMenu && subMenu.is_beta && subMenu.is_beta == 'y' && <span style={{ color: 'rgba(153, 169, 191,0.6)' }}> 试用 </span>}</span>
					</Menu.Item>
				)
			});
		}
		if (tenant.biz_type != 0) {
			menus = providerMenus;
		}
		if (tenant.biz_type == '0' || tenant.biz_type == '1') {
			return (
				<div className={cx(s.system_menu_wrap, 'system_menu_wrap')} style={{ height: height - 160 }}>
					<CustomScrollbars viewStyle={{ paddingRight: '10px' }} theme="white"> {/*onSelect*/}
						<Menu theme="dark" mode={this.state.mode} onClick={this.changeMenu} onOpenChange={this.changeOpenSubMenu} onCollapse={this.onCollapse}
							style={{ borderRight: '1px solid transparent', backgroundColor: '#0f2139' }}
							selectedKeys={[defaultSelectedKeys]} openKeys={[defaultOpenKeys]}>
							{menus && menus.map((menu, i) => {
								if (menu.children) {
									return (
										<SubMenu key={menu.module} title={
											<span><img style={{ width: '12px', marginRight: '8px', verticalAlign: 'baseline' }} src={menuIconMap[menu.module]} alt="" />{menu.display_name}</span>
										}>
											{renderSubMenu(menu.children)}
										</SubMenu>
									)
								} else {
									return (
										<Menu.Item key={menu.module} >
											<span className="nav-text">
												<img style={{ width: '12px', marginRight: '8px', verticalAlign: 'baseline' }} src={menuIconMap[menu.module]} alt="" />
												{menu.display_name}
												{/* {menu && menu.is_beta && menu.is_beta == 'y' && <span style={{ color: 'rgba(153, 169, 191,0.6)' }}>( 试用 )</span>} */}
												{/* TODO: 先写死 */}
												{menu && menu.module && menu.module == 'persontax' && <span className={cx(s.menu_new)}></span>}
											</span>
										</Menu.Item>
									)
								}
							})}
						</Menu>
					</CustomScrollbars>
				</div>
			)
		} else {
			return (
				<div className={cx(s.system_menu_wrap, 'system_menu_wrap')} style={{ height: '100vh' }}>
				</div>
			)
		}

	}
}


const mapState = (state) => {
	const {
		authed: { userInfo, userInfo: { perms = [], menus = [] }, tenant },
		hr: { tenants = [] },
		setting: { height, width }
	} = state;
	return {
		userInfo,
		tenants,
		perms,
		tenant,
		menus,
		height,
		width
	}
};

export default connect(mapState)(SystemMenu);