HeaderNotice.vue 13 KB

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