Header.vue 14 KB

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