leftMenu.jsx 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  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. this.state.type=1;
  135. if (this.state.type == '0' || this.state.type == '1') {
  136. newMenu = menu.concat(demandList);
  137. }else{
  138. newMenu=menu;
  139. }
  140. return (
  141. <div>
  142. <div className="acc-index">
  143. {!this.state.switch && <img src={logo} alt="" />}
  144. {!this.state.switch && (
  145. <span>
  146. <a href={globalConfig.context + '/user/account/index.html'}>会员中心管理</a>
  147. </span>
  148. )}
  149. <Icon type={this.state.switch ? 'forward' : 'backward'} className="icon" onClick={this.iconFun} />
  150. </div>
  151. <Menu
  152. onClick={this.menuClick}
  153. defaultOpenKeys={this.state.openKeys}
  154. selectedKeys={[ this.state.current ]}
  155. openKeys={this.state.openKeys}
  156. onOpenChange={this.onOpenChange}
  157. mode={this.state.switch ? 'vertical' : 'inline'}
  158. theme="light"
  159. className="account-left"
  160. >
  161. {newMenu.map((subMenu) => {
  162. if (subMenu.children && subMenu.children.length) {
  163. return (
  164. <SubMenu
  165. key={subMenu.url}
  166. title={
  167. <span>
  168. <Icon type={subMenu.icon} />
  169. {!this.state.switch && <span>{subMenu.name}</span>}
  170. </span>
  171. }
  172. >
  173. {subMenu.children.map((menu) => <Menu.Item key={menu.url}>{menu.name}</Menu.Item>)}
  174. </SubMenu>
  175. );
  176. }
  177. return (
  178. <Menu.Item key={subMenu.url}>
  179. <Link to={`/${subMenu.url}`}>
  180. <Icon type={subMenu.icon} />
  181. <span className="nav-text">{subMenu.name}</span>
  182. </Link>
  183. </Menu.Item>
  184. );
  185. })}
  186. </Menu>
  187. </div>
  188. );
  189. }
  190. });
  191. export default LeftTab;