Header.vue 15 KB

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