|
@@ -3,7 +3,7 @@
|
|
<van-nav-bar @click-left="router.back()" @click-right="onClickRight">
|
|
<van-nav-bar @click-left="router.back()" @click-right="onClickRight">
|
|
<template #left>
|
|
<template #left>
|
|
<div>
|
|
<div>
|
|
- <van-icon name="arrow-left" color="black" size="18" />
|
|
|
|
|
|
+ <van-icon name="arrow-left" color="black" size="18"/>
|
|
</div>
|
|
</div>
|
|
</template>
|
|
</template>
|
|
<template #title>
|
|
<template #title>
|
|
@@ -19,37 +19,34 @@
|
|
</div>
|
|
</div>
|
|
</template>
|
|
</template>
|
|
<template #right>
|
|
<template #right>
|
|
- <van-icon name="ellipsis" color="black" size="18" />
|
|
|
|
|
|
+ <van-icon name="ellipsis" color="black" size="18"/>
|
|
</template>
|
|
</template>
|
|
</van-nav-bar>
|
|
</van-nav-bar>
|
|
|
|
|
|
<van-notice-bar
|
|
<van-notice-bar
|
|
- v-if="groupInfo?.groupNotice?.messageContent"
|
|
|
|
- left-icon="volume-o"
|
|
|
|
- mode="link"
|
|
|
|
- :text="groupInfo?.groupNotice?.messageContent"
|
|
|
|
|
|
+ v-if="groupInfo?.groupNotice?.messageContent"
|
|
|
|
+ left-icon="volume-o"
|
|
|
|
+ mode="link"
|
|
|
|
+ :text="groupInfo?.groupNotice?.messageContent"
|
|
></van-notice-bar>
|
|
></van-notice-bar>
|
|
|
|
|
|
<template v-if="showPage">
|
|
<template v-if="showPage">
|
|
- <van-list
|
|
|
|
- ref="chatListRef"
|
|
|
|
- class="flex-1 overflow-y-auto px-12 flex flex-col"
|
|
|
|
- :finished="true"
|
|
|
|
- finished-text=""
|
|
|
|
- >
|
|
|
|
- <template v-for="(message, index) in currConversationChatList" :key="index">
|
|
|
|
- <ChatMessage :show-name="true" :message="message"></ChatMessage>
|
|
|
|
- </template>
|
|
|
|
- </van-list>
|
|
|
|
- <!-- <van-pull-refresh v-model="refreshing" @refresh="loadMore" class="flex-1">
|
|
|
|
- </van-pull-refresh>-->
|
|
|
|
- <!-- <div class="fixed bottom-0 left-0 right-0 w-full">-->
|
|
|
|
- <ChatInput
|
|
|
|
- :operates="['image', 'share-group']"
|
|
|
|
- @focus="scrollToBottom"
|
|
|
|
- @send="handleSendMessage"
|
|
|
|
- ></ChatInput>
|
|
|
|
- <!-- </div>-->
|
|
|
|
|
|
+ <van-pull-refresh v-model="refreshing" @refresh="loadMore" class="flex-1">
|
|
|
|
+ <van-list
|
|
|
|
+ ref="chatListRef"
|
|
|
|
+ class="h-full overflow-y-auto px-12 flex flex-col"
|
|
|
|
+ :finished="true"
|
|
|
|
+ finished-text=""
|
|
|
|
+ >
|
|
|
|
+ <template v-for="(message, index) in currConversationChatList" :key="index">
|
|
|
|
+ <ChatMessage :show-name="true" :message="message"></ChatMessage>
|
|
|
|
+ </template>
|
|
|
|
+ </van-list>
|
|
|
|
+ </van-pull-refresh>
|
|
|
|
+ <div class="h-70 w-full bg-[#fff]"></div>
|
|
|
|
+ <div class="fixed bottom-0 left-0 right-0 w-full bg-[#fff]">
|
|
|
|
+ <ChatInput :operates="['image', 'share-group']" @focus="scrollToBottom" @send="handleSendMessage"></ChatInput>
|
|
|
|
+ </div>
|
|
</template>
|
|
</template>
|
|
<template v-else>
|
|
<template v-else>
|
|
<div class="flex-1 grid place-items-center text-black-9">
|
|
<div class="flex-1 grid place-items-center text-black-9">
|
|
@@ -67,50 +64,48 @@
|
|
</template>
|
|
</template>
|
|
<script setup>
|
|
<script setup>
|
|
import ChatMessage from './chat-message'
|
|
import ChatMessage from './chat-message'
|
|
-import ChatInput from './chat-input'
|
|
|
|
-import { findHyperlinks } from '~/pages/chat/chat-message/link-message/handle'
|
|
|
|
-import { XYWebSocket } from '~/utils/XYWebSocket'
|
|
|
|
-import { isValidJson } from '~/utils'
|
|
|
|
|
|
+import ChatInput from "./chat-input";
|
|
|
|
+import {findHyperlinks} from "~/pages/chat/chat-message/link-message/handle";
|
|
|
|
+import {XYWebSocket} from "~/utils/XYWebSocket";
|
|
|
|
+import {isValidJson} from "~/utils";
|
|
|
|
|
|
const route = useRoute()
|
|
const route = useRoute()
|
|
const router = useRouter()
|
|
const router = useRouter()
|
|
|
|
|
|
-const chatsStore = useChatsStore()
|
|
|
|
-const userInfoStore = useUserInfoStore()
|
|
|
|
|
|
+const chatsStore = useChatsStore();
|
|
|
|
+const userInfoStore = useUserInfoStore();
|
|
|
|
|
|
-const { userInfo } = storeToRefs(userInfoStore)
|
|
|
|
|
|
+const {userInfo} = storeToRefs(userInfoStore)
|
|
// 群聊的标题
|
|
// 群聊的标题
|
|
const groupInfo = ref({})
|
|
const groupInfo = ref({})
|
|
// 每个成员的信息
|
|
// 每个成员的信息
|
|
const groupMemberInfo = ref({})
|
|
const groupMemberInfo = ref({})
|
|
|
|
|
|
// 聊天列表
|
|
// 聊天列表
|
|
-const chatListRef = ref(null)
|
|
|
|
|
|
+const chatListRef = ref(null);
|
|
|
|
|
|
-const pageLoading = ref(true)
|
|
|
|
-const getUserId = ref(null) // 消息接收者的用户id
|
|
|
|
|
|
+const pageLoading = ref(true);
|
|
|
|
+const getUserId = ref(null); // 消息接收者的用户id
|
|
const sendUserId = computed(() => userInfo?.value.pass) // 消息发送者:当前登录用户的加密id
|
|
const sendUserId = computed(() => userInfo?.value.pass) // 消息发送者:当前登录用户的加密id
|
|
-const groupId = ref(route.query?.groupId) // 会话ID
|
|
|
|
|
|
+const groupId = ref(route.query?.groupId); // 会话ID
|
|
const showPage = computed(() => groupId.value)
|
|
const showPage = computed(() => groupId.value)
|
|
|
|
|
|
const initGroupId = async () => {
|
|
const initGroupId = async () => {
|
|
try {
|
|
try {
|
|
- if (!groupId.value) return
|
|
|
|
- /*
|
|
|
|
- pageLoading.value = true;
|
|
|
|
- const res = await chatsStore.getCurrConversationId(getUserId.value)
|
|
|
|
- await handleResponse(res)
|
|
|
|
- groupId.value = res.data;*/
|
|
|
|
|
|
+ if (!groupId.value) return;
|
|
await getAnnouncement()
|
|
await getAnnouncement()
|
|
await getChatList('init')
|
|
await getChatList('init')
|
|
|
|
+
|
|
} catch (e) {
|
|
} catch (e) {
|
|
|
|
+
|
|
} finally {
|
|
} finally {
|
|
- pageLoading.value = false
|
|
|
|
|
|
+ pageLoading.value = false;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
let pageNum = ref(0)
|
|
let pageNum = ref(0)
|
|
-const currConversationChatList = ref([])
|
|
|
|
|
|
+let totalCount = ref(0)
|
|
|
|
+const currConversationChatList = ref([]);
|
|
/**
|
|
/**
|
|
* 加载当前聊天信息
|
|
* 加载当前聊天信息
|
|
* @param type init|more
|
|
* @param type init|more
|
|
@@ -118,26 +113,43 @@ const currConversationChatList = ref([])
|
|
*/
|
|
*/
|
|
const getChatList = async (type = 'init') => {
|
|
const getChatList = async (type = 'init') => {
|
|
try {
|
|
try {
|
|
- const page = type === 'init' ? 1 : pageNum.value + 1
|
|
|
|
|
|
+ let lastMessageId = null
|
|
|
|
+ let page = 1;
|
|
|
|
+ if (type === 'more') {
|
|
|
|
+ lastMessageId = getLastMessageId()
|
|
|
|
+ page = pageNum.value + 1
|
|
|
|
+ }
|
|
const res = await chatsStore.getChatHistory({
|
|
const res = await chatsStore.getChatHistory({
|
|
pageNum: page,
|
|
pageNum: page,
|
|
- pageSize: 100,
|
|
|
|
- groupId: groupId.value
|
|
|
|
|
|
+ pageSize: 50,
|
|
|
|
+ groupId: groupId.value,
|
|
|
|
+ messageId: type === 'more' ? lastMessageId : null
|
|
})
|
|
})
|
|
- pageNum.value = page
|
|
|
|
- await handleResponse(res)
|
|
|
|
- currConversationChatList.value = handleChatList(res.data?.data)
|
|
|
|
- console.log(currConversationChatList.value, 'currConversationChatList')
|
|
|
|
- if (type === 'init') await scrollToBottom()
|
|
|
|
|
|
+ pageNum.value = page;
|
|
|
|
+ await handleResponse(res);
|
|
|
|
+ let resList = chatsStore.handleMessageList(res.data?.data);
|
|
|
|
+ totalCount.value = res.data.count;
|
|
|
|
+
|
|
|
|
+ if (type === 'more') {// 滚动条位置
|
|
|
|
+ resList = resList.filter(o => o.messageId !== lastMessageId);
|
|
|
|
+ currConversationChatList.value = [...resList, ...currConversationChatList.value]
|
|
|
|
+ } else {
|
|
|
|
+ currConversationChatList.value = resList
|
|
|
|
+ await scrollToBottom()
|
|
|
|
+ }
|
|
} catch (e) {
|
|
} catch (e) {
|
|
console.error(e)
|
|
console.error(e)
|
|
} finally {
|
|
} finally {
|
|
|
|
+
|
|
}
|
|
}
|
|
}
|
|
}
|
|
-const handleChatList = (list = []) => {
|
|
|
|
- return (list ?? [])
|
|
|
|
- .filter((o) => isValidJson(o.messageContent))
|
|
|
|
- .map((o) => JSON.parse(o.messageContent))
|
|
|
|
|
|
+
|
|
|
|
+const getLastMessageId = () => {
|
|
|
|
+ const len = currConversationChatList.value.length;
|
|
|
|
+ if (len) {
|
|
|
|
+ return currConversationChatList.value[0]?.messageId ?? null
|
|
|
|
+ }
|
|
|
|
+ return null
|
|
}
|
|
}
|
|
|
|
|
|
// 发送文本消息
|
|
// 发送文本消息
|
|
@@ -161,7 +173,7 @@ const sendTextMessage = async (text) => {
|
|
}
|
|
}
|
|
}
|
|
}
|
|
const isLink = !!findHyperlinks(text)
|
|
const isLink = !!findHyperlinks(text)
|
|
- if (isLink) msg.messageType = 4
|
|
|
|
|
|
+ if (isLink) msg.messageType = 4;
|
|
currConversationChatList.value.push(msg)
|
|
currConversationChatList.value.push(msg)
|
|
await scrollToBottom()
|
|
await scrollToBottom()
|
|
const res = await chatsStore.sendSocketMessage(msg)
|
|
const res = await chatsStore.sendSocketMessage(msg)
|
|
@@ -169,6 +181,7 @@ const sendTextMessage = async (text) => {
|
|
} catch (e) {
|
|
} catch (e) {
|
|
console.log(e, '2')
|
|
console.log(e, '2')
|
|
} finally {
|
|
} finally {
|
|
|
|
+
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
@@ -179,7 +192,7 @@ const sendImageMessage = async (file) => {
|
|
formData.append('uploadFile', file)
|
|
formData.append('uploadFile', file)
|
|
formData.append('asImage', true)
|
|
formData.append('asImage', true)
|
|
formData.append('fieldName', 'messageContent')
|
|
formData.append('fieldName', 'messageContent')
|
|
- const { data } = await request('/website/tourMessage/upload', {
|
|
|
|
|
|
+ const {data} = await request('/website/tourMessage/upload', {
|
|
method: 'post',
|
|
method: 'post',
|
|
body: formData
|
|
body: formData
|
|
})
|
|
})
|
|
@@ -205,44 +218,58 @@ const sendImageMessage = async (file) => {
|
|
} catch (e) {
|
|
} catch (e) {
|
|
console.error(e, '??')
|
|
console.error(e, '??')
|
|
} finally {
|
|
} finally {
|
|
|
|
+
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
-const handleSendMessage = async ({ type, messageContent }) => {
|
|
|
|
|
|
+const handleSendMessage = async ({type, messageContent}) => {
|
|
try {
|
|
try {
|
|
switch (type) {
|
|
switch (type) {
|
|
case 'text':
|
|
case 'text':
|
|
await sendTextMessage(messageContent)
|
|
await sendTextMessage(messageContent)
|
|
- break
|
|
|
|
|
|
+ break;
|
|
case 'image':
|
|
case 'image':
|
|
await sendImageMessage(messageContent)
|
|
await sendImageMessage(messageContent)
|
|
- break
|
|
|
|
|
|
+ break;
|
|
|
|
|
|
default:
|
|
default:
|
|
- break
|
|
|
|
|
|
+
|
|
|
|
+ break;
|
|
}
|
|
}
|
|
|
|
+
|
|
} catch (e) {
|
|
} catch (e) {
|
|
|
|
+
|
|
} finally {
|
|
} finally {
|
|
|
|
+
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
const scrollToBottom = async () => {
|
|
const scrollToBottom = async () => {
|
|
setTimeout(async () => {
|
|
setTimeout(async () => {
|
|
- await nextTick() // 确保DOM已经更新
|
|
|
|
- const listElement = chatListRef.value?.$el
|
|
|
|
|
|
+ await nextTick(); // 确保DOM已经更新
|
|
|
|
+ const listElement = chatListRef.value?.$el;
|
|
if (listElement) {
|
|
if (listElement) {
|
|
- const scrollContainer = listElement
|
|
|
|
- scrollContainer.scrollTop = scrollContainer.scrollHeight
|
|
|
|
|
|
+ const scrollContainer = listElement;
|
|
|
|
+ scrollContainer.scrollTop = scrollContainer.scrollHeight;
|
|
}
|
|
}
|
|
}, 200)
|
|
}, 200)
|
|
-}
|
|
|
|
|
|
+};
|
|
|
|
+
|
|
|
|
|
|
// 加载更多
|
|
// 加载更多
|
|
const refreshing = ref(false)
|
|
const refreshing = ref(false)
|
|
const loadMore = async () => {
|
|
const loadMore = async () => {
|
|
try {
|
|
try {
|
|
refreshing.value = true
|
|
refreshing.value = true
|
|
- await getChatList('more')
|
|
|
|
|
|
+ if (currConversationChatList.value.length) {
|
|
|
|
+ if(totalCount.value === currConversationChatList.value.length) {
|
|
|
|
+ // 已经加载了全部
|
|
|
|
+ } else {
|
|
|
|
+ await getChatList('more')
|
|
|
|
+ }
|
|
|
|
+ } else {
|
|
|
|
+ await getChatList('init')
|
|
|
|
+ }
|
|
} catch (e) {
|
|
} catch (e) {
|
|
} finally {
|
|
} finally {
|
|
refreshing.value = false
|
|
refreshing.value = false
|
|
@@ -250,13 +277,13 @@ const loadMore = async () => {
|
|
}
|
|
}
|
|
|
|
|
|
const onClickRight = () => {
|
|
const onClickRight = () => {
|
|
- groupId.value &&
|
|
|
|
- navigateTo({
|
|
|
|
- path: '/chat/set',
|
|
|
|
- query: { groupId: groupId.value }
|
|
|
|
- })
|
|
|
|
|
|
+ groupId.value && navigateTo({
|
|
|
|
+ path: '/chat/set',
|
|
|
|
+ query: {groupId: groupId.value}
|
|
|
|
+ })
|
|
}
|
|
}
|
|
|
|
|
|
|
|
+
|
|
// 本地生成一个唯一消息id
|
|
// 本地生成一个唯一消息id
|
|
function getLocalId() {
|
|
function getLocalId() {
|
|
const random = Math.floor(Math.random() * 10000)
|
|
const random = Math.floor(Math.random() * 10000)
|
|
@@ -267,29 +294,33 @@ onMounted(() => {
|
|
initGroupId()
|
|
initGroupId()
|
|
|
|
|
|
XYWebSocket.SocketEventsBus.on(XYWebSocket.SocketEvents.chatEvent, async (chat) => {
|
|
XYWebSocket.SocketEventsBus.on(XYWebSocket.SocketEvents.chatEvent, async (chat) => {
|
|
- const isCurrGroupId = chat.groupId && chat.groupId === groupId.value
|
|
|
|
- const isOtherUserMessage = chat.sendUserId && chatsStore.isRealMessage(chat.sendUserId)
|
|
|
|
- if (isCurrGroupId && isOtherUserMessage) {
|
|
|
|
- currConversationChatList.value.push(chat)
|
|
|
|
- await scrollToBottom()
|
|
|
|
|
|
+ console.log('订阅群聊消息', chat)
|
|
|
|
+ const isCurrGroupId = chat.groupId && chat.groupId === groupId.value;
|
|
|
|
+ const isOtherUserMessage = chat.sendUserId && chatsStore.isRealMessage(chat.sendUserId);
|
|
|
|
+ if (isCurrGroupId) {
|
|
|
|
+ if (isOtherUserMessage) {
|
|
|
|
+ currConversationChatList.value.push(chat)
|
|
|
|
+ await scrollToBottom()
|
|
|
|
+ }
|
|
|
|
+ if (!isOtherUserMessage) {
|
|
|
|
+ await getChatList('init')
|
|
|
|
+ }
|
|
}
|
|
}
|
|
})
|
|
})
|
|
})
|
|
})
|
|
|
|
|
|
// 查寻群公告
|
|
// 查寻群公告
|
|
async function getAnnouncement() {
|
|
async function getAnnouncement() {
|
|
- let { data } = await request('/website/tourGroup/getGroupInfoAndMemberByGroupId', {
|
|
|
|
- query: { groupId: groupId.value }
|
|
|
|
|
|
+ let {data} = await request('/website/tourGroup/getGroupInfoAndMemberByGroupId', {
|
|
|
|
+ query: {groupId: groupId.value}
|
|
})
|
|
})
|
|
console.log(data.memberList)
|
|
console.log(data.memberList)
|
|
|
|
|
|
if (data) {
|
|
if (data) {
|
|
groupInfo.value = data
|
|
groupInfo.value = data
|
|
- if (Array.isArray(data?.memberList) && data?.memberList?.lenght) {
|
|
|
|
- data?.memberList?.map((el) => {
|
|
|
|
|
|
+ if (Array.isArray(data.memberList) && data.memberList?.lenght) {
|
|
|
|
+ data.memberList.map((el) => {
|
|
if (el.userId == userInfo.value.userId) {
|
|
if (el.userId == userInfo.value.userId) {
|
|
- // console.log(el, '8888-------')
|
|
|
|
-
|
|
|
|
groupMemberInfo.value = el
|
|
groupMemberInfo.value = el
|
|
}
|
|
}
|
|
})
|
|
})
|
|
@@ -304,18 +335,19 @@ const delMessage = (messageId) => {
|
|
message: '是否删除这条消息?',
|
|
message: '是否删除这条消息?',
|
|
confirmButtonColor: '#FF9300'
|
|
confirmButtonColor: '#FF9300'
|
|
})
|
|
})
|
|
- .then(async () => {
|
|
|
|
- const res = await request('/website/tourMessage/delMessage', {
|
|
|
|
- method: 'post',
|
|
|
|
- body: {
|
|
|
|
- messageId: [messageId]
|
|
|
|
|
|
+ .then(async () => {
|
|
|
|
+ const res = await request('/website/tourMessage/delMessage', {
|
|
|
|
+ method: 'post',
|
|
|
|
+ body: {
|
|
|
|
+ messageId: [messageId]
|
|
|
|
+ }
|
|
|
|
+ })
|
|
|
|
+
|
|
|
|
+ if (res && res?.success) {
|
|
}
|
|
}
|
|
})
|
|
})
|
|
-
|
|
|
|
- if (res && res?.success) {
|
|
|
|
- }
|
|
|
|
- })
|
|
|
|
- .catch(() => {})
|
|
|
|
|
|
+ .catch(() => {
|
|
|
|
+ })
|
|
}
|
|
}
|
|
|
|
|
|
definePageMeta({
|
|
definePageMeta({
|