App.vue 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. <template>
  2. <div id="app">
  3. <router-view />
  4. </div>
  5. </template>
  6. <script>
  7. import store from '@/store'
  8. import { mapGetters } from 'vuex'
  9. import messageApi from '@/api/system/message'
  10. export default {
  11. name: 'App',
  12. data() {
  13. return {
  14. url: process.env.VUE_APP_WEBSOCKET_URL + '?uid=',
  15. ws: null,
  16. interValId: '',
  17. timerId: '',
  18. }
  19. },
  20. computed: {
  21. ...mapGetters({
  22. userid: 'user/id',
  23. username: 'user/username',
  24. }),
  25. },
  26. created() {
  27. this.connectWebsocket()
  28. },
  29. destroyed() {
  30. clearInterval(this.interValId)
  31. },
  32. methods: {
  33. connectWebsocket() {
  34. if (this.userid) {
  35. this.ws = new WebSocket(this.url + this.userid + '-pc')
  36. } else {
  37. this.ws = null
  38. console.info('uid为空')
  39. }
  40. try {
  41. if (this.ws) {
  42. // ws连接成功
  43. this.ws.onopen = function () {
  44. console.info('WebSocket Server [' + this.url + '] 连接成功!')
  45. }
  46. // ws连接关闭
  47. this.ws.onclose = this.onClose
  48. // ws连接错误
  49. this.ws.onerror = this.onError
  50. // ws数据返回处理
  51. this.ws.onmessage = this.onMessage
  52. }
  53. } catch (e) {
  54. console.error(e)
  55. }
  56. this.interValId = setInterval(this.heartAndReconnect, 10000)
  57. },
  58. // 心跳且重新
  59. heartAndReconnect() {
  60. let hasToken = store.getters['user/token']
  61. console.log('hasToken', hasToken)
  62. if (!hasToken) {
  63. clearInterval(this.interValId)
  64. this.connectWebsocket()
  65. return
  66. }
  67. if (this.ws) {
  68. let data = {
  69. type: 'heartbeat',
  70. }
  71. this.ws.send(JSON.stringify(data))
  72. } else {
  73. clearInterval(this.interValId)
  74. this.connectWebsocket()
  75. }
  76. },
  77. // 关闭
  78. onClose() {
  79. if (this.ws) {
  80. this.ws.close()
  81. this.ws = null
  82. }
  83. console.info('WebSocket Server [' + this.url + '] 连接关闭!')
  84. },
  85. // 处理链接错误
  86. onError() {
  87. if (this.ws) {
  88. this.ws.close()
  89. this.ws = null
  90. }
  91. console.info('WebSocket Server [' + this.url + '] 连接错误!')
  92. },
  93. onMessage(result) {
  94. console.info(' > ' + result.data)
  95. this.showMessage(result.data)
  96. },
  97. // 打开消息弹窗
  98. showMessage(item) {
  99. let data = JSON.parse(item)
  100. if (data.type === 'SendMessage' && data.content.body) {
  101. this.$baseEventBus.$emit('receivedMessage')
  102. let msg = JSON.parse(item).content.body
  103. this.$notify({
  104. title: msg.msgTitle,
  105. position: 'top-right',
  106. type: 'info',
  107. customClass: 'notify-msg',
  108. duration: 1000 * 60 * 5,
  109. dangerouslyUseHTMLString: true,
  110. message: msg.msgContent,
  111. })
  112. }
  113. },
  114. // 执行路由跳转
  115. handleItem(item) {
  116. messageApi.getEntityById({ id: item.id }).then(() => {
  117. this.$baseEventBus.$emit('receivedMessage')
  118. })
  119. },
  120. },
  121. }
  122. </script>