HeaderNotice.vue 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347
  1. <template>
  2. <a-popover
  3. trigger="click"
  4. placement="bottomRight"
  5. :autoAdjustOverflow="true"
  6. :arrowPointAtCenter="true"
  7. overlayClassName="header-notice-wrapper"
  8. @visibleChange="handleHoverChange"
  9. :overlayStyle="{ width: '300px', top: '50px' }"
  10. >
  11. <template slot="content">
  12. <a-spin :spinning="loadding">
  13. <a-tabs>
  14. <!-- <a-tab-pane :tab="msg1Title" key="1">
  15. <a-list>
  16. <a-list-item :key="index" v-for="(record, index) in announcement1">
  17. <div style="margin-left: 5%;width: 80%">
  18. <p>
  19. <a @click="showAnnouncement(record)">{{ record.titile }}</a>
  20. </p>
  21. <p style="color: rgba(0,0,0,.45);margin-bottom: 0px">{{ record.createTime }} 发布</p>
  22. </div>
  23. <div style="text-align: right">
  24. <a-tag @click="showAnnouncement(record)" v-if="record.priority === 'L'" color="blue">一般消息</a-tag>
  25. <a-tag
  26. @click="showAnnouncement(record)"
  27. v-if="record.priority === 'M'"
  28. color="orange"
  29. >重要消息</a-tag
  30. >
  31. <a-tag @click="showAnnouncement(record)" v-if="record.priority === 'H'" color="red">紧急消息</a-tag>
  32. </div>
  33. </a-list-item>
  34. <div style="margin-top: 5px;text-align: center">
  35. <a-button @click="toMyAnnouncement()" type="dashed" block>查看更多</a-button>
  36. </div>
  37. </a-list>
  38. </a-tab-pane> -->
  39. <a-tab-pane :tab="msg2Title" key="2">
  40. <a-list>
  41. <a-list-item :key="index" v-for="(record, index) in announcement2">
  42. <div style="margin-left: 5%;width: 80%">
  43. <p>
  44. <a @click="showAnnouncement(record)">{{ record.titile }}</a>
  45. </p>
  46. <p style="color: rgba(0,0,0,.45);margin-bottom: 0px">{{ record.createTime }} 发布</p>
  47. </div>
  48. <div style="text-align: right">
  49. <a-tag @click="showAnnouncement(record)" v-if="record.priority === 'L'" color="blue">一般消息</a-tag>
  50. <a-tag
  51. @click="showAnnouncement(record)"
  52. v-if="record.priority === 'M'"
  53. color="orange"
  54. >重要消息</a-tag
  55. >
  56. <a-tag @click="showAnnouncement(record)" v-if="record.priority === 'H'" color="red">紧急消息</a-tag>
  57. </div>
  58. </a-list-item>
  59. <div style="margin-top: 5px;text-align: center">
  60. <a-button @click="toMyAnnouncement()" type="dashed" block>查看更多</a-button>
  61. </div>
  62. </a-list>
  63. </a-tab-pane>
  64. </a-tabs>
  65. </a-spin>
  66. </template>
  67. <span @click="fetchNotice" class="header-notice">
  68. <a-badge :count="msgTotal"><StepBackwardOutlined />
  69. <!-- 技能博物馆 -->
  70. <a-icon style="color:grey; font-size: 16px; padding: 4px" type="bell" />
  71. <!-- 森语 -->
  72. <!-- <a-icon style="font-size: 16px; padding: 4px" type="bell" /> -->
  73. </a-badge>
  74. </span>
  75. <show-announcement ref="ShowAnnouncement" @ok="modalFormOk"></show-announcement>
  76. <dynamic-notice ref="showDynamNotice" :path="openPath" :formData="formData" />
  77. </a-popover>
  78. </template>
  79. <script>
  80. import { getAction, putAction } from '@/api/manage'
  81. import ShowAnnouncement from './ShowAnnouncement'
  82. import store from '@/store/'
  83. import DynamicNotice from './DynamicNotice'
  84. import Utils from '../../api/util.js';
  85. export default {
  86. name: 'HeaderNotice',
  87. components: {
  88. DynamicNotice,
  89. ShowAnnouncement
  90. },
  91. data () {
  92. return {
  93. loadding: false,
  94. url: {
  95. listCementByUser: '/sys/annountCement/listByUser',
  96. editCementSend: '/sys/sysAnnouncementSend/editByAnntIdAndUserId',
  97. queryById: '/sys/annountCement/queryById'
  98. },
  99. hovered: false,
  100. announcement1: [],
  101. announcement2: [],
  102. msg1Count: '0',
  103. msg2Count: '0',
  104. msg1Title: '通知(0)',
  105. msg2Title: '',
  106. stopTimer: false,
  107. websock: null,
  108. lockReconnect: false,
  109. heartCheck: null,
  110. formData: {},
  111. openPath: ''
  112. }
  113. },
  114. computed: {
  115. msgTotal () {
  116. return parseInt(this.msg1Count) + parseInt(this.msg2Count)
  117. },
  118. },
  119. mounted () {
  120. var that = this;
  121. Utils.$on('demo', function (msg) {
  122. // console.log(msg);
  123. that.timerFun();
  124. })
  125. this.loadData()
  126. // this.timerFun();
  127. this.initWebSocket()
  128. this.heartCheckFun()
  129. },
  130. destroyed: function () {
  131. // 离开页面生命周期函数
  132. this.websocketclose()
  133. },
  134. methods: {
  135. timerFun () {
  136. this.stopTimer = false
  137. let myTimer = setInterval(() => {
  138. // 停止定时器
  139. if (this.stopTimer == true) {
  140. clearInterval(myTimer)
  141. return
  142. }
  143. this.loadData()
  144. }, 2000)
  145. },
  146. loadData () {
  147. try {
  148. // 获取系统消息
  149. getAction(this.url.listCementByUser)
  150. .then(res => {
  151. if (res.success) {
  152. this.announcement1 = res.result.anntMsgList
  153. this.msg1Count = res.result.anntMsgTotal
  154. this.msg1Title = '通知(' + res.result.anntMsgTotal + ')'
  155. this.announcement2 = res.result.sysMsgList
  156. this.msg2Count = res.result.sysMsgTotal
  157. this.msg2Title = '系统消息(' + res.result.sysMsgTotal + ')'
  158. }
  159. })
  160. .catch(error => {
  161. console.log('系统消息通知异常', error) // 这行打印permissionName is undefined
  162. this.stopTimer = true
  163. console.log('清理timer')
  164. })
  165. } catch (err) {
  166. this.stopTimer = true
  167. console.log('通知异常', err)
  168. }
  169. },
  170. fetchNotice () {
  171. if (this.loadding) {
  172. this.loadding = false
  173. return
  174. }
  175. this.loadding = true
  176. setTimeout(() => {
  177. this.loadding = false
  178. }, 200)
  179. },
  180. showAnnouncement (record) {
  181. putAction(this.url.editCementSend, { anntId: record.id }).then(res => {
  182. if (res.success) {
  183. this.loadData()
  184. }
  185. })
  186. this.hovered = false
  187. if (record.openType === 'component') {
  188. this.openPath = record.openPage
  189. this.formData = { id: record.busId }
  190. this.$refs.showDynamNotice.detail(record.openPage)
  191. } else {
  192. record.readFlag = '0' // 未阅读(用于以未阅读状态跳转到我的消息弹框)
  193. this.$refs.ShowAnnouncement.detail(record)
  194. }
  195. },
  196. toMyAnnouncement () {
  197. this.$router.push({
  198. path: '/isps/userAnnouncement',
  199. name: 'isps-userAnnouncement'
  200. })
  201. },
  202. modalFormOk () {},
  203. handleHoverChange (visible) {
  204. this.hovered = visible
  205. },
  206. initWebSocket: function () {
  207. // WebSocket与普通的请求所用协议有所不同,ws等同于http,wss等同于https
  208. var userId = store.getters.userInfo.id
  209. var url =
  210. window._CONFIG['domianURL'].replace('https://', 'wss://').replace('http://', 'ws://') + '/websocket/' + userId
  211. console.log(url)
  212. // this.websock = new WebSocket(url);
  213. // this.websock.onopen = this.websocketOnopen;
  214. // this.websock.onerror = this.websocketOnerror;
  215. // this.websock.onmessage = this.websocketOnmessage;
  216. // this.websock.onclose = this.websocketOnclose;
  217. },
  218. websocketOnopen: function () {
  219. console.log('WebSocket连接成功')
  220. // 心跳检测重置
  221. this.heartCheck.reset().start()
  222. },
  223. websocketOnerror: function (e) {
  224. console.log('WebSocket连接发生错误')
  225. this.reconnect()
  226. },
  227. websocketOnmessage: function (e) {
  228. console.log('-----接收消息-------', e.data)
  229. var data = eval('(' + e.data + ')') // 解析对象
  230. if (data.cmd == 'topic') {
  231. // 系统通知
  232. this.loadData()
  233. } else if (data.cmd == 'user') {
  234. // 用户消息
  235. this.loadData()
  236. }
  237. // 心跳检测重置
  238. this.heartCheck.reset().start()
  239. },
  240. websocketOnclose: function (e) {
  241. console.log('connection closed (' + e.code + ')')
  242. this.reconnect()
  243. },
  244. websocketSend (text) {
  245. // 数据发送
  246. try {
  247. this.websock.send(text)
  248. } catch (err) {
  249. console.log('send failed (' + err.code + ')')
  250. }
  251. },
  252. openNotification (data) {
  253. var text = data.msgTxt
  254. const key = `open${Date.now()}`
  255. this.$notification.open({
  256. message: '消息提醒',
  257. placement: 'bottomRight',
  258. description: text,
  259. key,
  260. btn: h => {
  261. return h(
  262. 'a-button',
  263. {
  264. props: {
  265. type: 'primary',
  266. size: 'small'
  267. },
  268. on: {
  269. click: () => this.showDetail(key, data)
  270. }
  271. },
  272. '查看详情'
  273. )
  274. }
  275. })
  276. },
  277. reconnect () {
  278. var that = this
  279. if (that.lockReconnect) return
  280. that.lockReconnect = true
  281. // 没连接上会一直重连,设置延迟避免请求过多
  282. setTimeout(function () {
  283. console.info('尝试重连...')
  284. that.initWebSocket()
  285. that.lockReconnect = false
  286. }, 5000)
  287. },
  288. heartCheckFun () {
  289. var that = this
  290. // 心跳检测,每20s心跳一次
  291. that.heartCheck = {
  292. timeout: 20000,
  293. timeoutObj: null,
  294. serverTimeoutObj: null,
  295. reset: function () {
  296. clearTimeout(this.timeoutObj)
  297. // clearTimeout(this.serverTimeoutObj);
  298. return this
  299. },
  300. start: function () {
  301. var self = this
  302. this.timeoutObj = setTimeout(function () {
  303. // 这里发送一个心跳,后端收到后,返回一个心跳消息,
  304. // onmessage拿到返回的心跳就说明连接正常
  305. that.websocketSend('HeartBeat')
  306. console.info('客户端发送心跳')
  307. // self.serverTimeoutObj = setTimeout(function(){//如果超过一定时间还没重置,说明后端主动断开了
  308. // that.websock.close();//如果onclose会执行reconnect,我们执行ws.close()就行了.如果直接执行reconnect 会触发onclose导致重连两次
  309. // }, self.timeout)
  310. }, this.timeout)
  311. }
  312. }
  313. },
  314. showDetail (key, data) {
  315. this.$notification.close(key)
  316. var id = data.msgId
  317. getAction(this.url.queryById, { id: id }).then(res => {
  318. if (res.success) {
  319. var record = res.result
  320. this.showAnnouncement(record)
  321. }
  322. })
  323. }
  324. }
  325. }
  326. </script>
  327. <style lang="css">
  328. .header-notice-wrapper {
  329. top: 50px !important;
  330. }
  331. </style>
  332. <style lang="less" scoped>
  333. .header-notice {
  334. display: inline-block;
  335. transition: all 0.3s;
  336. span {
  337. vertical-align: initial;
  338. }
  339. }
  340. </style>