Files
fad_oa/ruoyi-ui/src/views/oa/webSocket/index.vue
2024-10-21 16:54:36 +08:00

362 lines
12 KiB
Vue

<template>
<div class="app-container">
<el-container class="app">
<el-aside width="calc(30% - 20px)" style="background-color: white">
<el-container>
<el-header>
<el-row>
<el-col :span="24">
<el-input suffix-icon="el-icon-search" placeholder="Enter 回车搜索联系人" v-model="contactQueryParams.userName" @keyup.enter.native="getContactList"/>
</el-col>
</el-row>
<el-row style="margin-top: 5px">
<!-- TODO 这里搞一个弹窗去搜索用户表 从而进行添加好友操作 -->
<el-button size="mini">添加联系人</el-button>
</el-row>
<el-row style="margin-top: 5px">
<el-button size="mini">全部</el-button>
<el-button size="mini">个人</el-button>
<el-button size="mini">群聊</el-button>
</el-row>
</el-header>
<div v-loading="contactListLoading" style="margin-top: 30px">
<el-main v-if="contactList.length > 0" v-infinite-scroll="contactLoadMore" :infinite-scroll-distance="750" :infinite-scroll-disabled="contactListTotal < 10" class="msgListMain">
<el-row class="msgUserList" v-for="(item, index) in contactList" :key="item.contactUserId" :style="index > 0 && 'margin-top: 10px'" @click.native="loadMessage(item.id)">
<el-col :span="6">
<el-image :src="(item.user.avatar === '' || item.user.avatar == null) ? require('@/assets/images/profile.jpg') : item.user.avatar" fit="fill" style="width: 70%;border-radius: 50%;"/>
</el-col>
<el-col :span="18">
<el-row>
<el-col :span="15"><span style="font-weight: 500; font-size: 16px">{{item.user.nickName}}</span></el-col>
<el-col :span="5">
<el-divider direction="vertical"/>
</el-col>
</el-row>
<el-row>
<el-col :span="5" style="font-size: 13px; text-overflow: ellipsis; white-space: nowrap">
<span><i class="el-icon-circle-check"></i> {{item.endMsg}}</span>
</el-col>
<el-col :span="5" style="float: right">
<el-dropdown class="hover_down_menu">
<span class="el-dropdown-link">
<i class="el-icon-arrow-down el-icon-more"></i>
</span>
<el-dropdown-menu slot="dropdown">
<el-dropdown-item>置顶</el-dropdown-item>
<el-dropdown-item>删除</el-dropdown-item>
</el-dropdown-menu>
</el-dropdown>
</el-col>
</el-row>
</el-col>
</el-row>
</el-main>
<el-main v-else class="msgListMain_empty">
<el-row>
<el-col :span="24">
<img src="@/assets/images/contact.png" style="width: 80%; height: 80%"/>
</el-col>
</el-row>
<el-row>
<el-col :span="24">
<span style="color: gray">暂无联系人</span>
</el-col>
</el-row>
</el-main>
</div>
</el-container>
</el-aside>
<el-main :class="currentContact.id ? 'main' : 'main_empty'" v-loading="msgListLoading">
<div v-if="currentContact.id">
<el-row>
<el-col :span="8" style="color: #666">
<span style="font-weight: 500; font-size: 16px">{{currentContact.userName}}</span>
<span style="font-size: 16px; margin-left: 30px">{{currentContact.industry}}</span>
<el-divider direction="vertical"/>
<span style="font-size: 16px;">{{currentContact.job}}</span>
</el-col>
</el-row>
<el-row>
<el-col :span="24">
<span>{{currentContact.major}}</span>
<span style="color: red; font-size: 17px; margin-left: 20px">{{currentContact.salary}}</span>
<span style="margin-left: 20px">{{currentContact.city}}</span>
</el-col>
</el-row>
<el-row>
<el-col :span="24" class="msg_content" id="message_content">
<el-row v-for="(item, index) in msgList" :key="item.id" :style="index > 0 && 'margin-top: 30px'">
<div v-if="item.userId === currentContact.contactUserId">
<el-col :span="2" style="text-align: center">
<el-image :src="currentContact.avatar" fit="cover" style="width: 40%;border-radius: 50%"/>
</el-col>
<el-col :span="10" style="font-size: 16px; line-height: 40px;">
<span>{{item.content}}</span>
<span style="font-size: 11px; color: gray; margin-left: 5px">{{item.createTime}}</span>
</el-col>
</div>
<div v-else>
<el-col :span="24" style="font-size: 16px;">
<div class="chat_bubble">
<span>{{item.content}}</span>
</div>
<i class="el-icon-circle-check" style="float: right; margin-right: 5px; color: lightgray; vertical-align: bottom; margin-top: 23px"></i>
<span style="font-size: 11px; color: gray; margin-right: 5px; float: right; margin-top: 25px">{{item.createTime}}</span>
</el-col>
</div>
</el-row>
<el-row id="message_content_end" style="height: 15px"><el-col></el-col></el-row>
</el-col>
</el-row>
<el-row>
<el-col :span="24">
<el-row>
<el-col :span="24">
<el-popover
placement="top-start"
trigger="click">
<div>
<VEmojiPicker :showSearch="false" @select="insertEmoji" />
</div>
<img slot="reference" src="@/assets/images/emoji.png" title="表情" class="input_top_menu_img"/>
</el-popover>
</el-col>
</el-row>
<el-input type="textarea" :rows="3" v-model="inputVal" style="font-size: 17px; color: black" @keyup.enter.native="send" placeholder="Enter 回车发送消息"/>
</el-col>
</el-row>
</div>
<div v-else>
<el-row>
<el-col :span="24">
<img src="@/assets/images/message.png"/>
</el-col>
</el-row>
<el-row>
<el-col :span="24">
<span style="color: gray">与您进行过沟通的联系人都会在左侧列表中显示</span>
</el-col>
</el-row>
</div>
</el-main>
</el-container>
</div>
</template>
<script>
import { VEmojiPicker } from 'v-emoji-picker';
import { parseTime } from '@/utils/ruoyi';
import { listContact, getContact } from "@/api/system/contact";
import { addMessage } from "@/api/system/message";
export default {
name: "chat",
components: {
VEmojiPicker
},
data() {
return {
//联系人列表
contactList: [],
contactListTotal: 0,
contactListLoading: false,
//消息记录
msgList: [],
msgListTotal: 0,
msgListLoading: false,
inputVal: '',
search: '',
contactUserId: null,
userId: null,
contactQueryParams: {
pageSize: 10,
pageNum: 1
},
currentContact: {}
}
},
mounted() {
window.addEventListener("onmessageWS", this.subscribeMessage);
},
created() {
this.userId = this.$store.state.user.id;
this.subscribeMessage();
this.getContactList();
},
methods: {
getContactList() {
this.contactListLoading = true;
this.contactQueryParams.userId = this.userId;
listContact(this.contactQueryParams).then(response => {
if (response.code === 200) {
this.contactList = response.rows;
this.contactListTotal = response.total;
const contactUserId = this.$route.query.userId;
if (contactUserId) {
this.contactUserId = contactUserId;
let contact = response.rows.find(row => row.contactUserId == contactUserId);
this.loadMessage(contact.id);
}
}
this.contactListLoading = false;
})
},
contactLoadMore() {
// this.contactQueryParams.pageSize = 5;
// this.contactQueryParams.pageNum++;
this.getContactList();
},
loadMessage(concatId) {
this.msgListLoading = true;
getContact(concatId).then(response => {
if (response.code === 200) {
this.currentContact = response.data;
this.msgList = response.data.messages;
}
this.msgListLoading = false;
this.fleshScroll();
})
},
insertEmoji(emoji) {
this.inputVal += emoji.data;
},
send() {
const message = {
contactId: this.currentContact.id,
userId: this.userId,
content: this.inputVal,
roomId: this.currentContact.roomId
}
this.msgList.push({
...message,
id: this.msgList.length + 1,
createTime: parseTime(new Date())
})
this.fleshLastMsg();
addMessage(message);
const msg = {
sendUserId: this.userId,
sendUserName: this.$store.state.user.name,
userId: this.currentContact.contactUserId,
type: "chat",
detail: this.inputVal
}
this.$websocket.sendWebsocket(JSON.stringify(msg));
this.inputVal = '';
this.fleshScroll();
},
subscribeMessage(res) {
console.log(res);
if (res) {
const { sendUserId, sendUserName, userId, type, detail } = res.detail.data;
const message = {
id: 1,
contactId: userId,
userId: sendUserId,
content: detail,
roomId: this.currentContact.roomId,
createTime: parseTime(new Date())
}
this.msgList.push(message);
this.fleshLastMsg();
this.fleshScroll();
}
},
fleshLastMsg() {
const index = this.contactList.findIndex(e => e.id === this.currentContact.id);
this.contactList[index].endMsg = this.msgList[this.msgList.length - 1].content;
},
fleshScroll() {
this.$nextTick(() => {
document.getElementById("message_content_end").scrollIntoView();
})
}
}
}
</script>
<style scoped lang="scss">
.app-container {
background: linear-gradient(180deg, rgba(0, 190, 189, .1), rgba(136, 255, 254, .2) 50%, rgba(242, 244, 247, .1));
}
.app {
background-color: white;
border-radius: 12px 12px 0 0;
}
.msgListMain {
height: 500px;
overflow-y:auto;
margin-top: 15px;
}
.msgUserList {
border-radius: 10px;
}
.msgListMain_empty {
display: flex;
height: 500px;
flex-direction: column;
justify-content: center;
align-items: center;
text-align: center;
}
.hover_down_menu {
display: none;
}
.msgUserList:hover {
background-color: #f2f2f2;
cursor: pointer;
}
.msgUserList:hover .hover_down_menu{
display: block;
}
.el-dropdown-link {
cursor: pointer;
}
.el-icon-arrow-down {
font-size: 15px;
font-weight: 500;
}
.main {
background-color: white;
height: 600px;
margin-left: 5px;
}
.main_empty {
display: flex;
background-color: white;
height: 600px;
flex-direction: column;
justify-content: center;
align-items: center;
}
.main_empty .el-row {
text-align: center;
}
.main_empty img {
width: 25%;
}
.msg_content {
margin-top: 30px;
height: 390px;
overflow: auto;
//background-color: gray;
}
.chat_bubble {
float: right;
margin-right: 35px;
color: #333;
background-color: rgba(0, 190, 189, .2);
height: 40px;
line-height: 40px;
padding: 0 12px 0 12px;
border-radius: 5px;
}
.input_top_menu_img{
width: 22px;
height: 22px;
cursor: pointer;
}
</style>