|
|
@@ -17,7 +17,7 @@ import {
|
|
|
userInfo,
|
|
|
msgSend,
|
|
|
msgRecord,
|
|
|
- delMsg
|
|
|
+ delMsg,
|
|
|
} from "@/api/chat/msg";
|
|
|
import Addperson from "@/components/AddPerson/index.vue"; //添加人员的弹框
|
|
|
//websocket连接====
|
|
|
@@ -26,17 +26,11 @@ import { ElMessage } from "element-plus";
|
|
|
const websoctStore = useWebsoctStore();
|
|
|
//====
|
|
|
const { proxy } = getCurrentInstance();
|
|
|
-const userIds= useUserStore()
|
|
|
+const userIds = useUserStore();
|
|
|
const height = ref(document.documentElement.clientHeight - 74 + "px;");
|
|
|
const messageText = ref(""); //发送的内容
|
|
|
const headerName = ref("");
|
|
|
-const chatRecords = reactive(
|
|
|
- { data: [] }
|
|
|
- // { sender: "friend", content: "Hello" },
|
|
|
- // { sender: "me", content: "Hi" },
|
|
|
- // { sender: "me", content: "How are you?" },
|
|
|
- // { sender: "friend", content: "I am fine, thanks!" },
|
|
|
-);
|
|
|
+const chatRecords = reactive({ data: [] });
|
|
|
const sendCont = reactive({
|
|
|
//发送聊天内容数据组装
|
|
|
data: {
|
|
|
@@ -51,11 +45,10 @@ const searchData = ref([]);
|
|
|
const SearchChat = () => {
|
|
|
if (searchText.value) {
|
|
|
searchData.value = personList.data.filter((i) => {
|
|
|
- console.log('i.nickName', i)
|
|
|
return i.toName == searchText.value;
|
|
|
});
|
|
|
} else {
|
|
|
- searchData.value =personList.data;
|
|
|
+ searchData.value = personList.data;
|
|
|
}
|
|
|
}; //搜索的点击事件
|
|
|
//聊天列表数据模拟
|
|
|
@@ -64,30 +57,28 @@ const personList = reactive({ data: [] });
|
|
|
//获取好友列表
|
|
|
const getMsgList = async () => {
|
|
|
const resFriend = await msgFriend();
|
|
|
- personList.data = resFriend.rows.filter(
|
|
|
- (item) => item.toId !== useUserStore().uid
|
|
|
- );
|
|
|
- searchData.value=personList.data
|
|
|
- sendCont.data.toId = personList.data[0]?.toId;
|
|
|
- headerName.value = personList.data[0]?.toName;
|
|
|
- if (personList.data[0]?.toId) {
|
|
|
+ personList.data = resFriend.rows;
|
|
|
+ // .filter(
|
|
|
+ // (item) => item.toId !== useUserStore().uid
|
|
|
+ // );
|
|
|
+ searchData.value = personList.data;
|
|
|
+ sendCont.data.toId =
|
|
|
+ personList.data[0]?.toId == userIds.uid
|
|
|
+ ? personList.data[0].fromId
|
|
|
+ : personList.data[0].toId;
|
|
|
+ headerName.value =
|
|
|
+ personList.data[0]?.toId == userIds.uid
|
|
|
+ ? personList.data[0].fromName
|
|
|
+ : personList.data[0].toName;
|
|
|
+ if (sendCont.data.toId) {
|
|
|
//调用聊天记录
|
|
|
- msgRecordEvent(personList.data[0]?.toId);
|
|
|
+ msgRecordEvent(sendCont.data.toId);
|
|
|
}
|
|
|
};
|
|
|
-// const rightMySend=reactive({data:[]})
|
|
|
-// const leftFriendSend=reactive({data:[]})
|
|
|
//获取用户的聊天记录
|
|
|
const msgRecordEvent = async (toIdValue) => {
|
|
|
const resMsgData = await msgRecord(toIdValue);
|
|
|
- chatRecords.data=resMsgData.rows
|
|
|
- // rightMySend.data=resMsgData.rows.filter(
|
|
|
- // (item) => item.toId !== useUserStore().uid
|
|
|
- // );
|
|
|
- // leftFriendSend.data=resMsgData.rows.filter(
|
|
|
- // (item) => item.toId === useUserStore().uid
|
|
|
- // );
|
|
|
- console.log("resMsgData", resMsgData);
|
|
|
+ chatRecords.data = resMsgData.rows.reverse();
|
|
|
};
|
|
|
//点击左侧新建聊天
|
|
|
const open = ref(false);
|
|
|
@@ -101,63 +92,93 @@ const clickNewPerson = async () => {
|
|
|
//点击左侧聊天列表
|
|
|
const clickPersonIndex = ref("");
|
|
|
const clickPerson = (index, item) => {
|
|
|
- sendCont.data.toId = item.userId ? item.userId : item.toId;
|
|
|
- headerName.value = item.nickName ? item.nickName : item.fromName;
|
|
|
+ sendCont.data.toId = item.userId
|
|
|
+ ? item.userId
|
|
|
+ : item.toId == userIds.uid
|
|
|
+ ? item.fromId
|
|
|
+ : item.toId;
|
|
|
+ headerName.value = item.nickName
|
|
|
+ ? item.nickName
|
|
|
+ : item.toId == userIds.uid
|
|
|
+ ? item.fromName
|
|
|
+ : item.toName;
|
|
|
clickPersonIndex.value = index;
|
|
|
msgRecordEvent(sendCont.data.toId);
|
|
|
- console.log("first", clickPersonIndex.value);
|
|
|
};
|
|
|
//删除聊天
|
|
|
-const delClick=(msgId)=>{
|
|
|
- proxy.$modal.confirm('删除后,将清空该聊天的消息记录').then(function() {
|
|
|
- return delMsg(msgId);
|
|
|
- }).then(() => {
|
|
|
- getMsgList();
|
|
|
- proxy.$modal.msgSuccess("删除成功");
|
|
|
- })
|
|
|
-}
|
|
|
-//发送聊天点击事件
|
|
|
-// const messageChat = () => {
|
|
|
-// const message = { text: messageText.value };
|
|
|
-// websoctStore.send(message);
|
|
|
-// messageText.value = "";
|
|
|
-// };
|
|
|
+const delClick = (msgId) => {
|
|
|
+ console.log("msgId", msgId);
|
|
|
+ proxy.$modal
|
|
|
+ .confirm("删除后,将清空该聊天的消息记录")
|
|
|
+ .then(function () {
|
|
|
+ return delMsg(msgId);
|
|
|
+ })
|
|
|
+ .then(() => {
|
|
|
+ getMsgList();
|
|
|
+ proxy.$modal.msgSuccess("删除成功");
|
|
|
+ });
|
|
|
+};
|
|
|
//树选中的人传过来的选中人的信息,push进入列表中
|
|
|
const changeMsg = async (val) => {
|
|
|
const resInfo = await userInfo(val.id);
|
|
|
- // if (personList.data.length > 0) {
|
|
|
- // personList.data.map((vPerson) => {
|
|
|
- // if (vPerson.userId != resInfo.userId) {
|
|
|
- // personList.data.push(resInfo);
|
|
|
- // } else {
|
|
|
- // return ElMessage({ message: "该聊天已存在", type: "error" });
|
|
|
- // }
|
|
|
- // });
|
|
|
- // } else {
|
|
|
- // personList.data.push(resInfo);
|
|
|
- // }
|
|
|
if (searchData.value.length > 0) {
|
|
|
- searchData.value.map((vPerson) => {
|
|
|
- if (vPerson.userId != resInfo.userId) {
|
|
|
- searchData.value.push(resInfo);
|
|
|
- } else {
|
|
|
- return ElMessage({ message: "该聊天已存在", type: "error" });
|
|
|
- }
|
|
|
- });
|
|
|
+ const filerData = searchData.value.filter(
|
|
|
+ (vPerson) =>
|
|
|
+ (vPerson.toId && vPerson.toId == resInfo.userId) ||
|
|
|
+ (vPerson.userId && vPerson.userId == resInfo.userId)
|
|
|
+ );
|
|
|
+ if (filerData.length > 0) {
|
|
|
+ return ElMessage({ message: "该聊天已存在", type: "error" });
|
|
|
+ } else {
|
|
|
+ searchData.value.unshift(resInfo);
|
|
|
+ }
|
|
|
} else {
|
|
|
- searchData.value.push(resInfo);
|
|
|
+ //为空数组的时候无需判断
|
|
|
+ searchData.value.unshift(resInfo);
|
|
|
}
|
|
|
+ console.log("first", searchData.value);
|
|
|
sendCont.data.toId = resInfo.userId;
|
|
|
+ msgRecordEvent(sendCont.data.toId);
|
|
|
headerName.value = resInfo.nickName;
|
|
|
};
|
|
|
-//登录账号发送聊天
|
|
|
-const msgSendClick = async () => {
|
|
|
- sendCont.data.content = messageText.value;
|
|
|
- sendCont.data.msgType = "2";
|
|
|
- const mysendmsg = await msgSend(sendCont.data);
|
|
|
+//发送聊天
|
|
|
+const msgSendClick = () => {
|
|
|
+ const message = { content: messageText.value, msgType: "2",fileList: [],toId: sendCont.data.toId, };
|
|
|
+ // commonSendMsg(messageText.value, "2");
|
|
|
+ websoctStore.sendMessage(message);
|
|
|
messageText.value = "";
|
|
|
- console.log("mysendmsg", mysendmsg, sendCont.data);
|
|
|
};
|
|
|
+//发送文件
|
|
|
+const openFile = ref(false);
|
|
|
+const sendFileClick = () => {
|
|
|
+ openFile.value = true;
|
|
|
+};
|
|
|
+//文件和信息共用的调用
|
|
|
+const commonSendMsg = async (cont, type) => {
|
|
|
+ sendCont.data.content = cont;
|
|
|
+ sendCont.data.msgType = type;
|
|
|
+ const mysendmsg = await msgSend(sendCont.data);
|
|
|
+};
|
|
|
+// const confirmFile = () => {
|
|
|
+// // commonSendMsg(messageText.value,"1")
|
|
|
+// };
|
|
|
+// // const handleFileUpdate = (value) => {
|
|
|
+// // // 在这里处理子组件传递过来的文件
|
|
|
+// // console.log(value, "valuevaluevalue"); // 打印子组件传递的数据
|
|
|
+// // };
|
|
|
+// 监听新消息通知
|
|
|
+const handleNewMessage = () => {
|
|
|
+ if (websoctStore.newMessage) {
|
|
|
+ msgRecordEvent(sendCont.data.toId);
|
|
|
+ ElMessage.success("You have a new message");
|
|
|
+ websoctStore.newMessage = false; // 重置新消息标记
|
|
|
+ }
|
|
|
+};
|
|
|
+onMounted(() => {
|
|
|
+ getMsgList();
|
|
|
+ websoctStore.connect();
|
|
|
+ setInterval(handleNewMessage, 1000); // 每秒钟检查是否有新消息
|
|
|
+});
|
|
|
onMounted(() => {
|
|
|
try {
|
|
|
getMsgList();
|
|
|
@@ -183,6 +204,7 @@ onMounted(() => {
|
|
|
<el-icon size="24" color="#505870" @click="clickNewPerson"
|
|
|
><Plus
|
|
|
/></el-icon>
|
|
|
+ <!-- 新建聊天弹框 -->
|
|
|
<Addperson
|
|
|
:open="open"
|
|
|
@close="open = false"
|
|
|
@@ -190,6 +212,7 @@ onMounted(() => {
|
|
|
@changeMsg="changeMsg"
|
|
|
></Addperson>
|
|
|
</div>
|
|
|
+ <!-- 列表 -->
|
|
|
<div
|
|
|
:class="
|
|
|
clickPersonIndex == index
|
|
|
@@ -200,15 +223,24 @@ onMounted(() => {
|
|
|
:key="index"
|
|
|
@click="clickPerson(index, item)"
|
|
|
>
|
|
|
-
|
|
|
<img :src="cebian" class="cebian" v-if="clickPersonIndex == index" />
|
|
|
- <button class="del-chat" v-if="clickPersonIndex == index" @click="delClick(item.msgId)">删除聊天</button>
|
|
|
+ <button
|
|
|
+ class="del-chat"
|
|
|
+ v-if="clickPersonIndex == index"
|
|
|
+ @click="delClick(item.msgId)"
|
|
|
+ >
|
|
|
+ 删除聊天
|
|
|
+ </button>
|
|
|
<div>
|
|
|
<img :src="item.avatar ? item.avatar : chat" class="head-sculpture" />
|
|
|
</div>
|
|
|
- <div>
|
|
|
+ <div class="spill">
|
|
|
<span class="person-name">{{
|
|
|
- item.nickName ? item.nickName : item.toName
|
|
|
+ item.nickName
|
|
|
+ ? item.nickName
|
|
|
+ : item.toId == userIds.uid
|
|
|
+ ? item.fromName
|
|
|
+ : item.toName
|
|
|
}}</span
|
|
|
><span class="person-cont spill">
|
|
|
{{ item.cont ? item.cont : item.content }}</span
|
|
|
@@ -232,35 +264,35 @@ onMounted(() => {
|
|
|
class="message-container"
|
|
|
v-for="(record, index) in chatRecords.data"
|
|
|
:class="{
|
|
|
- 'message-left': useUserStore().uid== record.toId,
|
|
|
- 'message-right': useUserStore().uid !== record.toId,
|
|
|
+ 'message-left': useUserStore().uid == record.toId,
|
|
|
+ 'message-right': useUserStore().uid !== record.toId,
|
|
|
}"
|
|
|
:key="index"
|
|
|
>
|
|
|
- <span v-if=" useUserStore().uid!== record.toId">{{ record.content }}</span>
|
|
|
+ <span v-if="useUserStore().uid !== record.toId">{{
|
|
|
+ record.content
|
|
|
+ }}</span>
|
|
|
<img :src="chat" class="head-sculpture" />
|
|
|
- <span v-if=" useUserStore().uid == record.toId">{{ record.content }}</span>
|
|
|
+ <span v-if="useUserStore().uid == record.toId">{{
|
|
|
+ record.content
|
|
|
+ }}</span>
|
|
|
</div>
|
|
|
- <!-- <div class="chat-message">
|
|
|
- <div class="chat-item" :class="{'chat-item-out':item.sender=='out'}" v-for="(item,index) in chatRecords" :key="index">
|
|
|
- <div class="chat-item-content">
|
|
|
- <div :class="{'triangle':item.sender == 'me','triangle_tow': item.sender !== 'me'}"></div>
|
|
|
- <div :class="{'fill':item.sender == 'me','fill_tow': item.sender !== 'me'}"></div>
|
|
|
- {{ item.content }}
|
|
|
- </div>
|
|
|
-
|
|
|
- </div>
|
|
|
- </div> -->
|
|
|
</el-main>
|
|
|
<!-- 底部 -->
|
|
|
<el-footer height="72px" class="right-footer">
|
|
|
- <img :src="sendFile" class="send-info-file" />
|
|
|
+ <!-- 发送文件 -->
|
|
|
+ <img
|
|
|
+ :src="sendFile"
|
|
|
+ class="send-info-file"
|
|
|
+ @click="sendFileClick"
|
|
|
+ />
|
|
|
<el-input
|
|
|
v-model="messageText"
|
|
|
class="w-50 m-2"
|
|
|
size="small"
|
|
|
placeholder="请输入聊天内容"
|
|
|
/>
|
|
|
+ <!-- 发送按钮 -->
|
|
|
<img :src="send" class="send-info" @click="msgSendClick" />
|
|
|
</el-footer>
|
|
|
</el-container>
|
|
|
@@ -271,38 +303,4 @@ onMounted(() => {
|
|
|
|
|
|
<style lang="scss" scoped>
|
|
|
@import "@/assets/styles/my-common.scss";
|
|
|
-// .chat-item-out{
|
|
|
-// justify-content: flex-end;
|
|
|
-// .chat-item-content{
|
|
|
-// margin-right: 10px;
|
|
|
-// }
|
|
|
-// }
|
|
|
-// .chat-message{
|
|
|
-
|
|
|
-// }
|
|
|
-// .chat-item-content{
|
|
|
-// position: relative;
|
|
|
-// padding:10px 6px;
|
|
|
-// margin-top: 10px;
|
|
|
-// margin-left: 10px;
|
|
|
-// border-radius: 5px;
|
|
|
-// word-break: break-all;
|
|
|
-// background: skyblue;
|
|
|
-// color: white;
|
|
|
-// }
|
|
|
-// .chat-item{
|
|
|
-// display: flex;
|
|
|
-// }
|
|
|
-// .triangle,.triangle_tow{
|
|
|
-// position: absolute;
|
|
|
-// top: 15px;
|
|
|
-// border-width: 10px;
|
|
|
-// border-style: solid;
|
|
|
-// }
|
|
|
-// .fill_tow{
|
|
|
-// position: relative;
|
|
|
-// top: 15px;
|
|
|
-// border-width: 10px;
|
|
|
-// border-style:solid ;
|
|
|
-// }
|
|
|
</style>
|