2023-11-26版本

This commit is contained in:
Cool 2023-11-26 23:29:48 +08:00
parent cd0091292e
commit 2afcad01e6
3 changed files with 287 additions and 267 deletions

View File

@ -215,6 +215,7 @@ export default {
.then(function (response) { .then(function (response) {
if (response.data.responseStr !== "No") { if (response.data.responseStr !== "No") {
that.$store.commit("setToken", response.data.responseStr); that.$store.commit("setToken", response.data.responseStr);
localStorage.setItem("nickname", that.loginForm.account);
that.$router.push({ path: "/home" }); that.$router.push({ path: "/home" });
} else { } else {
that.$message.error('账号或密码错误'); that.$message.error('账号或密码错误');

View File

@ -1,137 +1,154 @@
<template> <template>
<div class="home"> <div class="home">
<div class="header"> <div class="header">
<h1 class="title">聊天室</h1> <h1 class="title">聊天室</h1>
<hr class="divide"> <hr class="divide">
</div>
<div class="body" id="chat">
<div v-for="(message, index) in messageList" :key="index">
<div class="word" v-if="!(message.nickname == 'COOL')">
<div class="information">
<p>{{ message.nickname }} {{ message.sendTime }}</p>
</div>
<div class="info">
<el-avatar :src="message.pic" class="head"></el-avatar>
<div class="message">
<div class="detail">
{{ message.message }}
</div>
</div>
</div>
</div> </div>
<div class="body" id="chat">
<div v-for="(message, index) in messageList" :key="index">
<div class="word-my" v-else> <div class="word" v-if="!(message.nickname == myName)">
<div class="outer"> <div class="information">
<div class="information"> <p>{{ message.nickname }} {{ message.sendTime }}</p>
<p> {{ message.sendTime }} {{ message.nickname }} </p> </div>
</div> <div class="info">
<el-avatar :src="message.pic" class="head"></el-avatar>
<div class="message">
<div class="detail">
{{ message.message }}
<div class="info"> </div>
<el-avatar :src="message.pic" class="head"></el-avatar> </div>
</div>
<div class="message">
<div class="detail">
{{ message.message }}
</div> </div>
</div>
<div class="word-my" v-else>
<div class="outer">
<div class="information">
<p> {{ message.sendTime }} {{ message.nickname }} </p>
</div>
<div class="info">
<el-avatar :src="message.pic" class="head"></el-avatar>
<div class="message">
<div class="detail">
{{ message.message }}
</div>
</div>
</div>
</div>
</div>
</div> </div>
</div>
</div> </div>
</div> <div class="footer">
<div class="footer1">
<el-input type="textarea" :rows="8" resize="none" placeholder="" v-model="textarea" class="textarea">
</el-input>
<el-button class="button" @click="send(textarea)">发送</el-button>
</div>
</div>
</div> </div>
<div class="footer">
<div class="footer1">
<el-input type="textarea" :rows="8" resize="none" placeholder="" v-model="textarea" class="textarea">
</el-input>
<el-button class="button" @click="send(textarea)">发送</el-button>
</div>
</div>
</div>
</template> </template>
<script> <script>
let socket; let socket;
export default { export default {
data() { data() {
return { return {
messageList: [], messageList: [],
textarea: "", textarea: "",
myName: '', myName: '',
myPic: '', myPic: '',
dateNow: '',
};
},
mounted() {
var container = document.getElementById('chat');
container.scrollTop = container.scrollHeight;
this.init()
},
methods: {
send() {
if (!this.textarea) {
this.$message({ type: 'warning', message: "请输入内容" })
} else {
let SendMessage = {
nickname: 'COOL',
pic: "1",
sendTime: '2023/11/20 18:23:18',
message: this.textarea,
}
socket.send(JSON.stringify(SendMessage));
this.textarea = "";
console.log(this.messageList, "这是send")
}
},
init() {
const that = this
if (typeof (WebSocket) === undefined) {
console.log("您的浏览器不支持WebSocket");
} else {
console.log("您的浏览器支持WebSocket");
let socketUrl = "ws://localhost:8080/chat/get";
if (socket != null) {
socket.close();
socket = null;
}
socket = new WebSocket(socketUrl);
socket.onopen = function () {
console.log("websocket已打开");
}; };
socket.onmessage = function (msg) {
var data = JSON.parse(msg.data);
that.messageList = data
console.log(that.messageList[1].message, "nickname")
console.log(that.messageList, 2)
}
socket.onerror = function () {
console.log("websocket发生了错误");
}
socket.onclose = function () {
console.log("websocket已关闭");
};
}
}, },
mounted() {
SendMessage(textarea) { var container = document.getElementById('chat');
// container.scrollTop = container.scrollHeight;
console.log(textarea); this.init();
this.myName = localStorage.getItem("nickname");
}, },
methods: {
getDate(){
var date = new Date();
var year = date.getFullYear();
var month = (date.getMonth() + 1).toString().padStart(2, '0');
var day = date.getDate().toString().padStart(2, '0');
var hour =date.getHours().toString().padStart(2, '0');
var minute = date.getMinutes().toString().padStart(2, '0');
var second = date.getSeconds().toString().padStart(2, '0');
this.dateNow = year + '/' + month + '/' + day + ' ' + hour + ':' + minute + ':' + second;
},
}, send() {
if (!this.textarea) {
this.$message({ type: 'warning', message: "请输入内容" })
} else {
this.getDate()
let SendMessage = {
nickname: this.myName,
pic: "1",
sendTime: this.dateNow,
message: this.textarea,
}
this.messageList.push(SendMessage)
this.$nextTick(function () {
var container = document.getElementById('chat');
container.scrollTop = container.scrollHeight;
});
socket.send(JSON.stringify(SendMessage));
this.textarea = "";
console.log(this.messageList, "这是send")
}
},
init() {
const that = this
if (typeof (WebSocket) === undefined) {
console.log("您的浏览器不支持WebSocket");
} else {
console.log("您的浏览器支持WebSocket");
let socketUrl = "ws://localhost:8080/chat/get";
if (socket != null) {
socket.close();
socket = null;
}
socket = new WebSocket(socketUrl);
socket.onopen = function () {
console.log("websocket已打开");
};
socket.onmessage = function (msg) {
var data = JSON.parse(msg.data);
that.messageList = data
console.log(that.messageList[1].message, "nickname")
console.log(that.messageList, 2)
that.$nextTick(function () {
var container = document.getElementById('chat');
container.scrollTop = container.scrollHeight;
});
}
socket.onerror = function () {
console.log("websocket发生了错误");
}
socket.onclose = function () {
console.log("websocket已关闭");
};
}
},
},
}; };
@ -141,178 +158,176 @@ export default {
.home { .home {
.header { .header {
.title { .title {
margin-left: 20px; margin-left: 20px;
font-size: 20px; font-size: 20px;
color: #000000; color: #000000;
}
.divide {
width: 100%;
margin-top: 10px;
border: 1;
height: 2px;
background-color: rgb(231, 231, 231);
border: none;
}
}
.body {
// overflow: hidden;
overflow: auto;
/* 可选:设置最大高度以限制滚动区域 */
max-height: 350px;
.word {
.information {
margin-left: 20px;
margin-top: 20px;
font-size: 12px;
color: rgb(32, 179, 86);
}
.info {
margin-left: 20px;
margin-top: 10px;
font-size: 16px;
display: flex;
.head {
margin-top: 10px;
margin-right: 10px;
} }
.message { .divide {
padding-left: 7px; width: 100%;
padding-right: 7px;
margin-left: 10px;
margin-top: 10px;
position: relative;
font-size: 16px;
color: rgb(0, 0, 0);
background-color: rgb(255, 255, 255);
max-width: 70%;
min-height: 40px;
//
display: flex;
align-items: center;
justify-content: center;
.detail::before {
position: absolute;
display: inline-block;
content: "";
width: 0px;
height: 0px;
border: 8px solid transparent;
top: 10px;
/*移到中间*/
border-right-color: #ffffff;
left: -16px;
}
}
}
}
.word-my {
//
display: flex;
// justify-content: flex-end;
direction: rtl;
.outer {
.information {
margin-left: 20px;
margin-top: 20px;
font-size: 12px;
color: rgb(32, 179, 86);
}
.info {
margin-left: 20px;
margin-top: 10px;
font-size: 16px;
display: flex;
position: relative;
right: 10px;
.head {
margin-top: 10px; margin-top: 10px;
position: relative; border: 1;
right: 10px; height: 2px;
} background-color: rgb(231, 231, 231);
border: none;
.message { }
}
padding-right: 7px; .body {
padding-left: 7px; // overflow: hidden;
margin-right: 28px; overflow: auto;
margin-top: 10px; /* 可选:设置最大高度以限制滚动区域 */
position: relative; max-height: 350px;
font-size: 16px;
color: rgb(0, 0, 0);
background-color: #95ec69;
max-width: 70%; .word {
min-height: 40px; .information {
// margin-left: 20px;
display: flex; margin-top: 20px;
align-items: center; font-size: 12px;
justify-content: center; color: rgb(32, 179, 86);
}
.detail::before {
position: absolute; .info {
display: inline-block; margin-left: 20px;
content: ""; margin-top: 10px;
width: 0px; font-size: 16px;
height: 0px; display: flex;
border: 8px solid transparent;
top: 10px; .head {
/*移到中间*/ margin-top: 10px;
border-left-color: #95ec69; margin-right: 10px;
right: -16px; }
.message {
padding-left: 7px;
padding-right: 7px;
margin-left: 10px;
margin-top: 10px;
position: relative;
font-size: 16px;
color: rgb(0, 0, 0);
background-color: rgb(255, 255, 255);
max-width: 70%;
min-height: 40px;
//
display: flex;
align-items: center;
justify-content: center;
.detail::before {
position: absolute;
display: inline-block;
content: "";
width: 0px;
height: 0px;
border: 8px solid transparent;
top: 10px;
/*移到中间*/
border-right-color: #ffffff;
left: -16px;
}
}
} }
}
} }
}
.word-my {
//
display: flex;
// justify-content: flex-end;
direction: rtl;
.outer {
.information {
margin-left: 20px;
margin-top: 20px;
font-size: 12px;
color: rgb(32, 179, 86);
}
.info {
margin-left: 20px;
margin-top: 10px;
font-size: 16px;
display: flex;
position: relative;
right: 10px;
.head {
margin-top: 10px;
position: relative;
right: 10px;
}
.message {
padding-right: 7px;
padding-left: 7px;
margin-right: 28px;
margin-top: 10px;
position: relative;
font-size: 16px;
color: rgb(0, 0, 0);
background-color: #95ec69;
max-width: 70%;
min-height: 40px;
//
display: flex;
align-items: center;
justify-content: center;
.detail::before {
position: absolute;
display: inline-block;
content: "";
width: 0px;
height: 0px;
border: 8px solid transparent;
top: 10px;
/*移到中间*/
border-left-color: #95ec69;
right: -16px;
}
}
}
}
}
} }
} .footer {
.footer { .footer1 {
position: fixed;
bottom: 60px;
width: 100%;
.footer1 { padding: 10px;
position: fixed;
bottom: 60px;
width: 100%;
padding: 10px;
.textarea {
color: rgb(32, 179, 86);
.textarea { width: 80%;
color: rgb(32, 179, 86);
width: 80%; }
} .button {
position: fixed;
right: 40px;
bottom: 20px;
background-color: rgb(233, 233, 233);
}
}
.button {
position: fixed;
right: 40px;
bottom: 20px;
background-color: rgb(233, 233, 233);
}
} }
}
} }
</style> </style>

View File

@ -14,7 +14,7 @@
<i class="el-icon-caret-bottom" style="font-size: 20px;color:aliceblue;"></i> <i class="el-icon-caret-bottom" style="font-size: 20px;color:aliceblue;"></i>
</div> </div>
<el-dropdown-menu slot="dropdown" class="dropdownMenu"> <el-dropdown-menu slot="dropdown" class="dropdownMenu">
<p style="color: aliceblue; font-size: 14px;text-align: center;">已登录用户Cool</p> <p style="color: aliceblue; font-size: 14px;text-align: center;">已登录用户{{ myName }}</p>
<hr style="border:none;height:1px;background-color: rgb(82, 87, 103);"> <hr style="border:none;height:1px;background-color: rgb(82, 87, 103);">
<el-dropdown-item icon="el-icon-user-solid" command="a" class="item">个人信息</el-dropdown-item> <el-dropdown-item icon="el-icon-user-solid" command="a" class="item">个人信息</el-dropdown-item>
<el-dropdown-item icon="el-icon-chat-dot-round" command="b" class="item">消息通知</el-dropdown-item> <el-dropdown-item icon="el-icon-chat-dot-round" command="b" class="item">消息通知</el-dropdown-item>
@ -50,9 +50,13 @@
export default { export default {
data() { data() {
return { return {
myName: "",
headPic: require("@/assets/img/cool.jpg"), headPic: require("@/assets/img/cool.jpg"),
}; };
}, },
mounted() {
this.myName = localStorage.getItem("nickname");
},
methods: { methods: {
download() { download() {
// //