notification.js 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. import { Notification } from 'element-ui';
  2. import i18n from './lang/index.js';
  3. /**
  4. * 通知管理器
  5. */
  6. export default {
  7. // 初始化
  8. init (vue, sessionId) {
  9. this.vue = vue;
  10. this.url = `${process.env.VUE_APP_BASE_API}/wss`
  11. this.sessionId = sessionId;
  12. this.sock = undefined;
  13. this.stomp = undefined;
  14. this.privateHandler = undefined;
  15. this.publicHandler = undefined;
  16. this.isListener = false;
  17. this._connect();
  18. },
  19. // 设置sessionId
  20. setSessionId (sessionId) {
  21. this.sessionId = sessionId;
  22. this.subscribePrivate();
  23. },
  24. // 开始监听通知
  25. start () {
  26. this.isListener = true;
  27. },
  28. // 结束监听通知
  29. end () {
  30. // this._disconnect();
  31. this.isListener = false;
  32. },
  33. // 链接到sock服务器
  34. _connect () {
  35. if (this.sock || this.stomp) {
  36. return;
  37. }
  38. this.sock = new window.SockJS(this.url);
  39. this.stomp = window.Stomp.over(this.sock);
  40. // 建立连接监听
  41. this.stomp.connect(
  42. {},
  43. frame => {
  44. console.log('con');
  45. this.subscribePrivate();
  46. this.subscribePublic();
  47. },
  48. error => {
  49. console.log('error', error);
  50. }
  51. );
  52. },
  53. // 断开连接
  54. _disconnect () {
  55. this.stomp && this.stomp.disconnect();
  56. this.sock && this.sock.close();
  57. this.sock = undefined;
  58. this.stomp = undefined;
  59. },
  60. // 监听私有通知
  61. subscribePrivate () {
  62. if (this.privateHandler) {
  63. this.privateHandler.unsubscribe();
  64. }
  65. this.privateHandler =
  66. this.stomp &&
  67. this.stomp.subscribe(
  68. `/user/${this.sessionId || 'null'}/message`,
  69. response => {
  70. this.handleNotification(response);
  71. }
  72. );
  73. },
  74. // 监听公共通知
  75. subscribePublic () {
  76. if (this.publicHandler) {
  77. this.publicHandler.unsubscribe();
  78. }
  79. this.publicHandler =
  80. this.stomp &&
  81. this.stomp.subscribe('/topic', response => {
  82. this.handleNotification(response);
  83. });
  84. },
  85. // 全部的消息都在此处处理
  86. handleNotification (response) {
  87. if (!this.isListener) {
  88. return;
  89. }
  90. var h = this.vue.$createElement;
  91. var url = '/work/work';
  92. var desc = '';
  93. try {
  94. var body = JSON.parse(response.body);
  95. desc = body.msg || body.test || '';
  96. } catch (error) {
  97. console.error(error);
  98. }
  99. Notification({
  100. duration: 4500,
  101. type: 'warning',
  102. title: '任务',
  103. dangerouslyUseHTMLString: true,
  104. message: h(
  105. 'div',
  106. { style: { width: '240px', height: '30px', display: 'flex' } },
  107. [
  108. h(
  109. 'div',
  110. { style: { flex: 1, alignSelf: 'center', color: '#333' } },
  111. desc
  112. ),
  113. h('div', { style: { alignSelf: 'center' } }, [
  114. h(
  115. 'el-link',
  116. {
  117. attrs: { type: 'info' },
  118. on: {
  119. click: () => {
  120. if (this.vue.$route.path === url) {
  121. // 刷新当前路由
  122. this.vue.$children[0].reload();
  123. } else {
  124. this.vue.$router.push(url);
  125. }
  126. }
  127. }
  128. },
  129. i18n.t('common.viewNow')
  130. )
  131. ])
  132. ]
  133. )
  134. });
  135. }
  136. };