Home.vue 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329
  1. <template>
  2. <el-container>
  3. <el-aside width="80px" class="navi-bar">
  4. <div class="user-head-image">
  5. <head-image :url="$store.state.userStore.userInfo.headImageThumb" :size="60" @click.native="showSettingDialog=true">
  6. </head-image>
  7. </div>
  8. <el-menu background-color="#333333" text-color="#ddd" style="margin-top: 30px;">
  9. <el-menu-item title="聊天">
  10. <router-link v-bind:to="'/home/chat'">
  11. <span class="el-icon-chat-dot-round"></span>
  12. <div v-show="unreadCount>0" class="unread-text">{{unreadCount}}</div>
  13. </router-link>
  14. </el-menu-item>
  15. <el-menu-item title="好友">
  16. <router-link v-bind:to="'/home/friend'">
  17. <span class="el-icon-user"></span>
  18. </router-link>
  19. </el-menu-item>
  20. <el-menu-item title="群聊">
  21. <router-link v-bind:to="'/home/group'">
  22. <span class="icon iconfont icon-group_fill"></span>
  23. </router-link>
  24. </el-menu-item>
  25. <el-menu-item title="设置" @click="showSetting()">
  26. <span class="el-icon-setting"></span>
  27. </el-menu-item>
  28. <el-menu-item title="小伙子这么帅,点点star吧~">
  29. <a href="https://gitee.com/bluexsx/box-im" target="_blank">
  30. <el-image style="width: 30px; height: 30px" src="https://gitee.com/favicon.ico" fit="fit">
  31. </el-image>
  32. </a>
  33. </el-menu-item>
  34. </el-menu>
  35. <div class="exit-box" @click="handleExit()" title="退出">
  36. <span class="el-icon-circle-close"></span>
  37. </div>
  38. </el-aside>
  39. <el-main class="content-box">
  40. <router-view></router-view>
  41. </el-main>
  42. <setting :visible="showSettingDialog" @close="closeSetting()"></setting>
  43. <user-info v-show="uiStore.userInfo.show" :pos="uiStore.userInfo.pos" :user="uiStore.userInfo.user" @close="$store.commit('closeUserInfoBox')"></user-info>
  44. <full-image :visible="uiStore.fullImage.show" :url="uiStore.fullImage.url" @close="$store.commit('closeFullImageBox')"></full-image>
  45. <chat-private-video ref="privateVideo" :visible="uiStore.chatPrivateVideo.show" :friend="uiStore.chatPrivateVideo.friend"
  46. :master="uiStore.chatPrivateVideo.master" :offer="uiStore.chatPrivateVideo.offer" @close="$store.commit('closeChatPrivateVideoBox')">
  47. </chat-private-video>
  48. <chat-video-acceptor ref="videoAcceptor" v-show="uiStore.videoAcceptor.show" :friend="uiStore.videoAcceptor.friend"
  49. @close="$store.commit('closeVideoAcceptorBox')">
  50. </chat-video-acceptor>
  51. </el-container>
  52. </template>
  53. <script>
  54. import HeadImage from '../components/common/HeadImage.vue';
  55. import Setting from '../components/setting/Setting.vue';
  56. import UserInfo from '../components/common/UserInfo.vue';
  57. import FullImage from '../components/common/FullImage.vue';
  58. import ChatPrivateVideo from '../components/chat/ChatPrivateVideo.vue';
  59. import ChatVideoAcceptor from '../components/chat/ChatVideoAcceptor.vue';
  60. export default {
  61. components: {
  62. HeadImage,
  63. Setting,
  64. UserInfo,
  65. FullImage,
  66. ChatPrivateVideo,
  67. ChatVideoAcceptor
  68. },
  69. data() {
  70. return {
  71. showSettingDialog: false,
  72. }
  73. },
  74. methods: {
  75. init(userInfo) {
  76. this.$store.commit("setUserInfo", userInfo);
  77. this.$store.commit("setUserState", this.$enums.USER_STATE.FREE);
  78. this.$store.commit("initStore");
  79. this.$wsApi.createWebSocket(process.env.VUE_APP_WS_URL, userInfo.id, sessionStorage.getItem("accessToken"));
  80. this.$wsApi.onopen(() => {
  81. this.pullUnreadMessage();
  82. });
  83. this.$wsApi.onmessage((cmd, msgInfo) => {
  84. if (cmd == 2) {
  85. // 异地登录,强制下线
  86. this.$message.error("您已在其他地方登陆,将被强制下线");
  87. setTimeout(() => {
  88. location.href = "/";
  89. }, 1000)
  90. } else if (cmd == 3) {
  91. // 标记这条消息是不是自己发的
  92. msgInfo.selfSend = msgInfo.sendId==this.$store.state.userStore.userInfo.id;
  93. // 插入私聊消息
  94. this.handlePrivateMessage(msgInfo);
  95. } else if (cmd == 4) {
  96. // 标记这条消息是不是自己发的
  97. msgInfo.selfSend = msgInfo.sendId==this.$store.state.userStore.userInfo.id;
  98. // 插入群聊消息
  99. this.handleGroupMessage(msgInfo);
  100. }
  101. })
  102. },
  103. pullUnreadMessage() {
  104. // 拉取未读私聊消息
  105. this.$http({
  106. url: "/message/private/pullUnreadMessage",
  107. method: 'post'
  108. });
  109. // 拉取未读群聊消息
  110. this.$http({
  111. url: "/message/group/pullUnreadMessage",
  112. method: 'post'
  113. });
  114. },
  115. handlePrivateMessage(msg) {
  116. // 好友列表存在好友信息,直接插入私聊消息
  117. let friendId = msg.selfSend?msg.recvId:msg.sendId;
  118. let friend = this.$store.state.friendStore.friends.find((f) => f.id == friendId);
  119. if (friend) {
  120. this.insertPrivateMessage(friend, msg);
  121. return;
  122. }
  123. // 好友列表不存在好友信息,则发请求获取好友信息
  124. this.$http({
  125. url: `/friend/find/${msg.sendId}`,
  126. method: 'get'
  127. }).then((friend) => {
  128. this.insertPrivateMessage(friend, msg);
  129. this.$store.commit("addFriend", friend);
  130. })
  131. },
  132. insertPrivateMessage(friend, msg) {
  133. // webrtc 信令
  134. if (msg.type >= this.$enums.MESSAGE_TYPE.RTC_CALL &&
  135. msg.type <= this.$enums.MESSAGE_TYPE.RTC_CANDIDATE) {
  136. // 呼叫
  137. if (msg.type == this.$enums.MESSAGE_TYPE.RTC_CALL ||
  138. msg.type == this.$enums.MESSAGE_TYPE.RTC_CANCEL) {
  139. this.$store.commit("showVideoAcceptorBox", friend);
  140. this.$refs.videoAcceptor.handleMessage(msg)
  141. } else {
  142. this.$refs.videoAcceptor.close()
  143. this.$refs.privateVideo.handleMessage(msg)
  144. }
  145. return;
  146. }
  147. let chatInfo = {
  148. type: 'PRIVATE',
  149. targetId: friend.id,
  150. showName: friend.nickName,
  151. headImage: friend.headImage
  152. };
  153. // 打开会话
  154. this.$store.commit("openChat", chatInfo);
  155. // 插入消息
  156. this.$store.commit("insertMessage", msg);
  157. // 播放提示音
  158. !msg.selfSend && this.playAudioTip();
  159. },
  160. handleGroupMessage(msg) {
  161. // 群聊缓存存在,直接插入群聊消息
  162. let group = this.$store.state.groupStore.groups.find((g) => g.id == msg.groupId);
  163. if (group) {
  164. this.insertGroupMessage(group, msg);
  165. return;
  166. }
  167. // 群聊缓存存在,直接插入群聊消息
  168. this.$http({
  169. url: `/group/find/${msg.groupId}`,
  170. method: 'get'
  171. }).then((group) => {
  172. this.insertGroupMessage(group, msg);
  173. this.$store.commit("addGroup", group);
  174. })
  175. },
  176. insertGroupMessage(group, msg) {
  177. let chatInfo = {
  178. type: 'GROUP',
  179. targetId: group.id,
  180. showName: group.remark,
  181. headImage: group.headImageThumb
  182. };
  183. // 打开会话
  184. this.$store.commit("openChat", chatInfo);
  185. // 插入消息
  186. this.$store.commit("insertMessage", msg);
  187. // 播放提示音
  188. !msg.selfSend && this.playAudioTip();
  189. },
  190. handleExit() {
  191. this.$wsApi.closeWebSocket();
  192. sessionStorage.removeItem("accessToken");
  193. location.href = "/";
  194. },
  195. playAudioTip() {
  196. let audio = new Audio();
  197. let url = require(`@/assets/audio/tip.wav`);
  198. audio.src = url;
  199. audio.play();
  200. },
  201. showSetting() {
  202. this.showSettingDialog = true;
  203. },
  204. closeSetting() {
  205. this.showSettingDialog = false;
  206. }
  207. },
  208. computed: {
  209. uiStore() {
  210. return this.$store.state.uiStore;
  211. },
  212. unreadCount() {
  213. let unreadCount = 0;
  214. let chats = this.$store.state.chatStore.chats;
  215. chats.forEach((chat) => {
  216. unreadCount += chat.unreadCount
  217. });
  218. return unreadCount;
  219. }
  220. },
  221. watch: {
  222. unreadCount: {
  223. handler(newCount, oldCount) {
  224. let tip = newCount > 0 ? `${newCount}条未读` : "";
  225. this.$elm.setTitleTip(tip);
  226. },
  227. immediate: true
  228. }
  229. },
  230. mounted() {
  231. this.$http({
  232. url: "/user/self",
  233. methods: 'get'
  234. }).then((userInfo) => {
  235. this.init(userInfo);
  236. })
  237. },
  238. unmounted() {
  239. this.$wsApi.closeWebSocket();
  240. }
  241. }
  242. </script>
  243. <style scoped lang="scss">
  244. .navi-bar {
  245. background: #333333;
  246. padding: 10px;
  247. padding-top: 50px;
  248. .user-head-image {
  249. position: relative;
  250. width: 50px;
  251. height: 50px;
  252. }
  253. .el-menu {
  254. border: none;
  255. flex: 1;
  256. .el-menu-item {
  257. margin: 25px 0;
  258. .router-link-exact-active span {
  259. color: white !important;
  260. }
  261. span {
  262. font-size: 24px !important;
  263. color: #aaaaaa;
  264. &:hover {
  265. color: white !important;
  266. }
  267. }
  268. .unread-text {
  269. position: absolute;
  270. line-height: 20px;
  271. background-color: #f56c6c;
  272. left: 36px;
  273. top: 7px;
  274. color: white;
  275. border-radius: 30px;
  276. padding: 0 5px;
  277. font-size: 10px;
  278. text-align: center;
  279. white-space: nowrap;
  280. border: 1px solid #f1e5e5;
  281. }
  282. }
  283. }
  284. .exit-box {
  285. position: absolute;
  286. width: 60px;
  287. bottom: 40px;
  288. color: #aaaaaa;
  289. font-size: 24px;
  290. text-align: center;
  291. cursor: pointer;
  292. &:hover {
  293. color: white !important;
  294. }
  295. }
  296. }
  297. .content-box {
  298. padding: 0;
  299. background-color: #E9EEF3;
  300. color: #333;
  301. text-align: center;
  302. }
  303. </style>