|
@@ -20,24 +20,21 @@
|
|
|
</van-nav-bar>
|
|
|
<van-list
|
|
|
ref="messageBoxRef"
|
|
|
- class="flex-1 overflow-y-auto px-12"
|
|
|
+ class="flex-1 overflow-y-auto px-12 flex flex-col"
|
|
|
:finished="true"
|
|
|
finished-text=""
|
|
|
>
|
|
|
<template v-for="(message, index) in receiveGetter" :key="index">
|
|
|
<ChatMessage :message="message"></ChatMessage>
|
|
|
</template>
|
|
|
+ <div v-if="false" class="text-[#979797] text-sm text-center mt-auto mb-10">在对方关注或回复前,最多只能发送1条信息</div>
|
|
|
</van-list>
|
|
|
- <div class="h-88 w-full bg-[#333]"></div>
|
|
|
- <ProfileNewsChatInput
|
|
|
- :shareGroup="false"
|
|
|
- @on-select-Img="selectImg"
|
|
|
- @on-send-message="sendMessage"
|
|
|
- ></ProfileNewsChatInput>
|
|
|
+ <ChatInput :operates="['image']" @focus="scrollToBottom" @send="handleSendMessage"></ChatInput>
|
|
|
</div>
|
|
|
</template>
|
|
|
<script setup>
|
|
|
import ChatMessage from './chat-message'
|
|
|
+import ChatInput from "./chat-input";
|
|
|
import {messageContentParse, formatTimestamp} from '~/utils/detalTime'
|
|
|
import {findHyperlinks} from "~/pages/chat/chat-message/link-message/handle";
|
|
|
|
|
@@ -46,7 +43,7 @@ const router = useRouter()
|
|
|
const refreshing = ref(false)
|
|
|
const uploadUrl = `${import.meta.env.VITE_APP_BASE_URL}/website/tourMessage/upload`
|
|
|
const chatStore = useChatStore()
|
|
|
-const {ws, curConversiton, receive, receiveGetter, connectSta, onNewMessage} =
|
|
|
+const {ws, curConversiton, receive,conversations, receiveGetter, connectSta, onNewMessage} =
|
|
|
storeToRefs(chatStore)
|
|
|
|
|
|
const user = computed(() => chatStore.user)
|
|
@@ -81,7 +78,7 @@ const followStatus = ref(0)
|
|
|
const querySwParams = reactive({
|
|
|
getUserId: computed(() => route.query.getUserId ?? ''),
|
|
|
groupId: computed(() => route.query.groupId ?? ''),
|
|
|
- sendUserId: computed(() => route.query.sendUserId ?? '')
|
|
|
+ sendUserId: computed(() => user.value?.pass)
|
|
|
})
|
|
|
|
|
|
// 本地生成一个唯一消息id
|
|
@@ -115,42 +112,6 @@ const onClickRight = () => {
|
|
|
})
|
|
|
}
|
|
|
|
|
|
-// 发送消息的方法
|
|
|
-// 发送文本消息
|
|
|
-function sendMessage(messageParams) {
|
|
|
- console.log(messageParams, 'messageParams')
|
|
|
- console.log(getUserId.value, '5555')
|
|
|
- console.log(noticeType, '5555')
|
|
|
- if (!messageParams.trim()) return
|
|
|
- let msg = {
|
|
|
- // getUserId: getUserId.value,
|
|
|
- // sendUserId: sendUserId.value,
|
|
|
- // groupId: groupId.value,
|
|
|
- ...querySwParams,
|
|
|
- specialUserId: specialUserId.value,
|
|
|
- messageContent: messageParams,
|
|
|
- messageType: 0,
|
|
|
- noticeType: 1,
|
|
|
- object: {
|
|
|
- id: getLocalId()
|
|
|
- }
|
|
|
- }
|
|
|
- const isLink = !!findHyperlinks(messageParams)
|
|
|
- if (isLink) {
|
|
|
- msg.messageType = 4
|
|
|
- }
|
|
|
-
|
|
|
- receive.value.push({
|
|
|
- ...msg,
|
|
|
- messageContent: JSON.stringify({messageContent: msg.messageContent})
|
|
|
- })
|
|
|
- messageContent.value = ''
|
|
|
- messageBoxRef.value.scrollTop = messageBoxRef.value.scrollHeight
|
|
|
-
|
|
|
- msg = JSON.stringify(msg)
|
|
|
- ws.value.send(msg)
|
|
|
-}
|
|
|
-
|
|
|
const pageSize = ref(10)
|
|
|
|
|
|
const messageCount = ref(0)
|
|
@@ -185,7 +146,7 @@ async function getChatHistory(messageId = '') {
|
|
|
if (!messageId) {
|
|
|
nextTick(() => {
|
|
|
setTimeout(() => {
|
|
|
- messageBoxRef.value && messageBoxRef.value.scrollTo({top: msgBottomRef.value.offsetTop})
|
|
|
+ mscrollToBottom()
|
|
|
}, 100)
|
|
|
})
|
|
|
}
|
|
@@ -201,7 +162,7 @@ function addEventListenerTextarea() {
|
|
|
inputBoxRef.value.addEventListener('keydown', function (event) {
|
|
|
if (event.key === 'Enter') {
|
|
|
event.preventDefault()
|
|
|
- sendMessage()
|
|
|
+ sendTextMessage()
|
|
|
}
|
|
|
})
|
|
|
}
|
|
@@ -226,16 +187,64 @@ function addEventListenerMessage() {
|
|
|
})
|
|
|
}
|
|
|
|
|
|
+// 获取我与对方的关注情况
|
|
|
+async function isFollow() {
|
|
|
+ if (noticeType.value !== 1) return //只有单聊中才需要获取关注情况
|
|
|
+
|
|
|
+ const query = {
|
|
|
+ userId: curConversiton.value.toUserId
|
|
|
+ }
|
|
|
+ const {data: status = 0} = await request('/website/tourGroup/isFollow', {query})
|
|
|
+ followStatus.value = status
|
|
|
+ console.log('关注情况:', status)
|
|
|
+}
|
|
|
+
|
|
|
+
|
|
|
+// 发送文本消息
|
|
|
+const sendTextMessage = (text) => {
|
|
|
+ if(!text) return
|
|
|
+ let msg = {
|
|
|
+ // getUserId: getUserId.value,
|
|
|
+ // sendUserId: sendUserId.value,
|
|
|
+ // groupId: groupId.value,
|
|
|
+ ...querySwParams,
|
|
|
+ specialUserId: specialUserId.value,
|
|
|
+ messageContent: text,
|
|
|
+ messageType: 0,
|
|
|
+ noticeType: 1,
|
|
|
+ object: {
|
|
|
+ id: getLocalId()
|
|
|
+ }
|
|
|
+ }
|
|
|
+ const isLink = !!findHyperlinks(text)
|
|
|
+ if (isLink) msg.messageType = 4;
|
|
|
+
|
|
|
+ receive.value.push({
|
|
|
+ ...msg,
|
|
|
+ messageContent: JSON.stringify({messageContent: msg.messageContent})
|
|
|
+ })
|
|
|
+ ws.value.send(JSON.stringify(msg))
|
|
|
+}
|
|
|
+
|
|
|
// 选择发送图片
|
|
|
-function selectImg(fileUrl) {
|
|
|
+const sendImageMessage = async (file) => {
|
|
|
try {
|
|
|
- console.log(fileUrl, 'fileUrl')
|
|
|
+ console.log(file, 'file')
|
|
|
+ const formData = new FormData()
|
|
|
+ formData.append('uploadFile', file)
|
|
|
+ formData.append('asImage', true)
|
|
|
+ formData.append('fieldName', 'messageContent')
|
|
|
+ const {data} = await request('/website/tourMessage/upload', {
|
|
|
+ method: 'post',
|
|
|
+ body: formData
|
|
|
+ })
|
|
|
+
|
|
|
const msg = {
|
|
|
getUserId: getUserId.value,
|
|
|
sendUserId: sendUserId.value,
|
|
|
specialUserId: '',
|
|
|
groupId: groupId.value,
|
|
|
- messageContent: JSON.stringify({messageContent: fileUrl}),
|
|
|
+ messageContent: JSON.stringify({messageContent: data.fileUrl}),
|
|
|
messageType: 1,
|
|
|
noticeType: noticeType.value,
|
|
|
object: {
|
|
@@ -244,9 +253,9 @@ function selectImg(fileUrl) {
|
|
|
}
|
|
|
receive.value.push({
|
|
|
...msg,
|
|
|
- messageContent: JSON.stringify({messageContent: fileUrl})
|
|
|
+ messageContent: JSON.stringify({messageContent: data.fileUrl})
|
|
|
})
|
|
|
- messageBoxRef.value.scrollTop = messageBoxRef.value.scrollHeight
|
|
|
+
|
|
|
ws.value.send(JSON.stringify(msg))
|
|
|
} catch (e) {
|
|
|
console.error(e, '??')
|
|
@@ -255,24 +264,42 @@ function selectImg(fileUrl) {
|
|
|
}
|
|
|
}
|
|
|
|
|
|
-// 获取我与对方的关注情况
|
|
|
-async function isFollow() {
|
|
|
- if (noticeType.value !== 1) return //只有单聊中才需要获取关注情况
|
|
|
+const handleSendMessage = async ({type, messageContent}) => {
|
|
|
+ try {
|
|
|
+ switch (type) {
|
|
|
+ case 'text':
|
|
|
+ sendTextMessage(messageContent)
|
|
|
+ break;
|
|
|
+ case 'image':
|
|
|
+ await sendImageMessage(messageContent)
|
|
|
+ break;
|
|
|
+ }
|
|
|
+ await scrollToBottom()
|
|
|
+ } catch (e) {
|
|
|
+
|
|
|
+ } finally {
|
|
|
|
|
|
- const query = {
|
|
|
- userId: curConversiton.value.toUserId
|
|
|
}
|
|
|
- const {data: status = 0} = await request('/website/tourGroup/isFollow', {query})
|
|
|
- followStatus.value = status
|
|
|
- console.log('关注情况:', status)
|
|
|
+
|
|
|
}
|
|
|
|
|
|
+
|
|
|
+const scrollToBottom = async () => {
|
|
|
+ await nextTick(); // 确保DOM已经更新
|
|
|
+ const listElement = messageBoxRef.value?.$el;
|
|
|
+ if (listElement) {
|
|
|
+ const scrollContainer = listElement;
|
|
|
+ scrollContainer.scrollTop = scrollContainer.scrollHeight;
|
|
|
+ }
|
|
|
+};
|
|
|
+
|
|
|
+
|
|
|
watch(groupId, async () => {
|
|
|
// 消息置空
|
|
|
receive.value = []
|
|
|
|
|
|
// 监听消息输入框键盘enter事件
|
|
|
- addEventListenerTextarea()
|
|
|
+ // addEventListenerTextarea()
|
|
|
|
|
|
// 监听聊天框消息滚动事件
|
|
|
addEventListenerMessage()
|
|
@@ -284,18 +311,23 @@ watch(groupId, async () => {
|
|
|
getChatHistory()
|
|
|
})
|
|
|
|
|
|
-watch(onNewMessage, getUserId, sendUserId, () => {
|
|
|
+watch(onNewMessage, () => {
|
|
|
+ scrollToBottom()
|
|
|
})
|
|
|
|
|
|
onMounted(() => {
|
|
|
// 获取前会话用户的聊天记录
|
|
|
getChatHistory()
|
|
|
|
|
|
- addEventListenerTextarea()
|
|
|
+ // addEventListenerTextarea()
|
|
|
addEventListenerMessage()
|
|
|
|
|
|
// 获取与当前会话用户的关注状态
|
|
|
isFollow()
|
|
|
+
|
|
|
+ setTimeout(() => {
|
|
|
+ scrollToBottom()
|
|
|
+ }, 500)
|
|
|
})
|
|
|
|
|
|
definePageMeta({
|