index.vue 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268
  1. <template>
  2. <div :class="prefixCls">
  3. <Badge :count="count" :overflowCount="9" :offset="[-4, 10]" :numberStyle="numberStyle" @click="clickBadge">
  4. <BellOutlined />
  5. </Badge>
  6. <DynamicNotice ref="dynamicNoticeRef" v-bind="dynamicNoticeProps" />
  7. <DetailModal @register="registerDetail" />
  8. <sys-message-modal @register="registerMessageModal" @refresh="reloadCount" />
  9. </div>
  10. </template>
  11. <script lang="ts">
  12. import { computed, defineComponent, ref, unref, reactive, onMounted, getCurrentInstance } from 'vue';
  13. import { Popover, Tabs, Badge } from 'ant-design-vue';
  14. import { BellOutlined } from '@ant-design/icons-vue';
  15. import { tabListData } from './data';
  16. import { listCementByUser, editCementSend } from './notify.api';
  17. import NoticeList from './NoticeList.vue';
  18. import DetailModal from '/@/views/monitor/mynews/DetailModal.vue';
  19. import DynamicNotice from '/@/views/monitor/mynews/DynamicNotice.vue';
  20. import { useModal } from '/@/components/Modal';
  21. import { useDesign } from '/@/hooks/web/useDesign';
  22. import { useGlobSetting } from '/@/hooks/setting';
  23. import { useUserStore } from '/@/store/modules/user';
  24. import { connectWebSocket, onWebSocket } from '/@/hooks/web/useWebSocket';
  25. import { readAllMsg } from '/@/views/monitor/mynews/mynews.api';
  26. import { getToken } from '/@/utils/auth';
  27. import md5 from 'crypto-js/md5';
  28. import SysMessageModal from '/@/views/system/message/components/SysMessageModal.vue';
  29. export default defineComponent({
  30. components: {
  31. Popover,
  32. BellOutlined,
  33. Tabs,
  34. TabPane: Tabs.TabPane,
  35. Badge,
  36. NoticeList,
  37. DetailModal,
  38. DynamicNotice,
  39. SysMessageModal,
  40. },
  41. setup() {
  42. const { prefixCls } = useDesign('header-notify');
  43. const instance: any = getCurrentInstance();
  44. const userStore = useUserStore();
  45. const glob = useGlobSetting();
  46. const dynamicNoticeProps = reactive({ path: '', formData: {} });
  47. const [registerDetail, detailModal] = useModal();
  48. const listData = ref(tabListData);
  49. const count = computed(() => {
  50. let count = 0;
  51. for (let i = 0; i < listData.value.length; i++) {
  52. count += listData.value[i].count;
  53. }
  54. return count;
  55. });
  56. const [registerMessageModal, { openModal: openMessageModal }] = useModal();
  57. function clickBadge() {
  58. //消息列表弹窗前去除角标
  59. for (let i = 0; i < listData.value.length; i++) {
  60. listData.value[i].count = 0;
  61. }
  62. openMessageModal(true, {});
  63. }
  64. const popoverVisible = ref<boolean>(false);
  65. onMounted(() => {
  66. initWebSocket();
  67. });
  68. function mapAnnouncement(item) {
  69. return {
  70. ...item,
  71. title: item.titile,
  72. description: item.msgAbstract,
  73. datetime: item.sendTime,
  74. };
  75. }
  76. // 获取系统消息
  77. async function loadData() {
  78. try {
  79. let { anntMsgList, sysMsgList, anntMsgTotal, sysMsgTotal } = await listCementByUser({
  80. pageSize: 5,
  81. });
  82. listData.value[0].list = anntMsgList.map(mapAnnouncement);
  83. listData.value[1].list = sysMsgList.map(mapAnnouncement);
  84. listData.value[0].count = anntMsgTotal;
  85. listData.value[1].count = sysMsgTotal;
  86. } catch (e) {
  87. console.warn('系统消息通知异常:', e);
  88. }
  89. }
  90. loadData();
  91. function onNoticeClick(record) {
  92. try {
  93. editCementSend(record.id);
  94. loadData();
  95. } catch (e) {
  96. console.error(e);
  97. }
  98. if (record.openType === 'component') {
  99. dynamicNoticeProps.path = record.openPage;
  100. dynamicNoticeProps.formData = { id: record.busId };
  101. instance.refs.dynamicNoticeRef?.detail(record.openPage);
  102. } else {
  103. detailModal.openModal(true, {
  104. record,
  105. isUpdate: true,
  106. });
  107. }
  108. popoverVisible.value = false;
  109. }
  110. // 初始化 WebSocket
  111. function initWebSocket() {
  112. let token = getToken();
  113. //将登录token生成一个短的标识
  114. let wsClientId = md5(token);
  115. let userId = unref(userStore.getUserInfo).id + '_' + wsClientId;
  116. // WebSocket与普通的请求所用协议有所不同,ws等同于http,wss等同于https
  117. let url = glob.domainUrl?.replace('https://', 'wss://').replace('http://', 'ws://') + '/websocket/' + userId;
  118. connectWebSocket(url);
  119. onWebSocket(onWebSocketMessage);
  120. }
  121. function onWebSocketMessage(data) {
  122. if (data.cmd === 'topic' || data.cmd === 'user') {
  123. //update-begin-author:taoyan date:2022-7-13 for: VUEN-1674【严重bug】系统通知,为什么必须刷新右上角才提示
  124. //后台保存数据太慢 前端延迟刷新消息
  125. setTimeout(() => {
  126. loadData();
  127. }, 1000);
  128. //update-end-author:taoyan date:2022-7-13 for: VUEN-1674【严重bug】系统通知,为什么必须刷新右上角才提示
  129. }
  130. }
  131. // 清空消息
  132. function onEmptyNotify() {
  133. popoverVisible.value = false;
  134. readAllMsg({}, loadData);
  135. }
  136. async function reloadCount(id) {
  137. try {
  138. await editCementSend(id);
  139. await loadData();
  140. } catch (e) {
  141. console.error(e);
  142. }
  143. }
  144. return {
  145. prefixCls,
  146. listData,
  147. count,
  148. clickBadge,
  149. registerMessageModal,
  150. reloadCount,
  151. onNoticeClick,
  152. onEmptyNotify,
  153. numberStyle: {},
  154. popoverVisible,
  155. registerDetail,
  156. dynamicNoticeProps,
  157. };
  158. },
  159. });
  160. </script>
  161. <style lang="less">
  162. //noinspection LessUnresolvedVariable
  163. @prefix-cls: ~'@{namespace}-header-notify';
  164. @ventSpace: zxm;
  165. .@{prefix-cls} {
  166. padding-top: 2px;
  167. position: absolute;
  168. &__overlay {
  169. max-width: 340px;
  170. .@{ventSpace}-popover-inner-content {
  171. padding: 0;
  172. }
  173. .@{ventSpace}-tabs-nav {
  174. margin-bottom: 12px;
  175. }
  176. .@{ventSpace}-list-item {
  177. padding: 12px 24px;
  178. transition: background-color 300ms;
  179. }
  180. .bottom-buttons {
  181. text-align: center;
  182. border-top: 1px solid #f0f0f0;
  183. height: 42px;
  184. .@{ventSpace}-btn {
  185. border: 0;
  186. height: 100%;
  187. &:first-child {
  188. border-right: 1px solid #f0f0f0;
  189. }
  190. }
  191. }
  192. }
  193. .@{ventSpace}-tabs-content {
  194. width: 300px;
  195. }
  196. .@{ventSpace}-badge {
  197. font-size: 18px;
  198. .@{ventSpace}-badge-count {
  199. @badget-size: 16px;
  200. width: @badget-size;
  201. height: @badget-size;
  202. min-width: @badget-size;
  203. line-height: @badget-size;
  204. padding: 0;
  205. .@{ventSpace}-scroll-number-only > p.@{ventSpace}-scroll-number-only-unit {
  206. font-size: 14px;
  207. height: @badget-size;
  208. }
  209. }
  210. .@{ventSpace}-badge-multiple-words {
  211. padding: 0 0 0 2px;
  212. font-size: 12px;
  213. }
  214. svg {
  215. width: 0.9em;
  216. }
  217. }
  218. }
  219. // 兼容黑暗模式
  220. [data-theme='dark'] .@{prefix-cls} {
  221. &__overlay {
  222. .@{ventSpace}-list-item {
  223. &:hover {
  224. background-color: #111b26;
  225. }
  226. }
  227. .bottom-buttons {
  228. border-top: 1px solid #303030;
  229. .@{ventSpace}-btn {
  230. &:first-child {
  231. border-right: 1px solid #303030;
  232. }
  233. }
  234. }
  235. }
  236. }
  237. </style>