leftMenu.jsx 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  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. hashFun(){
  66. if (window.location.hash) {
  67. let current = window.location.hash.substr(2),
  68. open = [];
  69. console.log(current)
  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. }
  83. });
  84. }
  85. });
  86. this.setState({
  87. openKeys: open,
  88. current
  89. });
  90. }
  91. } else {
  92. window.location.hash = 'normal';
  93. this.setState({
  94. openKeys: [ 'sub1' ],
  95. current: 'normal'
  96. });
  97. }
  98. },
  99. componentWillMount() {
  100. this.loadData();
  101. this.hashFun()
  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.url != 'unit') {
  123. menuList.push(item);
  124. }
  125. } else if (this.state.type == '1') {
  126. if (item.url != 'personal') {
  127. menuList.push(item);
  128. }
  129. }
  130. });
  131. if (this.state.type == '0' || this.state.type == '1') {
  132. menu[1].children = menuList;
  133. newMenu = menu.concat(demandList);
  134. }else{
  135. newMenu=menuInit
  136. }
  137. return (
  138. <div>
  139. <div className="acc-index">
  140. {!this.state.switch && <img src={logo} alt="" />}
  141. {!this.state.switch && (
  142. <span>
  143. <a href={globalConfig.context + '/user/account/index.html'}>会员中心管理</a>
  144. </span>
  145. )}
  146. <Icon type={this.state.switch ? 'forward' : 'backward'} className="icon" onClick={this.iconFun} />
  147. </div>
  148. <Menu
  149. onClick={this.menuClick}
  150. defaultOpenKeys={this.state.openKeys}
  151. selectedKeys={[ this.state.current ]}
  152. openKeys={this.state.openKeys}
  153. onOpenChange={this.onOpenChange}
  154. mode={this.state.switch ? 'vertical' : 'inline'}
  155. theme="light"
  156. className="account-left"
  157. >
  158. {newMenu.map((subMenu) => {
  159. if (subMenu.children && subMenu.children.length) {
  160. return (
  161. <SubMenu
  162. key={subMenu.url}
  163. title={
  164. <span>
  165. <Icon type={subMenu.icon} />
  166. {!this.state.switch && <span>{subMenu.name}</span>}
  167. </span>
  168. }
  169. >
  170. {subMenu.children.map((menu,index) => <Menu.Item key={menu.url}>{menu.name}</Menu.Item>)}
  171. </SubMenu>
  172. );
  173. }
  174. return (
  175. <Menu.Item key={subMenu.url}>
  176. <Link to={`/${subMenu.url}`}>
  177. <Icon type={subMenu.icon} />
  178. <span className="nav-text">{subMenu.name}</span>
  179. </Link>
  180. </Menu.Item>
  181. );
  182. })}
  183. </Menu>
  184. </div>
  185. );
  186. }
  187. });
  188. export default LeftTab;