|
|
@@ -4,7 +4,7 @@ export default {
|
|
|
};
|
|
|
</script>
|
|
|
<script setup>
|
|
|
-import { ref, reactive, toRaw, toRefs, nextTick } from "vue";
|
|
|
+import { ref, reactive, toRaw, toRefs, nextTick, computed } from "vue";
|
|
|
import { useRouter, useRoute } from "vue-router";
|
|
|
import useUserStore from "@/store/modules/user";
|
|
|
import word from "@/assets/images/word2.png";
|
|
|
@@ -96,6 +96,16 @@ const getMsgList = async () => {
|
|
|
}
|
|
|
};
|
|
|
const noMes = ref(false);
|
|
|
+const boottmScroll=()=>{
|
|
|
+//发送消息后滚动到最底部显示最新消息
|
|
|
+const chatContainer = document.querySelector(".right-container");
|
|
|
+ // 计算滚动的目标位置
|
|
|
+ const targetScrollTop = chatContainer.scrollHeight;
|
|
|
+ // 设置滚动位置
|
|
|
+ nextTick(() => {
|
|
|
+ chatContainer.scrollTop = targetScrollTop;
|
|
|
+ });
|
|
|
+}
|
|
|
//获取用户的聊天记录
|
|
|
const msgRecordEvent = async (toIdValue) => {
|
|
|
const queryParams = {
|
|
|
@@ -107,23 +117,16 @@ const msgRecordEvent = async (toIdValue) => {
|
|
|
chatRecords.data = resMsgData.rows.reverse();
|
|
|
const PageNum = Math.ceil(chatRecords.data.length / 10) + 1;
|
|
|
if (PageNum * 10 >= resMsgData.total && !noMes.value) {
|
|
|
- loading.value = false;
|
|
|
- noMes.value = true;
|
|
|
- }
|
|
|
+ loading.value = false;
|
|
|
+ noMes.value = true;
|
|
|
+ }
|
|
|
const nowtime = parseTime(new Date().getTime(), "{y}-{m}-{d}");
|
|
|
chatRecords.data.map((i) => {
|
|
|
if (nowtime == i.createTime.substr(0, 10))
|
|
|
i.createTime = i.createTime.substring(11);
|
|
|
});
|
|
|
total.value = resMsgData.total;
|
|
|
- //发送消息后滚动到最底部显示最新消息
|
|
|
- const chatContainer = document.querySelector(".right-container");
|
|
|
- // 计算滚动的目标位置
|
|
|
- const targetScrollTop = chatContainer.scrollHeight;
|
|
|
- // 设置滚动位置
|
|
|
- nextTick(() => {
|
|
|
- chatContainer.scrollTop = targetScrollTop;
|
|
|
- });
|
|
|
+ boottmScroll()
|
|
|
};
|
|
|
//点击左侧新建聊天
|
|
|
const open = ref(false);
|
|
|
@@ -230,12 +233,7 @@ const msgSendClick = (event) => {
|
|
|
toId: sendCont.data.toId,
|
|
|
};
|
|
|
websoctStore.sendMessage(message);
|
|
|
- //发送消息后滚动到最底部显示最新消息
|
|
|
- const chatContainer = document.querySelector(".right-container");
|
|
|
- // 计算滚动的目标位置
|
|
|
- const targetScrollTop = chatContainer.scrollHeight;
|
|
|
- // 设置滚动位置
|
|
|
- chatContainer.scrollTop = targetScrollTop;
|
|
|
+ boottmScroll()
|
|
|
event.preventDefault();
|
|
|
messageText.value = "";
|
|
|
// 将光标设置到输入框第一行
|
|
|
@@ -275,12 +273,17 @@ watchEffect(() => {
|
|
|
}
|
|
|
});
|
|
|
// 滚动翻页========
|
|
|
-const mainContainer=ref(null)
|
|
|
+const mainContainer = ref(null);
|
|
|
|
|
|
const handleScroll = (event) => {
|
|
|
// 在滚动到顶部时,加载上一页的聊天记录
|
|
|
const mainContainer2 = document.querySelector(".right-container");
|
|
|
- console.log(' event.deltaY', mainContainer2.scrollTop,noMes.value,loading.value)
|
|
|
+ console.log(
|
|
|
+ " event.deltaY",
|
|
|
+ mainContainer2.scrollTop,
|
|
|
+ noMes.value,
|
|
|
+ loading.value
|
|
|
+ );
|
|
|
if (
|
|
|
event.deltaY < 0 &&
|
|
|
mainContainer2.scrollTop <= 1 &&
|
|
|
@@ -298,29 +301,30 @@ const loadPreviousPage = async () => {
|
|
|
pageSize: 10,
|
|
|
};
|
|
|
// setTimeout(async () => {
|
|
|
- const resMsgData = await msgRecord(sendCont.data.toId, queryParams);
|
|
|
- const previousPageData = resMsgData.rows.reverse();
|
|
|
- chatRecords.data = [...previousPageData, ...chatRecords.data];
|
|
|
- const nowtime2 = parseTime(new Date().getTime(), "{y}-{m}-{d}");
|
|
|
- chatRecords.data.map((i) => {
|
|
|
- if (nowtime2 == i.createTime.substr(0, 10))
|
|
|
- i.createTime = i.createTime.substring(11);
|
|
|
- });
|
|
|
- noMes.value = false;
|
|
|
- if (currentPageNum * 10 >= resMsgData.total && !noMes.value) {
|
|
|
- loading.value = false;
|
|
|
- noMes.value = true;
|
|
|
- return;
|
|
|
- }
|
|
|
- // await nextTick();
|
|
|
- // const mainContainer = document.querySelector(".right-container");
|
|
|
- // const newMessages = document.querySelectorAll(".message-container");
|
|
|
- // const firstNewMessage = newMessages[previousPageData.length];
|
|
|
- // const firstNewMessageTop = mainContainer.offsetHeight;
|
|
|
- // console.log('firstNewMessageTop', firstNewMessageTop)
|
|
|
- // mainContainer.scrollTop = firstNewMessageTop;
|
|
|
- // 将加载的上一页聊天记录插入到 chatRecords.data 的前面
|
|
|
+ const resMsgData = await msgRecord(sendCont.data.toId, queryParams);
|
|
|
+ const previousPageData = resMsgData.rows.reverse();
|
|
|
+ chatRecords.data = [...previousPageData, ...chatRecords.data];
|
|
|
+ const nowtime2 = parseTime(new Date().getTime(), "{y}-{m}-{d}");
|
|
|
+ chatRecords.data.map((i) => {
|
|
|
+ if (nowtime2 == i.createTime.substr(0, 10))
|
|
|
+ i.createTime = i.createTime.substring(11);
|
|
|
+ });
|
|
|
+ noMes.value = false;
|
|
|
+ if (currentPageNum * 10 >= resMsgData.total && !noMes.value) {
|
|
|
loading.value = false;
|
|
|
+ noMes.value = true;
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ //获取数据后滚动到获得新消息的最后一条,而不是第一条=========
|
|
|
+ await nextTick();
|
|
|
+ const mainContainer = document.querySelector(".right-container");
|
|
|
+ const newMessages = document.querySelectorAll(".message-container");
|
|
|
+ const firstNewMessage = newMessages[previousPageData.length];
|
|
|
+ const firstNewMessageTop = mainContainer.offsetHeight + 100;
|
|
|
+ mainContainer.scrollTop = firstNewMessageTop;chatRecords.data
|
|
|
+ //===========
|
|
|
+ // 将加载的上一页聊天记录插入到 chatRecords.data 的前面
|
|
|
+ loading.value = false;
|
|
|
// }, 50);
|
|
|
};
|
|
|
const transferFiles = (forwardVal, msgIds, indexs) => {
|
|
|
@@ -366,7 +370,11 @@ const formatText = (text) => {
|
|
|
const formattedText = text.replace(/\n/g, "<br>");
|
|
|
return formattedText;
|
|
|
};
|
|
|
+//获得个人头像转换
|
|
|
|
|
|
+const getMyThoto=computed(() => item => {
|
|
|
+ return item.toId == userIds.uid?item.fromAvatar:item.toAvatar
|
|
|
+})
|
|
|
// 滚动翻页========
|
|
|
onMounted(() => {
|
|
|
getMsgList();
|
|
|
@@ -426,15 +434,13 @@ onMounted(() => {
|
|
|
删除聊天
|
|
|
</button> -->
|
|
|
<div>
|
|
|
- <img :src="item.avatar ? item.avatar : chat" class="head-sculpture" />
|
|
|
+ <!-- <img :src="item.avatar?item.avatar : chat" class="head-sculpture" /> -->
|
|
|
+ <img :src="getMyThoto(item)" class="head-sculpture" v-if="getMyThoto(item)"/>
|
|
|
+ <span style="background-color: #7a89ba;display: inline-block;color: #fff;font-weight: 600;text-align: center;line-height: 40px;" class="head-sculpture" v-else>{{item.toId == userIds.uid?item.fromName?.slice(0,1):item.toName?.slice(0,1)}}</span>
|
|
|
</div>
|
|
|
<div class="spill">
|
|
|
<span class="person-name">{{
|
|
|
- item.nickName
|
|
|
- ? item.nickName
|
|
|
- : item.toId == userIds.uid
|
|
|
- ? item.fromName
|
|
|
- : item.toName
|
|
|
+ item.nickName? item.nickName: (item.toId == userIds.uid? item.fromName: item.toName)
|
|
|
}}</span
|
|
|
><span class="person-cont spill">
|
|
|
{{ item.file?.fileName ? item.file?.fileName : item.content }}</span
|
|
|
@@ -508,7 +514,10 @@ onMounted(() => {
|
|
|
></span>
|
|
|
</div>
|
|
|
</div>
|
|
|
- <img :src="chat" class="head-sculpture" />
|
|
|
+ <!-- 头像 -->
|
|
|
+ <!-- <img :src="chat" class="head-sculpture" /> -->
|
|
|
+ <img :src="getMyThoto(record)" class="head-sculpture" v-if="getMyThoto(record)"/>
|
|
|
+ <span style="background-color: #7a89ba;display: inline-block;color: #fff;font-weight: 600;text-align: center;line-height: 40px;" class="head-sculpture" v-else>{{ record.fromName.slice(0,1) }}</span>
|
|
|
<div style="display: flex; align-items: center">
|
|
|
<div
|
|
|
v-if="useUserStore().uid == record.toId"
|