index.vue 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. <template>
  2. <div class="messageBox" :class="panelIsShow?'show':''">
  3. <div class="flex-align-center flex-between title-panel">
  4. <p>消息提示</p>
  5. <p @click="panelIsShow = false">忽略</p>
  6. </div>
  7. <p class="message-item" v-for="item in list" :key="item.index">
  8. [{{item.type}}消息]&nbsp;{{item.title}}
  9. </p>
  10. <p class="unread-panel">共有{{total}}条未读消息</p>
  11. </div>
  12. </template>
  13. <script>
  14. export default {
  15. components:{
  16. },
  17. data () {
  18. return {
  19. id:'',
  20. // path:"ws://121.37.152.76:8080/yos/webSocket/",
  21. path:"wss://meida.cnyunl.com/yos/webSocket/",
  22. panelIsShow:false,
  23. list:[],
  24. total:0,
  25. }
  26. },
  27. created () {
  28. let token = JSON.parse(sessionStorage.getItem('active_account')).token
  29. this.path = this.path + token
  30. console.log(this.path)
  31. },
  32. mounted () {
  33. this.init()
  34. this.getMessageList()
  35. },
  36. methods:{
  37. async getMessageList () {
  38. let param = {
  39. "classname": "system.message.Message",
  40. "method": "queryMessage",
  41. "content": {
  42. "isread":0,
  43. "nocache": true,
  44. "pageNumber": 1,
  45. "pageSize": 5,
  46. "type": '',
  47. "where":{}
  48. }
  49. }
  50. let res = await this.$api.requested(param)
  51. this.total = res.total
  52. },
  53. onCloseThemeChat () {
  54. },
  55. onTypeChange (val) {
  56. },
  57. onThemeChat (bool) {
  58. },
  59. onChange (type) {
  60. },
  61. init: function () {
  62. if(typeof(WebSocket) === "undefined"){
  63. alert("您的浏览器不支持socket")
  64. }else{
  65. // 实例化socket
  66. this.socket = new WebSocket(this.path)
  67. // 监听socket连接
  68. this.socket.onopen = this.open
  69. // 监听socket错误信息
  70. this.socket.onerror = this.error
  71. // 监听socket消息
  72. this.socket.onmessage = this.getMessage
  73. this.contectIsLive()
  74. }
  75. },
  76. open: function (val) {
  77. // console.log("socket连接成功",val)
  78. },
  79. error: function () {
  80. console.log("连接错误")
  81. },
  82. getMessage: function (msg) {
  83. if(msg.data !== 'alive') {
  84. this.panelIsShow = true
  85. this.list = [JSON.parse(msg.data).message]
  86. this.getMessageList()
  87. }
  88. },
  89. send: function (type,val,timsubjectid) {
  90. },
  91. close: function () {
  92. console.log("socket已经关闭")
  93. },
  94. // 心跳链接
  95. contectIsLive () {
  96. this.realTimeClData = setInterval(() => {
  97. this.socket.send('isalive')
  98. }, 10000);
  99. }
  100. },
  101. destroyed () {
  102. // 销毁监听
  103. this.socket.onclose = this.close
  104. },
  105. beforeDestroy () {
  106. clearInterval(this.realTimeClData);
  107. }
  108. }
  109. </script>
  110. <style>
  111. </style>
  112. <style scoped>
  113. .messageBox{
  114. position: fixed;
  115. right:10px;
  116. bottom: -310px;
  117. width:250px;
  118. height: 300px;
  119. background: #fff;
  120. box-shadow: 0 0 10px #ccc;
  121. border-radius: 4px;
  122. z-index: 9999;
  123. transition: .5s linear;
  124. overflow: hidden;
  125. }
  126. .show{
  127. bottom:10px
  128. }
  129. .title-panel{
  130. padding: 10px;
  131. background: #3874F6;
  132. color: #fff;
  133. font-size: 14px;
  134. cursor: pointer;
  135. }
  136. .message-item{
  137. padding: 10px;
  138. font-size: 14px;
  139. width: 100%;
  140. overflow: hidden;
  141. white-space: nowrap;
  142. text-overflow: ellipsis
  143. }
  144. .unread-panel{
  145. position: absolute;
  146. bottom:0px;
  147. width: 100%;
  148. text-align: center;
  149. font-size: 14px;
  150. padding: 10px;
  151. color:#999999;
  152. border-top:1px solid #CCCCCC
  153. }
  154. </style>