leftMenu.jsx 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  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','sub5','sub6','sub7' ]
  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. if (!current.length) {
  70. window.location.hash = 'normal';
  71. this.setState({
  72. openKeys: [ 'sub1' ],
  73. current: 'normal'
  74. });
  75. } else {
  76. menu.map((item) => {
  77. if (item.children) {
  78. item.children.map((atem) => {
  79. if (current == atem.url) {
  80. open.push(item.url);
  81. }
  82. });
  83. }
  84. });
  85. this.setState({
  86. openKeys: open,
  87. current
  88. });
  89. }
  90. } else {
  91. window.location.hash = 'normal';
  92. this.setState({
  93. openKeys: [ 'sub1' ],
  94. current: 'normal'
  95. });
  96. }
  97. },
  98. componentWillMount() {
  99. this.loadData();
  100. this.hashFun()
  101. },
  102. render() {
  103. let menuList = [],newMenu=[];
  104. let menuNO = menu[1].children;
  105. menuNO.map((item) => {
  106. if (this.state.type == '0') {
  107. if (item.url != 'unit') {
  108. menuList.push(item);
  109. }
  110. } else if (this.state.type == '1') {
  111. if (item.url != 'personal'&&item.url!='expert') {
  112. menuList.push(item);
  113. }
  114. }
  115. });
  116. if (this.state.type == '0' || this.state.type == '1') {
  117. menu[1].children = menuList;
  118. newMenu=menu
  119. }else{
  120. newMenu=menuInit
  121. }
  122. return (
  123. <div>
  124. <div className="acc-index">
  125. {!this.state.switch && <img src={logo} alt="" />}
  126. {!this.state.switch && (
  127. <span>
  128. <a href={globalConfig.context + '/user/account/index.html'}>会员中心管理</a>
  129. </span>
  130. )}
  131. <Icon type={this.state.switch ? 'forward' : 'backward'} className="icon" onClick={this.iconFun} />
  132. </div>
  133. <Menu
  134. onClick={this.menuClick}
  135. defaultOpenKeys={this.state.openKeys}
  136. selectedKeys={[ this.state.current ]}
  137. openKeys={this.state.openKeys}
  138. onOpenChange={this.onOpenChange}
  139. mode={this.state.switch ? 'vertical' : 'inline'}
  140. theme="light"
  141. className="account-left"
  142. >
  143. {newMenu.map((subMenu) => {
  144. if (subMenu.children && subMenu.children.length) {
  145. return (
  146. <SubMenu
  147. key={subMenu.url}
  148. title={
  149. <span>
  150. <Icon type={subMenu.icon} />
  151. {!this.state.switch && <span>{subMenu.name}</span>}
  152. </span>
  153. }
  154. >
  155. {subMenu.children.map((menu,index) => <Menu.Item key={menu.url}>{menu.name}</Menu.Item>)}
  156. </SubMenu>
  157. );
  158. }
  159. return (
  160. <Menu.Item key={subMenu.url}>
  161. <Link to={`/${subMenu.url}`}>
  162. <Icon type={subMenu.icon} />
  163. <span className="nav-text">{subMenu.name}</span>
  164. </Link>
  165. </Menu.Item>
  166. );
  167. })}
  168. </Menu>
  169. </div>
  170. );
  171. }
  172. });
  173. export default LeftTab;