|
|
@@ -4,67 +4,159 @@ export default {
|
|
|
};
|
|
|
</script>
|
|
|
<script setup>
|
|
|
-import { ref, reactive } from "vue";
|
|
|
+import { ref, reactive, toRaw } from "vue";
|
|
|
+import useUserStore from "@/store/modules/user";
|
|
|
import chat from "@/assets/images/profile.jpg";
|
|
|
import cebian from "@/assets/images/cebian.png";
|
|
|
import send from "@/assets/images/send.png";
|
|
|
import sendFile from "@/assets/images/send-file.png";
|
|
|
import store from "@/store";
|
|
|
-import { msgFriend } from "@/api/chat/msg";
|
|
|
+import {
|
|
|
+ msgFriend,
|
|
|
+ userTree,
|
|
|
+ userInfo,
|
|
|
+ msgSend,
|
|
|
+ msgRecord,
|
|
|
+ delMsg
|
|
|
+} from "@/api/chat/msg";
|
|
|
import Addperson from "@/components/AddPerson/index.vue"; //添加人员的弹框
|
|
|
//websocket连接====
|
|
|
import useWebsoctStore from "@/store/modules/websocket";
|
|
|
+import { ElMessage } from "element-plus";
|
|
|
const websoctStore = useWebsoctStore();
|
|
|
//====
|
|
|
-
|
|
|
+const { proxy } = getCurrentInstance();
|
|
|
+const userIds= useUserStore()
|
|
|
const height = ref(document.documentElement.clientHeight - 74 + "px;");
|
|
|
-const searchText = ref(""); //搜
|
|
|
const messageText = ref(""); //发送的内容
|
|
|
-const SearchChat = () => {}; //搜索的点击事件
|
|
|
-//聊天列表数据模拟
|
|
|
-const personList = reactive({
|
|
|
- data: [
|
|
|
- {
|
|
|
- imgs: chat,
|
|
|
- name: "Kathry Murl",
|
|
|
- cont: "comment inside the import() call to suppress this",
|
|
|
- },
|
|
|
- {
|
|
|
- imgs: chat,
|
|
|
- name: "Mathry Murl",
|
|
|
- cont: "comment inside o suppress this",
|
|
|
- },
|
|
|
- ],
|
|
|
+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 sendCont = reactive({
|
|
|
+ //发送聊天内容数据组装
|
|
|
+ data: {
|
|
|
+ content: "",
|
|
|
+ fileList: [],
|
|
|
+ toId: 0,
|
|
|
+ msgType: "",
|
|
|
+ },
|
|
|
});
|
|
|
+const searchText = ref(""); //搜
|
|
|
+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;
|
|
|
+ }
|
|
|
+}; //搜索的点击事件
|
|
|
+//聊天列表数据模拟
|
|
|
+const personList = reactive({ data: [] });
|
|
|
// const personList =reactive({data: []})
|
|
|
//获取好友列表
|
|
|
-function getMsgList() {
|
|
|
- msgFriend().then((res) => {
|
|
|
- // personList.data=res.rows
|
|
|
- });
|
|
|
-}
|
|
|
-const chatRecords = reactive([
|
|
|
- { sender: "friend", content: "Hello" },
|
|
|
- { sender: "me", content: "Hi" },
|
|
|
- { sender: "me", content: "How are you?" },
|
|
|
- { sender: "friend", content: "I am fine, thanks!" },
|
|
|
-]);
|
|
|
+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) {
|
|
|
+ //调用聊天记录
|
|
|
+ msgRecordEvent(personList.data[0]?.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);
|
|
|
+};
|
|
|
//点击左侧新建聊天
|
|
|
const open = ref(false);
|
|
|
-const clickNewPerson = () => {
|
|
|
+const userTreeData = reactive({ data: [] });
|
|
|
+const clickNewPerson = async () => {
|
|
|
+ const res = await userTree();
|
|
|
+ userTreeData.data = res;
|
|
|
+ toRaw(userTreeData.data);
|
|
|
open.value = true;
|
|
|
- console.log("1111", 1111);
|
|
|
};
|
|
|
//点击左侧聊天列表
|
|
|
const clickPersonIndex = ref("");
|
|
|
-const clickPerson = (index) => {
|
|
|
+const clickPerson = (index, item) => {
|
|
|
+ sendCont.data.toId = item.userId ? item.userId : item.toId;
|
|
|
+ headerName.value = item.nickName ? item.nickName : item.fromName;
|
|
|
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);
|
|
|
+// const messageChat = () => {
|
|
|
+// const message = { text: messageText.value };
|
|
|
+// websoctStore.send(message);
|
|
|
+// messageText.value = "";
|
|
|
+// };
|
|
|
+//树选中的人传过来的选中人的信息,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" });
|
|
|
+ }
|
|
|
+ });
|
|
|
+ } else {
|
|
|
+ searchData.value.push(resInfo);
|
|
|
+ }
|
|
|
+ sendCont.data.toId = resInfo.userId;
|
|
|
+ headerName.value = resInfo.nickName;
|
|
|
+};
|
|
|
+//登录账号发送聊天
|
|
|
+const msgSendClick = async () => {
|
|
|
+ sendCont.data.content = messageText.value;
|
|
|
+ sendCont.data.msgType = "2";
|
|
|
+ const mysendmsg = await msgSend(sendCont.data);
|
|
|
messageText.value = "";
|
|
|
+ console.log("mysendmsg", mysendmsg, sendCont.data);
|
|
|
};
|
|
|
onMounted(() => {
|
|
|
try {
|
|
|
@@ -91,18 +183,12 @@ onMounted(() => {
|
|
|
<el-icon size="24" color="#505870" @click="clickNewPerson"
|
|
|
><Plus
|
|
|
/></el-icon>
|
|
|
- <Addperson :open="open" @close="open = false"></Addperson>
|
|
|
- <!-- <el-dialog
|
|
|
- title="新建聊天"
|
|
|
- append-to-body
|
|
|
- v-model="open"
|
|
|
- style="
|
|
|
- width: 512px;
|
|
|
- height: 428px;
|
|
|
- "
|
|
|
- >
|
|
|
- <Addperson></Addperson>
|
|
|
- </el-dialog> -->
|
|
|
+ <Addperson
|
|
|
+ :open="open"
|
|
|
+ @close="open = false"
|
|
|
+ :userTreeData="userTreeData.data"
|
|
|
+ @changeMsg="changeMsg"
|
|
|
+ ></Addperson>
|
|
|
</div>
|
|
|
<div
|
|
|
:class="
|
|
|
@@ -110,15 +196,23 @@ onMounted(() => {
|
|
|
? 'activ-left-container left-container'
|
|
|
: 'left-container'
|
|
|
"
|
|
|
- v-for="(item, index) in personList.data"
|
|
|
+ v-for="(item, index) in searchData"
|
|
|
:key="index"
|
|
|
- @click="clickPerson(index)"
|
|
|
+ @click="clickPerson(index, item)"
|
|
|
>
|
|
|
+
|
|
|
<img :src="cebian" class="cebian" v-if="clickPersonIndex == index" />
|
|
|
- <div><img :src="item.imgs" class="head-sculpture" /></div>
|
|
|
+ <button class="del-chat" v-if="clickPersonIndex == index" @click="delClick(item.msgId)">删除聊天</button>
|
|
|
<div>
|
|
|
- <span class="person-name">{{ item.name }}</span
|
|
|
- ><span class="person-cont spill"> {{ item.cont }}</span>
|
|
|
+ <img :src="item.avatar ? item.avatar : chat" class="head-sculpture" />
|
|
|
+ </div>
|
|
|
+ <div>
|
|
|
+ <span class="person-name">{{
|
|
|
+ item.nickName ? item.nickName : item.toName
|
|
|
+ }}</span
|
|
|
+ ><span class="person-cont spill">
|
|
|
+ {{ item.cont ? item.cont : item.content }}</span
|
|
|
+ >
|
|
|
</div>
|
|
|
</div>
|
|
|
</div>
|
|
|
@@ -129,23 +223,23 @@ onMounted(() => {
|
|
|
style="display: flex; flex-direction: column; height: 100vh"
|
|
|
>
|
|
|
<el-container>
|
|
|
- <el-header height="64px" class="right-header flex-buju"
|
|
|
- >Header</el-header
|
|
|
- >
|
|
|
+ <el-header height="64px" class="right-header flex-buju">{{
|
|
|
+ headerName
|
|
|
+ }}</el-header>
|
|
|
<!-- 聊天 -->
|
|
|
<el-main class="right-container">
|
|
|
<div
|
|
|
class="message-container"
|
|
|
- v-for="(record, index) in chatRecords"
|
|
|
+ v-for="(record, index) in chatRecords.data"
|
|
|
:class="{
|
|
|
- 'message-left': record.sender !== 'me',
|
|
|
- 'message-right': record.sender === 'me',
|
|
|
+ 'message-left': useUserStore().uid== record.toId,
|
|
|
+ 'message-right': useUserStore().uid !== record.toId,
|
|
|
}"
|
|
|
:key="index"
|
|
|
>
|
|
|
- <span v-if="record.sender == 'me'">{{ record.content }}</span>
|
|
|
+ <span v-if=" useUserStore().uid!== record.toId">{{ record.content }}</span>
|
|
|
<img :src="chat" class="head-sculpture" />
|
|
|
- <span v-if="record.sender !== 'me'">{{ 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">
|
|
|
@@ -166,9 +260,8 @@ onMounted(() => {
|
|
|
class="w-50 m-2"
|
|
|
size="small"
|
|
|
placeholder="请输入聊天内容"
|
|
|
- @change="messageChat"
|
|
|
/>
|
|
|
- <img :src="send" class="send-info" />
|
|
|
+ <img :src="send" class="send-info" @click="msgSendClick" />
|
|
|
</el-footer>
|
|
|
</el-container>
|
|
|
</div>
|