Header.vue 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452
  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. </div>
  24. <div class="message">
  25. <span class="main" @click="goMessage('unread')">
  26. <img src="@/assets/img/btn_news.png" alt="" />
  27. <span class="iconfont point" v-if="messageNumber"></span>
  28. </span>
  29. <div class="msg-content" v-if="messageList && messageList.length">
  30. <p>消息通知</p>
  31. <ul>
  32. <li v-for="(items, index) in messageList" :key="index" @click="toUrl(items.typeId)" style="cursor: pointer">
  33. <img :src="imgList[items.typeId - 1]" style="width: 30px; height: 30px" />
  34. <div>
  35. <span class="cont">【{{ items.shortName }}】{{ items.messageContent }}</span>
  36. <span class="time">{{ items.dateCreate }}</span>
  37. </div>
  38. </li>
  39. </ul>
  40. <el-button type="text" class="more" @click="goMessage('more')">查看更多</el-button>
  41. </div>
  42. <div class="msg-content" v-else>
  43. <p>消息通知</p>
  44. <ul>
  45. <li style="text-align: center; display: block; padding: 0 20px; line-height: 60px">暂无消息</li>
  46. </ul>
  47. <el-button type="text" class="more" disabled>查看更多</el-button>
  48. </div>
  49. </div>
  50. <span class="border"></span>
  51. <div class="logout">
  52. <el-tooltip placement="bottom" content="退出">
  53. <img src="@/assets/img/btn_quit.png" class="tab_head-right" @click="logOut" />
  54. </el-tooltip>
  55. </div>
  56. </div>
  57. </div>
  58. </div>
  59. </template>
  60. <script>
  61. import userInfoVue from '../../views/system/users/userInfo.vue';
  62. import { mapState, mapGetters, mapMutations, mapActions } from 'vuex';
  63. import envConfig from '@/config';
  64. import vTags from './Tags.vue';
  65. export default {
  66. data() {
  67. return {
  68. name: '',
  69. envConfig: envConfig,
  70. imgList: [
  71. require('@/assets/img/icon_msg1.png'),
  72. require('@/assets/img/icon_msg2.png'),
  73. require('@/assets/img/icon_msg3.png'),
  74. require('@/assets/img/icon_msg4.png'),
  75. require('@/assets/img/icon_msg5.png'),
  76. require('@/assets/img/icon_msg6.png'),
  77. require('@/assets/img/icon_msg7.png'),
  78. require('@/assets/img/icon_msg8.png'),
  79. require('@/assets/img/icon_msg9.png')
  80. ]
  81. };
  82. },
  83. components: {
  84. vTags
  85. },
  86. computed: {
  87. cruUserInfo() {
  88. return this.$store.getters['getCruUserInfo'];
  89. },
  90. collapse() {
  91. return this.$store.getters['getCollapse'];
  92. },
  93. ...mapState(['messageNumber']),
  94. ...mapState(['messageList'])
  95. },
  96. methods: {
  97. toUrl(type) {
  98. let url, appId;
  99. if (type == 1 || type == 2) {
  100. url = this.envConfig.warnDetailUrl;
  101. appId = '1002';
  102. } else if (type == 4) {
  103. url = this.envConfig.taskManagementUrl;
  104. appId = '1003';
  105. } else if (type == 3 || type == 7) {
  106. url = this.envConfig.eventHiddenDangerUrl;
  107. appId = '1003';
  108. } else if (type == 9) {
  109. // url=this.envConfig.eventHiddenDangerUrl;
  110. appId = '1012';
  111. this.$router.push({
  112. path: '/msg'
  113. });
  114. return;
  115. }
  116. this.$http
  117. .postForm('/sc-message/message/judgingTheMessageJumpPermission', {
  118. url: url.split('/')[url.split('/').length - 1],
  119. appId: appId
  120. })
  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-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-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-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:
  241. "<div class='notice'><img src=" +
  242. that.imgList[i] +
  243. " class='img'/><div class='notice-content'><span class='title'>新消息通知</span><span class='nowrap'>" +
  244. '【' +
  245. msg.type +
  246. '】' +
  247. msg.content +
  248. '</span></div></div>'
  249. });
  250. that.getMessageList();
  251. that.getUnreadNumber();
  252. }
  253. };
  254. //连接关闭的回调方法
  255. this.websocket.onclose = function (ev) {
  256. console.log('WebSocket连接关闭');
  257. this.websocket = null;
  258. };
  259. },
  260. send(message) {
  261. if (this.websocket && this.websocket != null) {
  262. // this.websocket.send(message);
  263. console.log('发送的消息:' + message);
  264. }
  265. },
  266. //关闭WebSocket连接
  267. closeWebSocket() {
  268. if (this.websocket != null) {
  269. this.websocket.close();
  270. }
  271. if (this.interval) {
  272. window.clearInterval(this.interval);
  273. }
  274. }
  275. },
  276. mounted() {
  277. let vm = this;
  278. window.onbeforeunload = function () {
  279. vm.closeWebSocket();
  280. };
  281. },
  282. destoryed() {
  283. this.closeWebSocket();
  284. },
  285. created() {
  286. new Promise((resolve) => {
  287. this.getUserInfo(resolve);
  288. }).then((_) => {
  289. let id = this.$store.state.cruUserInfo.id;
  290. this.initWebSocket(id);
  291. this.getMessageList();
  292. this.getUnreadNumber();
  293. });
  294. }
  295. };
  296. </script>
  297. <style lang='scss' scoped>
  298. .border {
  299. width: 1px;
  300. height: 10px;
  301. background: #ffffff;
  302. opacity: 0.3;
  303. }
  304. .header {
  305. height: 46px;
  306. font-size: 14px;
  307. color: #fff;
  308. background: #2c354a;
  309. display: flex;
  310. align-items: center;
  311. justify-content: space-between;
  312. &.expand::before {
  313. content: '';
  314. display: inline-block;
  315. width: 230px;
  316. height: 46px;
  317. background: #2c354a;
  318. position: absolute;
  319. left: 0;
  320. }
  321. &.collapse::before {
  322. content: '';
  323. display: inline-block;
  324. width: 95px;
  325. height: 46px;
  326. background: #2c354a;
  327. position: absolute;
  328. left: 0;
  329. }
  330. }
  331. .header-right {
  332. padding-right: 20px;
  333. color: rgba(255, 255, 255, 0.6);
  334. .name-txt {
  335. font-size: 12px;
  336. font-family: PingFangSC-Regular, PingFang SC;
  337. font-weight: 400;
  338. color: #ffffff;
  339. }
  340. }
  341. .header-user-con {
  342. display: flex;
  343. height: 60px;
  344. align-items: center;
  345. }
  346. .logout {
  347. margin-left: 20px;
  348. img {
  349. width: 20px;
  350. height: 20px;
  351. }
  352. }
  353. .iconfont {
  354. font-size: 20px;
  355. color: rgba(255, 255, 255, 0.6);
  356. &:hover {
  357. color: rgba(255, 255, 255, 1);
  358. }
  359. }
  360. .user-avator {
  361. margin-left: 10px;
  362. margin-right: 20px;
  363. cursor: pointer;
  364. }
  365. .user-avator .imgs {
  366. background: no-repeat center top;
  367. background-size: cover;
  368. width: 20px;
  369. height: 20px;
  370. }
  371. .message {
  372. margin: 10px 20px 0px 0px;
  373. color: #000000;
  374. cursor: pointer;
  375. &:hover {
  376. .msg-content {
  377. display: block;
  378. background: #ffffff;
  379. box-shadow: 0px 5px 20px 0px rgba(144, 144, 144, 0.4);
  380. border-radius: 6px;
  381. z-index: 200;
  382. }
  383. }
  384. .main {
  385. height: 60px;
  386. line-height: 60px;
  387. position: relative;
  388. img {
  389. width: 20px;
  390. height: 20px;
  391. }
  392. .point {
  393. position: absolute;
  394. top: -4px;
  395. right: -1px;
  396. width: 4px;
  397. height: 4px;
  398. background: #fe7271;
  399. border-radius: 50%;
  400. display: block;
  401. }
  402. }
  403. .msg-content {
  404. position: absolute;
  405. width: 380px;
  406. background: #ffffff;
  407. z-index: 20;
  408. right: 20px;
  409. top: 40px;
  410. transition: all 0.3s;
  411. display: none;
  412. p {
  413. height: 40px;
  414. line-height: 40px;
  415. padding-left: 20px;
  416. color: #212226;
  417. }
  418. li {
  419. display: flex;
  420. align-items: center;
  421. height: 60px;
  422. padding-left: 20px;
  423. border-top: 1px solid #f6f6f6;
  424. font-size: 12px;
  425. cursor: auto;
  426. span {
  427. width: 300px;
  428. overflow: hidden; //超出的文本隐藏
  429. text-overflow: ellipsis; //溢出用省略号显示
  430. white-space: nowrap; //溢出不换行
  431. display: block;
  432. line-height: 20px;
  433. padding-left: 15px;
  434. }
  435. .cont {
  436. color: #424656;
  437. text-indent: -0.4em;
  438. }
  439. .time {
  440. color: #7d7f87;
  441. }
  442. }
  443. .more {
  444. width: 100%;
  445. }
  446. }
  447. }
  448. </style>