HeaderNotice.vue 13 KB

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