VoiceBroadcast.vue 9.5 KB

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