mock平台

Header.js 8.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327
  1. import './Header.scss';
  2. import React, { PureComponent as Component } from 'react';
  3. import PropTypes from 'prop-types';
  4. import { connect } from 'react-redux';
  5. import { Link } from 'react-router-dom';
  6. import { Icon, Layout, Menu, Dropdown, message, Tooltip, Popover, Tag } from 'antd';
  7. import { checkLoginState, logoutActions, loginTypeAction } from '../../reducer/modules/user';
  8. import { changeMenuItem } from '../../reducer/modules/menu';
  9. import { withRouter } from 'react-router';
  10. import Srch from './Search/Search';
  11. const { Header } = Layout;
  12. import LogoSVG from '../LogoSVG/index.js';
  13. import Breadcrumb from '../Breadcrumb/Breadcrumb.js';
  14. import GuideBtns from '../GuideBtns/GuideBtns.js';
  15. const plugin = require('client/plugin.js');
  16. let HeaderMenu = {
  17. user: {
  18. path: '/user/profile',
  19. name: '个人中心',
  20. icon: 'user',
  21. adminFlag: false
  22. },
  23. solution: {
  24. path: '/user/list',
  25. name: '用户管理',
  26. icon: 'solution',
  27. adminFlag: true
  28. }
  29. };
  30. plugin.emitHook('header_menu', HeaderMenu);
  31. const MenuUser = props => (
  32. <Menu theme="dark" className="user-menu">
  33. {Object.keys(HeaderMenu).map(key => {
  34. let item = HeaderMenu[key];
  35. const isAdmin = props.role === 'admin';
  36. if (item.adminFlag && !isAdmin) {
  37. return null;
  38. }
  39. return (
  40. <Menu.Item key={key}>
  41. {item.name === '个人中心' ? (
  42. <Link to={item.path + `/${props.uid}`}>
  43. <Icon type={item.icon} />
  44. {item.name}
  45. </Link>
  46. ) : (
  47. <Link to={item.path}>
  48. <Icon type={item.icon} />
  49. {item.name}
  50. </Link>
  51. )}
  52. </Menu.Item>
  53. );
  54. })}
  55. <Menu.Item key="9">
  56. <a onClick={props.logout}>
  57. <Icon type="logout" />退出
  58. </a>
  59. </Menu.Item>
  60. </Menu>
  61. );
  62. const tipFollow = (
  63. <div className="title-container">
  64. <h3 className="title">
  65. <Icon type="star" /> 关注
  66. </h3>
  67. <p>这里是你的专属收藏夹,便于你找到自己的项目</p>
  68. </div>
  69. );
  70. const tipAdd = (
  71. <div className="title-container">
  72. <h3 className="title">
  73. <Icon type="plus-circle" /> 新建项目
  74. </h3>
  75. <p>在任何页面都可以快速新建项目</p>
  76. </div>
  77. );
  78. const tipDoc = (
  79. <div className="title-container">
  80. <h3 className="title">
  81. 使用文档 <Tag color="orange">推荐!</Tag>
  82. </h3>
  83. <p>
  84. 初次使用 YApi,强烈建议你阅读{' '}
  85. <a target="_blank" href="https://hellosean1025.github.io/yapi/" rel="noopener noreferrer">
  86. 使用文档
  87. </a>
  88. ,我们为你提供了通俗易懂的快速入门教程,更有详细的使用说明,欢迎阅读!{' '}
  89. </p>
  90. </div>
  91. );
  92. MenuUser.propTypes = {
  93. user: PropTypes.string,
  94. msg: PropTypes.string,
  95. role: PropTypes.string,
  96. uid: PropTypes.number,
  97. relieveLink: PropTypes.func,
  98. logout: PropTypes.func
  99. };
  100. const ToolUser = props => {
  101. let imageUrl = props.imageUrl ? props.imageUrl : `/api/user/avatar?uid=${props.uid}`;
  102. return (
  103. <ul>
  104. <li className="toolbar-li item-search">
  105. <Srch groupList={props.groupList} />
  106. </li>
  107. <Popover
  108. overlayClassName="popover-index"
  109. content={<GuideBtns />}
  110. title={tipFollow}
  111. placement="bottomRight"
  112. arrowPointAtCenter
  113. visible={props.studyTip === 1 && !props.study}
  114. >
  115. <Tooltip placement="bottom" title={'我的关注'}>
  116. <li className="toolbar-li">
  117. <Link to="/follow">
  118. <Icon className="dropdown-link" style={{ fontSize: 16 }} type="star" />
  119. </Link>
  120. </li>
  121. </Tooltip>
  122. </Popover>
  123. <Popover
  124. overlayClassName="popover-index"
  125. content={<GuideBtns />}
  126. title={tipAdd}
  127. placement="bottomRight"
  128. arrowPointAtCenter
  129. visible={props.studyTip === 2 && !props.study}
  130. >
  131. <Tooltip placement="bottom" title={'新建项目'}>
  132. <li className="toolbar-li">
  133. <Link to="/add-project">
  134. <Icon className="dropdown-link" style={{ fontSize: 16 }} type="plus-circle" />
  135. </Link>
  136. </li>
  137. </Tooltip>
  138. </Popover>
  139. <Popover
  140. overlayClassName="popover-index"
  141. content={<GuideBtns isLast={true} />}
  142. title={tipDoc}
  143. placement="bottomRight"
  144. arrowPointAtCenter
  145. visible={props.studyTip === 3 && !props.study}
  146. >
  147. <Tooltip placement="bottom" title={'使用文档'}>
  148. <li className="toolbar-li">
  149. <a target="_blank" href="https://hellosean1025.github.io/yapi" rel="noopener noreferrer">
  150. <Icon className="dropdown-link" style={{ fontSize: 16 }} type="question-circle" />
  151. </a>
  152. </li>
  153. </Tooltip>
  154. </Popover>
  155. <li className="toolbar-li">
  156. <Dropdown
  157. placement="bottomRight"
  158. trigger={['click']}
  159. overlay={
  160. <MenuUser
  161. user={props.user}
  162. msg={props.msg}
  163. uid={props.uid}
  164. role={props.role}
  165. relieveLink={props.relieveLink}
  166. logout={props.logout}
  167. />
  168. }
  169. >
  170. <a className="dropdown-link">
  171. <span className="avatar-image">
  172. <img src={imageUrl} />
  173. </span>
  174. {/*props.imageUrl? <Avatar src={props.imageUrl} />: <Avatar src={`/api/user/avatar?uid=${props.uid}`} />*/}
  175. <span className="name">
  176. <Icon type="down" />
  177. </span>
  178. </a>
  179. </Dropdown>
  180. </li>
  181. </ul>
  182. );
  183. };
  184. ToolUser.propTypes = {
  185. user: PropTypes.string,
  186. msg: PropTypes.string,
  187. role: PropTypes.string,
  188. uid: PropTypes.number,
  189. relieveLink: PropTypes.func,
  190. logout: PropTypes.func,
  191. groupList: PropTypes.array,
  192. studyTip: PropTypes.number,
  193. study: PropTypes.bool,
  194. imageUrl: PropTypes.any
  195. };
  196. @connect(
  197. state => {
  198. return {
  199. user: state.user.userName,
  200. uid: state.user.uid,
  201. msg: null,
  202. role: state.user.role,
  203. login: state.user.isLogin,
  204. studyTip: state.user.studyTip,
  205. study: state.user.study,
  206. imageUrl: state.user.imageUrl
  207. };
  208. },
  209. {
  210. loginTypeAction,
  211. logoutActions,
  212. checkLoginState,
  213. changeMenuItem
  214. }
  215. )
  216. @withRouter
  217. export default class HeaderCom extends Component {
  218. constructor(props) {
  219. super(props);
  220. }
  221. static propTypes = {
  222. router: PropTypes.object,
  223. user: PropTypes.string,
  224. msg: PropTypes.string,
  225. uid: PropTypes.number,
  226. role: PropTypes.string,
  227. login: PropTypes.bool,
  228. relieveLink: PropTypes.func,
  229. logoutActions: PropTypes.func,
  230. checkLoginState: PropTypes.func,
  231. loginTypeAction: PropTypes.func,
  232. changeMenuItem: PropTypes.func,
  233. history: PropTypes.object,
  234. location: PropTypes.object,
  235. study: PropTypes.bool,
  236. studyTip: PropTypes.number,
  237. imageUrl: PropTypes.any
  238. };
  239. linkTo = e => {
  240. if (e.key != '/doc') {
  241. this.props.changeMenuItem(e.key);
  242. if (!this.props.login) {
  243. message.info('请先登录', 1);
  244. }
  245. }
  246. };
  247. relieveLink = () => {
  248. this.props.changeMenuItem('');
  249. };
  250. logout = e => {
  251. e.preventDefault();
  252. this.props
  253. .logoutActions()
  254. .then(res => {
  255. if (res.payload.data.errcode == 0) {
  256. this.props.history.push('/');
  257. this.props.changeMenuItem('/');
  258. message.success('退出成功! ');
  259. } else {
  260. message.error(res.payload.data.errmsg);
  261. }
  262. })
  263. .catch(err => {
  264. message.error(err);
  265. });
  266. };
  267. handleLogin = e => {
  268. e.preventDefault();
  269. this.props.loginTypeAction('1');
  270. };
  271. handleReg = e => {
  272. e.preventDefault();
  273. this.props.loginTypeAction('2');
  274. };
  275. checkLoginState = () => {
  276. this.props.checkLoginState
  277. .then(res => {
  278. if (res.payload.data.errcode !== 0) {
  279. this.props.history.push('/');
  280. }
  281. })
  282. .catch(err => {
  283. console.log(err);
  284. });
  285. };
  286. render() {
  287. const { login, user, msg, uid, role, studyTip, study, imageUrl } = this.props;
  288. return (
  289. <Header className="header-box m-header">
  290. <div className="content g-row">
  291. <Link onClick={this.relieveLink} to="/group" className="logo">
  292. <div className="href">
  293. <span className="img">
  294. <LogoSVG length="32px" />
  295. </span>
  296. </div>
  297. </Link>
  298. <Breadcrumb />
  299. <div
  300. className="user-toolbar"
  301. style={{ position: 'relative', zIndex: this.props.studyTip > 0 ? 3 : 1 }}
  302. >
  303. {login ? (
  304. <ToolUser
  305. {...{ studyTip, study, user, msg, uid, role, imageUrl }}
  306. relieveLink={this.relieveLink}
  307. logout={this.logout}
  308. />
  309. ) : (
  310. ''
  311. )}
  312. </div>
  313. </div>
  314. </Header>
  315. );
  316. }
  317. }