AppMain.vue 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. <template>
  2. <section class="app-main">
  3. <transition name="fade-transform" mode="out-in">
  4. <keep-alive :include="cachedViews">
  5. <router-view :key="key" />
  6. </keep-alive>
  7. </transition>
  8. <span class="footer">
  9. Copyright © 2023 厦门市巨龙信息科技有限公司 建议使用1280*768以上分辨率
  10. </span>
  11. <section>
  12. <information ref="Information" />
  13. </section>
  14. </section>
  15. </template>
  16. <script>
  17. import { fetchRealCount } from '@/api/message'
  18. import { isNull } from '@/utils/convert'
  19. import Information from '@/views/home/Information'
  20. let sseClient
  21. export default {
  22. name: 'AppMain',
  23. components: {
  24. Information
  25. },
  26. computed: {
  27. cachedViews() {
  28. return this.$store.state.tagsView.cachedViews
  29. },
  30. key() {
  31. return this.$route.path
  32. }
  33. },
  34. mounted() {
  35. this.getMessageCount()
  36. this.connect()
  37. },
  38. beforeDestroy() {
  39. if (sseClient) {
  40. sseClient.disconnect()
  41. }
  42. },
  43. methods: {
  44. getMessageCount() {
  45. fetchRealCount().then(res => {
  46. if (!isNull(res.data)) {
  47. this.$store.dispatch('message/setMessgeCount', res.data)
  48. }
  49. }).catch(error => {
  50. console.log(error)
  51. this.$message({
  52. type: 'error',
  53. duration: 0,
  54. showClose: true,
  55. message: '获取消息数量出错: ' + error.message
  56. })
  57. })
  58. },
  59. connect() {
  60. sseClient = this.$sse.create({
  61. url: process.env.VUE_APP_BASE_API + '/subscribe/' + this.$store.getters.idCard,
  62. format: 'json',
  63. withCredentials: false
  64. // forcePolyfill: true,
  65. // polyfillOptions: {
  66. // headers: {
  67. // 'Authorization': 'Bearer ' + this.$store.getters.token
  68. // }
  69. // }
  70. })
  71. sseClient.on('error', (e) => {
  72. console.error('lost connection or failed to parse!', e)
  73. // If this error is due to an unexpected disconnection, EventSource will
  74. // automatically attempt to reconnect indefinitely. You will _not_ need to
  75. // re-add your handlers.
  76. })
  77. sseClient.on('message', this.handleMessage)
  78. sseClient.connect()
  79. .then(sse => {
  80. console.log('We\'re connected!')
  81. })
  82. .catch((err) => {
  83. // When this error is caught, it means the initial connection to the
  84. // events server failed. No automatic attempts to reconnect will be made.
  85. console.error('Failed to connect to server', err)
  86. })
  87. },
  88. handleMessage(message) {
  89. console.log(message)
  90. if (message && message.recordType) {
  91. this.$store.dispatch('message/addNewMessage', message)
  92. if (message.recordType === 1) {
  93. this.$refs['Information'].open(message)
  94. }
  95. }
  96. }
  97. }
  98. }
  99. </script>
  100. <style lang="scss" scoped>
  101. @import "~@/styles/variables.scss";
  102. .app-main {
  103. min-height: calc(100vh - #{$headerHeight});
  104. width: 100%;
  105. position: relative;
  106. // overflow: auto;
  107. padding-bottom: $footerHeight;
  108. &>div:not(.home-container) {
  109. height: calc(100vh - #{$layoutHeight});
  110. padding: 10px 0 0 10px;
  111. }
  112. }
  113. .hasTagsView {
  114. .app-main {
  115. min-height: calc(100vh - #{$headerHeight} - #{$tagHeight});
  116. }
  117. .fixed-header+.app-main {
  118. min-height: calc(100vh - #{$headerHeight});
  119. padding-top: $tagHeight;
  120. }
  121. }
  122. .footer {
  123. display: block;
  124. width: 100%;
  125. height: $footerHeight;
  126. line-height: $footerHeight;
  127. font-size: 12px;
  128. color: rgba(0,0,0,0.45);
  129. text-align: center;
  130. position: absolute;
  131. left: 0;
  132. bottom: 0;
  133. }
  134. </style>
  135. <style lang="scss">
  136. // fix css style bug in open el-dialog
  137. .el-popup-parent--hidden {
  138. .fixed-header {
  139. padding-right: 15px;
  140. }
  141. }
  142. </style>