index.vue 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  1. <template>
  2. <div :class="prefixCls">
  3. <Popover v-model:visible="popoverVisible" title="" trigger="click" :overlayClassName="`${prefixCls}__overlay`">
  4. <Badge :count="count" :overflowCount="9" :offset="[-4, 10]" :numberStyle="numberStyle">
  5. <BellOutlined />
  6. </Badge>
  7. <template #content>
  8. <Tabs>
  9. <template v-for="item in listData" :key="item.key">
  10. <TabPane>
  11. <template #tab>
  12. {{ item.name }}
  13. <span v-if="item.list.length !== 0">({{ item.count }})</span>
  14. </template>
  15. <!-- 绑定title-click事件的通知列表中标题是“可点击”的-->
  16. <NoticeList :list="item.list" @title-click="onNoticeClick" />
  17. </TabPane>
  18. </template>
  19. </Tabs>
  20. <a-row class="bottom-buttons">
  21. <a-col :span="count === 0 ? 0 : 12">
  22. <a-button @click="onEmptyNotify" type="dashed" block>清空消息</a-button>
  23. </a-col>
  24. <a-col :span="count === 0 ? 24 : 12">
  25. <a-button @click="popoverVisible = false" type="dashed" block>
  26. <router-link to="/monitor/mynews">查看更多</router-link>
  27. </a-button>
  28. </a-col>
  29. </a-row>
  30. </template>
  31. </Popover>
  32. <DynamicNotice ref="dynamicNoticeRef" v-bind="dynamicNoticeProps" />
  33. <DetailModal @register="registerDetail" />
  34. </div>
  35. </template>
  36. <script lang="ts">
  37. import { computed, defineComponent, ref, unref, reactive, onMounted, getCurrentInstance } from 'vue';
  38. import { Popover, Tabs, Badge } from 'ant-design-vue';
  39. import { BellOutlined } from '@ant-design/icons-vue';
  40. import { tabListData } from './data';
  41. import { listCementByUser, editCementSend } from './notify.api';
  42. import NoticeList from './NoticeList.vue';
  43. import DetailModal from '/@/views/monitor/mynews/DetailModal.vue';
  44. import DynamicNotice from '/@/views/monitor/mynews/DynamicNotice.vue';
  45. import { useModal } from '/@/components/Modal';
  46. import { useDesign } from '/@/hooks/web/useDesign';
  47. import { useGlobSetting } from '/@/hooks/setting';
  48. import { useUserStore } from '/@/store/modules/user';
  49. import { connectWebSocket, onWebSocket } from '/@/hooks/web/useWebSocket';
  50. import { readAllMsg } from '/@/views/monitor/mynews/mynews.api';
  51. import { getToken } from '/@/utils/auth';
  52. export default defineComponent({
  53. components: {
  54. Popover,
  55. BellOutlined,
  56. Tabs,
  57. TabPane: Tabs.TabPane,
  58. Badge,
  59. NoticeList,
  60. DetailModal,
  61. DynamicNotice,
  62. },
  63. setup() {
  64. const { prefixCls } = useDesign('header-notify');
  65. const instance: any = getCurrentInstance();
  66. const userStore = useUserStore();
  67. const glob = useGlobSetting();
  68. const dynamicNoticeProps = reactive({ path: '', formData: {} });
  69. const [registerDetail, detailModal] = useModal();
  70. const popoverVisible = ref<boolean>(false);
  71. const listData = ref(tabListData);
  72. listData.value[0].list = [];
  73. listData.value[1].list = [];
  74. listData.value[0].count = 0;
  75. listData.value[1].count = 0;
  76. onMounted(() => {
  77. initWebSocket();
  78. });
  79. const count = computed(() => {
  80. let count = 0;
  81. for (let i = 0; i < listData.value.length; i++) {
  82. count += listData.value[i].count;
  83. }
  84. return count;
  85. });
  86. function mapAnnouncement(item) {
  87. return {
  88. ...item,
  89. title: item.titile,
  90. description: item.msgAbstract,
  91. datetime: item.sendTime,
  92. };
  93. }
  94. // 获取系统消息
  95. async function loadData() {
  96. try {
  97. let { anntMsgList, sysMsgList, anntMsgTotal, sysMsgTotal } = await listCementByUser({
  98. pageSize: 5,
  99. });
  100. listData.value[0].list = anntMsgList.map(mapAnnouncement);
  101. listData.value[1].list = sysMsgList.map(mapAnnouncement);
  102. listData.value[0].count = anntMsgTotal;
  103. listData.value[1].count = sysMsgTotal;
  104. } catch (e) {
  105. console.warn('系统消息通知异常:', e);
  106. }
  107. }
  108. loadData();
  109. function onNoticeClick(record) {
  110. try {
  111. editCementSend(record.id);
  112. loadData();
  113. } catch (e) {
  114. console.error(e);
  115. }
  116. if (record.openType === 'component') {
  117. dynamicNoticeProps.path = record.openPage;
  118. dynamicNoticeProps.formData = { id: record.busId };
  119. instance.refs.dynamicNoticeRef?.detail(record.openPage);
  120. } else {
  121. detailModal.openModal(true, {
  122. record,
  123. isUpdate: true,
  124. });
  125. }
  126. popoverVisible.value = false;
  127. }
  128. // 初始化 WebSocket
  129. function initWebSocket() {
  130. let userId = unref(userStore.getUserInfo).id;
  131. let token = getToken();
  132. // WebSocket与普通的请求所用协议有所不同,ws等同于http,wss等同于https
  133. let url = glob.domainUrl?.replace('https://', 'wss://').replace('http://', 'ws://') + '/websocket/' + userId;
  134. connectWebSocket(url);
  135. onWebSocket(onWebSocketMessage);
  136. }
  137. function onWebSocketMessage(data) {
  138. if (data.cmd === 'topic' || data.cmd === 'user') {
  139. //update-begin-author:taoyan date:2022-7-13 for: VUEN-1674【严重bug】系统通知,为什么必须刷新右上角才提示
  140. //后台保存数据太慢 前端延迟刷新消息
  141. setTimeout(() => {
  142. loadData();
  143. }, 1000);
  144. //update-end-author:taoyan date:2022-7-13 for: VUEN-1674【严重bug】系统通知,为什么必须刷新右上角才提示
  145. }
  146. }
  147. // 清空消息
  148. function onEmptyNotify() {
  149. popoverVisible.value = false;
  150. readAllMsg({}, loadData);
  151. }
  152. return {
  153. prefixCls,
  154. listData,
  155. count,
  156. onNoticeClick,
  157. onEmptyNotify,
  158. numberStyle: {},
  159. popoverVisible,
  160. registerDetail,
  161. dynamicNoticeProps,
  162. };
  163. },
  164. });
  165. </script>
  166. <style lang="less">
  167. //noinspection LessUnresolvedVariable
  168. @prefix-cls: ~'@{namespace}-header-notify';
  169. .@{prefix-cls} {
  170. padding-top: 2px;
  171. &__overlay {
  172. max-width: 340px;
  173. .ant-popover-inner-content {
  174. padding: 0;
  175. }
  176. .ant-tabs-bar {
  177. margin-bottom: 12px;
  178. }
  179. .ant-list-item {
  180. padding: 12px 24px;
  181. transition: background-color 300ms;
  182. &:hover {
  183. background-color: #e6f7ff;
  184. }
  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>