index.vue 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274
  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. const messageCount = ref<number>(0);
  69. function mapAnnouncement(item) {
  70. return {
  71. ...item,
  72. title: item.titile,
  73. description: item.msgAbstract,
  74. datetime: item.sendTime,
  75. };
  76. }
  77. // 获取系统消息
  78. async function loadData() {
  79. try {
  80. let { anntMsgList, sysMsgList, anntMsgTotal, sysMsgTotal } = await listCementByUser({
  81. pageSize: 5,
  82. });
  83. listData.value[0].list = anntMsgList.map(mapAnnouncement);
  84. listData.value[1].list = sysMsgList.map(mapAnnouncement);
  85. listData.value[0].count = anntMsgTotal;
  86. listData.value[1].count = sysMsgTotal;
  87. //update-begin-author:taoyan date:2022-8-30 for: 消息数量改变触发chat组件事件
  88. let msgCount = anntMsgTotal + sysMsgTotal;
  89. //update-begin-author:wangshuai date:2022-09-02 for: 消息未读数为0也需要传递,因为聊天需要计算总数
  90. messageCount.value = msgCount;
  91. //update-end-author:wangshuai date:2022-09-02 for: 消息未读数为0也需要传递,因为聊天需要计算总数
  92. //update-end-author:taoyan date:2022-8-30 for: 消息数量改变触发chat组件事件
  93. } catch (e) {
  94. console.warn('系统消息通知异常:', e);
  95. }
  96. }
  97. loadData();
  98. function onNoticeClick(record) {
  99. try {
  100. editCementSend(record.id);
  101. loadData();
  102. } catch (e) {
  103. console.error(e);
  104. }
  105. if (record.openType === 'component') {
  106. dynamicNoticeProps.path = record.openPage;
  107. dynamicNoticeProps.formData = { id: record.busId };
  108. instance.refs.dynamicNoticeRef?.detail(record.openPage);
  109. } else {
  110. detailModal.openModal(true, {
  111. record,
  112. isUpdate: true,
  113. });
  114. }
  115. popoverVisible.value = false;
  116. }
  117. // 初始化 WebSocket
  118. function initWebSocket() {
  119. let token = getToken();
  120. //将登录token生成一个短的标识
  121. let wsClientId = md5(token);
  122. let userId = unref(userStore.getUserInfo).id + '_' + wsClientId;
  123. // WebSocket与普通的请求所用协议有所不同,ws等同于http,wss等同于https
  124. let url = glob.domainUrl?.replace('https://', 'wss://').replace('http://', 'ws://') + '/websocket/' + userId;
  125. connectWebSocket(url);
  126. onWebSocket(onWebSocketMessage);
  127. }
  128. function onWebSocketMessage(data) {
  129. if (data.cmd === 'topic' || data.cmd === 'user') {
  130. //update-begin-author:taoyan date:2022-7-13 for: VUEN-1674【严重bug】系统通知,为什么必须刷新右上角才提示
  131. //后台保存数据太慢 前端延迟刷新消息
  132. setTimeout(() => {
  133. loadData();
  134. }, 1000);
  135. //update-end-author:taoyan date:2022-7-13 for: VUEN-1674【严重bug】系统通知,为什么必须刷新右上角才提示
  136. }
  137. }
  138. // 清空消息
  139. function onEmptyNotify() {
  140. popoverVisible.value = false;
  141. readAllMsg({}, loadData);
  142. }
  143. async function reloadCount(id) {
  144. try {
  145. await editCementSend(id);
  146. await loadData();
  147. } catch (e) {
  148. console.error(e);
  149. }
  150. }
  151. return {
  152. prefixCls,
  153. listData,
  154. count,
  155. clickBadge,
  156. registerMessageModal,
  157. reloadCount,
  158. onNoticeClick,
  159. onEmptyNotify,
  160. numberStyle: {},
  161. popoverVisible,
  162. registerDetail,
  163. dynamicNoticeProps,
  164. };
  165. },
  166. });
  167. </script>
  168. <style lang="less">
  169. //noinspection LessUnresolvedVariable
  170. @prefix-cls: ~'@{namespace}-header-notify';
  171. .@{prefix-cls} {
  172. padding-top: 2px;
  173. position: absolute;
  174. &__overlay {
  175. max-width: 340px;
  176. .ant-popover-inner-content {
  177. padding: 0;
  178. }
  179. .ant-tabs-nav {
  180. margin-bottom: 12px;
  181. }
  182. .ant-list-item {
  183. padding: 12px 24px;
  184. transition: background-color 300ms;
  185. }
  186. .bottom-buttons {
  187. text-align: center;
  188. border-top: 1px solid #f0f0f0;
  189. height: 42px;
  190. .ant-btn {
  191. border: 0;
  192. height: 100%;
  193. &:first-child {
  194. border-right: 1px solid #f0f0f0;
  195. }
  196. }
  197. }
  198. }
  199. .ant-tabs-content {
  200. width: 300px;
  201. }
  202. .ant-badge {
  203. font-size: 18px;
  204. .ant-badge-count {
  205. @badget-size: 16px;
  206. width: @badget-size;
  207. height: @badget-size;
  208. min-width: @badget-size;
  209. line-height: @badget-size;
  210. padding: 0;
  211. .ant-scroll-number-only > p.ant-scroll-number-only-unit {
  212. font-size: 14px;
  213. height: @badget-size;
  214. }
  215. }
  216. .ant-badge-multiple-words {
  217. padding: 0 0 0 2px;
  218. font-size: 12px;
  219. }
  220. svg {
  221. width: 0.9em;
  222. }
  223. }
  224. }
  225. // 兼容黑暗模式
  226. [data-theme='dark'] .@{prefix-cls} {
  227. &__overlay {
  228. .ant-list-item {
  229. &:hover {
  230. background-color: #111b26;
  231. }
  232. }
  233. .bottom-buttons {
  234. border-top: 1px solid #303030;
  235. .ant-btn {
  236. &:first-child {
  237. border-right: 1px solid #303030;
  238. }
  239. }
  240. }
  241. }
  242. }
  243. </style>