index.js 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  1. import { Layout, Dropdown, Menu, Modal, Input, Form, Space, Button } from 'antd';
  2. import { withRouter } from 'react-router'
  3. import { useState, useEffect } from 'react';
  4. import { useSelector, useDispatch } from 'react-redux';
  5. import MyMessage from "../MyMessage";
  6. import { panesNow } from '@reducers/tabPanes.js';
  7. import utils from '@utils/index';
  8. import logo from '@images/logo.png';
  9. import msg from '@images/msg.png';
  10. import me from '@images/me.png';
  11. import down from '@images/down.png';
  12. import './index.less'
  13. import { message } from "antd/lib/index";
  14. import { add, active } from '@reducers/tabPanes'
  15. import { setSys, setUser, setUnReadNum} from '@reducers/userInfo.js';
  16. import apiObj from '@api/index';
  17. const { post, api, xPost } = apiObj;
  18. const propTypes = {
  19. }
  20. const defaultProps = {};
  21. const { Header } = Layout;
  22. const { getTimeDetail } = utils;
  23. function AHeader({ history, hideName }) {
  24. const dispatch = useDispatch();//当前选中的菜单
  25. const { panes } = useSelector(state => {
  26. return state.tabPanes;
  27. });
  28. const [form] = Form.useForm();
  29. const [dateTime, setDateTime] = useState('');
  30. const [visible, setVisible] = useState(false);
  31. const { user, sysName,unReadNum } = useSelector((state) => {
  32. return state.userInfo;
  33. });
  34. //退出
  35. function loginOut() {
  36. localStorage.removeItem("token");
  37. localStorage.removeItem("systemId");
  38. localStorage.removeItem("hospitalId");
  39. localStorage.removeItem("software")
  40. dispatch(panesNow([]));
  41. history.push('/login');
  42. }
  43. //获取未读消息数量
  44. function getNotNoticeCount(){
  45. xPost(api.getNotNoticeCount).then((res) => {
  46. if (res.data.code === 200) {
  47. let count = res.data.count;
  48. count = count>99?'99+':count;
  49. dispatch(setUnReadNum(count));
  50. } else {
  51. //message.warning(res.data.msg || '请求失败');
  52. }
  53. })
  54. }
  55. //未读消息页面显示并带入未读筛选条件
  56. function showMyMsgPage(){
  57. const item = panes.find((it) => it.key === 'ZNTZ-WDTZ');
  58. if (item) { //已存在当前tab,则定位即可不增加
  59. dispatch(active({idName:'ZNTZ-WDTZ',isUnRead:true}));
  60. return;
  61. }
  62. dispatch(
  63. add({ title: '我的通知', content: <MyMessage />, key: 'ZNTZ-WDTZ&我的通知',isUnRead:true})
  64. )
  65. }
  66. //获取组织列表
  67. function getOrgList() {
  68. xPost(api.getUserHospitals).then((res) => {
  69. if (res.data.code === 200) {
  70. const data = res.data.data;
  71. const { userInfo, software } = data;
  72. const sysId = localStorage.getItem("systemId");
  73. const hisId = localStorage.getItem("hospitalId");
  74. const sys = software.find((it) => {
  75. return +it.id === +sysId
  76. });
  77. dispatch(setUser(userInfo));
  78. sys && dispatch(setSys({ sysId, sysName: sys.name, hisId }));
  79. } else {
  80. message.warning(res.data.msg || '获取医院列表失败');
  81. }
  82. })
  83. }
  84. //修改密码
  85. function changePsd() {
  86. setVisible(true)
  87. }
  88. //时间
  89. let interVal;
  90. function countTime() {
  91. interVal = setInterval(() => {
  92. setDateTime(getTimeDetail())
  93. }, 1000);
  94. }
  95. function cancel() {
  96. setVisible(false)
  97. form.resetFields();
  98. }
  99. const onFinish = values => {
  100. const params = values
  101. post(api.midifyPassword, params).then((res) => {
  102. if (res.data.code === 200) {
  103. message.success('修改成功');
  104. }
  105. })
  106. }
  107. useEffect(() => {
  108. countTime();
  109. getNotNoticeCount();
  110. if (!user.name) {
  111. getOrgList();
  112. }
  113. return function clearUp() {
  114. clearInterval(interVal)
  115. }
  116. }, []);
  117. const menu = (
  118. <Menu>
  119. <Menu.Item key="0">
  120. <span className='changePsd' onClick={changePsd}>修改密码</span>
  121. </Menu.Item>
  122. <Menu.Divider />
  123. <Menu.Item key="3" onClick={loginOut}>退出</Menu.Item>
  124. </Menu>
  125. );
  126. return (
  127. <Header className='page-header'>
  128. <img className='logo' src={logo} alt="" />
  129. {hideName ? '' : <><span className='break-line'>|</span>
  130. <span className='sys-name'>{sysName}</span></>}
  131. <div className='infos'>
  132. <span className='time'>{dateTime}</span>
  133. <span className='break-line'>|</span>
  134. <div className="unRead-msg-cont" onClick={showMyMsgPage}>
  135. <img className='msg-icon' src={msg} alt="未读消息"/>
  136. {unReadNum?<span className='unRead-msg'>{unReadNum}</span>:''}
  137. </div>
  138. <div className="user">
  139. <Dropdown overlay={menu} trigger={['click']}>
  140. <span className="ant-dropdown-link" onClick={e => e.preventDefault()}>
  141. <img className='user-icon' src={me} alt="用户头像" />
  142. <i className='user-name'>{user.name}</i>
  143. <img src={down} alt="" />
  144. </span>
  145. </Dropdown>
  146. </div>
  147. </div>
  148. {visible ?
  149. <Modal
  150. title='修改密码'
  151. okText='确定'
  152. cancelText='取消'
  153. width={'40%'}
  154. visible={visible}
  155. onCancel={cancel}
  156. footer={null}
  157. forceRender={true}
  158. centered={true}
  159. >
  160. <Form
  161. labelCol={{ span: 6 }}
  162. wrapperCol={{ span: 16 }}
  163. form={form}
  164. name="register"
  165. onFinish={onFinish}
  166. >
  167. <Form.Item
  168. name="password"
  169. label="原密码"
  170. required
  171. >
  172. <Input.Password placeholder="8-12位大小写字母、数字、特殊字符" />
  173. </Form.Item>
  174. <Form.Item
  175. name="modifyPassword"
  176. label="新密码"
  177. dependencies={['password']}
  178. rules={[
  179. {
  180. required: true,
  181. message: '8-12位大小写字母、数字、特殊字符',
  182. },
  183. ({ getFieldValue }) => ({
  184. validator(_, value) {
  185. const passwordReg = /^(?=.*?[a-z])(?=.*?[A-Z])(?=.*?\d)(?=.*?[#@*&.]).*$/;
  186. if (!value || getFieldValue('password') === value) {
  187. return Promise.reject(new Error('新密码不能于原密码相同'));
  188. }
  189. if (!passwordReg.test(value)) {
  190. return Promise.reject(new Error('密码必须同时包含大写字母、小写字母和数字'));
  191. }
  192. if (value.length < 8 || value.length > 12) {
  193. return Promise.reject(new Error('密码长度8-12位'));
  194. }
  195. },
  196. }),
  197. ]}
  198. >
  199. <Input.Password placeholder="8-12位大小写字母、数字、特殊字符" />
  200. </Form.Item>
  201. <Form.Item
  202. name="againnewpassword"
  203. label="确认新密码"
  204. dependencies={['password']}
  205. rules={[
  206. {
  207. required: true,
  208. message: '8-12位大小写字母、数字、特殊字符',
  209. },
  210. ({ getFieldValue }) => ({
  211. validator(_, value) {
  212. if (!value || getFieldValue('modifyPassword') === value) {
  213. return Promise.resolve();
  214. }
  215. return Promise.reject(new Error('两次密码不一致,请确认密码'));
  216. },
  217. }),
  218. ]}
  219. >
  220. <Input.Password placeholder="8-12位大小写字母、数字、特殊字符" />
  221. </Form.Item>
  222. <Form.Item wrapperCol={{ offset: 8, span: 16 }}>
  223. <Space size="middle">
  224. <Button htmlType="button" onClick={e => cancel()}>
  225. 取消
  226. </Button>
  227. <Button type="primary" htmlType="submit">
  228. 保存
  229. </Button>
  230. </Space>
  231. </Form.Item>
  232. </Form>
  233. </Modal>
  234. : ''}
  235. </Header>
  236. )
  237. }
  238. AHeader.propTypes = propTypes;
  239. AHeader.defaultProps = defaultProps;
  240. export default withRouter(AHeader);