Header.vue 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442
  1. <!--
  2. * @Author: zouwenying
  3. * @Date: 2020-10-26 10:32:32
  4. * @LastEditTime: 2021-04-30 14:34:19
  5. * @LastEditors: zwy
  6. * @Description: In User Settings Edit
  7. * @FilePath: \UMIS\src\components\others\Header.vue
  8. -->
  9. <template>
  10. <div class="header" :class="[collapse? 'collapse':'expand']">
  11. <div class="el-fl-left"><v-tags></v-tags></div>
  12. <div class="header-right">
  13. <div class="header-user-con">
  14. <div>欢迎你,{{ cruUserInfo.username }}</div>
  15. <!-- 用户头像 -->
  16. <div class="user-avator" @click="editUserInfo">
  17. <i class="iconfont" v-show="!cruUserInfo.photo" v-bottom-txt-tip data-txt="编辑用户信息">&#xe6e8;</i>
  18. <img v-show="cruUserInfo.photo" :src="envConfig.baseImgApi + cruUserInfo.photo" />
  19. </div>
  20. <!-- <span style="width: 1px; height: 16px; background: #FFFFFF; opacity: 0.5"></span> -->
  21. <div class="message">
  22. <span class="main" @click="goMessage('unread')">
  23. <!-- <i class="iconfont btn">&#xe6ea;</i> -->
  24. <img :src='"@assets/img/btn_news.png"' alt="">
  25. <span class="iconfont point" v-if="messageNumber"></span>
  26. </span>
  27. <div class="msg-content" v-if="messageList&&messageList.length">
  28. <p>消息通知</p>
  29. <ul>
  30. <li v-for="(items, index) in messageList" :key="index" @click="toUrl(items.typeId)" style="cursor: pointer;">
  31. <img :src='"@/assets/img/icon_msg"+(items.typeId)+".png"' style="width: 30px; height: 30px" />
  32. <div>
  33. <span class="cont">【{{items.shortName}}】{{items.messageContent}}</span>
  34. <span class="time">{{items.dateCreate}}</span>
  35. </div>
  36. </li>
  37. </ul>
  38. <el-button type="text" class="more" @click="goMessage('more')">查看更多</el-button>
  39. </div>
  40. <div class="msg-content" v-else>
  41. <p>消息通知</p>
  42. <ul>
  43. <li style="text-align: center;display: block;padding: 0 20px;line-height: 60px;">
  44. 暂无消息
  45. </li>
  46. </ul>
  47. <el-button type="text" class="more" disabled>查看更多</el-button>
  48. </div>
  49. </div>
  50. <!-- <span style="width: 1px; height: 16px; background: #FFFFFF; opacity: 0.5"></span> -->
  51. <span class="border"></span>
  52. <div class="logout" @click="logOut">
  53. <el-tooltip placement="bottom" content="退出">
  54. <img src="@/assets/img/btn_quit.png" class="tab_head-right" @click="logOut" />
  55. </el-tooltip>
  56. <!-- <i class="iconfont" v-bottom-txt-tip data-txt="退出">&#xe6e9;</i> -->
  57. </div>
  58. </div>
  59. </div>
  60. </div>
  61. </template>
  62. <script>
  63. import userInfoVue from '../../views/system/users/userInfo.vue';
  64. import { mapState, mapGetters, mapMutations, mapActions } from 'vuex';
  65. import envConfig from '@/config';
  66. import vTags from './Tags.vue';
  67. export default {
  68. data() {
  69. return {
  70. name: '',
  71. envConfig: envConfig,
  72. imgList:[
  73. require("@/assets/img/icon_msg1.png"),
  74. require("@/assets/img/icon_msg2.png"),
  75. require("@/assets/img/icon_msg3.png"),
  76. require("@/assets/img/icon_msg4.png"),
  77. require("@/assets/img/icon_msg5.png"),
  78. require("@/assets/img/icon_msg6.png"),
  79. require("@/assets/img/icon_msg7.png"),
  80. require("@/assets/img/icon_msg8.png"),
  81. require("@/assets/img/icon_msg9.png")
  82. ]
  83. };
  84. },
  85. components: {
  86. vTags
  87. },
  88. computed: {
  89. cruUserInfo() {
  90. return this.$store.getters['getCruUserInfo'];
  91. },
  92. collapse() {
  93. return this.$store.getters['getCollapse'];
  94. },
  95. ...mapState(['messageNumber']),
  96. ...mapState(['messageList'])
  97. },
  98. methods: {
  99. toUrl(type){
  100. let url,appId;
  101. if(type==1||type==2){
  102. url=this.envConfig.warnDetailUrl;
  103. appId="1002";
  104. }else if(type==4){
  105. url=this.envConfig.taskManagementUrl;
  106. appId="1003";
  107. }else if(type==3||type==7){
  108. url=this.envConfig.eventHiddenDangerUrl;
  109. appId="1003";
  110. }else if(type==9){
  111. // url=this.envConfig.eventHiddenDangerUrl;
  112. appId="1012";
  113. this.$router.push({
  114. path: '/msg',
  115. });
  116. return;
  117. }
  118. this.$http.postForm('/sc-uims-message/message/judgingTheMessageJumpPermission',{
  119. url:url.split('/')[url.split('/').length - 1],
  120. appId:appId
  121. }).then(({ status, data, msg })=>{
  122. if(status==0&&data==0){
  123. window.open(url);
  124. }else{
  125. this.$message.error("您没有该系统权限")
  126. }
  127. })
  128. },
  129. // 退出登录
  130. logOut() {
  131. var access_token = localStorage.getItem('SC_token');
  132. this.$http.postForm('/sc-user-auth/user/logout', { access_token: access_token }).then(({ status, data, msg }) => {
  133. if (0 === status) {
  134. this.$message({
  135. type: 'success',
  136. message: '您已退出登录'
  137. });
  138. localStorage.removeItem('SC_token');
  139. this.$store.commit('setloginInfo', '');
  140. this.$store.dispatch("tags", []);
  141. sessionStorage.removeItem("tabs");
  142. location.href = this.envConfig.loginUrl;
  143. } else {
  144. this.$message.error(msg);
  145. }
  146. });
  147. },
  148. getUserInfo(resolve) {
  149. this.$http.postForm('/user/findLoginUserById').then(({ status, data, msg }) => {
  150. if (status === 0) {
  151. this.$store.commit('setcCruUserInfo', data);
  152. } else {
  153. this.$message.error('获取用户信息失败');
  154. }
  155. resolve && resolve(true);
  156. });
  157. },
  158. editUserInfo() {
  159. new Promise((resolve) => {
  160. this.$store.dispatch('openModal', {
  161. url: '/system/users/popups/edituser.vue',
  162. width: '500px',
  163. height: '500px',
  164. props: {
  165. data: JSON.parse(JSON.stringify(this.cruUserInfo)),
  166. callback: resolve
  167. },
  168. title: '编辑用户信息'
  169. });
  170. }).then(() => {
  171. this.getUserInfo();
  172. });
  173. },
  174. goMessage(msg) {
  175. let messageStatus = 2;//全部
  176. if (msg == 'unread') {
  177. messageStatus = 0;//未读
  178. }
  179. this.$store.commit('setmessageStatus',messageStatus);
  180. this.$router.push({
  181. path: '/msg',
  182. });
  183. },
  184. //获取最新消息列表
  185. getMessageList(){
  186. this.$http.postForm('/sc-uims-message/message/queryLastMessage',{num:'5'}).then(({ status, data, msg }) => {
  187. if (status === 0) {
  188. this.$store.commit('setmessageList', data);
  189. }
  190. });
  191. },
  192. //查询未读消息数量
  193. getUnreadNumber(){
  194. this.$http.postForm('/sc-uims-message/message/queryUnreadMessageStatic').then(({ status, data, msg }) => {
  195. if (status === 0) {
  196. data.map((item,index)=>{
  197. if(item.cn){
  198. this.$store.commit('setmessageNumber', true);
  199. }
  200. })
  201. }
  202. });
  203. },
  204. //初始化websoket
  205. initWebSocket(id) {
  206. if ('WebSocket' in window) {
  207. var serviceIp = this.envConfig.websoketUrl;
  208. this.websocket = new WebSocket('ws://' + serviceIp + '/sc-uims-message/webSocket/' + id);
  209. }else {
  210. console.log('当前浏览器 Not support websocket');
  211. }
  212. let that = this;
  213. this.interval = window.setInterval(function () {
  214. //每隔30秒钟发送一次心跳,避免websocket连接因超时而自动断开
  215. if (that.websocket != null) {
  216. // that.websocket.send('HeartBeat');
  217. // console.log('发送心跳包:HeartBeat');
  218. }
  219. }, 30000);
  220. //连接发生错误的回调方法
  221. this.websocket.onerror = function (ev) {
  222. console.log('WebSocket连接发生错误');
  223. };
  224. //连接成功建立的回调方法
  225. this.websocket.onopen = function (ev) {
  226. console.log('WebSocket连接成功');
  227. // this.send('addsocket');
  228. };
  229. //接收到消息的回调方法
  230. this.websocket.onmessage = function (event) {
  231. let msg=JSON.parse(event.data);
  232. let i=msg.typeId-1;
  233. if(msg.userId){
  234. that.$notify({
  235. dangerouslyUseHTMLString: true,
  236. showClose: true,
  237. customClass:'notice_icon',
  238. offset: 60,
  239. duration:3000,
  240. message: "<div class='notice'><img src="+that.imgList[i]+" class='img'/><div class='notice-content'><span class='title'>新消息通知</span><span class='nowrap'>"+"【"+msg.type+"】"+msg.content+"</span></div></div>"
  241. });
  242. that.getMessageList();
  243. that.getUnreadNumber();
  244. }
  245. };
  246. //连接关闭的回调方法
  247. this.websocket.onclose = function (ev) {
  248. console.log('WebSocket连接关闭');
  249. this.websocket = null;
  250. };
  251. },
  252. send(message) {
  253. if (this.websocket && this.websocket != null) {
  254. // this.websocket.send(message);
  255. console.log('发送的消息:' + message);
  256. }
  257. },
  258. //关闭WebSocket连接
  259. closeWebSocket() {
  260. if (this.websocket != null) {
  261. this.websocket.close();
  262. }
  263. if (this.interval) {
  264. window.clearInterval(this.interval);
  265. }
  266. }
  267. },
  268. mounted() {
  269. let vm = this;
  270. window.onbeforeunload = function () {
  271. vm.closeWebSocket();
  272. };
  273. },
  274. destoryed() {
  275. this.closeWebSocket();
  276. },
  277. created() {
  278. new Promise((resolve) => {
  279. this.getUserInfo(resolve);
  280. }).then((_) => {
  281. let id = this.$store.state.cruUserInfo.id;
  282. this.initWebSocket(id);
  283. this.getMessageList();
  284. this.getUnreadNumber();
  285. });
  286. }
  287. };
  288. </script>
  289. <style lang='scss' scoped>
  290. .border{
  291. width:1px;
  292. height: 10px;
  293. background: #FFFFFF;
  294. opacity: 0.3
  295. }
  296. .header {
  297. height: 46px;
  298. font-size: 14px;
  299. color: #fff;
  300. background: #2c354a;
  301. display: flex;
  302. align-items: center;
  303. justify-content: space-between;
  304. &.expand::before{
  305. content: "";
  306. display: inline-block;
  307. width:230px;
  308. height:46px;
  309. background: #2c354a;
  310. position: absolute;
  311. left:0;
  312. }
  313. &.collapse::before{
  314. content: "";
  315. display: inline-block;
  316. width:95px;
  317. height:46px;
  318. background: #2c354a;
  319. position: absolute;
  320. left:0;
  321. }
  322. }
  323. .header-right {
  324. padding-right: 20px;
  325. color:rgba(255, 255, 255, 0.6);
  326. .name-txt{
  327. font-size: 12px;
  328. font-family: PingFangSC-Regular, PingFang SC;
  329. font-weight: 400;
  330. color: #FFFFFF;
  331. }
  332. }
  333. .header-user-con {
  334. display: flex;
  335. height: 60px;
  336. align-items: center;
  337. }
  338. .logout {
  339. margin-left: 20px;
  340. img{
  341. width:20px;
  342. height: 20px;
  343. }
  344. }
  345. .iconfont{
  346. font-size: 20px;
  347. color:rgba(255, 255, 255, 0.6);
  348. &:hover{
  349. color:rgba(255, 255, 255, 1);;
  350. }
  351. }
  352. .user-avator {
  353. margin-left: 10px;
  354. margin-right: 20px;
  355. }
  356. .user-avator img {
  357. display: block;
  358. width: 20px;
  359. height: 20px;
  360. }
  361. .message {
  362. margin: 10px 20px 0px 0px;
  363. color: #000000;
  364. cursor: pointer;
  365. &:hover {
  366. .msg-content {
  367. display: block;
  368. background: #FFFFFF;
  369. box-shadow: 0px 5px 20px 0px rgba(144, 144, 144, 0.4);
  370. border-radius: 6px;
  371. z-index:200;
  372. }
  373. }
  374. .main {
  375. height: 60px;
  376. line-height: 60px;
  377. position: relative;
  378. img{
  379. width:20px;
  380. height: 20px;
  381. }
  382. .point {
  383. position: absolute;
  384. top: -4px;
  385. right: -1px;
  386. width: 4px;
  387. height: 4px;
  388. background: #FE7271;
  389. border-radius: 50%;
  390. display: block;
  391. }
  392. }
  393. .msg-content {
  394. position: absolute;
  395. width: 380px;
  396. background: #ffffff;
  397. z-index: 20;
  398. right: 20px;
  399. top: 40px;
  400. transition: all 0.3s;
  401. display: none;
  402. p {
  403. height: 40px;
  404. line-height: 40px;
  405. padding-left: 20px;
  406. color: #212226;
  407. }
  408. li {
  409. display: flex;
  410. align-items: center;
  411. height: 60px;
  412. padding-left: 20px;
  413. border-top: 1px solid #F6F6F6;
  414. font-size: 12px;
  415. cursor: auto;
  416. span {
  417. width: 300px;
  418. overflow: hidden; //超出的文本隐藏
  419. text-overflow: ellipsis; //溢出用省略号显示
  420. white-space: nowrap; //溢出不换行
  421. display: block;
  422. line-height: 20px;
  423. padding-left: 15px;
  424. }
  425. .cont {
  426. color: #424656;
  427. text-indent: -0.4em
  428. }
  429. .time {
  430. color: #7D7F87;
  431. }
  432. }
  433. .more {
  434. width:100%;
  435. }
  436. }
  437. }
  438. </style>