yapi/client/components/Header/Header.js

201 lines
5.4 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-11 17:49:47 +08:00
import { Icon, Layout, Menu, Dropdown, message, Tooltip, Avatar } 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-09-14 18:19:13 +08:00
import { logoSVG } from '../../common.js';
2017-08-28 17:24:17 +08:00
import Breadcrumb from '../Breadcrumb/Breadcrumb.js'
const MenuUser = (props) => (
2017-09-14 18:19:13 +08:00
<Menu theme="dark" className="user-menu" >
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-09-14 18:19:13 +08:00
<Link to="/follow">
<Tooltip placement="bottom" title={'我的关注'}>
<li className="toolbar-li">
<Icon className="dropdown-link" style={{ fontSize: 16 }} type="star" />
</li>
</Tooltip>
</Link>
2017-08-10 18:26:35 +08:00
<Link to="/add-project">
2017-08-08 20:27:18 +08:00
<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">
2017-09-12 15:12:13 +08:00
<a target="_blank" href="https://ued.qunar.com/yapi/" rel="noopener noreferrer"><Icon className="dropdown-link" style={{ fontSize: 16 }} type="question-circle" /></a>
2017-08-08 20:27:18 +08:00
</li>
</Tooltip>
<li className="toolbar-li">
2017-09-14 18:19:13 +08:00
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-11 17:49:47 +08:00
<Avatar src={`/api/user/avatar?uid=${props.uid}`} />
{/*<img style={{width:24,height:24}} src={`/api/user/avatar?uid=${props.uid}`} />*/}
2017-09-14 18:19:13 +08:00
{/*<span className="name">{props.user}</span>*/}
<span className="name"><Icon type="down-circle-o" /></span>
</a>
</Dropdown>
2017-09-14 18:19:13 +08:00
</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-08-10 21:20:23 +08:00
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-11 14:22:20 +08:00
<Header className="header-box m-header">
2017-08-08 20:27:18 +08:00
<div className="content g-row">
<div className="logo">
2017-08-15 12:08:59 +08:00
<Link to="/group" onClick={this.relieveLink} className="href">
2017-09-14 18:19:13 +08:00
<span className="img">{logoSVG('32px')}</span>
{/*<span className="logo-name">YApi<span className="ui-badge">{betaSVG}</span></span>*/}
2017-08-08 20:27:18 +08:00
</Link>
</div>
2017-08-28 17:24:17 +08:00
<Breadcrumb />
2017-08-08 20:27:18 +08:00
<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
}