Header.js 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  1. import React, { PureComponent,Fragment } from 'react';
  2. import { formatMessage } from 'umi/locale';
  3. import { Layout, message } from 'antd';
  4. import Animate from 'rc-animate';
  5. import { connect } from 'dva';
  6. import router from 'umi/router';
  7. import GlobalHeader from '@/components/GlobalHeader';
  8. import TopNavHeader from '@/components/TopNavHeader';
  9. import styles from './Header.less';
  10. import SldModal from "@/components/SldModal/SldModal";
  11. import {
  12. failTip ,
  13. sucTip , loginOut
  14. } from '@/utils/utils';
  15. const { Header } = Layout;
  16. class HeaderView extends PureComponent {
  17. state = {
  18. modalVisible: false,
  19. submiting: false,
  20. visible: true,
  21. addData: [{
  22. type : 'input' ,
  23. label : '旧密码' ,
  24. input_type : 'password' ,
  25. name : 'oldPassword' ,
  26. placeholder : '请输入旧密码' ,
  27. initialValue : '' ,
  28. rules : [{
  29. required : true ,
  30. message : '请输入旧密码' ,
  31. }] ,
  32. },{
  33. type : 'input' ,
  34. label : '新密码' ,
  35. input_type : 'password' ,
  36. name : 'newPassword' ,
  37. placeholder : '请输入新密码' ,
  38. initialValue : '' ,
  39. rules : [{
  40. required : true ,
  41. message : '请输入新密码' ,
  42. }] ,
  43. },{
  44. type : 'input' ,
  45. label : '新密码确认' ,
  46. input_type : 'password' ,
  47. name : 'newPasswordCfm' ,
  48. placeholder : '请再次输入新密码' ,
  49. initialValue : '' ,
  50. rules : [{
  51. required : true ,
  52. message : '请再次输入新密码' ,
  53. }] ,
  54. },
  55. ],
  56. };
  57. static getDerivedStateFromProps(props, state) {
  58. if (!props.autoHideHeader && !state.visible) {
  59. return {
  60. visible: true,
  61. };
  62. }
  63. return null;
  64. }
  65. componentDidMount() {
  66. document.addEventListener('scroll', this.handScroll, { passive: true });
  67. }
  68. componentWillUnmount() {
  69. document.removeEventListener('scroll', this.handScroll);
  70. }
  71. getHeadWidth = () => {
  72. const { isMobile, collapsed, setting } = this.props;
  73. const { fixedHeader, layout } = setting;
  74. if (isMobile || !fixedHeader || layout === 'topmenu') {
  75. return '100%';
  76. }
  77. return collapsed ? 'calc(100%)' : 'calc(100%)';
  78. };
  79. handleNoticeClear = type => {
  80. message.success(
  81. `${formatMessage({ id: 'component.noticeIcon.cleared' })} ${formatMessage({
  82. id: `component.globalHeader.${type}`,
  83. })}`
  84. );
  85. const { dispatch } = this.props;
  86. dispatch({
  87. type: 'global/clearNotices',
  88. payload: type,
  89. });
  90. };
  91. handleMenuClick = ({ key }) => {
  92. const { dispatch } = this.props;
  93. if (key === 'userCenter') {
  94. router.push('/account/center');
  95. return;
  96. }
  97. if (key === 'triggerError') {
  98. router.push('/exception/trigger');
  99. return;
  100. }
  101. //修改密码
  102. if (key === 'userinfo') {
  103. this.setState({modalVisible:true});
  104. return;
  105. }
  106. if (key === 'logout') {
  107. loginOut();
  108. }
  109. };
  110. handleNoticeVisibleChange = visible => {
  111. if (visible) {
  112. const { dispatch } = this.props;
  113. dispatch({
  114. type: 'global/fetchNotices',
  115. });
  116. }
  117. };
  118. handScroll = () => {
  119. const { autoHideHeader } = this.props;
  120. const { visible } = this.state;
  121. if (!autoHideHeader) {
  122. return;
  123. }
  124. const scrollTop = document.body.scrollTop + document.documentElement.scrollTop;
  125. if (!this.ticking) {
  126. this.ticking = true;
  127. requestAnimationFrame(() => {
  128. if (this.oldScrollTop > scrollTop) {
  129. this.setState({
  130. visible: true,
  131. });
  132. } else if (scrollTop > 300 && visible) {
  133. this.setState({
  134. visible: false,
  135. });
  136. } else if (scrollTop < 300 && !visible) {
  137. this.setState({
  138. visible: true,
  139. });
  140. }
  141. this.oldScrollTop = scrollTop;
  142. this.ticking = false;
  143. });
  144. }
  145. };
  146. sldHandleConfirm = (val) => {
  147. this.setState({submiting: true});
  148. const { dispatch } = this.props;
  149. if(val.newPassword != val.newPasswordCfm ){
  150. failTip('两次密码不一致,请重新输入');
  151. this.setState({submiting: false});
  152. return false;
  153. }
  154. dispatch({
  155. type: 'global/change_manager_pwd',
  156. payload:val,
  157. callback: (res) => {
  158. if(res.state==200){
  159. sucTip(res.msg);
  160. this.setState({modalVisible: false})
  161. //跳到登录界面
  162. router.replace('/user/login');
  163. }else{
  164. failTip(res.msg);
  165. }
  166. this.setState({submiting: false});
  167. }
  168. });
  169. }
  170. sldHandleCancle = () => {
  171. this.setState({modalVisible:false});
  172. }
  173. render() {
  174. const { isMobile, handleMenuCollapse, setting} = this.props;
  175. const { navTheme, layout, fixedHeader } = setting;
  176. const { visible,addData,submiting,modalVisible } = this.state;
  177. const isTop = layout === 'topmenu';
  178. const width = this.getHeadWidth();
  179. const HeaderDom = visible ? (
  180. <Header style={{ padding: 0, width }} className={fixedHeader ? styles.fixedHeader : ''}>
  181. {isTop && !isMobile ? (
  182. <TopNavHeader
  183. theme={navTheme}
  184. mode="horizontal"
  185. onCollapse={handleMenuCollapse}
  186. onNoticeClear={this.handleNoticeClear}
  187. onMenuClick={this.handleMenuClick}
  188. onNoticeVisibleChange={this.handleNoticeVisibleChange}
  189. {...this.props}
  190. />
  191. ) : (
  192. <GlobalHeader
  193. onCollapse={handleMenuCollapse}
  194. onNoticeClear={this.handleNoticeClear}
  195. onMenuClick={this.handleMenuClick}
  196. onNoticeVisibleChange={this.handleNoticeVisibleChange}
  197. {...this.props}
  198. />
  199. )}
  200. </Header>
  201. ) : null;
  202. const formItemLayoutModal = {
  203. labelCol: {
  204. span: 6,
  205. },
  206. wrapperCol: {
  207. span: 14,
  208. },
  209. };
  210. return (
  211. <Fragment>
  212. <Animate component="" transitionName="fade">
  213. {HeaderDom}
  214. </Animate>
  215. {/*修改密码对话框-start*/}
  216. <SldModal
  217. title={'修改密码'}
  218. submiting={submiting}
  219. width={550}
  220. modalVisible={modalVisible}
  221. sldHandleConfirm={(val) => this.sldHandleConfirm(val)}
  222. sldHandleCancle={this.sldHandleCancle}
  223. formItemLayoutModal={formItemLayoutModal}
  224. content={addData}
  225. />
  226. {/*修改密码对话框-end*/}
  227. </Fragment>
  228. );
  229. }
  230. }
  231. export default connect(({ user, global, setting, loading }) => ({
  232. currentUser: user.currentUser,
  233. collapsed: global.collapsed,
  234. fetchingNotices: loading.effects['global/fetchNotices'],
  235. notices: global.notices,
  236. setting,
  237. }))(HeaderView);