MessageBroadcast.vue 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  1. <template>
  2. <div style="position: fixed; z-index: 999; right: 95px; top: 18px; color: #fff">
  3. <div class="btn" @click="showWarningBroad">
  4. <a-badge :dot="isWarningDot">
  5. <BellOutlined style="font-size: 22px; color: #fff; margin-right: 20px" />
  6. </a-badge>
  7. </div>
  8. <div v-if="isShowWarningBroad" class="broadcast">
  9. <div class="title">
  10. <div class="message-title">预警通知</div>
  11. <div class="badge-box">
  12. <SoundOutlined :class="{ 'no-play': !isBroad }" style="font-size: 20px; color: #000" @click="handleBroad" />
  13. </div>
  14. </div>
  15. <div class="broadcast-context">
  16. <div class="context-tab">
  17. <div class="context-tab-item" :class="{ 'context-tab-item-active': activeKey == 0 }" @click="toSelectList(0)">全部</div>
  18. <div class="context-tab-item" :class="{ 'context-tab-item-active': activeKey == 1 }" @click="toSelectList(1)">未读</div>
  19. <div class="context-tab-item" :class="{ 'context-tab-item-active': activeKey == 2 }" @click="toSelectList(2)">已读</div>
  20. </div>
  21. <div class="context-box">
  22. <div v-if="broadcastList.length == 0" class="no-context">暂无内容</div>
  23. <div
  24. class="context-detail"
  25. v-else
  26. v-for="(item, index) in broadcastList"
  27. :key="index"
  28. :style="{ color: item['isok'] == 0 ? 'red' : '#000' }"
  29. >
  30. <div>{{ item['createTime'] }}</div>
  31. <div>{{ item['devicekind_dictText'] }}</div>
  32. <div>{{ item['wardescrip'] || item['nwartype_dictText'] }}</div>
  33. <div>{{ item['isok'] ? '已解决' : '未解决' }}</div>
  34. </div>
  35. <div v-if="broadcastList.length > 0" class="more" @click="toMore">更多>></div>
  36. </div>
  37. </div>
  38. </div>
  39. </div>
  40. </template>
  41. <script lang="ts">
  42. import { Tooltip, Badge } from 'ant-design-vue';
  43. import { SoundOutlined, BellOutlined, WarningOutlined } from '@ant-design/icons-vue';
  44. import Icon from '/@/components/Icon';
  45. import { defineComponent, ref, unref, onMounted } from 'vue';
  46. import { defHttp } from '/@/utils/http/axios';
  47. import { useRouter } from 'vue-router';
  48. import { connectWebSocket, onWebSocket } from '/@/hooks/web/useWebSocket';
  49. import { getToken } from '/@/utils/auth';
  50. import { useUserStore } from '/@/store/modules/user';
  51. import { useGlobSetting } from '/@/hooks/setting';
  52. import SpeakVoice from './notify/speakVoice';
  53. export default defineComponent({
  54. name: 'VoiceBroadcast',
  55. components: { Icon, Tooltip, Badge, SoundOutlined, BellOutlined, WarningOutlined },
  56. setup() {
  57. const speakVoice = new SpeakVoice();
  58. const userStore = useUserStore();
  59. const glob = useGlobSetting();
  60. const router = useRouter();
  61. const list = (params) => defHttp.get({ url: '/safety/ventanalyAlarmLog/list', params });
  62. const activeKey = ref(0);
  63. const isShowWarningBroad = ref(false);
  64. const isBroad = ref(true);
  65. const isWarningDot = ref(false);
  66. const broadcastList = ref([]);
  67. function showWarningBroad() {
  68. isShowWarningBroad.value = !isShowWarningBroad.value;
  69. if (isShowWarningBroad.value) {
  70. toSelectList(0);
  71. }
  72. }
  73. function handleBroad() {
  74. isBroad.value = !isBroad.value;
  75. }
  76. async function toSelectList(key) {
  77. activeKey.value = key;
  78. const res = await list({ pageSize: 20, devicetype: '', isok: key == 1 ? 0 : key == 2 ? 1 : null });
  79. broadcastList.value = res['records'];
  80. // const isHasWarning = broadcastList.value.findIndex((item) => !item['isok']);
  81. // isWarningDot.value = isHasWarning > -1 ? true : false;
  82. }
  83. async function toMore() {
  84. await router.push({ path: '/monitorChannel/device-monitor/warningHistory' });
  85. showWarningBroad();
  86. }
  87. // 初始化 WebSocket
  88. function initWebSocket() {
  89. let token = getToken();
  90. //将登录token生成一个短的标识
  91. // let wsClientId = md5(token);
  92. // let userId = unref(userStore.getUserInfo).id + '_' + wsClientId;
  93. let userId = unref(userStore.getUserInfo).id + '?token=' + token;
  94. // WebSocket与普通的请求所用协议有所不同,ws等同于http,wss等同于https
  95. let url = glob.wsUrl?.replace('https://', 'wss://').replace('http://', 'ws://') + '/websocket/' + userId;
  96. connectWebSocket(url);
  97. onWebSocket(onWebSocketMessage);
  98. }
  99. function onWebSocketMessage(data) {
  100. // console.log('WebSocket 监测消息--------------》', data);
  101. if (data.topic === 'warn' || data.cmd === 'user') {
  102. if (isBroad.value) {
  103. const messageText = data['warndata'];
  104. // const messageText = '这是一个测试';
  105. speakVoice.handleReply(messageText);
  106. }
  107. if (!isShowWarningBroad.value) {
  108. isWarningDot.value = true;
  109. } else {
  110. isWarningDot.value = false;
  111. }
  112. setTimeout(() => {
  113. if (isShowWarningBroad.value) {
  114. toSelectList(0);
  115. }
  116. }, 0);
  117. }
  118. }
  119. onMounted(() => {
  120. initWebSocket();
  121. });
  122. return { showWarningBroad, isShowWarningBroad, activeKey, toSelectList, broadcastList, toMore, isBroad, handleBroad, isWarningDot };
  123. },
  124. });
  125. </script>
  126. <style lang="less" scoped>
  127. .btn {
  128. line-height: 30px;
  129. margin-right: 20px;
  130. cursor: pointer;
  131. display: flex;
  132. }
  133. .no-play {
  134. background: linear-gradient(
  135. to bottom left,
  136. transparent 0%,
  137. transparent calc(50% - 1px),
  138. #000000 50%,
  139. transparent calc(50% + 1px),
  140. transparent 100%
  141. );
  142. }
  143. .broadcast {
  144. width: 400px;
  145. height: 250px;
  146. border-radius: 4px;
  147. position: fixed;
  148. top: 42px;
  149. right: 20px;
  150. background-color: rgb(255, 255, 255);
  151. z-index: 9999999;
  152. color: #000;
  153. .title {
  154. height: 48px;
  155. padding: 0 20px;
  156. box-shadow: 2px 2px 2px 1px rgba(0, 0, 0, 0.2);
  157. :deep(.ant-badge:not(.ant-badge-status)) {
  158. margin-right: 40px !important;
  159. }
  160. display: flex;
  161. align-items: center;
  162. justify-content: space-between;
  163. margin-bottom: 5px;
  164. .message-title {
  165. font-size: 20px;
  166. // font-weight: 600;
  167. }
  168. .badge-box {
  169. display: flex;
  170. align-items: center;
  171. .badge-title {
  172. display: inline-block;
  173. width: 62px;
  174. line-height: 24px;
  175. background-color: #2174f0;
  176. border-radius: 26px;
  177. text-align: center;
  178. color: #fff;
  179. padding-bottom: 2px;
  180. }
  181. }
  182. }
  183. .broadcast-context {
  184. .context-tab {
  185. display: flex;
  186. .context-tab-item {
  187. line-height: 30px;
  188. background-color: #6b6b6b;
  189. border-radius: 26px;
  190. text-align: center;
  191. padding: 0 10px;
  192. color: #fff;
  193. margin: 5px;
  194. cursor: pointer;
  195. }
  196. .context-tab-item-active {
  197. background-color: #2174f0;
  198. }
  199. }
  200. .context-box {
  201. flex: 1;
  202. padding: 0 10px;
  203. height: 150px;
  204. overflow-y: auto;
  205. .no-context {
  206. display: flex;
  207. justify-content: center;
  208. padding-top: 30px;
  209. font-size: 16px;
  210. }
  211. .context-detail {
  212. display: flex;
  213. div {
  214. padding: 0 3px;
  215. line-height: 24px;
  216. }
  217. }
  218. .more {
  219. cursor: pointer;
  220. &:hover {
  221. color: #2174f0;
  222. }
  223. }
  224. }
  225. }
  226. }
  227. </style>