index.vue 7.7 KB

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