VoiceBroadcastGsd.vue 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328
  1. <template>
  2. <div>
  3. <div class="btn" @click="showWarningBroad">
  4. <a-badge :dot="isWarningDot" style="display: flex; flex-direction: row">
  5. <BellOutlined style="font-size: 22px; color: #fff; margin-right: 10px" />
  6. <WarningOutlined style="font-size: 22px; color: #fff" />
  7. </a-badge>
  8. </div>
  9. <div v-if="isShowWarningBroad" class="broadcast" ref="VoiceBroadcastRef" id="VoiceBroadcast">
  10. <div class="title">
  11. <div class="message-title">预警通知</div>
  12. <div class="badge-box">
  13. <ClearOutlined style="font-size: 20px; color: #fff" @click="clearInfo" />
  14. <SoundOutlined :class="{ 'no-play': !isBroad }" style="font-size: 20px; color: #fff" @click="handleBroad" />
  15. <CloseOutlined style="font-size: 20px; color: #fff" @click="closeWarningBroad" />
  16. </div>
  17. </div>
  18. <div class="broadcast-context">
  19. <div class="context-tab">
  20. <div class="context-tab-item" :class="{ 'context-tab-item-active': activeKey == 0 }"> 全部</div>
  21. <!-- <div class="context-tab-item" :class="{ 'context-tab-item-active': activeKey == 1 }"
  22. > 未解决</div>
  23. <div class="context-tab-item" :class="{ 'context-tab-item-active': activeKey == 2 }"
  24. > 已解决</div> -->
  25. </div>
  26. <div class="context-box">
  27. <div v-if="(broadcastList && broadcastList.length == 0) || !broadcastList" class="no-context">暂无内容 </div>
  28. <div class="context-detail" v-else v-for="(item, index) in broadcastList" :key="index">
  29. <div>{{ item['label'] }}</div>
  30. </div>
  31. </div>
  32. <div class="more" @click="toMore">更多>></div>
  33. </div>
  34. </div>
  35. </div>
  36. </template>
  37. <script lang="ts">
  38. import { SoundOutlined, ClearOutlined, BellOutlined, WarningOutlined, CloseOutlined } from '@ant-design/icons-vue';
  39. import { defineComponent, ref, unref, onMounted, onUnmounted, nextTick } from 'vue';
  40. import { useRouter } from 'vue-router';
  41. import { connectWebSocket, onWebSocket, offWebSocket } from '/@/hooks/web/useWebSocket';
  42. import { getToken } from '/@/utils/auth';
  43. import { useUserStore } from '/@/store/modules/user';
  44. import { usePermission } from '/@/hooks/web/usePermission';
  45. import { usePermissionStore } from '/@/store/modules/permission';
  46. import SpeakVoice from './notify/speakVoice';
  47. import dayjs from 'dayjs';
  48. import { useDrag } from '@/hooks/event/useDrag';
  49. export default defineComponent({
  50. name: 'VoiceBroadcast',
  51. components: { SoundOutlined, ClearOutlined, BellOutlined, WarningOutlined, CloseOutlined },
  52. setup() {
  53. let speakVoice;
  54. let broadcastList = ref<any[]>([]);
  55. const userStore = useUserStore();
  56. const { hasPermission } = usePermission();
  57. const permissionStore = usePermissionStore();
  58. const router = useRouter();
  59. const activeKey = ref(0);
  60. const isShowWarningBroad = ref(false);
  61. const isBroad = ref(false);
  62. const isWarningDot = ref(false);
  63. //点击切换预警信息弹窗显示
  64. function showWarningBroad() {
  65. isShowWarningBroad.value = !isShowWarningBroad.value;
  66. if (isShowWarningBroad.value) {
  67. nextTick(() => {
  68. const dom = document.getElementById('VoiceBroadcast');
  69. if (dom) useDrag(dom);
  70. });
  71. }
  72. }
  73. //点击关闭预警信息弹窗
  74. function closeWarningBroad() {
  75. isShowWarningBroad.value = false;
  76. }
  77. function handleBroad() {
  78. isBroad.value = !isBroad.value;
  79. }
  80. async function clearInfo() {
  81. broadcastList.value = [];
  82. localStorage.removeItem('messageArr');
  83. }
  84. //点击跳转预警历史详情
  85. async function toMore() {
  86. await router.push({ path: '/monitorChannel/device-monitor/warningHistory/company/home' });
  87. showWarningBroad();
  88. }
  89. // 初始化 WebSocket
  90. function initWebSocket() {
  91. let token = getToken();
  92. //将登录token生成一个短的标识
  93. // let wsClientId = md5(token);
  94. // let userId = unref(userStore.getUserInfo).id + '_' + wsClientId;
  95. let userId = unref(userStore.getUserInfo).id + '?token=' + token;
  96. // WebSocket与普通的请求所用协议有所不同,ws等同于http,wss等同于https
  97. let url = 'http://' + window.location.hostname + ':9999'?.replace('https://', 'wss://').replace('http://', 'ws://') + '/websocket/' + userId;
  98. connectWebSocket(url);
  99. onWebSocket(onWebSocketMessage);
  100. }
  101. async function onWebSocketMessage(data) {
  102. console.log('WebSocket 监测消息--------------》', data);
  103. if (hasPermission('show:noWarning')) return;
  104. if (!permissionStore.getPermCodeList?.length) return;
  105. if (data.topic === 'warn') {
  106. const messageText = data['warndata'];
  107. const msgList = [...broadcastList.value];
  108. if (msgList.length <= 20) {
  109. msgList.push({ label: messageText });
  110. localStorage.removeItem('messageArr');
  111. localStorage.setItem('messageArr', msgList as any);
  112. } else {
  113. msgList.shift();
  114. msgList.push({ label: messageText });
  115. localStorage.removeItem('messageArr');
  116. localStorage.setItem('messageArr', msgList as any);
  117. }
  118. broadcastList.value = msgList;
  119. if (isBroad.value) {
  120. await speakVoice.getSpeechCnVoices();
  121. speakVoice.handleReply(messageText);
  122. const time = dayjs().format('YYYY-MM-DD HH:mm:ss');
  123. console.log(time + '语音播报开始报警------>', data);
  124. }
  125. // const messageText = '这是一个测试';
  126. if (!isShowWarningBroad.value) {
  127. isWarningDot.value = true;
  128. } else {
  129. isWarningDot.value = false;
  130. }
  131. }
  132. }
  133. onMounted(() => {
  134. speakVoice = new SpeakVoice();
  135. nextTick(async () => {
  136. initWebSocket();
  137. });
  138. window.speechSynthesis.onvoiceschanged = () => {
  139. console.log('语音列表已更新');
  140. };
  141. });
  142. onUnmounted(() => {
  143. offWebSocket(onWebSocketMessage);
  144. window.speechSynthesis?.cancel();
  145. });
  146. return {
  147. showWarningBroad,
  148. isShowWarningBroad,
  149. activeKey,
  150. broadcastList,
  151. toMore,
  152. clearInfo,
  153. isWarningDot,
  154. handleBroad,
  155. closeWarningBroad,
  156. isBroad,
  157. };
  158. },
  159. });
  160. </script>
  161. <style lang="less" scoped>
  162. .btn {
  163. line-height: 30px;
  164. margin-right: 10px;
  165. cursor: pointer;
  166. display: flex;
  167. }
  168. .no-play {
  169. position: relative;
  170. &::after {
  171. position: absolute;
  172. width: 70%;
  173. height: 100%;
  174. content: '';
  175. left: 15%;
  176. top: 0;
  177. background: linear-gradient(
  178. to bottom left,
  179. transparent 0%,
  180. transparent calc(50% - 1px),
  181. #ffffff 50%,
  182. transparent calc(50% + 1px),
  183. transparent 100%
  184. );
  185. }
  186. }
  187. .broadcast {
  188. width: 500px;
  189. height: 350px;
  190. border-radius: 4px;
  191. position: fixed;
  192. top: 50px;
  193. right: 20px;
  194. background-color: rgb(255, 255, 255);
  195. background: url('../../../../assets/images/warn-dialog-bg.png') no-repeat center;
  196. background-size: 100% 100%;
  197. z-index: 9999999;
  198. color: #fff;
  199. .title {
  200. height: 32px;
  201. padding: 0 20px;
  202. :deep(.ant-badge:not(.ant-badge-status)) {
  203. margin-right: 40px !important;
  204. }
  205. display: flex;
  206. align-items: center;
  207. justify-content: space-between;
  208. margin-bottom: 5px;
  209. .message-title {
  210. font-size: 18px;
  211. padding-top: 10px;
  212. }
  213. .badge-box {
  214. display: flex;
  215. align-items: center;
  216. padding-top: 10px;
  217. .badge-title {
  218. display: inline-block;
  219. width: 62px;
  220. line-height: 32px;
  221. background-color: #2174f0;
  222. border-radius: 26px;
  223. text-align: center;
  224. color: #fff;
  225. padding-bottom: 2px;
  226. }
  227. }
  228. }
  229. .broadcast-context {
  230. .context-tab {
  231. display: flex;
  232. padding: 20px;
  233. .context-tab-item {
  234. line-height: 24px;
  235. background-color: #6b6b6b;
  236. border-radius: 24px;
  237. text-align: center;
  238. padding: 0 10px;
  239. color: #fff;
  240. margin: 5px;
  241. cursor: pointer;
  242. font-size: 14px;
  243. }
  244. .context-tab-item-active {
  245. background-color: #2174f0;
  246. }
  247. }
  248. .context-box {
  249. flex: 1;
  250. padding: 0 10px;
  251. height: 200px;
  252. overflow-y: auto;
  253. .no-context {
  254. display: flex;
  255. justify-content: center;
  256. padding-top: 30px;
  257. font-size: 16px;
  258. }
  259. .context-detail {
  260. width: 100%;
  261. display: flex;
  262. justify-content: space-between;
  263. line-height: 24px;
  264. padding: 0px 16px;
  265. // div {
  266. // display: flex;
  267. // justify-content: flex-start;
  268. // &:nth-child(1) {
  269. // width: 44%;
  270. // }
  271. // &:nth-child(2) {
  272. // width: 40%;
  273. // }
  274. // &:nth-child(3) {
  275. // width: 25%;
  276. // }
  277. // &:nth-child(4) {
  278. // width: 15%;
  279. // }
  280. // }
  281. }
  282. }
  283. .more {
  284. position: absolute;
  285. left: 24px;
  286. bottom: 10px;
  287. cursor: pointer;
  288. &:hover {
  289. color: #2174f0;
  290. }
  291. }
  292. }
  293. }
  294. :deep(.zxm-badge-count) {
  295. width: 8px;
  296. height: 8px;
  297. }
  298. </style>