Skip to content

Commit

Permalink
refactor: 消息通知点开后才查询消息列表
Browse files Browse the repository at this point in the history
  • Loading branch information
Charles7c committed May 23, 2024
1 parent 5c6d311 commit c104ba5
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 44 deletions.
5 changes: 5 additions & 0 deletions src/apis/system/message.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,8 @@ export function deleteMessage(ids: string | Array<string>) {
export function readMessage(ids?: string | Array<string>) {
return http.patch(`${BASE_URL}/read`, ids)
}

/** @desc 查询未读消息数量 */
export function getUnreadMessageCount() {
return http.get(`${BASE_URL}/unread`)
}
48 changes: 32 additions & 16 deletions src/layout/components/HeaderRightBar/Message.vue
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
<template>
<div class="message">
<a-list>
<a-list :loading="loading">
<template #header>通知</template>
<a-list-item v-for="item in props.data" :key="item.id">
<a-list-item v-for="item in messageList" :key="item.id">
<div class="content-wrapper" @click="open">
<div class="content">{{ item.title }}</div>
<div class="date">{{ item.createTime }}</div>
Expand All @@ -19,19 +19,33 @@
</template>

<script setup lang="ts">
import { readMessage } from '@/apis'
import { onMounted } from 'vue'
import { type MessageResp, listMessage, readMessage } from '@/apis'
const props = defineProps({
data: {
type: Array as PropType<any>, // 简化数据结构以便测试
required: true
},
fetch: {
type: Function, // 简化数据结构以便测试
required: true
}
const emit = defineEmits<{
(e: 'readall-success'): void
}>()
const queryParam = reactive({
isRead: false,
sort: ['createTime,desc'],
page: 1,
size: 5
})
const messageList = ref<MessageResp[]>()
const loading = ref(false)
// 查询消息数据
const getMessageData = async () => {
try {
loading.value = true
const { data } = await listMessage(queryParam)
messageList.value = data.list
} finally {
loading.value = false
}
}
// 打开消息中心
const open = () => {
window.open('/#/setting/message')
Expand All @@ -40,8 +54,13 @@ const open = () => {
// 全部已读
const readAll = async () => {
await readMessage()
props.fetch()
await getMessageData()
emit('readall-success')
}
onMounted(() => {
getMessageData()
})
</script>

<style lang="scss" scoped>
Expand Down Expand Up @@ -71,9 +90,6 @@ const readAll = async () => {
&:hover {
background-color: var(--color-bg-4);
}
&:active {
color: rgb(var(--arcoblue-6));
}
}
Expand Down
40 changes: 13 additions & 27 deletions src/layout/components/HeaderRightBar/index.vue
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,15 @@
:content-style="{ marginTop: '-5px', padding: 0, border: 'none' }"
:arrow-style="{ width: 0, height: 0 }"
>
<a-badge :count="messageData.length" dot>
<a-badge :count="unreadMessageCount" dot>
<a-button size="mini" class="gi_hover_btn">
<template #icon>
<icon-notification :size="18" />
</template>
</a-button>
</a-badge>
<template #content>
<Message :fetch="getMessageData" :data="messageData" />
<Message @readall-success="getMessageCount" />
</template>
</a-popover>

Expand Down Expand Up @@ -74,10 +74,9 @@
import { Modal } from '@arco-design/web-vue'
import { useFullscreen } from '@vueuse/core'
import { onMounted, ref } from 'vue'
import dayjs from 'dayjs'
import Message from './Message.vue'
import SettingDrawer from './SettingDrawer.vue'
import { listMessage } from '@/apis'
import { getUnreadMessageCount } from '@/apis'
import { useUserStore } from '@/stores'
import { isMobile } from '@/utils'
import { getToken } from '@/utils/auth'
Expand All @@ -91,24 +90,17 @@ onBeforeUnmount(() => {
}
})
const messageData = ref<Array<any>>([])
const createWebSocket = (token) => {
const unreadMessageCount = ref(0)
// 初始化 WebSocket
const initWebSocket = (token: string) => {
socket = new WebSocket(`${import.meta.env.VITE_API_WS_URL}/ws?token=${token}`)
socket.onopen = () => {
// console.log('WebSocket connection opened')
}
socket.onmessage = (event) => {
const data = JSON.parse(event.data)
messageData.value.unshift({
id: data?.id,
title: data?.content,
type: data?.type,
isRead: data?.isRead,
createTime:
data?.sendTime
&& dayjs(data.sendTime).format('YYYY-MM-DD HH:mm:ss')
})
unreadMessageCount.value = Number.parseInt(data?.content)
}
socket.onerror = () => {
Expand All @@ -120,19 +112,13 @@ const createWebSocket = (token) => {
}
}
// 查询消息通知
const queryMessageParam = reactive({
isRead: false,
sort: ['createTime,desc'],
page: 1,
size: 5
})
const getMessageData = async () => {
// 查询未读消息数量
const getMessageCount = async () => {
const { data } = await getUnreadMessageCount()
unreadMessageCount.value = data.total
const token = getToken()
const { data } = await listMessage(queryMessageParam)
messageData.value = data.list
if (token) {
createWebSocket(token)
initWebSocket(token)
}
}
Expand Down Expand Up @@ -187,7 +173,7 @@ const checkPasswordExpired = () => {
onMounted(() => {
checkPasswordExpired()
getMessageData()
getMessageCount()
})
</script>

Expand Down
4 changes: 3 additions & 1 deletion src/views/system/user/UserDetailDrawer.vue
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
<template>
<a-drawer v-model:visible="visible" title="用户详情" :width="width >= 600 ? 600 : '100%'" :footer="false">
<a-descriptions :column="2" size="large" class="general-description">
<a-descriptions-item label="ID" :span="2">{{ dataDetail?.id }}</a-descriptions-item>
<a-descriptions-item label="ID" :span="2">
<a-typography-paragraph copyable>{{ dataDetail?.id }}</a-typography-paragraph>
</a-descriptions-item>
<a-descriptions-item label="用户名">
<a-typography-paragraph :copyable="!!dataDetail?.username">{{ dataDetail?.username }}</a-typography-paragraph>
</a-descriptions-item>
Expand Down

0 comments on commit c104ba5

Please sign in to comment.