Home.vue 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  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"
  6. @click.native="showSettingDialog=true"> </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. </router-link>
  13. </el-menu-item>
  14. <el-menu-item title="好友">
  15. <router-link v-bind:to="'/home/friend'">
  16. <span class="el-icon-user"></span>
  17. </router-link>
  18. </el-menu-item>
  19. <el-menu-item title="群聊">
  20. <router-link v-bind:to="'/home/group'">
  21. <span class="el-icon-s-check"></span>
  22. </router-link>
  23. </el-menu-item>
  24. <el-menu-item title="设置" @click="showSetting()">
  25. <span class="el-icon-setting"></span>
  26. </el-menu-item>
  27. </el-menu>
  28. <div class="exit-box" @click="handleExit()" title="退出">
  29. <span class="el-icon-circle-close"></span>
  30. </div>
  31. </el-aside>
  32. <el-main class="content-box">
  33. <router-view></router-view>
  34. </el-main>
  35. <setting :visible="showSettingDialog" @close="closeSetting()"></setting>
  36. <user-info v-show="uiStore.userInfo.show"
  37. :pos="uiStore.userInfo.pos"
  38. :user="uiStore.userInfo.user"
  39. @close="$store.commit('closeUserInfoBox')"></user-info>
  40. <full-image :visible="uiStore.fullImage.show"
  41. :url="uiStore.fullImage.url"
  42. @close="$store.commit('closeFullImageBox')"
  43. ></full-image>
  44. </el-container>
  45. </template>
  46. <script>
  47. import HeadImage from '../components/common/HeadImage.vue';
  48. import Setting from '../components/setting/Setting.vue';
  49. import UserInfo from '../components/common/UserInfo.vue';
  50. import FullImage from '../components/common/FullImage.vue';
  51. export default {
  52. components: {
  53. HeadImage,
  54. Setting,
  55. UserInfo,
  56. FullImage
  57. },
  58. data() {
  59. return {
  60. showSettingDialog: false
  61. }
  62. },
  63. methods: {
  64. init(userInfo) {
  65. this.$store.commit("setUserInfo", userInfo);
  66. this.$store.commit("initStore");
  67. this.$wsApi.createWebSocket(process.env.VUE_APP_WS_URL, this.$store);
  68. this.$wsApi.onopen(() => {
  69. this.pullUnreadMessage();
  70. });
  71. this.$wsApi.onmessage((e) => {
  72. if (e.cmd == 2) {
  73. // 异地登录,强制下线
  74. this.$message.error("您已在其他地方登陆,将被强制下线");
  75. setTimeout(() => {
  76. location.href = "/";
  77. }, 1000)
  78. } else if (e.cmd == 3) {
  79. // 插入私聊消息
  80. this.handlePrivateMessage(e.data);
  81. } else if (e.cmd == 4) {
  82. // 插入群聊消息
  83. this.handleGroupMessage(e.data);
  84. }
  85. })
  86. },
  87. pullUnreadMessage() {
  88. // 拉取未读私聊消息
  89. this.$http({
  90. url: "/message/private/pullUnreadMessage",
  91. method: 'post'
  92. });
  93. // 拉取未读群聊消息
  94. this.$http({
  95. url: "/message/group/pullUnreadMessage",
  96. method: 'post'
  97. });
  98. },
  99. handlePrivateMessage(msg) {
  100. // 好友列表存在好友信息,直接插入私聊消息
  101. let friend = this.$store.state.friendStore.friends.find((f) => f.id == msg.sendId);
  102. if (friend) {
  103. this.insertPrivateMessage(friend, msg);
  104. return;
  105. }
  106. // 好友列表不存在好友信息,则发请求获取好友信息
  107. this.$http({
  108. url: `/friend/find/${msg.sendId}`,
  109. method: 'get'
  110. }).then((friend) => {
  111. this.insertPrivateMessage(friend, msg);
  112. this.$store.commit("addFriend", friend);
  113. })
  114. },
  115. insertPrivateMessage(friend, msg) {
  116. let chatInfo = {
  117. type: 'PRIVATE',
  118. targetId: friend.id,
  119. showName: friend.nickName,
  120. headImage: friend.headImage
  121. };
  122. // 打开会话
  123. this.$store.commit("openChat", chatInfo);
  124. // 插入消息
  125. this.$store.commit("insertMessage", msg);
  126. },
  127. handleGroupMessage(msg) {
  128. // 群聊缓存存在,直接插入群聊消息
  129. let group = this.$store.state.groupStore.groups.find((g) => g.id == msg.groupId);
  130. if (group) {
  131. this.insertGroupMessage(group, msg);
  132. return;
  133. }
  134. // 群聊缓存存在,直接插入群聊消息
  135. this.$http({
  136. url: `/group/find/${msg.groupId}`,
  137. method: 'get'
  138. }).then((group) => {
  139. this.insertGroupMessage(group, msg);
  140. this.$store.commit("addGroup", group);
  141. })
  142. },
  143. insertGroupMessage(group, msg) {
  144. let chatInfo = {
  145. type: 'GROUP',
  146. targetId: group.id,
  147. showName: group.remark,
  148. headImage: group.headImageThumb
  149. };
  150. // 打开会话
  151. this.$store.commit("openChat", chatInfo);
  152. // 插入消息
  153. this.$store.commit("insertMessage", msg);
  154. },
  155. handleExit() {
  156. this.$http({
  157. url: "/logout",
  158. method: 'get'
  159. }).then(() => {
  160. this.$wsApi.closeWebSocket();
  161. location.href = "/";
  162. })
  163. },
  164. showSetting() {
  165. this.showSettingDialog = true;
  166. },
  167. closeSetting() {
  168. this.showSettingDialog = false;
  169. }
  170. },
  171. computed:{
  172. uiStore(){
  173. return this.$store.state.uiStore;
  174. }
  175. },
  176. mounted() {
  177. this.$http({
  178. url: "/user/self",
  179. methods: 'get'
  180. }).then((userInfo) => {
  181. this.init(userInfo);
  182. })
  183. },
  184. unmounted() {
  185. this.$wsApi.closeWebSocket();
  186. }
  187. }
  188. </script>
  189. <style scoped lang="scss">
  190. .navi-bar {
  191. background: #333333;
  192. padding: 10px;
  193. padding-top: 50px;
  194. .user-head-image {
  195. position: relative;
  196. width: 50px;
  197. height: 50px;
  198. }
  199. .el-menu {
  200. border: none;
  201. flex: 1;
  202. .el-menu-item {
  203. margin-top: 20px;
  204. .router-link-exact-active span {
  205. color: white !important;
  206. }
  207. span {
  208. font-size: 24px !important;
  209. color: #aaaaaa;
  210. &:hover {
  211. color: white !important;
  212. }
  213. }
  214. }
  215. }
  216. .exit-box {
  217. position: absolute;
  218. width: 60px;
  219. bottom: 40px;
  220. color: #aaaaaa;
  221. font-size: 24px;
  222. text-align: center;
  223. cursor: pointer;
  224. &:hover {
  225. color: white !important;
  226. }
  227. }
  228. }
  229. .content-box {
  230. padding: 0;
  231. background-color: #E9EEF3;
  232. color: #333;
  233. text-align: center;
  234. }
  235. </style>