VoiceBroadcast.vue 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356
  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; padding-left: 0" />
  10. <WarningOutlined style="font-size: 22px; color: #fff; padding-right: 0" />
  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. <ClearOutlined style="font-size: 20px; color: #fff" @click="clearInfo" />
  18. <SoundOutlined :class="{ 'no-play': !isBroad }" style="font-size: 20px; color: #fff" @click="handleBroad" />
  19. <CloseOutlined style="font-size: 20px; color: #fff" @click="closeWarningBroad" />
  20. </div>
  21. </div>
  22. <div class="broadcast-context">
  23. <div class="context-tab">
  24. <div class="context-tab-item" :class="{ 'context-tab-item-active': activeKey == 0 }" @click="toSelectList(0)"> 全部</div>
  25. <div class="context-tab-item" :class="{ 'context-tab-item-active': activeKey == 1 }" @click="toSelectList(1)"> 未解决</div>
  26. <div class="context-tab-item" :class="{ 'context-tab-item-active': activeKey == 2 }" @click="toSelectList(2)"> 已解决</div>
  27. </div>
  28. <div class="context-box">
  29. <div v-if="(broadcastList && broadcastList.length == 0) || !broadcastList" class="no-context">暂无内容</div>
  30. <div
  31. class="context-detail"
  32. v-else
  33. v-for="(item, index) in broadcastList"
  34. :key="index"
  35. :style="{ color: item['isok'] == 0 ? '#f73210' : '#eee', fontWeight: item['isok'] == 0 ? '600' : '500' }"
  36. >
  37. <div>{{ item['createTime'] }}</div>
  38. <div>{{ item['devicename'] }}</div>
  39. <div>{{ item['wardescrip'] || item['nwartype_dictText'] }}</div>
  40. <div>{{ item['isok'] ? '已解决' : '未解决' }}</div>
  41. </div>
  42. </div>
  43. <div class="more" @click="toMore">更多>></div>
  44. <!-- <div v-if="broadcastList.length > 0" class="more" @click.self="test()">测试>></div> -->
  45. </div>
  46. </div>
  47. </div>
  48. </template>
  49. <script lang="ts">
  50. import { SoundOutlined, ClearOutlined, BellOutlined, WarningOutlined, CloseOutlined } from '@ant-design/icons-vue';
  51. import { defineComponent, ref, unref, onMounted, onUnmounted, nextTick } from 'vue';
  52. import { defHttp } from '/@/utils/http/axios';
  53. import { useRouter } from 'vue-router';
  54. import { connectWebSocket, onWebSocket, offWebSocket } from '/@/hooks/web/useWebSocket';
  55. import { getToken } from '/@/utils/auth';
  56. import { useUserStore } from '/@/store/modules/user';
  57. import { usePermission } from '/@/hooks/web/usePermission';
  58. import { usePermissionStore } from '/@/store/modules/permission';
  59. import SpeakVoice from './notify/speakVoice';
  60. import { useDrag } from '@/hooks/event/useDrag';
  61. import { useGlobSetting } from '/@/hooks/setting';
  62. import dayjs from 'dayjs';
  63. export default defineComponent({
  64. name: 'VoiceBroadcast',
  65. components: { SoundOutlined, ClearOutlined, BellOutlined, WarningOutlined, CloseOutlined },
  66. setup() {
  67. // debugger;
  68. let speakVoice;
  69. const userStore = useUserStore();
  70. const { hasPermission } = usePermission();
  71. const permissionStore = usePermissionStore();
  72. const router = useRouter();
  73. const glob = useGlobSetting();
  74. const list = (params) => defHttp.get({ url: '/safety/ventanalyAlarmLog/list', params });
  75. const cleanAlarm = () => defHttp.get({ url: '/safety/ventanalyAlarmLog/cleanAlarmLog' });
  76. const activeKey = ref(0);
  77. const isShowWarningBroad = ref(false);
  78. const isBroad = ref(true);
  79. const isWarningDot = ref(false);
  80. const broadcastList = ref([]);
  81. function showWarningBroad() {
  82. isShowWarningBroad.value = !isShowWarningBroad.value;
  83. if (isShowWarningBroad.value) {
  84. toSelectList(0);
  85. nextTick(() => {
  86. const dom = document.getElementById('VoiceBroadcast');
  87. if (dom) useDrag(dom);
  88. });
  89. }
  90. }
  91. function handleBroad() {
  92. isBroad.value = !isBroad.value;
  93. }
  94. //点击关闭预警信息弹窗
  95. function closeWarningBroad() {
  96. isShowWarningBroad.value = false;
  97. }
  98. async function clearInfo() {
  99. await cleanAlarm();
  100. const res = await list({ pageSize: 20, devicetype: '', sort: 'createTime', delFlag: 0, nwartype_end: '999' });
  101. broadcastList.value = res['records'];
  102. }
  103. async function toSelectList(key) {
  104. activeKey.value = key;
  105. const res = await list({
  106. pageSize: 20,
  107. devicetype: '',
  108. isok: key == 1 ? 0 : key == 2 ? 1 : null,
  109. sort: 'createTime',
  110. delFlag: 0,
  111. nwartype_end: '999',
  112. });
  113. broadcastList.value = res['records'];
  114. const isHasWarning = broadcastList.value.findIndex((item) => !item['isok']);
  115. if (key !== 2) isWarningDot.value = isHasWarning > -1 ? true : false;
  116. }
  117. async function toMore() {
  118. await router.push({ path: '/monitorChannel/device-monitor/warningHistory/home' });
  119. showWarningBroad();
  120. }
  121. // 初始化 WebSocket
  122. function initWebSocket() {
  123. let token = getToken();
  124. //将登录token生成一个短的标识
  125. // let wsClientId = md5(token);
  126. // let userId = unref(userStore.getUserInfo).id + '_' + wsClientId;
  127. let userId = unref(userStore.getUserInfo).id + '?token=' + token;
  128. console.log(userStore.getUserInfo, 'userId---');
  129. // WebSocket与普通的请求所用协议有所不同,ws等同于http,wss等同于https
  130. // [R3-10] WS 端点恢复读运行时配置 glob.wsUrl(public/js/config.js 单点配置),去掉对网关端口的 http 硬编码直连。
  131. // 原 'http://' + hostname + '<端口>'?.replace(...) 写法中 ?. 只作用于端口字面量,协议改写从未生效;
  132. // 现把协议改写锚定在完整 URL 的协议头上。glob.wsUrl 缺失时回退 同源 /modelreq(nginx 反代至网关),
  133. // 回退地址的协议取自 location.protocol,replace 后即得到正确的 ws/wss。
  134. const wsBase = glob.wsUrl || window.location.origin + '/modelreq';
  135. let url = wsBase.replace(/^https:\/\//, 'wss://').replace(/^http:\/\//, 'ws://') + '/websocket/' + userId;
  136. console.log(url, 'url------');
  137. connectWebSocket(url);
  138. onWebSocket(onWebSocketMessage);
  139. }
  140. async function onWebSocketMessage(data) {
  141. console.log('WebSocket 监测消息--------------》', data);
  142. if (hasPermission('show:noWarning')) return;
  143. if (!permissionStore.getPermCodeList?.length) return;
  144. if (data.topic === 'warn' || data.cmd === 'user') {
  145. if (isBroad.value) {
  146. await speakVoice.getSpeechCnVoices();
  147. const messageText = data['warndata'];
  148. speakVoice.handleReply(messageText);
  149. const time = dayjs().format('YYYY-MM-DD HH:mm:ss');
  150. console.log(time + '语音播报开始报警------>', data);
  151. }
  152. if (!isShowWarningBroad.value) {
  153. isWarningDot.value = true;
  154. } else {
  155. isWarningDot.value = false;
  156. }
  157. setTimeout(() => {
  158. if (isShowWarningBroad.value) {
  159. toSelectList(0);
  160. }
  161. }, 0);
  162. }
  163. }
  164. onMounted(() => {
  165. speakVoice = new SpeakVoice();
  166. nextTick(async () => {
  167. initWebSocket();
  168. await toSelectList(1);
  169. });
  170. window.speechSynthesis.onvoiceschanged = () => {
  171. console.log('语音列表已更新');
  172. };
  173. });
  174. onUnmounted(() => {
  175. offWebSocket(onWebSocketMessage);
  176. window.speechSynthesis?.cancel();
  177. });
  178. return {
  179. showWarningBroad,
  180. isShowWarningBroad,
  181. activeKey,
  182. toSelectList,
  183. broadcastList,
  184. toMore,
  185. isBroad,
  186. handleBroad,
  187. clearInfo,
  188. closeWarningBroad,
  189. isWarningDot,
  190. };
  191. },
  192. });
  193. </script>
  194. <style lang="less" scoped>
  195. .btn {
  196. line-height: 30px;
  197. margin-right: 10px;
  198. cursor: pointer;
  199. display: flex;
  200. }
  201. .no-play {
  202. position: relative;
  203. &::after {
  204. position: absolute;
  205. width: 70%;
  206. height: 100%;
  207. content: '';
  208. left: 15%;
  209. top: 0;
  210. background: linear-gradient(
  211. to bottom left,
  212. transparent 0%,
  213. transparent calc(50% - 1px),
  214. #ffffff 50%,
  215. transparent calc(50% + 1px),
  216. transparent 100%
  217. );
  218. }
  219. }
  220. .broadcast {
  221. width: 500px;
  222. height: 350px;
  223. border-radius: 4px;
  224. position: fixed;
  225. top: 57px;
  226. right: 2px;
  227. background-color: rgb(255, 255, 255);
  228. background: url('../../../../assets/images/warn-dialog-bg.png') no-repeat center;
  229. background-size: 100% 100%;
  230. z-index: 9999999;
  231. color: #fff;
  232. .title {
  233. height: 32px;
  234. padding: 0 20px;
  235. :deep(.ant-badge:not(.ant-badge-status)) {
  236. margin-right: 40px !important;
  237. }
  238. display: flex;
  239. align-items: center;
  240. justify-content: space-between;
  241. margin-bottom: 5px;
  242. .message-title {
  243. font-size: 18px;
  244. padding-top: 10px;
  245. }
  246. .badge-box {
  247. display: flex;
  248. align-items: center;
  249. padding-top: 10px;
  250. .badge-title {
  251. display: inline-block;
  252. width: 62px;
  253. line-height: 32px;
  254. background-color: #2174f0;
  255. border-radius: 26px;
  256. text-align: center;
  257. color: #fff;
  258. padding-bottom: 2px;
  259. }
  260. }
  261. }
  262. .broadcast-context {
  263. .context-tab {
  264. display: flex;
  265. padding: 20px;
  266. .context-tab-item {
  267. line-height: 24px;
  268. background-color: #6b6b6b;
  269. border-radius: 24px;
  270. text-align: center;
  271. padding: 0 10px;
  272. color: #fff;
  273. margin: 5px;
  274. cursor: pointer;
  275. font-size: 14px;
  276. }
  277. .context-tab-item-active {
  278. background-color: #2174f0;
  279. }
  280. }
  281. .context-box {
  282. flex: 1;
  283. padding: 0 10px;
  284. height: 200px;
  285. overflow-y: auto;
  286. .no-context {
  287. display: flex;
  288. justify-content: center;
  289. padding-top: 30px;
  290. font-size: 16px;
  291. }
  292. .context-detail {
  293. display: flex;
  294. justify-content: space-between;
  295. line-height: 24px;
  296. padding: 0px 16px;
  297. div {
  298. display: flex;
  299. justify-content: flex-start;
  300. &:nth-child(1) {
  301. width: 44%;
  302. }
  303. &:nth-child(2) {
  304. width: 40%;
  305. }
  306. &:nth-child(3) {
  307. width: 25%;
  308. }
  309. &:nth-child(4) {
  310. width: 15%;
  311. }
  312. }
  313. }
  314. }
  315. .more {
  316. position: absolute;
  317. left: 24px;
  318. bottom: 10px;
  319. cursor: pointer;
  320. &:hover {
  321. color: #2174f0;
  322. }
  323. }
  324. }
  325. }
  326. :deep(.zxm-badge-count) {
  327. width: 8px;
  328. height: 8px;
  329. }
  330. </style>