Compare commits

..
6 Commits
Author SHA1 Message Date
Seven 9cdeeec91c 导航菜单切换 2026-09-14 22:29:53 +08:00
Seven dd00aff742 修复render时的报错 2026-09-14 21:44:44 +08:00
Seven 25c684d16b 优化联系人消息获取 2026-09-14 21:31:34 +08:00
Seven b959d1dcc5 修改聊天界面,增加联系人信息获取接口 2026-09-13 15:31:56 +08:00
Seven f27b9bec46 ai编写聊天界面 2026-09-12 16:31:41 +08:00
Seven 60d7ecea5f 登录后获取信息,用于判断登录后是否过期 2026-09-12 15:51:53 +08:00
2 changed files with 834 additions and 39 deletions
+810 -31
View File
@@ -1,56 +1,835 @@
<template> <template>
<div class="hello"> <div class="chat-page">
<el-input placeholder="请输入用户名" v-model="username" clearable> <!-- 左侧导航 -->
</el-input> <aside class="app-sidebar">
<el-input <div class="avatar my-avatar">
placeholder="请输入密码" {{ currentUser.username ? currentUser.username.charAt(0) : "" }}
v-model="password" </div>
show-password
></el-input> <nav class="sidebar-nav" aria-label="功能导航">
<el-button <button
class="login-button" class="nav-item"
type="primary" :class="{ active: activeMenu === 'messages' }"
native-type="submit" type="button"
@click="handleLogout" title="消息"
> @click="changeMenu('messages')"
退出 >
</el-button> 💬
</button>
<button
class="nav-item"
:class="{ active: activeMenu === 'contacts' }"
type="button"
title="联系人"
@click="changeMenu('contacts')"
>
👥
</button>
<button
class="nav-item"
:class="{ active: activeMenu === 'settings' }"
type="button"
title="设置"
@click="changeMenu('settings')"
>
⚙️
</button>
</nav>
<div class="sidebar-footer">
<button class="nav-item" type="button" title="退出" @click="logout">
↪
</button>
</div>
</aside>
<!-- 左侧联系人 -->
<div class="contact-panel">
<!-- 用户信息 -->
<div class="user-header">
<!-- 用户信息 -->
<div class="my-info">
<div class="my-name">{{ currentUser.username }}</div>
</div>
</div>
<!-- 搜索框 -->
<div class="search-box">
<input v-model="searchText" type="text" placeholder="搜索联系人" />
</div>
<!-- 最近消息列表 -->
<div
class="message-list"
:class="activeMenu === 'messages' ? 'active' : ''"
>
<div
v-for="contact in filteredContacts"
:key="contact.contactId"
class="contact-item"
:class="{
active:
currentContact && currentContact.contactId === contact.contactId,
}"
@click="selectContact(contact)"
>
<div class="avatar">
{{ contact.contactName.charAt(0) }}
</div>
<div class="contact-info">
<div class="contact-name">
{{ contact.contactName }}
</div>
<div class="last-message">
{{ getLastMessage(contact.contactId) }}
</div>
</div>
<div v-if="contact.unread > 0" class="unread">
{{ contact.unread }}
</div>
</div>
</div>
<!-- 联系人列表 -->
<div
class="message-list"
:class="activeMenu === 'contacts' ? 'active' : ''"
>
<div
v-for="contact in contactList"
:key="contact.contactId"
class="contact-item"
@click="selectContact(contact)"
>
<div class="avatar">
{{ contact.contactName.charAt(0) }}
</div>
<div class="contact-info">
<div class="contact-name">
{{ contact.contactName }}
</div>
</div>
</div>
</div>
</div>
<!-- 右侧聊天区域 -->
<div class="chat-panel">
<!-- 没有选择联系人 -->
<div v-if="!currentContact" class="empty-chat">
<div class="empty-icon">💬</div>
<div>请选择一个联系人开始聊天</div>
</div>
<!-- 当前聊天 -->
<template v-else>
<!-- 聊天顶部 -->
<div class="chat-header">
<div class="avatar">
{{ currentContact.contactName.charAt(0) }}
</div>
<div class="chat-user-info">
<div class="chat-user-name">
{{ currentContact.name }}
</div>
<div class="chat-user-status">
{{ currentContact.online ? "在线" : "离线" }}
</div>
</div>
</div>
<!-- 消息区域 -->
<div
ref="messageList"
class="message-list"
:class="activeMenu === 'messages' ? 'active' : ''"
>
<div
v-for="message in currentMessages"
:key="message.id"
class="message-row"
:class="{
'message-me': message.from === 'me',
'message-other': message.from === 'other',
}"
>
<!-- 对方头像 -->
<div v-if="message.from === 'other'" class="avatar message-avatar">
{{ currentContact.contactName.charAt(0) }}
</div>
<div class="message-content">
<div class="message-time">
{{ message.time }}
</div>
<div class="message-bubble">
{{ message.content }}
</div>
</div>
<!-- 我的头像 -->
<div
v-if="message.from === 'me'"
class="avatar message-avatar my-avatar"
>
我
</div>
</div>
</div>
<div
ref="messageList"
class="message-list"
:class="activeMenu === 'contacts' ? 'empty' : ''"
>
<div class="empty-content">💬</div>
</div>
<!-- 输入区域 -->
<div class="input-panel">
<div class="toolbar">
<button type="button">😊</button>
<button type="button">📎</button>
</div>
<textarea
v-model="messageText"
placeholder="请输入消息..."
@keydown.enter.exact.prevent="sendMessage"
></textarea>
<div class="send-area">
<span class="enter-tip"> Enter 发送 </span>
<button
class="send-button"
type="button"
:disabled="!messageText.trim()"
@click="sendMessage"
>
发送
</button>
</div>
</div>
</template>
</div>
</div> </div>
</template> </template>
<script> <script>
import { getUserInfo, getContactInfo, getContactMessages } from "@/utils/user";
export default { export default {
name: "ChatPage", name: "ChatPage",
props: {},
data() { data() {
return { return {
username: "", // 当前用户
password: "", currentUser: {},
// 联系人
contactList: [],
// 当前聊天联系人
currentContact: null,
// 聊天消息
groupedMessages: {},
// 搜索联系人
searchText: "",
// 输入的消息内容
messageText: "",
// 当前选中的菜单
activeMenu: "messages",
}; };
}, },
computed: {
// 搜索联系人
filteredContacts() {
if (!this.searchText.trim()) {
return this.contactList;
}
const keyword = this.searchText.trim().toLowerCase();
return this.contactList.filter((contact) =>
contact.name.toLowerCase().includes(keyword),
);
},
// 当前联系人对应的消息
currentMessages() {
if (!this.currentContact) {
return [];
}
return this.groupedMessages[this.currentContact.contactId] || [];
},
},
created() {
this.getCurrentUser();
this.getContactList();
this.getMessageList();
},
methods: { methods: {
handleLogout() { logout() {
this.$store.commit("REMOVE_TOKEN", ''); // 清除 token 和用户信息
this.$store.commit("SET_USERNAME", ''); this.$store.commit("SET_TOKEN", "");
this.$store.commit("SET_USERNAME", "");
this.$router.push("/login"); this.$router.push("/login");
}, },
getCurrentUser() {
// 获取当前登录用户信息
getUserInfo()
.then((response) => {
this.currentUser = response.data;
})
.catch((error) => {
console.error(error);
this.$message.error("获取用户信息失败,请重新登录");
this.$router.push("/login");
});
},
getMessageList() {
// 获取当前用户的联系人消息列表
getContactMessages()
.then((response) => {
const groupedMessages = {};
const messages = response.data.messages || [];
// 把联系人的消息按联系人 id 分组存储
messages.forEach((item) => {
// 这个联系人的 id 是和当前用户聊天消息的发送者 id 或接收者 id
const contactId =
item.senderId === this.currentUser.userId
? item.receiverId
: item.senderId;
// 把联系人 id 为属性进行分组,每个联系人id对应一个消息数组,内容有接收消息也有发送消息
if (!groupedMessages[contactId]) {
groupedMessages[contactId] = [];
}
groupedMessages[contactId].push({
...item,
from: item.senderId === this.currentUser.userId ? "me" : "other",
time: item.time || item.createdAt,
});
});
this.groupedMessages = groupedMessages;
console.log("groupedMessages", groupedMessages);
})
.catch((error) => {
console.error(error);
this.$message.error("获取消息列表失败");
});
},
getContactList() {
getContactInfo()
.then((response) => {
this.contactList = response.data.contacts.map((contact) => ({
...contact,
unread: contact.unread || 0,
}));
console.log("contactList", this.contactList);
this.selectFirstContact();
})
.catch((error) => {
console.error(error);
this.$message.error("获取联系人信息失败");
});
},
changeMenu(menu) {
this.activeMenu = menu;
},
// 选择联系人
selectContact(contact) {
this.currentContact = contact;
// 查看联系人后清除未读数量
contact.unread = 0;
this.$nextTick(() => {
this.scrollToBottom();
});
},
// 选择第一个联系人
selectFirstContact() {
if (!this.currentContact && this.contactList.length > 0) {
this.selectContact(this.contactList[0]);
}
},
// 获取最后一条消息
getLastMessage(contactId) {
const messageList = this.groupedMessages[contactId];
if (!messageList || messageList.length === 0) {
return "暂无消息";
}
return messageList[messageList.length - 1].content;
},
// 发送消息
sendMessage() {
const content = this.messageText.trim();
if (!content || !this.currentContact) {
return;
}
if (!this.groupedMessages[this.currentContact.id]) {
this.$set(this.groupedMessages, this.currentContact.id, []);
}
this.groupedMessages[this.currentContact.id].push({
id: Date.now(),
from: "me",
content: content,
time: this.getCurrentTime(),
});
// 清空输入框
this.messageText = "";
this.$nextTick(() => {
this.scrollToBottom();
});
},
// 获取当前时间
getCurrentTime() {
const date = new Date();
const hours = String(date.getHours()).padStart(2, "0");
const minutes = String(date.getMinutes()).padStart(2, "0");
return `${hours}:${minutes}`;
},
// 滚动到底部
scrollToBottom() {
const messageList = this.$refs.messageList;
if (messageList) {
messageList.scrollTop = messageList.scrollHeight;
}
},
}, },
}; };
</script> </script>
<style scoped> <style scoped>
h3 { * {
margin: 40px 0 0; box-sizing: border-box;
} }
ul {
list-style-type: none; .chat-page {
padding: 0; display: flex;
width: 100%;
height: 100vh;
min-width: 980px;
background: #f5f5f5;
color: #333;
overflow: hidden;
} }
li {
display: inline-block; /* =========================
左侧导航栏(微信风格)
========================= */
.app-sidebar {
width: 78px;
height: 100%;
background: #f2f3f5;
border-right: 1px solid #e5e5e5;
display: flex;
flex-direction: column;
align-items: center;
padding: 16px 0 12px;
}
.app-logo {
width: 42px;
height: 42px;
border-radius: 12px;
background: linear-gradient(135deg, #67c23a, #4caf50);
color: #fff;
display: flex;
align-items: center;
justify-content: center;
font-size: 22px;
box-shadow: 0 6px 14px rgba(103, 194, 58, 0.25);
}
.sidebar-nav {
margin-top: 20px;
display: flex;
flex-direction: column;
gap: 12px;
width: 100%;
align-items: center;
}
.nav-item {
width: 42px;
height: 42px;
border: none;
border-radius: 12px;
background: transparent;
color: #666;
font-size: 20px;
cursor: pointer;
display: flex;
align-items: center;
justify-content: center;
transition: all 0.2s ease;
}
.nav-item:hover {
background: #e9edf3;
}
.nav-item.active {
background: #dfeeff;
color: #409eff;
box-shadow: inset 0 0 0 1px rgba(64, 158, 255, 0.12);
}
.sidebar-footer {
margin-top: auto;
width: 100%;
display: flex;
justify-content: center;
}
/* =========================
左侧联系人
========================= */
.contact-panel {
width: 300px;
height: 100%;
background: #ffffff;
border-right: 1px solid #e5e5e5;
display: flex;
flex-direction: column;
}
.user-header {
height: 72px;
padding: 0 20px;
display: flex;
align-items: center;
border-bottom: 1px solid #eeeeee;
}
.avatar {
width: 42px;
height: 42px;
flex-shrink: 0;
border-radius: 50%;
background: #4c8bf5;
color: #ffffff;
display: flex;
align-items: center;
justify-content: center;
font-size: 16px;
font-weight: 500;
}
.my-avatar {
background: #67c23a;
}
.my-name {
font-size: 15px;
font-weight: 600;
color: #67c23a;
}
.search-box {
padding: 12px;
background: #fafafa;
}
.search-box input {
width: 100%;
height: 34px;
padding: 0 12px;
border: 1px solid #e5e5e5;
border-radius: 4px;
outline: none;
background: #ffffff;
font-size: 13px;
}
.search-box input:focus {
border-color: #409eff;
}
.contact-list {
flex: 1;
overflow-y: auto;
}
.contact-item {
height: 70px;
padding: 0 15px;
display: flex;
align-items: center;
cursor: pointer;
position: relative;
}
.contact-item:hover {
background: #f5f5f5;
}
.contact-item.active {
background: #eaf3ff;
}
.contact-info {
flex: 1;
min-width: 0;
margin-left: 12px;
}
.contact-name {
font-size: 14px;
font-weight: 500;
}
.last-message {
margin-top: 7px;
color: #999;
font-size: 12px;
white-space: nowrap;
overflow: hidden;
text-overflow: ellipsis;
}
.unread {
min-width: 18px;
height: 18px;
padding: 0 5px;
border-radius: 10px;
background: #f56c6c;
color: #fff;
font-size: 11px;
display: flex;
align-items: center;
justify-content: center;
}
/* =========================
右侧聊天
========================= */
.chat-panel {
flex: 1;
height: 100%;
display: flex;
flex-direction: column;
background: #f5f5f5;
}
.chat-header {
height: 72px;
padding: 0 24px;
display: flex;
align-items: center;
background: #ffffff;
border-bottom: 1px solid #e5e5e5;
}
.chat-user-info {
margin-left: 12px;
}
.chat-user-name {
font-size: 16px;
font-weight: 600;
}
.chat-user-status {
margin-top: 5px;
font-size: 12px;
color: #67c23a;
}
.empty-chat {
flex: 1;
display: flex;
flex-direction: column;
align-items: center;
justify-content: center;
color: #999;
}
.empty-icon {
margin-bottom: 15px;
font-size: 50px;
}
.empty-content {
margin-top: 15px;
font-size: 50px;
color: #999;
height: 100%;
display: flex;
align-items: center;
justify-content: center;
}
.message-list {
flex: 1;
padding: 25px 35px;
overflow-y: auto;
display: none;
}
.message-list.active {
display: block;
}
.message-list.empty {
display: block;
height: 100%;
}
.message-row {
display: flex;
margin-bottom: 20px;
}
.message-other {
justify-content: flex-start;
}
.message-me {
justify-content: flex-end;
}
.message-avatar {
width: 38px;
height: 38px;
font-size: 14px;
}
.message-content {
max-width: 60%;
margin: 0 10px; margin: 0 10px;
} }
a {
color: #42b983; .message-time {
margin-bottom: 5px;
color: #aaa;
font-size: 11px;
}
.message-me .message-time {
text-align: right;
}
.message-bubble {
padding: 10px 14px;
border-radius: 6px;
background: #ffffff;
font-size: 14px;
line-height: 1.5;
word-break: break-word;
box-shadow: 0 1px 2px rgba(0, 0, 0, 0.05);
}
.message-me .message-bubble {
background: #95ec69;
}
/* =========================
输入区域
========================= */
.input-panel {
height: 180px;
padding: 10px 20px 15px;
background: #ffffff;
border-top: 1px solid #e5e5e5;
}
.toolbar {
height: 30px;
display: flex;
align-items: center;
gap: 12px;
}
.toolbar button {
border: none;
background: transparent;
cursor: pointer;
font-size: 18px;
}
.input-panel textarea {
width: 100%;
height: 75px;
padding: 8px 0;
resize: none;
border: none;
outline: none;
font-size: 14px;
line-height: 1.5;
}
.send-area {
display: flex;
justify-content: flex-end;
align-items: center;
}
.enter-tip {
margin-right: 15px;
color: #aaa;
font-size: 12px;
}
.send-button {
width: 70px;
height: 32px;
border: none;
border-radius: 4px;
background: #409eff;
color: #ffffff;
cursor: pointer;
}
.send-button:hover {
background: #66b1ff;
}
.send-button:disabled {
background: #c0c4cc;
cursor: not-allowed;
}
/* 滚动条 */
.contact-list::-webkit-scrollbar,
.message-list::-webkit-scrollbar {
width: 5px;
}
.contact-list::-webkit-scrollbar-thumb,
.message-list::-webkit-scrollbar-thumb {
background: #ddd;
border-radius: 3px;
} }
</style> </style>
+24 -8
View File
@@ -9,14 +9,6 @@ export function login(data) {
}) })
} }
// 获取用户信息
export function getUserInfo() {
return request({
url: '/user/info',
method: 'get'
})
}
// 注册账号 // 注册账号
export function register(data) { export function register(data) {
return request({ return request({
@@ -24,4 +16,28 @@ export function register(data) {
method: 'post', method: 'post',
data data
}) })
}
// 获取用户信息
export function getUserInfo() {
return request({
url: '/userInfo',
method: 'get'
})
}
// 获取用户的联系人信息
export function getContactInfo() {
return request({
url: '/contactsInfo',
method: 'get'
})
}
// 获取用户的联系人消息
export function getContactMessages() {
return request({
url: '/messagesInfo',
method: 'get'
})
} }