yapi/client/components/Header/Header.js

192 lines
4.9 KiB
JavaScript
Raw Normal View History

2017-07-10 11:50:56 +08:00
import './Header.scss'
2017-07-07 17:26:31 +08:00
import React, { Component } from 'react'
2017-07-12 16:27:06 +08:00
import PropTypes from 'prop-types'
import { connect } from 'react-redux'
2017-07-10 21:29:03 +08:00
import { Link } from 'react-router-dom'
2017-08-08 20:27:18 +08:00
import { Icon, Layout, Menu, Dropdown, message, Tooltip } from 'antd'
2017-08-10 20:08:23 +08:00
import { checkLoginState, logoutActions, loginTypeAction} from '../../reducer/modules/user'
2017-08-08 10:07:55 +08:00
import { changeMenuItem } from '../../reducer/modules/menu'
import { withRouter } from 'react-router';
import Srch from './Search/Search'
2017-07-17 21:11:58 +08:00
const { Header } = Layout;
2017-08-08 20:27:18 +08:00
const headerStyle = {
'height': '.56rem',
'lineHeight': '.56rem',
'padding': 0
};
const MenuUser = (props) => (
2017-07-26 16:06:54 +08:00
<Menu
style={{
"boxShadow":"0 1px 6px rgba(0, 0, 0, 0.3)"
}}
>
2017-07-27 14:45:51 +08:00
<Menu.Item key="0">
2017-07-26 16:06:54 +08:00
<Link to={`/user/profile/${props.uid}`} onClick={props.relieveLink}><Icon type="user"/>个人中心</Link>
</Menu.Item>
2017-07-27 14:45:51 +08:00
<Menu.Item key="1">
2017-07-26 16:06:54 +08:00
<a onClick={props.logout}><Icon type="logout" />退出</a>
</Menu.Item>
</Menu>
);
2017-08-08 20:27:18 +08:00
MenuUser.propTypes={
2017-08-08 20:27:18 +08:00
user: PropTypes.string,
msg: PropTypes.string,
uid: PropTypes.number,
2017-08-08 20:27:18 +08:00
relieveLink: PropTypes.func,
logout: PropTypes.func
}
2017-07-12 16:27:06 +08:00
const ToolUser = (props)=> (
<ul>
2017-08-08 20:27:18 +08:00
<li className="toolbar-li item-search">
<Srch groupList={props.groupList}/>
</li>
2017-08-08 20:27:18 +08:00
<Link to="/">
<Tooltip placement="bottom" title={'新建项目'}>
<li className="toolbar-li">
<Icon className="dropdown-link" style={{ fontSize: 16 }} type="plus-circle" />
</li>
</Tooltip>
</Link>
<Tooltip placement="bottom" title={'使用文档'}>
<li className="toolbar-li">
<a target="_blank" href="./doc/index.html"><Icon className="dropdown-link" style={{ fontSize: 16 }} type="question-circle" /></a>
</li>
</Tooltip>
<li className="toolbar-li">
2017-07-26 16:06:54 +08:00
<Dropdown
placement = "bottomRight"
overlay={
<MenuUser
user={props.user}
msg={props.msg}
uid={props.uid}
relieveLink={props.relieveLink}
logout={props.logout}
/>
}>
<a className="dropdown-link">
2017-08-10 15:37:20 +08:00
<img style={{width:24,height:24}} src="/user/avatar" /><span className="name">{props.user}</span>
</a>
</Dropdown>
</li>
2017-07-12 16:27:06 +08:00
</ul>
);
ToolUser.propTypes={
2017-08-08 20:27:18 +08:00
user: PropTypes.string,
msg: PropTypes.string,
uid: PropTypes.number,
2017-08-08 20:27:18 +08:00
relieveLink: PropTypes.func,
logout: PropTypes.func,
groupList: PropTypes.array
2017-07-12 16:27:06 +08:00
};
@connect(
(state) => {
return{
2017-08-10 20:08:23 +08:00
user: state.user.userName,
uid: state.user.uid,
msg: null,
2017-08-10 20:08:23 +08:00
login:state.user.isLogin
}
},
{
loginTypeAction,
logoutActions,
checkLoginState,
changeMenuItem
}
)
2017-07-19 14:08:24 +08:00
@withRouter
export default class HeaderCom extends Component {
2017-07-07 17:26:31 +08:00
constructor(props) {
2017-07-12 16:27:06 +08:00
super(props);
2017-07-18 18:25:53 +08:00
}
2017-07-19 10:38:29 +08:00
static propTypes ={
router: PropTypes.object,
2017-07-19 10:38:29 +08:00
user: PropTypes.string,
msg: PropTypes.string,
uid: PropTypes.number,
2017-07-19 10:38:29 +08:00
login:PropTypes.bool,
relieveLink:PropTypes.func,
logoutActions:PropTypes.func,
checkLoginState:PropTypes.func,
loginTypeAction:PropTypes.func,
2017-07-20 21:30:35 +08:00
changeMenuItem:PropTypes.func,
history: PropTypes.object,
location: PropTypes.object
2017-07-19 10:38:29 +08:00
}
2017-07-18 18:25:53 +08:00
linkTo = (e) =>{
2017-07-28 21:19:45 +08:00
if(e.key != '/doc'){
this.props.changeMenuItem(e.key);
if(!this.props.login){
message.info('请先登录',1);
}
2017-07-26 17:22:09 +08:00
}
2017-07-18 18:25:53 +08:00
}
2017-07-19 10:38:29 +08:00
relieveLink = () => {
2017-07-20 21:30:35 +08:00
this.props.changeMenuItem("");
}
2017-07-19 10:38:29 +08:00
logout = (e) => {
e.preventDefault();
2017-07-23 18:24:46 +08:00
this.props.logoutActions().then((res) => {
if (res.payload.data.errcode == 0) {
this.props.history.push('/');
this.props.changeMenuItem("/");
message.success('退出成功! ');
} else {
message.error(res.payload.data.errmsg);
}
}).catch((err) => {
message.error(err);
});
2017-07-19 10:38:29 +08:00
}
handleLogin = (e) => {
e.preventDefault();
this.props.loginTypeAction("1");
}
handleReg = (e)=>{
e.preventDefault();
this.props.loginTypeAction("2");
2017-07-07 17:26:31 +08:00
}
checkLoginState = () => {
2017-07-23 18:24:46 +08:00
this.props.checkLoginState.then((res) => {
if (res.payload.data.errcode !== 0) {
this.props.history.push('/');
}
}).catch((err) => {
console.log(err);
})
}
2017-07-07 17:26:31 +08:00
render () {
2017-08-08 20:27:18 +08:00
const { login, user, msg, uid } = this.props;
2017-07-07 17:26:31 +08:00
return (
2017-08-08 20:27:18 +08:00
<Header className="header-box" style={headerStyle}>
<div className="content g-row">
<div className="logo">
2017-08-08 20:46:19 +08:00
<Link to="/" onClick={this.relieveLink} className="href">
2017-08-08 20:27:18 +08:00
<img className="img" src="./image/logo_header@1x.png" /><span className="logo-name">YAPI<span className="ui-badge"></span></span>
</Link>
</div>
<div className="user-toolbar">
{login?
<ToolUser
user = { user }
msg = { msg }
uid = { uid }
relieveLink = { this.relieveLink }
logout = { this.logout }
/>
:""}
2017-07-26 16:06:54 +08:00
</div>
2017-08-08 20:27:18 +08:00
</div>
</Header>
2017-07-07 17:26:31 +08:00
)
}
2017-07-27 14:35:27 +08:00
}