提交 | 用户 | 时间
|
820397
|
1 |
<script lang="ts" setup> |
H |
2 |
import { formatDate } from '@/utils/formatTime' |
|
3 |
import * as NotifyMessageApi from '@/api/system/notify/message' |
3e359e
|
4 |
import { useUserStoreWithOut } from '@/store/modules/user' |
820397
|
5 |
|
H |
6 |
defineOptions({ name: 'Message' }) |
|
7 |
|
|
8 |
const { push } = useRouter() |
3e359e
|
9 |
const userStore = useUserStoreWithOut() |
820397
|
10 |
const activeName = ref('notice') |
H |
11 |
const unreadCount = ref(0) // 未读消息数量 |
|
12 |
const list = ref<any[]>([]) // 消息列表 |
|
13 |
|
|
14 |
// 获得消息列表 |
|
15 |
const getList = async () => { |
|
16 |
list.value = await NotifyMessageApi.getUnreadNotifyMessageList() |
|
17 |
// 强制设置 unreadCount 为 0,避免小红点因为轮询太慢,不消除 |
|
18 |
unreadCount.value = 0 |
|
19 |
} |
|
20 |
|
|
21 |
// 获得未读消息数 |
|
22 |
const getUnreadCount = async () => { |
|
23 |
NotifyMessageApi.getUnreadNotifyMessageCount().then((data) => { |
|
24 |
unreadCount.value = data |
|
25 |
}) |
|
26 |
} |
|
27 |
|
|
28 |
// 跳转我的站内信 |
|
29 |
const goMyList = () => { |
|
30 |
push({ |
|
31 |
name: 'MyNotifyMessage' |
|
32 |
}) |
|
33 |
} |
|
34 |
|
|
35 |
// ========== 初始化 ========= |
|
36 |
onMounted(() => { |
|
37 |
// 首次加载小红点 |
|
38 |
getUnreadCount() |
|
39 |
// 轮询刷新小红点 |
|
40 |
setInterval( |
|
41 |
() => { |
3e359e
|
42 |
if (userStore.getIsSetUser) { |
H |
43 |
getUnreadCount() |
|
44 |
} else { |
|
45 |
unreadCount.value = 0 |
|
46 |
} |
820397
|
47 |
}, |
H |
48 |
1000 * 60 * 2 |
|
49 |
) |
|
50 |
}) |
|
51 |
</script> |
|
52 |
<template> |
|
53 |
<div class="message"> |
|
54 |
<ElPopover :width="400" placement="bottom" trigger="click"> |
|
55 |
<template #reference> |
|
56 |
<ElBadge :is-dot="unreadCount > 0" class="item"> |
|
57 |
<Icon :size="18" class="cursor-pointer" icon="ep:bell" @click="getList" /> |
|
58 |
</ElBadge> |
|
59 |
</template> |
|
60 |
<ElTabs v-model="activeName"> |
|
61 |
<ElTabPane label="我的站内信" name="notice"> |
|
62 |
<el-scrollbar class="message-list"> |
|
63 |
<template v-for="item in list" :key="item.id"> |
|
64 |
<div class="message-item"> |
|
65 |
<img alt="" class="message-icon" src="@/assets/imgs/avatar.gif" /> |
|
66 |
<div class="message-content"> |
|
67 |
<span class="message-title"> |
|
68 |
{{ item.templateNickname }}:{{ item.templateContent }} |
|
69 |
</span> |
|
70 |
<span class="message-date"> |
|
71 |
{{ formatDate(item.createTime) }} |
|
72 |
</span> |
|
73 |
</div> |
|
74 |
</div> |
|
75 |
</template> |
|
76 |
</el-scrollbar> |
|
77 |
</ElTabPane> |
|
78 |
</ElTabs> |
|
79 |
<!-- 更多 --> |
|
80 |
<div style="margin-top: 10px; text-align: right"> |
|
81 |
<XButton preIcon="ep:view" title="查看全部" type="primary" @click="goMyList" /> |
|
82 |
</div> |
|
83 |
</ElPopover> |
|
84 |
</div> |
|
85 |
</template> |
|
86 |
<style lang="scss" scoped> |
|
87 |
.message-empty { |
|
88 |
display: flex; |
|
89 |
flex-direction: column; |
|
90 |
align-items: center; |
|
91 |
justify-content: center; |
|
92 |
height: 260px; |
|
93 |
line-height: 45px; |
|
94 |
} |
|
95 |
|
|
96 |
.message-list { |
|
97 |
display: flex; |
|
98 |
height: 400px; |
|
99 |
flex-direction: column; |
|
100 |
|
|
101 |
.message-item { |
|
102 |
display: flex; |
|
103 |
align-items: center; |
|
104 |
padding: 20px 0; |
|
105 |
border-bottom: 1px solid var(--el-border-color-light); |
|
106 |
|
|
107 |
&:last-child { |
|
108 |
border: none; |
|
109 |
} |
|
110 |
|
|
111 |
.message-icon { |
|
112 |
width: 40px; |
|
113 |
height: 40px; |
|
114 |
margin: 0 20px 0 5px; |
|
115 |
} |
|
116 |
|
|
117 |
.message-content { |
|
118 |
display: flex; |
|
119 |
flex-direction: column; |
|
120 |
|
|
121 |
.message-title { |
|
122 |
margin-bottom: 5px; |
|
123 |
} |
|
124 |
|
|
125 |
.message-date { |
|
126 |
font-size: 12px; |
|
127 |
color: var(--el-text-color-secondary); |
|
128 |
} |
|
129 |
} |
|
130 |
} |
|
131 |
} |
|
132 |
</style> |