Home.vue 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398
  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" :size="60"
  7. @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"
  40. @close="$store.commit('closeUserInfoBox')"></user-info>
  41. <full-image :visible="uiStore.fullImage.show" :url="uiStore.fullImage.url"
  42. @close="$store.commit('closeFullImageBox')"></full-image>
  43. <chat-private-video ref="privateVideo" :visible="uiStore.chatPrivateVideo.show"
  44. :friend="uiStore.chatPrivateVideo.friend" :master="uiStore.chatPrivateVideo.master"
  45. :offer="uiStore.chatPrivateVideo.offer" @close="$store.commit('closeChatPrivateVideoBox')">
  46. </chat-private-video>
  47. <chat-video-acceptor ref="videoAcceptor" v-show="uiStore.videoAcceptor.show"
  48. :friend="uiStore.videoAcceptor.friend" @close="$store.commit('closeVideoAcceptorBox')">
  49. </chat-video-acceptor>
  50. </el-container>
  51. </template>
  52. <script>
  53. import HeadImage from '../components/common/HeadImage.vue';
  54. import Setting from '../components/setting/Setting.vue';
  55. import UserInfo from '../components/common/UserInfo.vue';
  56. import FullImage from '../components/common/FullImage.vue';
  57. import ChatPrivateVideo from '../components/chat/ChatPrivateVideo.vue';
  58. import ChatVideoAcceptor from '../components/chat/ChatVideoAcceptor.vue';
  59. export default {
  60. components: {
  61. HeadImage,
  62. Setting,
  63. UserInfo,
  64. FullImage,
  65. ChatPrivateVideo,
  66. ChatVideoAcceptor
  67. },
  68. data() {
  69. return {
  70. showSettingDialog: false,
  71. lastPlayAudioTime: new Date()-1000
  72. }
  73. },
  74. methods: {
  75. init() {
  76. this.$store.dispatch("load").then(() => {
  77. // 加载离线消息
  78. this.loadPrivateMessage(this.$store.state.chatStore.privateMsgMaxId);
  79. this.loadGroupMessage(this.$store.state.chatStore.groupMsgMaxId);
  80. // ws初始化
  81. this.$wsApi.init(process.env.VUE_APP_WS_URL, sessionStorage.getItem("accessToken"));
  82. this.$wsApi.connect();
  83. this.$wsApi.onOpen();
  84. this.$wsApi.onMessage((cmd, msgInfo) => {
  85. if (cmd == 2) {
  86. // 异地登录,强制下线
  87. this.$message.error("您已在其他地方登陆,将被强制下线");
  88. setTimeout(() => {
  89. location.href = "/";
  90. }, 1000)
  91. } else if (cmd == 3) {
  92. // 插入私聊消息
  93. this.handlePrivateMessage(msgInfo);
  94. } else if (cmd == 4) {
  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. }).catch((e) => {
  110. console.log("初始化失败",e);
  111. })
  112. },
  113. loadPrivateMessage(minId) {
  114. this.$store.commit("loadingPrivateMsg",true)
  115. this.$http({
  116. url: "/message/private/loadMessage?minId=" + minId,
  117. method: 'get'
  118. }).then((msgInfos) => {
  119. msgInfos.forEach((msgInfo) => {
  120. this.handlePrivateMessage(msgInfo);
  121. })
  122. if (msgInfos.length == 100) {
  123. // 继续拉取
  124. this.loadPrivateMessage(msgInfos[99].id);
  125. }else{
  126. this.$store.commit("loadingPrivateMsg",false)
  127. }
  128. })
  129. },
  130. loadGroupMessage(minId) {
  131. this.$store.commit("loadingGroupMsg",true)
  132. this.$http({
  133. url: "/message/group/loadMessage?minId=" + minId,
  134. method: 'get'
  135. }).then((msgInfos) => {
  136. msgInfos.forEach((msgInfo) => {
  137. this.handleGroupMessage(msgInfo);
  138. })
  139. if (msgInfos.length == 100) {
  140. // 继续拉取
  141. this.loadGroupMessage(msgInfos[99].id);
  142. }else{
  143. this.$store.commit("loadingGroupMsg",false)
  144. }
  145. })
  146. },
  147. handlePrivateMessage(msg) {
  148. // 标记这条消息是不是自己发的
  149. msg.selfSend = msg.sendId == this.$store.state.userStore.userInfo.id;
  150. // 好友id
  151. let friendId = msg.selfSend ? msg.recvId : msg.sendId;
  152. // 消息已读处理
  153. if (msg.type == this.$enums.MESSAGE_TYPE.READED) {
  154. if (msg.selfSend) {
  155. // 我已读对方的消息,清空已读数量
  156. let chatInfo = {
  157. type: 'PRIVATE',
  158. targetId: friendId
  159. }
  160. this.$store.commit("resetUnreadCount", chatInfo)
  161. } else {
  162. // 对方已读我的消息,修改消息状态为已读
  163. this.$store.commit("readedMessage", friendId)
  164. }
  165. return;
  166. }
  167. this.loadFriendInfo(friendId).then((friend) => {
  168. this.insertPrivateMessage(friend, msg);
  169. })
  170. },
  171. insertPrivateMessage(friend, msg) {
  172. // webrtc 信令
  173. if (msg.type >= this.$enums.MESSAGE_TYPE.RTC_CALL &&
  174. msg.type <= this.$enums.MESSAGE_TYPE.RTC_CANDIDATE) {
  175. // 呼叫
  176. if (msg.type == this.$enums.MESSAGE_TYPE.RTC_CALL ||
  177. msg.type == this.$enums.MESSAGE_TYPE.RTC_CANCEL) {
  178. this.$store.commit("showVideoAcceptorBox", friend);
  179. this.$refs.videoAcceptor.handleMessage(msg)
  180. } else {
  181. this.$refs.videoAcceptor.close()
  182. this.$refs.privateVideo.handleMessage(msg)
  183. }
  184. return;
  185. }
  186. let chatInfo = {
  187. type: 'PRIVATE',
  188. targetId: friend.id,
  189. showName: friend.nickName,
  190. headImage: friend.headImage
  191. };
  192. // 打开会话
  193. this.$store.commit("openChat", chatInfo);
  194. // 插入消息
  195. this.$store.commit("insertMessage", msg);
  196. // 播放提示音
  197. if(!msg.selfSend && msg.status != this.$enums.MESSAGE_STATUS.READED){
  198. this.playAudioTip();
  199. }
  200. },
  201. handleGroupMessage(msg) {
  202. // 标记这条消息是不是自己发的
  203. msg.selfSend = msg.sendId == this.$store.state.userStore.userInfo.id;
  204. let groupId = msg.groupId;
  205. // 消息已读处理
  206. if (msg.type == this.$enums.MESSAGE_TYPE.READED) {
  207. // 我已读对方的消息,清空已读数量
  208. let chatInfo = {
  209. type: 'GROUP',
  210. targetId: groupId
  211. }
  212. this.$store.commit("resetUnreadCount", chatInfo)
  213. return;
  214. }
  215. this.loadGroupInfo(groupId).then((group) => {
  216. // 插入群聊消息
  217. this.insertGroupMessage(group, msg);
  218. })
  219. },
  220. insertGroupMessage(group, msg) {
  221. let chatInfo = {
  222. type: 'GROUP',
  223. targetId: group.id,
  224. showName: group.remark,
  225. headImage: group.headImageThumb
  226. };
  227. // 打开会话
  228. this.$store.commit("openChat", chatInfo);
  229. // 插入消息
  230. this.$store.commit("insertMessage", msg);
  231. // 播放提示音
  232. if(!msg.selfSend && msg.status != this.$enums.MESSAGE_STATUS.READED){
  233. this.playAudioTip();
  234. }
  235. },
  236. handleExit() {
  237. this.$wsApi.close();
  238. sessionStorage.removeItem("accessToken");
  239. location.href = "/";
  240. },
  241. playAudioTip() {
  242. if(new Date() - this.lastPlayAudioTime > 1000){
  243. this.lastPlayAudioTime = new Date();
  244. let audio = new Audio();
  245. let url = require(`@/assets/audio/tip.wav`);
  246. audio.src = url;
  247. audio.play();
  248. }
  249. },
  250. showSetting() {
  251. this.showSettingDialog = true;
  252. },
  253. closeSetting() {
  254. this.showSettingDialog = false;
  255. },
  256. loadFriendInfo(id) {
  257. return new Promise((resolve, reject) => {
  258. let friend = this.$store.state.friendStore.friends.find((f) => f.id == id);
  259. if (friend) {
  260. resolve(friend);
  261. } else {
  262. this.$http({
  263. url: `/friend/find/${id}`,
  264. method: 'get'
  265. }).then((friend) => {
  266. this.$store.commit("addFriend", friend);
  267. resolve(friend)
  268. })
  269. }
  270. });
  271. },
  272. loadGroupInfo(id) {
  273. return new Promise((resolve, reject) => {
  274. let group = this.$store.state.groupStore.groups.find((g) => g.id == id);
  275. if (group) {
  276. resolve(group);
  277. } else {
  278. this.$http({
  279. url: `/group/find/${id}`,
  280. method: 'get'
  281. }).then((group) => {
  282. resolve(group)
  283. this.$store.commit("addGroup", group);
  284. })
  285. }
  286. });
  287. }
  288. },
  289. computed: {
  290. uiStore() {
  291. return this.$store.state.uiStore;
  292. },
  293. unreadCount() {
  294. let unreadCount = 0;
  295. let chats = this.$store.state.chatStore.chats;
  296. chats.forEach((chat) => {
  297. unreadCount += chat.unreadCount
  298. });
  299. return unreadCount;
  300. }
  301. },
  302. watch: {
  303. unreadCount: {
  304. handler(newCount, oldCount) {
  305. let tip = newCount > 0 ? `${newCount}条未读` : "";
  306. this.$elm.setTitleTip(tip);
  307. },
  308. immediate: true
  309. }
  310. },
  311. mounted() {
  312. this.init();
  313. },
  314. unmounted() {
  315. this.$wsApi.close();
  316. }
  317. }
  318. </script>
  319. <style scoped lang="scss">
  320. .navi-bar {
  321. background: #333333;
  322. padding: 10px;
  323. padding-top: 50px;
  324. .el-menu {
  325. border: none;
  326. flex: 1;
  327. .el-menu-item {
  328. margin: 25px 0;
  329. .router-link-exact-active span {
  330. color: white !important;
  331. }
  332. span {
  333. font-size: 24px !important;
  334. color: #aaaaaa;
  335. &:hover {
  336. color: white !important;
  337. }
  338. }
  339. .unread-text {
  340. position: absolute;
  341. line-height: 20px;
  342. background-color: #f56c6c;
  343. left: 36px;
  344. top: 7px;
  345. color: white;
  346. border-radius: 30px;
  347. padding: 0 5px;
  348. font-size: 10px;
  349. text-align: center;
  350. white-space: nowrap;
  351. border: 1px solid #f1e5e5;
  352. }
  353. }
  354. }
  355. .exit-box {
  356. position: absolute;
  357. width: 60px;
  358. bottom: 40px;
  359. color: #aaaaaa;
  360. font-size: 24px;
  361. text-align: center;
  362. cursor: pointer;
  363. &:hover {
  364. color: white !important;
  365. }
  366. }
  367. }
  368. .content-box {
  369. padding: 0;
  370. background-color: #E9EEF3;
  371. color: #333;
  372. text-align: center;
  373. }
  374. </style>