Home.vue 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328
  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>
  29. <div class="gitee-box" title="gitee仓库,帮忙点点star哟">
  30. <a href="https://gitee.com/bluexsx/box-im" target="_blank">
  31. <el-image style="width: 30px; height: 30px" src="https://gitee.com/favicon.ico" fit="fit">
  32. </el-image>
  33. </a>
  34. </div>
  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. this.handlePrivateMessage(msgInfo);
  93. } else if (cmd == 4) {
  94. // 插入群聊消息
  95. this.handleGroupMessage(msgInfo);
  96. }
  97. })
  98. },
  99. pullUnreadMessage() {
  100. // 拉取未读私聊消息
  101. this.$http({
  102. url: "/message/private/pullUnreadMessage",
  103. method: 'post'
  104. });
  105. // 拉取未读群聊消息
  106. this.$http({
  107. url: "/message/group/pullUnreadMessage",
  108. method: 'post'
  109. });
  110. },
  111. handlePrivateMessage(msg) {
  112. // 好友列表存在好友信息,直接插入私聊消息
  113. let friend = this.$store.state.friendStore.friends.find((f) => f.id == msg.sendId);
  114. if (friend) {
  115. this.insertPrivateMessage(friend, msg);
  116. return;
  117. }
  118. // 好友列表不存在好友信息,则发请求获取好友信息
  119. this.$http({
  120. url: `/friend/find/${msg.sendId}`,
  121. method: 'get'
  122. }).then((friend) => {
  123. this.insertPrivateMessage(friend, msg);
  124. this.$store.commit("addFriend", friend);
  125. })
  126. },
  127. insertPrivateMessage(friend, msg) {
  128. // webrtc 信令
  129. if (msg.type >= this.$enums.MESSAGE_TYPE.RTC_CALL &&
  130. msg.type <= this.$enums.MESSAGE_TYPE.RTC_CANDIDATE) {
  131. // 呼叫
  132. console.log(msg)
  133. if (msg.type == this.$enums.MESSAGE_TYPE.RTC_CALL ||
  134. msg.type == this.$enums.MESSAGE_TYPE.RTC_CANCEL) {
  135. this.$store.commit("showVideoAcceptorBox", friend);
  136. this.$refs.videoAcceptor.handleMessage(msg)
  137. } else {
  138. this.$refs.privateVideo.handleMessage(msg)
  139. }
  140. return;
  141. }
  142. let chatInfo = {
  143. type: 'PRIVATE',
  144. targetId: friend.id,
  145. showName: friend.nickName,
  146. headImage: friend.headImage
  147. };
  148. // 打开会话
  149. this.$store.commit("openChat", chatInfo);
  150. // 插入消息
  151. this.$store.commit("insertMessage", msg);
  152. // 播放提示音
  153. this.playAudioTip();
  154. },
  155. handleGroupMessage(msg) {
  156. // 群聊缓存存在,直接插入群聊消息
  157. let group = this.$store.state.groupStore.groups.find((g) => g.id == msg.groupId);
  158. if (group) {
  159. this.insertGroupMessage(group, msg);
  160. return;
  161. }
  162. // 群聊缓存存在,直接插入群聊消息
  163. this.$http({
  164. url: `/group/find/${msg.groupId}`,
  165. method: 'get'
  166. }).then((group) => {
  167. this.insertGroupMessage(group, msg);
  168. this.$store.commit("addGroup", group);
  169. })
  170. },
  171. insertGroupMessage(group, msg) {
  172. let chatInfo = {
  173. type: 'GROUP',
  174. targetId: group.id,
  175. showName: group.remark,
  176. headImage: group.headImageThumb
  177. };
  178. // 打开会话
  179. this.$store.commit("openChat", chatInfo);
  180. // 插入消息
  181. this.$store.commit("insertMessage", msg);
  182. // 播放提示音
  183. this.playAudioTip();
  184. },
  185. handleExit() {
  186. this.$wsApi.closeWebSocket();
  187. sessionStorage.removeItem("accessToken");
  188. location.href = "/";
  189. },
  190. playAudioTip() {
  191. let audio = new Audio();
  192. let url = require(`@/assets/audio/tip.wav`);
  193. audio.src = url;
  194. audio.play();
  195. },
  196. showSetting() {
  197. this.showSettingDialog = true;
  198. },
  199. closeSetting() {
  200. this.showSettingDialog = false;
  201. }
  202. },
  203. computed: {
  204. uiStore() {
  205. return this.$store.state.uiStore;
  206. },
  207. unreadCount() {
  208. let unreadCount = 0;
  209. let chats = this.$store.state.chatStore.chats;
  210. chats.forEach((chat) => {
  211. unreadCount += chat.unreadCount
  212. });
  213. return unreadCount;
  214. }
  215. },
  216. watch: {
  217. unreadCount: {
  218. handler(newCount, oldCount) {
  219. let tip = newCount > 0 ? `${newCount}条未读` : "";
  220. this.$elm.setTitleTip(tip);
  221. },
  222. immediate: true
  223. }
  224. },
  225. mounted() {
  226. this.$http({
  227. url: "/user/self",
  228. methods: 'get'
  229. }).then((userInfo) => {
  230. this.init(userInfo);
  231. })
  232. },
  233. unmounted() {
  234. this.$wsApi.closeWebSocket();
  235. }
  236. }
  237. </script>
  238. <style scoped lang="scss">
  239. .navi-bar {
  240. background: #333333;
  241. padding: 10px;
  242. padding-top: 50px;
  243. .user-head-image {
  244. position: relative;
  245. width: 50px;
  246. height: 50px;
  247. }
  248. .el-menu {
  249. border: none;
  250. flex: 1;
  251. .el-menu-item {
  252. margin: 25px 0;
  253. .router-link-exact-active span {
  254. color: white !important;
  255. }
  256. span {
  257. font-size: 24px !important;
  258. color: #aaaaaa;
  259. &:hover {
  260. color: white !important;
  261. }
  262. }
  263. .unread-text {
  264. position: absolute;
  265. line-height: 20px;
  266. background-color: #f56c6c;
  267. left: 36px;
  268. top: 7px;
  269. color: white;
  270. border-radius: 30px;
  271. padding: 0 5px;
  272. font-size: 10px;
  273. text-align: center;
  274. white-space: nowrap;
  275. border: 1px solid #f1e5e5;
  276. }
  277. }
  278. }
  279. .gitee-box {
  280. position: absolute;
  281. width: 60px;
  282. bottom: 100px;
  283. display: flex;
  284. justify-content: center;
  285. cursor: pointer;
  286. }
  287. .exit-box {
  288. position: absolute;
  289. width: 60px;
  290. bottom: 40px;
  291. color: #aaaaaa;
  292. font-size: 24px;
  293. text-align: center;
  294. cursor: pointer;
  295. &:hover {
  296. color: white !important;
  297. }
  298. }
  299. }
  300. .content-box {
  301. padding: 0;
  302. background-color: #E9EEF3;
  303. color: #333;
  304. text-align: center;
  305. }
  306. </style>