AppMain.vue 3.7 KB

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