Header.vue 14 KB

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