index.js 11 KB

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