Home.vue 9.0 KB

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