leftMenu.jsx 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. import React from 'react';
  2. import { Menu, Icon, Layout, message } from 'antd';
  3. import '@/account/leftTab.less';
  4. const SubMenu = Menu.SubMenu;
  5. const MenuItemGroup = Menu.ItemGroup;
  6. import { Link } from 'react-router';
  7. import ajax from 'jquery/src/ajax/xhr.js';
  8. import $ from 'jquery/src/ajax';
  9. import { menu } from '@/account/menu';
  10. import logo from 'img/acc-logo.png';
  11. const { Sider } = Layout;
  12. const LeftTab = React.createClass({
  13. getInitialState() {
  14. return {
  15. openKeys: [ 'sub1' ],
  16. current: 'normal',
  17. switch: false,
  18. rootSubmenuKeys: [ 'sub1', 'sub2', 'sub3', 'sub4' ]
  19. };
  20. },
  21. loadData() {
  22. $.ajax({
  23. method: 'get',
  24. dataType: 'json',
  25. url: globalConfig.context + '/api/user/homePage',
  26. data: {}
  27. }).done(
  28. function(data) {
  29. if (!data.error.length) {
  30. this.setState({
  31. type: data.data.type
  32. });
  33. } else {
  34. message.warning(data.error[0].message);
  35. }
  36. }.bind(this)
  37. );
  38. },
  39. menuClick(e) {
  40. this.setState({
  41. current: e.key
  42. });
  43. window.location.hash = e.key;
  44. },
  45. iconFun() {
  46. let menuW = document.querySelector('.ant-layout-sider');
  47. if (this.state.switch) {
  48. menuW.style.width = '200px';
  49. menuW.style.flex = '0 0 200px';
  50. } else {
  51. menuW.style.width = '50px';
  52. menuW.style.flex = '0 0 50px';
  53. }
  54. this.setState({
  55. switch: !this.state.switch
  56. });
  57. },
  58. onOpenChange(openKeys) {
  59. const latestOpenKey = openKeys.find((key) => this.state.openKeys.indexOf(key) === -1);
  60. if (this.state.rootSubmenuKeys.indexOf(latestOpenKey) === -1) {
  61. this.setState({ openKeys });
  62. } else {
  63. this.setState({
  64. openKeys: latestOpenKey ? [ latestOpenKey ] : []
  65. });
  66. }
  67. },
  68. componentWillMount() {
  69. this.loadData();
  70. if (window.location.hash) {
  71. let current = window.location.hash.substr(2),
  72. open = [];
  73. if (!current.length) {
  74. window.location.hash = 'normal';
  75. this.setState({
  76. openKeys: [ 'sub1' ],
  77. current: 'normal'
  78. });
  79. } else {
  80. menu.map((item) => {
  81. if (item.children) {
  82. item.children.map((atem) => {
  83. if (current == atem.url) {
  84. open.push(item.url);
  85. return false;
  86. }
  87. });
  88. }
  89. });
  90. this.setState({
  91. openKeys: open,
  92. current
  93. });
  94. }
  95. } else {
  96. window.location.hash = 'normal';
  97. this.setState({
  98. openKeys: [ 'sub1' ],
  99. current: 'normal'
  100. });
  101. }
  102. },
  103. render() {
  104. let demandList = [
  105. {
  106. name: '我的需求',
  107. url: 'sub3',
  108. icon: 'bulb',
  109. children: [ { name: '科技需求列表', url: 'demand' } ]
  110. },
  111. {
  112. name: '我的成果',
  113. url: 'sub4',
  114. icon: 'compass',
  115. children: [ { name: '科技成果列表', url: 'achievement' } ]
  116. }
  117. ];
  118. let menuList = [],newMenu=[];
  119. let menuNO = menu[1].children;
  120. menuNO.map((item) => {
  121. if (this.state.type == '0') {
  122. if (item.name != '企业资料') {
  123. menuList.push(item);
  124. }
  125. } else if (this.state.type == '1') {
  126. if (item.name != '个人资料') {
  127. menuList.push(item);
  128. }
  129. } else {
  130. menuList.push(item);
  131. }
  132. });
  133. menu[1].children = menuList;
  134. if (this.state.type == '0' || this.state.type == '1') {
  135. newMenu = menu.concat(demandList);
  136. }else{
  137. newMenu=menu;
  138. }
  139. return (
  140. <div>
  141. <div className="acc-index">
  142. {!this.state.switch && <img src={logo} alt="" />}
  143. {!this.state.switch && (
  144. <span>
  145. <a href={globalConfig.context + '/user/account/index.html'}>会员中心管理</a>
  146. </span>
  147. )}
  148. <Icon type={this.state.switch ? 'forward' : 'backward'} className="icon" onClick={this.iconFun} />
  149. </div>
  150. <Menu
  151. onClick={this.menuClick}
  152. defaultOpenKeys={this.state.openKeys}
  153. selectedKeys={[ this.state.current ]}
  154. openKeys={this.state.openKeys}
  155. onOpenChange={this.onOpenChange}
  156. mode={this.state.switch ? 'vertical' : 'inline'}
  157. theme="light"
  158. className="account-left"
  159. >
  160. {newMenu.map((subMenu) => {
  161. if (subMenu.children && subMenu.children.length) {
  162. return (
  163. <SubMenu
  164. key={subMenu.url}
  165. title={
  166. <span>
  167. <Icon type={subMenu.icon} />
  168. {!this.state.switch && <span>{subMenu.name}</span>}
  169. </span>
  170. }
  171. >
  172. {subMenu.children.map((menu) => <Menu.Item key={menu.url}>{menu.name}</Menu.Item>)}
  173. </SubMenu>
  174. );
  175. }
  176. return (
  177. <Menu.Item key={subMenu.url}>
  178. <Link to={`/${subMenu.url}`}>
  179. <Icon type={subMenu.icon} />
  180. <span className="nav-text">{subMenu.name}</span>
  181. </Link>
  182. </Menu.Item>
  183. );
  184. })}
  185. </Menu>
  186. </div>
  187. );
  188. }
  189. });
  190. export default LeftTab;