MessageBroadcast.vue 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275
  1. <template>
  2. <div style="position: fixed; z-index: 999; right: 120px; top: 20px; 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: 16px; color: #fff" @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' : '#eee' }"
  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. import dayjs from 'dayjs';
  54. export default defineComponent({
  55. name: 'VoiceBroadcast',
  56. components: { Icon, Tooltip, Badge, SoundOutlined, BellOutlined, WarningOutlined },
  57. setup() {
  58. const speakVoice = new SpeakVoice();
  59. const userStore = useUserStore();
  60. const glob = useGlobSetting();
  61. const router = useRouter();
  62. const list = (params) => defHttp.get({ url: '/safety/ventanalyAlarmLog/list', params });
  63. const activeKey = ref(0);
  64. const isShowWarningBroad = ref(false);
  65. const isBroad = ref(true);
  66. const isWarningDot = ref(false);
  67. const broadcastList = ref([]);
  68. function showWarningBroad() {
  69. isShowWarningBroad.value = !isShowWarningBroad.value;
  70. if (isShowWarningBroad.value) {
  71. toSelectList(0);
  72. }
  73. }
  74. function handleBroad() {
  75. isBroad.value = !isBroad.value;
  76. }
  77. async function toSelectList(key) {
  78. activeKey.value = key;
  79. const res = await list({ pageSize: 20, devicetype: '', isok: key == 1 ? 0 : key == 2 ? 1 : null });
  80. broadcastList.value = res['records'];
  81. // const isHasWarning = broadcastList.value.findIndex((item) => !item['isok']);
  82. // isWarningDot.value = isHasWarning > -1 ? true : false;
  83. }
  84. async function toMore() {
  85. await router.push({ path: '/monitorChannel/device-monitor/warningHistory' });
  86. showWarningBroad();
  87. }
  88. // 初始化 WebSocket
  89. function initWebSocket() {
  90. let token = getToken();
  91. //将登录token生成一个短的标识
  92. // let wsClientId = md5(token);
  93. // let userId = unref(userStore.getUserInfo).id + '_' + wsClientId;
  94. let userId = unref(userStore.getUserInfo).id + '?token=' + token;
  95. // WebSocket与普通的请求所用协议有所不同,ws等同于http,wss等同于https
  96. let url = glob.wsUrl?.replace('https://', 'wss://').replace('http://', 'ws://') + '/websocket/' + userId;
  97. connectWebSocket(url);
  98. onWebSocket(onWebSocketMessage);
  99. }
  100. function onWebSocketMessage(data) {
  101. console.log('WebSocket 监测消息--------------》', data);
  102. if (data.topic === 'warn' || data.cmd === 'user') {
  103. if (isBroad.value) {
  104. const messageText = data['warndata'];
  105. // const messageText = '这是一个测试';
  106. speakVoice.handleReply(messageText);
  107. const time = dayjs().format('YYYY-MM-DD HH:mm:ss');
  108. console.log(time + '语音播报开始报警------>', data);
  109. }
  110. if (!isShowWarningBroad.value) {
  111. isWarningDot.value = true;
  112. } else {
  113. isWarningDot.value = false;
  114. }
  115. setTimeout(() => {
  116. if (isShowWarningBroad.value) {
  117. toSelectList(0);
  118. }
  119. }, 0);
  120. }
  121. }
  122. onMounted(() => {
  123. initWebSocket();
  124. });
  125. return { showWarningBroad, isShowWarningBroad, activeKey, toSelectList, broadcastList, toMore, isBroad, handleBroad, isWarningDot };
  126. },
  127. });
  128. </script>
  129. <style lang="less" scoped>
  130. .btn {
  131. line-height: 30px;
  132. margin-right: 20px;
  133. cursor: pointer;
  134. display: flex;
  135. }
  136. .no-play {
  137. background: linear-gradient(
  138. to bottom left,
  139. transparent 0%,
  140. transparent calc(50% - 1px),
  141. #000000 50%,
  142. transparent calc(50% + 1px),
  143. transparent 100%
  144. );
  145. }
  146. .broadcast {
  147. width: 400px;
  148. height: 250px;
  149. border-radius: 4px;
  150. position: fixed;
  151. top: 50px;
  152. right: 20px;
  153. background-color: rgb(255, 255, 255);
  154. background: url('../../../../assets/images/warn-dialog-bg.png') no-repeat center;
  155. background-size: 100% 100%;
  156. z-index: 9999999;
  157. color: #fff;
  158. .title {
  159. height: 32px;
  160. padding: 0 20px;
  161. :deep(.ant-badge:not(.ant-badge-status)) {
  162. margin-right: 40px !important;
  163. }
  164. display: flex;
  165. align-items: center;
  166. justify-content: space-between;
  167. margin-bottom: 5px;
  168. .message-title {
  169. font-size: 16px;
  170. }
  171. .badge-box {
  172. display: flex;
  173. align-items: center;
  174. .badge-title {
  175. display: inline-block;
  176. width: 62px;
  177. line-height: 32px;
  178. background-color: #2174f0;
  179. border-radius: 26px;
  180. text-align: center;
  181. color: #fff;
  182. padding-bottom: 2px;
  183. }
  184. }
  185. }
  186. .broadcast-context {
  187. .context-tab {
  188. display: flex;
  189. .context-tab-item {
  190. line-height: 24px;
  191. background-color: #6b6b6b;
  192. border-radius: 24px;
  193. text-align: center;
  194. padding: 0 10px;
  195. color: #fff;
  196. margin: 5px;
  197. cursor: pointer;
  198. font-size: 14px;
  199. }
  200. .context-tab-item-active {
  201. background-color: #2174f0;
  202. }
  203. }
  204. .context-box {
  205. flex: 1;
  206. padding: 0 10px;
  207. height: 178px;
  208. overflow-y: auto;
  209. .no-context {
  210. display: flex;
  211. justify-content: center;
  212. padding-top: 30px;
  213. font-size: 16px;
  214. }
  215. .context-detail {
  216. display: flex;
  217. justify-content: space-between;
  218. line-height: 24px;
  219. padding: 0px 3px;
  220. div {
  221. display: flex;
  222. justify-content: flex-start;
  223. &:nth-child(1) {
  224. width: 40%;
  225. }
  226. &:nth-child(2) {
  227. width: 20%;
  228. }
  229. &:nth-child(3) {
  230. width: 25%;
  231. }
  232. &:nth-child(4) {
  233. width: 15%;
  234. }
  235. }
  236. }
  237. .more {
  238. cursor: pointer;
  239. &:hover {
  240. color: #2174f0;
  241. }
  242. }
  243. }
  244. }
  245. }
  246. </style>