| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619 |
- <template>
- <div class="home-page" @click="closeUserInfo">
- <div class="app-container" :class="{ fullscreen: isFullscreen }">
- <div class="navi-bar">
- <div class="navi-bar-box">
- <div class="top">
- <div class="user-head-image">
- <head-image :name="userStore.userInfo.nickName" :size="38"
- :url="userStore.userInfo.headImageThumb" @click.native="showSettingDialog = true">
- </head-image>
- </div>
- <div class="menu">
- <router-link class="link" v-bind:to="'/home/chat'">
- <div class="menu-item">
- <span class="icon iconfont icon-chat"></span>
- <div v-show="unreadCount > 0" class="unread-text">{{ unreadCount }}</div>
- </div>
- </router-link>
- <router-link class="link" v-bind:to="'/home/friend'">
- <div class="menu-item">
- <span class="icon iconfont icon-friend"></span>
- </div>
- </router-link>
- <router-link class="link" v-bind:to="'/home/group'">
- <div class="menu-item">
- <span class="icon iconfont icon-group" style="font-size: 28px"></span>
- </div>
- </router-link>
- </div>
- </div>
- <div class="botoom">
- <div class="bottom-item" @click="isFullscreen = !isFullscreen">
- <i class="el-icon-full-screen"></i>
- </div>
- <div class="bottom-item" @click="showSetting">
- <span class="icon iconfont icon-setting" style="font-size: 20px"></span>
- </div>
- <div class="bottom-item" @click="onExit()" title="退出">
- <span class="icon iconfont icon-exit"></span>
- </div>
- </div>
- </div>
- </div>
- <div class="content-box">
- <router-view></router-view>
- </div>
- <setting :visible="showSettingDialog" @close="closeSetting()"></setting>
- <user-info ref="userInfo"></user-info>
- <full-image ref="fullImage"></full-image>
- <rtc-private-video ref="rtcPrivateVideo"></rtc-private-video>
- <rtc-group-video ref="rtcGroupVideo"></rtc-group-video>
- </div>
- </div>
- </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';
- import RtcPrivateVideo from '../components/rtc/RtcPrivateVideo.vue';
- import RtcPrivateAcceptor from '../components/rtc/RtcPrivateAcceptor.vue';
- import RtcGroupVideo from '../components/rtc/RtcGroupVideo.vue';
- export default {
- components: {
- HeadImage,
- Setting,
- UserInfo,
- FullImage,
- RtcPrivateVideo,
- RtcPrivateAcceptor,
- RtcGroupVideo
- },
- data() {
- return {
- showSettingDialog: false,
- lastPlayAudioTime: new Date().getTime() - 1000,
- isFullscreen: true,
- reconnecting: false,
- privateMessagesBuffer: [],
- groupMessagesBuffer: []
- }
- },
- methods: {
- init() {
- this.$eventBus.$on('openPrivateVideo', (rctInfo) => {
- // 进入单人视频通话
- this.$refs.rtcPrivateVideo.open(rctInfo);
- });
- this.$eventBus.$on('openGroupVideo', (rctInfo) => {
- // 进入多人视频通话
- this.$refs.rtcGroupVideo.open(rctInfo);
- });
- this.$eventBus.$on('openUserInfo', (user, pos) => {
- // 打开用户卡片
- this.$refs.userInfo.open(user, pos);
- });
- this.$eventBus.$on('openFullImage', url => {
- // 图片全屏
- this.$refs.fullImage.open(url);
- });
- this.configStore.setAppInit(false)
- this.loadStore().then(() => {
- // ws初始化
- this.$wsApi.connect(process.env.VUE_APP_WS_URL, sessionStorage.getItem("accessToken"));
- this.$wsApi.onConnect(() => {
- if (this.reconnecting) {
- this.onReconnectWs();
- } else {
- // 加载离线消息
- this.pullOfflineMessage();
- this.configStore.setAppInit(true);
- }
- });
- this.$wsApi.onMessage((cmd, msgInfo) => {
- if (cmd == 2) {
- // 关闭ws
- this.$wsApi.close(3000)
- // 异地登录,强制下线
- this.$alert("您已在其他地方登录,将被强制下线", "强制下线通知", {
- confirmButtonText: '确定',
- callback: action => {
- location.href = "/";
- }
- });
- } else if (cmd == 3) {
- if (!this.configStore.appInit || this.chatStore.loading) {
- // 如果正在拉取离线消息,先放进缓存区,等待消息拉取完成再处理,防止消息乱序
- this.privateMessagesBuffer.push(msgInfo);
- } else {
- // 插入私聊消息
- this.handlePrivateMessage(msgInfo);
- }
- } else if (cmd == 4) {
- if (!this.configStore.appInit || this.chatStore.loading) {
- // 如果正在拉取离线消息,先放进缓存区,等待消息拉取完成再处理,防止消息乱序
- this.groupMessagesBuffer.push(msgInfo);
- } else {
- // 插入群聊消息
- this.handleGroupMessage(msgInfo);
- }
- } else if (cmd == 5) {
- // 处理系统消息
- this.handleSystemMessage(msgInfo);
- }
- });
- this.$wsApi.onClose((e) => {
- if (e.code != 3000) {
- // 断线重连
- this.reconnectWs();
- this.configStore.setAppInit(false)
- }
- });
- }).catch((e) => {
- console.log("初始化失败", e);
- })
- },
- reconnectWs() {
- // 记录标志
- this.reconnecting = true;
- // 重新加载一次个人信息,目的是为了保证网络已经正常且token有效
- this.userStore.loadUser().then(() => {
- // 断线重连
- this.$message.error("连接断开,正在尝试重新连接...");
- this.$wsApi.reconnect(process.env.VUE_APP_WS_URL, sessionStorage.getItem(
- "accessToken"));
- }).catch(() => {
- // 10s后重试
- setTimeout(() => this.reconnectWs(), 10000)
- })
- },
- onReconnectWs() {
- // 重连成功
- this.reconnecting = false;
- // 重新加载群和好友
- const promises = [];
- promises.push(this.friendStore.loadFriend());
- promises.push(this.groupStore.loadGroup());
- Promise.all(promises).then(() => {
- // 加载离线消息
- this.pullOfflineMessage();
- this.configStore.setAppInit(true)
- this.$message.success("重新连接成功");
- }).catch(() => {
- this.$message.error("初始化失败");
- this.onExit();
- })
- },
- loadStore() {
- return this.userStore.loadUser().then(() => {
- const promises = [];
- promises.push(this.friendStore.loadFriend());
- promises.push(this.groupStore.loadGroup());
- promises.push(this.chatStore.loadChat());
- promises.push(this.configStore.loadConfig());
- return Promise.all(promises);
- })
- },
- unloadStore() {
- this.friendStore.clear();
- this.groupStore.clear();
- this.chatStore.clear();
- this.userStore.clear();
- },
- pullOfflineMessage() {
- this.chatStore.setLoading(true);
- const promises = [];
- promises.push(this.pullPrivateOfflineMessage(this.chatStore.privateMsgMaxId));
- promises.push(this.pullGroupOfflineMessage(this.chatStore.groupMsgMaxId));
- Promise.all(promises).then(messages => {
- // 处理离线消息
- messages[0].forEach(m => this.handlePrivateMessage(m));
- messages[1].forEach(m => this.handleGroupMessage(m));
- // 处理缓冲区收到的实时消息
- this.privateMessagesBuffer.forEach(m => this.handlePrivateMessage(m));
- this.groupMessagesBuffer.forEach(m => this.handleGroupMessage(m));
- // 清空缓冲区
- this.privateMessagesBuffer = [];
- this.groupMessagesBuffer = [];
- // 关闭加载离线标记
- this.chatStore.setLoading(false);
- // 刷新会话
- this.chatStore.refreshChats();
- }).catch((e) => {
- console.log(e)
- this.$message.error("拉取离线消息失败");
- this.onExit();
- })
- },
- pullPrivateOfflineMessage(minId) {
- return this.$http({
- url: "/message/private/loadOfflineMessage?minId=" + minId,
- method: 'GET'
- })
- },
- pullGroupOfflineMessage(minId) {
- return this.$http({
- url: "/message/group/loadOfflineMessage?minId=" + minId,
- method: 'GET'
- })
- },
- handlePrivateMessage(msg) {
- // 标记这条消息是不是自己发的
- msg.selfSend = msg.sendId == this.userStore.userInfo.id;
- // 好友id
- let friendId = msg.selfSend ? msg.recvId : msg.sendId;
- // 会话信息
- let chatInfo = {
- type: 'PRIVATE',
- targetId: friendId
- }
- // 消息已读处理,清空已读数量
- if (msg.type == this.$enums.MESSAGE_TYPE.READED) {
- this.chatStore.resetUnreadCount(chatInfo)
- return;
- }
- // 消息回执处理,改消息状态为已读
- if (msg.type == this.$enums.MESSAGE_TYPE.RECEIPT) {
- this.chatStore.readedMessage({
- friendId: msg.sendId
- })
- return;
- }
- // 消息撤回
- if (msg.type == this.$enums.MESSAGE_TYPE.RECALL) {
- this.chatStore.recallMessage(msg, chatInfo)
- return;
- }
- // 新增好友
- if (msg.type == this.$enums.MESSAGE_TYPE.FRIEND_NEW) {
- this.friendStore.addFriend(JSON.parse(msg.content));
- return;
- }
- // 删除好友
- if (msg.type == this.$enums.MESSAGE_TYPE.FRIEND_DEL) {
- this.friendStore.removeFriend(friendId);
- return;
- }
- // 对好友设置免打扰
- if (msg.type == this.$enums.MESSAGE_TYPE.FRIEND_DND) {
- this.friendStore.setDnd(friendId, JSON.parse(msg.content));
- this.chatStore.setDnd(chatInfo, JSON.parse(msg.content));
- return;
- }
- // 单人webrtc 信令
- if (this.$msgType.isRtcPrivate(msg.type)) {
- this.$refs.rtcPrivateVideo.onRTCMessage(msg)
- return;
- }
- // 插入消息
- if (this.$msgType.isNormal(msg.type) || this.$msgType.isTip(msg.type) || this.$msgType.isAction(msg.type)) {
- let friend = this.loadFriendInfo(friendId);
- this.insertPrivateMessage(friend, msg);
- }
- },
- insertPrivateMessage(friend, msg) {
- let chatInfo = {
- type: 'PRIVATE',
- targetId: friend.id,
- showName: friend.nickName,
- headImage: friend.headImage,
- isDnd: friend.isDnd
- };
- // 打开会话
- this.chatStore.openChat(chatInfo);
- // 插入消息
- this.chatStore.insertMessage(msg, chatInfo);
- // 播放提示音
- if (!friend.isDnd && !this.chatStore.loading && !msg.selfSend && this.$msgType.isNormal(msg.type) &&
- msg.status != this.$enums.MESSAGE_STATUS.READED) {
- this.playAudioTip();
- }
- },
- handleGroupMessage(msg) {
- // 标记这条消息是不是自己发的
- msg.selfSend = msg.sendId == this.userStore.userInfo.id;
- let chatInfo = {
- type: 'GROUP',
- targetId: msg.groupId
- }
- // 消息已读处理
- if (msg.type == this.$enums.MESSAGE_TYPE.READED) {
- // 我已读对方的消息,清空已读数量
- this.chatStore.resetUnreadCount(chatInfo)
- return;
- }
- // 消息回执处理
- if (msg.type == this.$enums.MESSAGE_TYPE.RECEIPT) {
- // 更新消息已读人数
- let msgInfo = {
- id: msg.id,
- groupId: msg.groupId,
- readedCount: msg.readedCount,
- receiptOk: msg.receiptOk
- };
- this.chatStore.updateMessage(msgInfo, chatInfo)
- return;
- }
- // 消息撤回
- if (msg.type == this.$enums.MESSAGE_TYPE.RECALL) {
- this.chatStore.recallMessage(msg, chatInfo)
- return;
- }
- // 新增群
- if (msg.type == this.$enums.MESSAGE_TYPE.GROUP_NEW) {
- this.groupStore.addGroup(JSON.parse(msg.content));
- return;
- }
- // 删除群
- if (msg.type == this.$enums.MESSAGE_TYPE.GROUP_DEL) {
- this.groupStore.removeGroup(msg.groupId);
- return;
- }
- // 对群设置免打扰
- if (msg.type == this.$enums.MESSAGE_TYPE.GROUP_DND) {
- this.groupStore.setDnd(msg.groupId, JSON.parse(msg.content));
- this.chatStore.setDnd(chatInfo, JSON.parse(msg.content));
- return;
- }
- // 群视频信令
- if (this.$msgType.isRtcGroup(msg.type)) {
- this.$nextTick(() => {
- this.$refs.rtcGroupVideo.onRTCMessage(msg);
- })
- return;
- }
- // 插入群聊消息
- if (this.$msgType.isNormal(msg.type) || this.$msgType.isTip(msg.type) || this.$msgType.isAction(msg.type)) {
- let group = this.loadGroupInfo(msg.groupId);
- this.insertGroupMessage(group, msg);
- }
- },
- insertGroupMessage(group, msg) {
- let chatInfo = {
- type: 'GROUP',
- targetId: group.id,
- showName: group.showGroupName,
- headImage: group.headImageThumb,
- isDnd: group.isDnd
- };
- // 打开会话
- this.chatStore.openChat(chatInfo);
- // 插入消息
- this.chatStore.insertMessage(msg, chatInfo);
- // 播放提示音
- if (!group.isDnd && !this.chatStore.loading &&
- !msg.selfSend && this.$msgType.isNormal(msg.type)
- && msg.status != this.$enums.MESSAGE_STATUS.READED) {
- this.playAudioTip();
- }
- },
- handleSystemMessage(msg) {
- // 用户被封禁
- if (msg.type == this.$enums.MESSAGE_TYPE.USER_BANNED) {
- this.$wsApi.close(3000);
- this.$alert("您的账号已被管理员封禁,原因:" + msg.content, "账号被封禁", {
- confirmButtonText: '确定',
- callback: () => {
- this.onExit();
- }
- });
- return;
- }
- },
- closeUserInfo() {
- this.$refs.userInfo.close();
- },
- onExit() {
- this.unloadStore();
- this.configStore.setAppInit(false);
- this.$wsApi.close(3000);
- sessionStorage.removeItem("accessToken");
- location.href = "/";
- },
- playAudioTip() {
- // 防止过于密集播放
- if (new Date().getTime() - this.lastPlayAudioTime > 1000) {
- this.lastPlayAudioTime = new Date().getTime();
- let audio = new Audio();
- let url = require(`@/assets/audio/tip.mp3`);
- audio.src = url;
- audio.play();
- }
- },
- showSetting() {
- this.showSettingDialog = true;
- },
- closeSetting() {
- this.showSettingDialog = false;
- },
- loadFriendInfo(id) {
- let friend = this.friendStore.findFriend(id);
- if (!friend) {
- friend = {
- id: id,
- showNickName: "未知用户",
- headImage: ""
- }
- }
- return friend;
- },
- loadGroupInfo(id) {
- let group = this.groupStore.findGroup(id);
- if (!group) {
- group = {
- id: id,
- showGroupName: "未知群聊",
- headImageThumb: ""
- }
- }
- return group;
- }
- },
- computed: {
- unreadCount() {
- let unreadCount = 0;
- let chats = this.chatStore.chats;
- chats.forEach(chat => {
- if (!chat.delete && !chat.isDnd) {
- unreadCount += chat.unreadCount
- }
- });
- return unreadCount;
- }
- },
- watch: {
- unreadCount: {
- handler(newCount, oldCount) {
- let tip = newCount > 0 ? `${newCount}条未读` : "";
- this.$elm.setTitleTip(tip);
- },
- immediate: true
- }
- },
- mounted() {
- this.init();
- },
- unmounted() {
- this.$wsApi.close();
- }
- }
- </script>
- <style scoped lang="scss">
- .home-page {
- height: 100vh;
- width: 100vw;
- display: flex;
- justify-content: center;
- align-items: center;
- border-radius: 4px;
- overflow: hidden;
- background: var(--im-color-primary-light-9);
- .app-container {
- width: 62vw;
- height: 80vh;
- display: flex;
- min-height: 600px;
- min-width: 970px;
- position: absolute;
- border-radius: 4px;
- overflow: hidden;
- box-shadow: var(--im-box-shadow-dark);
- transition: 0.2s;
- &.fullscreen {
- transition: 0.2s;
- width: 100vw;
- height: 100vh;
- }
- }
- .navi-bar {
- --icon-font-size: 22px;
- --width: 60px;
- width: var(--width);
- background: var(--im-color-primary-light-1);
- padding-top: 20px;
- .navi-bar-box {
- height: 100%;
- display: flex;
- flex-direction: column;
- justify-content: space-between;
- .botoom {
- margin-bottom: 30px;
- }
- }
- .user-head-image {
- display: flex;
- justify-content: center;
- }
- .menu {
- height: 200px;
- //margin-top: 10px;
- display: flex;
- flex-direction: column;
- justify-content: center;
- align-content: center;
- .link {
- text-decoration: none;
- }
- .router-link-active .menu-item {
- color: #fff;
- background: var(--im-color-primary-light-2);
- }
- .link:not(.router-link-active) .menu-item:hover {
- color: var(--im-color-primary-light-7);
- }
- .menu-item {
- position: relative;
- color: var(--im-color-primary-light-4);
- width: var(--width);
- height: 46px;
- display: flex;
- justify-content: center;
- align-items: center;
- margin-bottom: 12px;
- .icon {
- font-size: var(--icon-font-size)
- }
- .unread-text {
- position: absolute;
- background-color: var(--im-color-danger);
- left: 28px;
- top: 8px;
- color: white;
- border-radius: 30px;
- padding: 0 5px;
- font-size: var(--im-font-size-smaller);
- text-align: center;
- white-space: nowrap;
- border: 1px solid #f1e5e5;
- }
- }
- }
- .bottom-item {
- display: flex;
- justify-content: center;
- align-items: center;
- height: 50px;
- width: 100%;
- cursor: pointer;
- color: var(--im-color-primary-light-4);
- font-size: var(--icon-font-size);
- .icon {
- font-size: var(--icon-font-size)
- }
- &:hover {
- font-weight: 600;
- color: var(--im-color-primary-light-7);
- }
- }
- }
- .content-box {
- flex: 1;
- padding: 0;
- background-color: #fff;
- text-align: center;
- }
- }
- </style>
|