diff --git a/src/pages/index/ChatPage.vue b/src/pages/index/ChatPage.vue index 694af87..b2c4b9e 100644 --- a/src/pages/index/ChatPage.vue +++ b/src/pages/index/ChatPage.vue @@ -1,20 +1,168 @@ @@ -22,44 +170,594 @@ import { getUserInfo } from "@/utils/user"; export default { name: "ChatPage", - props: {}, + data() { return { - username: "", - password: "", + currentUser: {}, + searchText: "", + messageText: "", + + // 联系人 + contacts: [ + { + id: 1, + name: "张三", + online: true, + unread: 2, + }, + { + id: 2, + name: "李四", + online: true, + unread: 0, + }, + { + id: 3, + name: "王五", + online: false, + unread: 5, + }, + { + id: 4, + name: "赵六", + online: true, + unread: 0, + }, + ], + + // 当前聊天联系人 + currentContact: null, + + // 聊天消息 + messages: { + 1: [ + { + id: 1, + from: "other", + content: "你好!", + time: "15:20", + }, + { + id: 2, + from: "me", + content: "你好,最近怎么样?", + time: "15:21", + }, + { + id: 3, + from: "other", + content: "挺好的,你呢?", + time: "15:22", + }, + ], + + 2: [ + { + id: 4, + from: "other", + content: "晚上一起吃饭吗?", + time: "14:30", + }, + ], + + 3: [ + { + id: 5, + from: "other", + content: "明天记得开会。", + time: "10:15", + }, + ], + + 4: [], + }, }; }, - created() { - getUserInfo().then(response => { - this.username = response.data.username; - }).catch(error => { - console.error(error); - this.$message.error("登录过期,请重新登录"); - }) + + computed: { + // 搜索联系人 + filteredContacts() { + if (!this.searchText.trim()) { + return this.contacts; + } + + const keyword = this.searchText.trim().toLowerCase(); + + return this.contacts.filter((contact) => + contact.name.toLowerCase().includes(keyword), + ); + }, + + // 当前联系人对应的消息 + currentMessages() { + if (!this.currentContact) { + return []; + } + + return this.messages[this.currentContact.id] || []; + }, }, + + mounted() { + // 默认选择第一个联系人 + if (this.contacts.length > 0) { + this.selectContact(this.contacts[0]); + } + }, + + created() { + this.getCurrentUser() + }, + methods: { - handleLogout() { - this.$store.commit("REMOVE_TOKEN", ''); - this.$store.commit("SET_USERNAME", ''); - this.$router.push("/login"); + getCurrentUser() { + // 获取当前登录用户信息 + getUserInfo() + .then((response) => { + this.currentUser = response.data; + }) + .catch((error) => { + console.error(error); + this.$message.error("获取用户信息失败,请重新登录"); + this.$router.push("/login"); + }); + }, + // 选择联系人 + selectContact(contact) { + this.currentContact = contact; + + // 查看联系人后清除未读数量 + contact.unread = 0; + + this.$nextTick(() => { + this.scrollToBottom(); + }); + }, + + // 获取最后一条消息 + getLastMessage(contactId) { + const messageList = this.messages[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.messages[this.currentContact.id]) { + this.$set(this.messages, this.currentContact.id, []); + } + + this.messages[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; + } }, }, };