leftMenu.jsx 4.3 KB

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