You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

252 lines
6.2 KiB

3 years ago
<template>
<el-container >
3 years ago
<el-aside width="80px" class="navi-bar">
<div class="user-head-image">
<head-image :url="$store.state.userStore.userInfo.headImageThumb" :size="60"
@click.native="showSettingDialog=true"> </head-image>
3 years ago
</div>
<el-menu background-color="#333333" text-color="#ddd" style="margin-top: 30px;">
3 years ago
<el-menu-item title="聊天">
<router-link v-bind:to="'/home/chat'">
<span class="el-icon-chat-dot-round"></span>
</router-link>
</el-menu-item>
<el-menu-item title="好友">
<router-link v-bind:to="'/home/friend'">
3 years ago
<span class="el-icon-user"></span>
</router-link>
</el-menu-item>
<el-menu-item title="群聊">
<router-link v-bind:to="'/home/group'">
<span class="el-icon-s-check"></span>
</router-link>
</el-menu-item>
<el-menu-item title="设置" @click="onClickSetting()">
<span class="el-icon-setting"></span>
3 years ago
</el-menu-item>
</el-menu>
<div class="exit-box" @click="handleExit()" title="退出">
3 years ago
<span class="el-icon-circle-close"></span>
</div>
</el-aside>
<el-main class="content-box">
<router-view></router-view>
</el-main>
<setting :visible="showSettingDialog" @close="onCloseSetting()"></setting>
<user-info v-show="uiStore.userInfo.show"
:pos="uiStore.userInfo.pos"
:user="uiStore.userInfo.user"
@close="$store.commit('closeUserInfoBox')"></user-info>
<full-image :visible="uiStore.fullImage.show"
:url="uiStore.fullImage.url"
@close="$store.commit('closeFullImageBox')"
></full-image>
3 years ago
</el-container>
</template>
<script>
import HeadImage from '../components/common/HeadImage.vue';
import Setting from '../components/setting/Setting.vue';
import UserInfo from '../components/common/UserInfo.vue';
import FullImage from '../components/common/FullImage.vue';
3 years ago
export default {
components: {
HeadImage,
Setting,
UserInfo,
FullImage
},
data() {
return {
showSettingDialog: false
}
},
3 years ago
methods: {
init(userInfo) {
3 years ago
this.$store.commit("setUserInfo", userInfo);
this.$store.commit("initStore");
this.$wsApi.createWebSocket("ws://localhost:8878/im", this.$store);
this.$wsApi.onopen(() => {
3 years ago
this.pullUnreadMessage();
});
this.$wsApi.onmessage((e) => {
if (e.cmd == 2) {
// 异地登录,强制下线
this.$message.error("您已在其他地方登陆,将被强制下线");
setTimeout(() => {
location.href = "/";
}, 1000)
} else if (e.cmd == 3) {
3 years ago
// 插入私聊消息
this.handlePrivateMessage(e.data);
} else if (e.cmd == 4) {
// 插入群聊消息
this.handleGroupMessage(e.data);
3 years ago
}
})
},
pullUnreadMessage() {
// 拉取未读私聊消息
3 years ago
this.$http({
url: "/api/message/private/pullUnreadMessage",
3 years ago
method: 'post'
});
// 拉取未读群聊消息
this.$http({
url: "/api/message/group/pullUnreadMessage",
method: 'post'
});
3 years ago
},
handlePrivateMessage(msg) {
// 好友列表存在好友信息,直接插入私聊消息
let friend = this.$store.state.friendStore.friends.find((f) => f.id == msg.sendId);
if (friend) {
this.insertPrivateMessage(friend, msg);
return;
}
// 好友列表不存在好友信息,则发请求获取好友信息
this.$http({
url: `/api/friend/find/${msg.sendId}`,
method: 'get'
}).then((friend) => {
this.insertPrivateMessage(friend, msg);
this.$store.commit("addFriend", friend);
})
},
insertPrivateMessage(friend, msg) {
3 years ago
let chatInfo = {
type: 'PRIVATE',
targetId: friend.id,
showName: friend.nickName,
headImage: friend.headImage
};
// 打开会话
this.$store.commit("openChat", chatInfo);
// 插入消息
this.$store.commit("insertMessage", msg);
},
handleGroupMessage(msg) {
// 群聊缓存存在,直接插入群聊消息
let group = this.$store.state.groupStore.groups.find((g) => g.id == msg.groupId);
if (group) {
this.insertGroupMessage(group, msg);
return;
}
// 群聊缓存存在,直接插入群聊消息
this.$http({
url: `/api/group/find/${msg.groupId}`,
method: 'get'
}).then((group) => {
this.insertGroupMessage(group, msg);
this.$store.commit("addGroup", group);
})
},
insertGroupMessage(group, msg) {
let chatInfo = {
type: 'GROUP',
targetId: group.id,
showName: group.remark,
headImage: group.headImageThumb
3 years ago
};
// 打开会话
this.$store.commit("openChat", chatInfo);
3 years ago
// 插入消息
this.$store.commit("insertMessage", msg);
3 years ago
},
handleExit() {
3 years ago
this.$http({
url: "/api/logout",
method: 'get'
}).then(() => {
this.$wsApi.closeWebSocket();
location.href = "/";
3 years ago
})
},
onClickSetting() {
this.showSettingDialog = true;
},
onCloseSetting() {
this.showSettingDialog = false;
3 years ago
}
},
computed:{
uiStore(){
return this.$store.state.uiStore;
}
},
3 years ago
mounted() {
this.$http({
url: "/api/user/self",
methods: 'get'
}).then((userInfo) => {
this.init(userInfo);
})
},
unmounted() {
3 years ago
this.$wsApi.closeWebSocket();
}
}
</script>
<style scoped lang="scss">
.navi-bar {
background: #333333;
padding: 10px;
padding-top: 50px;
.user-head-image {
3 years ago
position: relative;
width: 50px;
height: 50px;
}
3 years ago
.el-menu {
border: none;
flex: 1;
3 years ago
.el-menu-item {
margin-top: 20px;
.router-link-exact-active span {
3 years ago
color: white !important;
}
3 years ago
span {
font-size: 24px !important;
color: #aaaaaa;
&:hover {
3 years ago
color: white !important;
}
}
}
}
3 years ago
.exit-box {
position: absolute;
width: 60px;
bottom: 40px;
3 years ago
color: #aaaaaa;
font-size: 24px;
text-align: center;
cursor: pointer;
&:hover {
3 years ago
color: white !important;
}
}
}
.content-box {
padding: 0;
background-color: #E9EEF3;
color: #333;
text-align: center;
}
</style>