diff --git a/src/@core/utils/formatters.ts b/src/@core/utils/formatters.ts index dd8cfefd..342de36b 100644 --- a/src/@core/utils/formatters.ts +++ b/src/@core/utils/formatters.ts @@ -147,3 +147,23 @@ export function formatEp(nums: number[]): string { return formattedRanges.join('、') } + +// 将yyyy-mm-dd hh:mm:ss转换为时间差,如:1小时前,1天前 +export function formatDateDifference(dateString: string): string { + const date = new Date(dateString) + const currentDate = new Date() + const timeDifference = currentDate.getTime() - date.getTime() + const secondsDifference = Math.floor(timeDifference / 1000) + const minutesDifference = Math.floor(secondsDifference / 60) + const hoursDifference = Math.floor(minutesDifference / 60) + const daysDifference = Math.floor(hoursDifference / 24) + + if (daysDifference > 0) + return `${daysDifference}天前` + else if (hoursDifference > 0) + return `${hoursDifference}小时前` + else if (minutesDifference > 0) + return `${minutesDifference}分钟前` + else + return '刚刚' +} diff --git a/src/api/types.ts b/src/api/types.ts index 08c1e1e7..55a84427 100644 --- a/src/api/types.ts +++ b/src/api/types.ts @@ -1051,12 +1051,15 @@ export interface Message { // 消息时间 date?: string + // 登记时间 + reg_time?: string + // 用户ID userid?: string - // 用户名称 - username?: string - // 消息方向:0-接收,1-发送 action?: number + + // JSON + note?: string } diff --git a/src/components/cards/DownloadingCard.vue b/src/components/cards/DownloadingCard.vue index 05b4a556..a509602b 100644 --- a/src/components/cards/DownloadingCard.vue +++ b/src/components/cards/DownloadingCard.vue @@ -25,8 +25,8 @@ const isDownloading = ref(props.info?.state === 'downloading') // 监听props.info?.state的变化 watch(() => props.info?.state, (newValue) => { - isDownloading.value = newValue === 'downloading'; -}); + isDownloading.value = newValue === 'downloading' +}) // 图片是否加载完成 const imageLoaded = ref(false) diff --git a/src/components/cards/MessageCard.vue b/src/components/cards/MessageCard.vue index e562d284..62b06801 100644 --- a/src/components/cards/MessageCard.vue +++ b/src/components/cards/MessageCard.vue @@ -1,5 +1,6 @@ diff --git a/src/layouts/components/ShortcutBar.vue b/src/layouts/components/ShortcutBar.vue index e53b191a..c221dd2a 100644 --- a/src/layouts/components/ShortcutBar.vue +++ b/src/layouts/components/ShortcutBar.vue @@ -6,6 +6,7 @@ import RuleTestView from '@/views/system/RuleTestView.vue' import ModuleTestView from '@/views/system/ModuleTestView.vue' import MessageView from '@/views/system/MessageView.vue' import store from '@/store' +import api from '@/api' // App捷径 const appsMenu = ref(false) @@ -28,11 +29,48 @@ const systemTestDialog = ref(false) // 消息中心弹窗 const messageDialog = ref(false) +// 输入消息 +const user_message = ref('') + +// 发送按钮是否可用 +const sendButtonDisabled = ref(false) + +// 聊天容器 +const chatContainer = ref() + +// 滚动到底部 +function scrollMessageToEnd() { + nextTick(() => { + if (chatContainer.value) + chatContainer.value.scrollTop = chatContainer.value.scrollHeight + }) +} + // 拼接全部日志url function allLoggingUrl() { const token = store.state.auth.token return `${import.meta.env.VITE_API_BASE_URL}system/logging?token=${token}&length=-1` } + +// 发送消息 +async function sendMessage() { + if (user_message.value) { + try { + sendButtonDisabled.value = true + await api.post(`message/web?text=${user_message.value}`) + user_message.value = '' + sendButtonDisabled.value = false + scrollMessageToEnd() + } + catch (error) { + console.error(error) + } + } +} + +onMounted(() => { + scrollMessageToEnd() +}) diff --git a/src/views/system/MessageView.vue b/src/views/system/MessageView.vue index 73363bef..669f5c6e 100644 --- a/src/views/system/MessageView.vue +++ b/src/views/system/MessageView.vue @@ -4,24 +4,15 @@ import MessageCard from '@/components/cards/MessageCard.vue' import type { Message } from '@/api/types' import api from '@/api' +// 定义事件 +const emit = defineEmits(['scroll']) + // 消息列表 const messages = ref([]) // 是否完成加载 const isLoaded = ref(false) -// 输入消息 -const content = ref('') - -// 聊天容器 -const chatContainer = ref() - -// 滚动到底部 -function scrollToEnd() { - if (chatContainer.value) - chatContainer.value.scrollTop = chatContainer.value.scrollHeight -} - // SSE持续获取消息 function startSSEMessager() { const token = store.state.auth.token @@ -35,7 +26,7 @@ function startSSEMessager() { if (message) { const object = JSON.parse(message) messages.value.push(object) - scrollToEnd() + emit('scroll') } isLoaded.value = true }) @@ -52,7 +43,7 @@ async function loadMessages() { messages.value = await api.get('message/web') if (messages.value.length > 0) { isLoaded.value = true - scrollToEnd() + emit('scroll') } } catch (error) { @@ -86,7 +77,7 @@ onMounted(() => { > 当前没有消息 - +
{ }" > { /> - +