添加获取历史消息,添加消息提示
This commit is contained in:
parent
2f68ef1f1d
commit
19c671de8b
|
|
@ -18,7 +18,9 @@
|
||||||
<el-menu-item-group v-for="user in filteredUsers" :key="user.id">
|
<el-menu-item-group v-for="user in filteredUsers" :key="user.id">
|
||||||
<el-menu-item :index="String(user.id)" @click="handleGetMessage(user.id)" style="height: 40px;">
|
<el-menu-item :index="String(user.id)" @click="handleGetMessage(user.id)" style="height: 40px;">
|
||||||
{{ user.name }}
|
{{ user.name }}
|
||||||
|
<span v-if="hasUnreadMsg[user.id]" class="unread-dot"></span>
|
||||||
</el-menu-item>
|
</el-menu-item>
|
||||||
|
|
||||||
</el-menu-item-group>
|
</el-menu-item-group>
|
||||||
</el-scrollbar>
|
</el-scrollbar>
|
||||||
</el-sub-menu>
|
</el-sub-menu>
|
||||||
|
|
@ -44,6 +46,12 @@
|
||||||
<el-container width="800px">
|
<el-container width="800px">
|
||||||
<el-header style="text-align: right; font-size: 12px">
|
<el-header style="text-align: right; font-size: 12px">
|
||||||
<div class="toolbar">
|
<div class="toolbar">
|
||||||
|
<el-button
|
||||||
|
type="primary"
|
||||||
|
size="mini"
|
||||||
|
@click.prevent="getHistory()"
|
||||||
|
>历史消息</el-button
|
||||||
|
>
|
||||||
<el-button
|
<el-button
|
||||||
type="primary"
|
type="primary"
|
||||||
size="mini"
|
size="mini"
|
||||||
|
|
@ -135,6 +143,7 @@ export default {
|
||||||
to_user_id:0,
|
to_user_id:0,
|
||||||
cur_user_id:0,
|
cur_user_id:0,
|
||||||
searchName: '', // 搜索框内容
|
searchName: '', // 搜索框内容
|
||||||
|
history_cnt: 1,
|
||||||
|
|
||||||
cur_user_name: "",
|
cur_user_name: "",
|
||||||
eventSource: null, // 事件源
|
eventSource: null, // 事件源
|
||||||
|
|
@ -142,6 +151,7 @@ export default {
|
||||||
currentMsg:"",
|
currentMsg:"",
|
||||||
MsgList: [],
|
MsgList: [],
|
||||||
groupList: [],
|
groupList: [],
|
||||||
|
hasUnreadMsg: {}, // 未读消息, key为用户id,value为true/false
|
||||||
};
|
};
|
||||||
},
|
},
|
||||||
|
|
||||||
|
|
@ -157,6 +167,9 @@ export default {
|
||||||
}
|
}
|
||||||
let data = result.data;
|
let data = result.data;
|
||||||
this.userList=data.friends;
|
this.userList=data.friends;
|
||||||
|
for(let i = 0; i < data.friends.length; i++){
|
||||||
|
this.hasUnreadMsg[data.friends[i].id] = false;
|
||||||
|
}
|
||||||
this.groupList=data.groups;
|
this.groupList=data.groups;
|
||||||
},
|
},
|
||||||
filterUsers() {
|
filterUsers() {
|
||||||
|
|
@ -164,8 +177,15 @@ export default {
|
||||||
return user.name.toLowerCase().includes(this.searchName.toLowerCase());
|
return user.name.toLowerCase().includes(this.searchName.toLowerCase());
|
||||||
});
|
});
|
||||||
},
|
},
|
||||||
|
getHistory() {
|
||||||
|
this.history_cnt++
|
||||||
|
this.handleGetMessage(this.cur_user_id);
|
||||||
|
},
|
||||||
async handleGetMessage(id){
|
async handleGetMessage(id){
|
||||||
let result = {};
|
let result = {};
|
||||||
|
if(this.to_user_id!=id){
|
||||||
|
this.history_cnt = 2;
|
||||||
|
}
|
||||||
this.to_user_id=id;
|
this.to_user_id=id;
|
||||||
this.cur_user_id=id;
|
this.cur_user_id=id;
|
||||||
console.log("uid:",this.uid,"\tcur_user_id:",this.cur_user_id)
|
console.log("uid:",this.uid,"\tcur_user_id:",this.cur_user_id)
|
||||||
|
|
@ -175,6 +195,7 @@ export default {
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
this.hasUnreadMsg[id] = false;
|
||||||
try {
|
try {
|
||||||
let req={
|
let req={
|
||||||
token: localStorage.getItem("token"),
|
token: localStorage.getItem("token"),
|
||||||
|
|
@ -183,7 +204,7 @@ export default {
|
||||||
username: localStorage.getItem("username"),
|
username: localStorage.getItem("username"),
|
||||||
from_user_id:id,
|
from_user_id:id,
|
||||||
to_user_id:localStorage.getItem("userId"),
|
to_user_id:localStorage.getItem("userId"),
|
||||||
index:2,
|
index:this.history_cnt,
|
||||||
type:1,
|
type:1,
|
||||||
}
|
}
|
||||||
result = await getMessageService(req);
|
result = await getMessageService(req);
|
||||||
|
|
@ -199,17 +220,26 @@ export default {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
//data sort by created time
|
//data sort by created time
|
||||||
for (let i = 0; i < data.length; i++) {
|
// for (let i = 0; i < data.length; i++) {
|
||||||
for (let j = i + 1; j < data.length; j++) {
|
// for (let j = i + 1; j < data.length; j++) {
|
||||||
if (data[i].CreatedAt > data[j].CreatedAt) {
|
// if (data[i].CreatedAt > data[j].CreatedAt) {
|
||||||
let temp = data[i];
|
// let temp = data[i];
|
||||||
data[i] = data[j];
|
// data[i] = data[j];
|
||||||
data[j] = temp;
|
// data[j] = temp;
|
||||||
}
|
// }
|
||||||
}
|
// }
|
||||||
}
|
// }
|
||||||
|
data.sort((a, b) => {
|
||||||
|
const dateA = new Date(a.CreatedAt);
|
||||||
|
const dateB = new Date(b.CreatedAt);
|
||||||
|
|
||||||
|
// 返回时间差,用于排序
|
||||||
|
return dateA - dateB;
|
||||||
|
});
|
||||||
this.MsgList=data;
|
this.MsgList=data;
|
||||||
this.scrollToBottom();
|
if(this.history_cnt <=2){
|
||||||
|
this.scrollToBottom();
|
||||||
|
}
|
||||||
},
|
},
|
||||||
formatTime(time){
|
formatTime(time){
|
||||||
let date = new Date(time);
|
let date = new Date(time);
|
||||||
|
|
@ -276,7 +306,14 @@ export default {
|
||||||
if (data.type == "msg") { // 如果是消息类型,解密消息内容
|
if (data.type == "msg") { // 如果是消息类型,解密消息内容
|
||||||
// console.log("收到数据====" + JSON.stringify(msg.data));
|
// console.log("收到数据====" + JSON.stringify(msg.data));
|
||||||
console.log("msg_:", data.data);
|
console.log("msg_:", data.data);
|
||||||
_this.MsgList.push(JSON.parse(data.data));
|
let msg_data=JSON.parse(data.data)
|
||||||
|
_this.MsgList.push(msg_data);
|
||||||
|
if(_this.cur_user_id != msg_data.FromUserID){
|
||||||
|
_this.hasUnreadMsg[msg_data.FromUserID] = true;
|
||||||
|
}else{
|
||||||
|
_this.scrollToBottom();
|
||||||
|
}
|
||||||
|
|
||||||
//console.log("msglist:", _this.MsgList);
|
//console.log("msglist:", _this.MsgList);
|
||||||
// 构建消息内容
|
// 构建消息内容
|
||||||
}else if (data.type == "check") {
|
}else if (data.type == "check") {
|
||||||
|
|
@ -479,4 +516,14 @@ export default {
|
||||||
margin-top: 20px;
|
margin-top: 20px;
|
||||||
color: white;
|
color: white;
|
||||||
}
|
}
|
||||||
|
.unread-dot {
|
||||||
|
position: absolute;
|
||||||
|
top: 0;
|
||||||
|
right: 0;
|
||||||
|
width: 8px;
|
||||||
|
height: 8px;
|
||||||
|
background-color: red;
|
||||||
|
border-radius: 50%;
|
||||||
|
transform: translate(50%, -50%);
|
||||||
|
}
|
||||||
</style>
|
</style>
|
||||||
|
|
@ -13,6 +13,7 @@
|
||||||
<script>
|
<script>
|
||||||
import { ref, onMounted, inject, onUnmounted } from "vue";
|
import { ref, onMounted, inject, onUnmounted } from "vue";
|
||||||
import router from "@/router/index.js";
|
import router from "@/router/index.js";
|
||||||
|
import { ElMessage } from 'element-plus';
|
||||||
export default {
|
export default {
|
||||||
data() {
|
data() {
|
||||||
return {
|
return {
|
||||||
|
|
@ -60,14 +61,16 @@ export default {
|
||||||
//打开事件
|
//打开事件
|
||||||
this.socket.onopen = function () {
|
this.socket.onopen = function () {
|
||||||
this.loading = false;
|
this.loading = false;
|
||||||
alert("连接成功");
|
//alert("连接成功");
|
||||||
|
ElMessage.success("连接成功!");
|
||||||
};
|
};
|
||||||
this.socket.onerror = (error) => {
|
this.socket.onerror = (error) => {
|
||||||
console.error("WebSocket Error:", error);
|
console.error("WebSocket Error:", error);
|
||||||
};
|
};
|
||||||
//关闭事件
|
//关闭事件
|
||||||
this.socket.onclose = function () {
|
this.socket.onclose = function () {
|
||||||
alert("连接已关闭!");
|
//alert("连接已关闭!");
|
||||||
|
ElMessage.success("连接已关闭!");
|
||||||
router.push("/user");
|
router.push("/user");
|
||||||
};
|
};
|
||||||
// 浏览器端收消息,获得从服务端发送过来的文本消息
|
// 浏览器端收消息,获得从服务端发送过来的文本消息
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue