Home.vue 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619
  1. <template>
  2. <div class="home-page" @click="closeUserInfo">
  3. <div class="app-container" :class="{ fullscreen: isFullscreen }">
  4. <div class="navi-bar">
  5. <div class="navi-bar-box">
  6. <div class="top">
  7. <div class="user-head-image">
  8. <head-image :name="userStore.userInfo.nickName" :size="38"
  9. :url="userStore.userInfo.headImageThumb" @click.native="showSettingDialog = true">
  10. </head-image>
  11. </div>
  12. <div class="menu">
  13. <router-link class="link" v-bind:to="'/home/chat'">
  14. <div class="menu-item">
  15. <span class="icon iconfont icon-chat"></span>
  16. <div v-show="unreadCount > 0" class="unread-text">{{ unreadCount }}</div>
  17. </div>
  18. </router-link>
  19. <router-link class="link" v-bind:to="'/home/friend'">
  20. <div class="menu-item">
  21. <span class="icon iconfont icon-friend"></span>
  22. </div>
  23. </router-link>
  24. <router-link class="link" v-bind:to="'/home/group'">
  25. <div class="menu-item">
  26. <span class="icon iconfont icon-group" style="font-size: 28px"></span>
  27. </div>
  28. </router-link>
  29. </div>
  30. </div>
  31. <div class="botoom">
  32. <div class="bottom-item" @click="isFullscreen = !isFullscreen">
  33. <i class="el-icon-full-screen"></i>
  34. </div>
  35. <div class="bottom-item" @click="showSetting">
  36. <span class="icon iconfont icon-setting" style="font-size: 20px"></span>
  37. </div>
  38. <div class="bottom-item" @click="onExit()" title="退出">
  39. <span class="icon iconfont icon-exit"></span>
  40. </div>
  41. </div>
  42. </div>
  43. </div>
  44. <div class="content-box">
  45. <router-view></router-view>
  46. </div>
  47. <setting :visible="showSettingDialog" @close="closeSetting()"></setting>
  48. <user-info ref="userInfo"></user-info>
  49. <full-image ref="fullImage"></full-image>
  50. <rtc-private-video ref="rtcPrivateVideo"></rtc-private-video>
  51. <rtc-group-video ref="rtcGroupVideo"></rtc-group-video>
  52. </div>
  53. </div>
  54. </template>
  55. <script>
  56. import HeadImage from '../components/common/HeadImage.vue';
  57. import Setting from '../components/setting/Setting.vue';
  58. import UserInfo from '../components/common/UserInfo.vue';
  59. import FullImage from '../components/common/FullImage.vue';
  60. import RtcPrivateVideo from '../components/rtc/RtcPrivateVideo.vue';
  61. import RtcPrivateAcceptor from '../components/rtc/RtcPrivateAcceptor.vue';
  62. import RtcGroupVideo from '../components/rtc/RtcGroupVideo.vue';
  63. export default {
  64. components: {
  65. HeadImage,
  66. Setting,
  67. UserInfo,
  68. FullImage,
  69. RtcPrivateVideo,
  70. RtcPrivateAcceptor,
  71. RtcGroupVideo
  72. },
  73. data() {
  74. return {
  75. showSettingDialog: false,
  76. lastPlayAudioTime: new Date().getTime() - 1000,
  77. isFullscreen: true,
  78. reconnecting: false,
  79. privateMessagesBuffer: [],
  80. groupMessagesBuffer: []
  81. }
  82. },
  83. methods: {
  84. init() {
  85. this.$eventBus.$on('openPrivateVideo', (rctInfo) => {
  86. // 进入单人视频通话
  87. this.$refs.rtcPrivateVideo.open(rctInfo);
  88. });
  89. this.$eventBus.$on('openGroupVideo', (rctInfo) => {
  90. // 进入多人视频通话
  91. this.$refs.rtcGroupVideo.open(rctInfo);
  92. });
  93. this.$eventBus.$on('openUserInfo', (user, pos) => {
  94. // 打开用户卡片
  95. this.$refs.userInfo.open(user, pos);
  96. });
  97. this.$eventBus.$on('openFullImage', url => {
  98. // 图片全屏
  99. this.$refs.fullImage.open(url);
  100. });
  101. this.configStore.setAppInit(false)
  102. this.loadStore().then(() => {
  103. // ws初始化
  104. this.$wsApi.connect(process.env.VUE_APP_WS_URL, sessionStorage.getItem("accessToken"));
  105. this.$wsApi.onConnect(() => {
  106. if (this.reconnecting) {
  107. this.onReconnectWs();
  108. } else {
  109. // 加载离线消息
  110. this.pullOfflineMessage();
  111. this.configStore.setAppInit(true);
  112. }
  113. });
  114. this.$wsApi.onMessage((cmd, msgInfo) => {
  115. if (cmd == 2) {
  116. // 关闭ws
  117. this.$wsApi.close(3000)
  118. // 异地登录,强制下线
  119. this.$alert("您已在其他地方登录,将被强制下线", "强制下线通知", {
  120. confirmButtonText: '确定',
  121. callback: action => {
  122. location.href = "/";
  123. }
  124. });
  125. } else if (cmd == 3) {
  126. if (!this.configStore.appInit || this.chatStore.loading) {
  127. // 如果正在拉取离线消息,先放进缓存区,等待消息拉取完成再处理,防止消息乱序
  128. this.privateMessagesBuffer.push(msgInfo);
  129. } else {
  130. // 插入私聊消息
  131. this.handlePrivateMessage(msgInfo);
  132. }
  133. } else if (cmd == 4) {
  134. if (!this.configStore.appInit || this.chatStore.loading) {
  135. // 如果正在拉取离线消息,先放进缓存区,等待消息拉取完成再处理,防止消息乱序
  136. this.groupMessagesBuffer.push(msgInfo);
  137. } else {
  138. // 插入群聊消息
  139. this.handleGroupMessage(msgInfo);
  140. }
  141. } else if (cmd == 5) {
  142. // 处理系统消息
  143. this.handleSystemMessage(msgInfo);
  144. }
  145. });
  146. this.$wsApi.onClose((e) => {
  147. if (e.code != 3000) {
  148. // 断线重连
  149. this.reconnectWs();
  150. this.configStore.setAppInit(false)
  151. }
  152. });
  153. }).catch((e) => {
  154. console.log("初始化失败", e);
  155. })
  156. },
  157. reconnectWs() {
  158. // 记录标志
  159. this.reconnecting = true;
  160. // 重新加载一次个人信息,目的是为了保证网络已经正常且token有效
  161. this.userStore.loadUser().then(() => {
  162. // 断线重连
  163. this.$message.error("连接断开,正在尝试重新连接...");
  164. this.$wsApi.reconnect(process.env.VUE_APP_WS_URL, sessionStorage.getItem(
  165. "accessToken"));
  166. }).catch(() => {
  167. // 10s后重试
  168. setTimeout(() => this.reconnectWs(), 10000)
  169. })
  170. },
  171. onReconnectWs() {
  172. // 重连成功
  173. this.reconnecting = false;
  174. // 重新加载群和好友
  175. const promises = [];
  176. promises.push(this.friendStore.loadFriend());
  177. promises.push(this.groupStore.loadGroup());
  178. Promise.all(promises).then(() => {
  179. // 加载离线消息
  180. this.pullOfflineMessage();
  181. this.configStore.setAppInit(true)
  182. this.$message.success("重新连接成功");
  183. }).catch(() => {
  184. this.$message.error("初始化失败");
  185. this.onExit();
  186. })
  187. },
  188. loadStore() {
  189. return this.userStore.loadUser().then(() => {
  190. const promises = [];
  191. promises.push(this.friendStore.loadFriend());
  192. promises.push(this.groupStore.loadGroup());
  193. promises.push(this.chatStore.loadChat());
  194. promises.push(this.configStore.loadConfig());
  195. return Promise.all(promises);
  196. })
  197. },
  198. unloadStore() {
  199. this.friendStore.clear();
  200. this.groupStore.clear();
  201. this.chatStore.clear();
  202. this.userStore.clear();
  203. },
  204. pullOfflineMessage() {
  205. this.chatStore.setLoading(true);
  206. const promises = [];
  207. promises.push(this.pullPrivateOfflineMessage(this.chatStore.privateMsgMaxId));
  208. promises.push(this.pullGroupOfflineMessage(this.chatStore.groupMsgMaxId));
  209. Promise.all(promises).then(messages => {
  210. // 处理离线消息
  211. messages[0].forEach(m => this.handlePrivateMessage(m));
  212. messages[1].forEach(m => this.handleGroupMessage(m));
  213. // 处理缓冲区收到的实时消息
  214. this.privateMessagesBuffer.forEach(m => this.handlePrivateMessage(m));
  215. this.groupMessagesBuffer.forEach(m => this.handleGroupMessage(m));
  216. // 清空缓冲区
  217. this.privateMessagesBuffer = [];
  218. this.groupMessagesBuffer = [];
  219. // 关闭加载离线标记
  220. this.chatStore.setLoading(false);
  221. // 刷新会话
  222. this.chatStore.refreshChats();
  223. }).catch((e) => {
  224. console.log(e)
  225. this.$message.error("拉取离线消息失败");
  226. this.onExit();
  227. })
  228. },
  229. pullPrivateOfflineMessage(minId) {
  230. return this.$http({
  231. url: "/message/private/loadOfflineMessage?minId=" + minId,
  232. method: 'GET'
  233. })
  234. },
  235. pullGroupOfflineMessage(minId) {
  236. return this.$http({
  237. url: "/message/group/loadOfflineMessage?minId=" + minId,
  238. method: 'GET'
  239. })
  240. },
  241. handlePrivateMessage(msg) {
  242. // 标记这条消息是不是自己发的
  243. msg.selfSend = msg.sendId == this.userStore.userInfo.id;
  244. // 好友id
  245. let friendId = msg.selfSend ? msg.recvId : msg.sendId;
  246. // 会话信息
  247. let chatInfo = {
  248. type: 'PRIVATE',
  249. targetId: friendId
  250. }
  251. // 消息已读处理,清空已读数量
  252. if (msg.type == this.$enums.MESSAGE_TYPE.READED) {
  253. this.chatStore.resetUnreadCount(chatInfo)
  254. return;
  255. }
  256. // 消息回执处理,改消息状态为已读
  257. if (msg.type == this.$enums.MESSAGE_TYPE.RECEIPT) {
  258. this.chatStore.readedMessage({
  259. friendId: msg.sendId
  260. })
  261. return;
  262. }
  263. // 消息撤回
  264. if (msg.type == this.$enums.MESSAGE_TYPE.RECALL) {
  265. this.chatStore.recallMessage(msg, chatInfo)
  266. return;
  267. }
  268. // 新增好友
  269. if (msg.type == this.$enums.MESSAGE_TYPE.FRIEND_NEW) {
  270. this.friendStore.addFriend(JSON.parse(msg.content));
  271. return;
  272. }
  273. // 删除好友
  274. if (msg.type == this.$enums.MESSAGE_TYPE.FRIEND_DEL) {
  275. this.friendStore.removeFriend(friendId);
  276. return;
  277. }
  278. // 对好友设置免打扰
  279. if (msg.type == this.$enums.MESSAGE_TYPE.FRIEND_DND) {
  280. this.friendStore.setDnd(friendId, JSON.parse(msg.content));
  281. this.chatStore.setDnd(chatInfo, JSON.parse(msg.content));
  282. return;
  283. }
  284. // 单人webrtc 信令
  285. if (this.$msgType.isRtcPrivate(msg.type)) {
  286. this.$refs.rtcPrivateVideo.onRTCMessage(msg)
  287. return;
  288. }
  289. // 插入消息
  290. if (this.$msgType.isNormal(msg.type) || this.$msgType.isTip(msg.type) || this.$msgType.isAction(msg.type)) {
  291. let friend = this.loadFriendInfo(friendId);
  292. this.insertPrivateMessage(friend, msg);
  293. }
  294. },
  295. insertPrivateMessage(friend, msg) {
  296. let chatInfo = {
  297. type: 'PRIVATE',
  298. targetId: friend.id,
  299. showName: friend.nickName,
  300. headImage: friend.headImage,
  301. isDnd: friend.isDnd
  302. };
  303. // 打开会话
  304. this.chatStore.openChat(chatInfo);
  305. // 插入消息
  306. this.chatStore.insertMessage(msg, chatInfo);
  307. // 播放提示音
  308. if (!friend.isDnd && !this.chatStore.loading && !msg.selfSend && this.$msgType.isNormal(msg.type) &&
  309. msg.status != this.$enums.MESSAGE_STATUS.READED) {
  310. this.playAudioTip();
  311. }
  312. },
  313. handleGroupMessage(msg) {
  314. // 标记这条消息是不是自己发的
  315. msg.selfSend = msg.sendId == this.userStore.userInfo.id;
  316. let chatInfo = {
  317. type: 'GROUP',
  318. targetId: msg.groupId
  319. }
  320. // 消息已读处理
  321. if (msg.type == this.$enums.MESSAGE_TYPE.READED) {
  322. // 我已读对方的消息,清空已读数量
  323. this.chatStore.resetUnreadCount(chatInfo)
  324. return;
  325. }
  326. // 消息回执处理
  327. if (msg.type == this.$enums.MESSAGE_TYPE.RECEIPT) {
  328. // 更新消息已读人数
  329. let msgInfo = {
  330. id: msg.id,
  331. groupId: msg.groupId,
  332. readedCount: msg.readedCount,
  333. receiptOk: msg.receiptOk
  334. };
  335. this.chatStore.updateMessage(msgInfo, chatInfo)
  336. return;
  337. }
  338. // 消息撤回
  339. if (msg.type == this.$enums.MESSAGE_TYPE.RECALL) {
  340. this.chatStore.recallMessage(msg, chatInfo)
  341. return;
  342. }
  343. // 新增群
  344. if (msg.type == this.$enums.MESSAGE_TYPE.GROUP_NEW) {
  345. this.groupStore.addGroup(JSON.parse(msg.content));
  346. return;
  347. }
  348. // 删除群
  349. if (msg.type == this.$enums.MESSAGE_TYPE.GROUP_DEL) {
  350. this.groupStore.removeGroup(msg.groupId);
  351. return;
  352. }
  353. // 对群设置免打扰
  354. if (msg.type == this.$enums.MESSAGE_TYPE.GROUP_DND) {
  355. this.groupStore.setDnd(msg.groupId, JSON.parse(msg.content));
  356. this.chatStore.setDnd(chatInfo, JSON.parse(msg.content));
  357. return;
  358. }
  359. // 群视频信令
  360. if (this.$msgType.isRtcGroup(msg.type)) {
  361. this.$nextTick(() => {
  362. this.$refs.rtcGroupVideo.onRTCMessage(msg);
  363. })
  364. return;
  365. }
  366. // 插入群聊消息
  367. if (this.$msgType.isNormal(msg.type) || this.$msgType.isTip(msg.type) || this.$msgType.isAction(msg.type)) {
  368. let group = this.loadGroupInfo(msg.groupId);
  369. this.insertGroupMessage(group, msg);
  370. }
  371. },
  372. insertGroupMessage(group, msg) {
  373. let chatInfo = {
  374. type: 'GROUP',
  375. targetId: group.id,
  376. showName: group.showGroupName,
  377. headImage: group.headImageThumb,
  378. isDnd: group.isDnd
  379. };
  380. // 打开会话
  381. this.chatStore.openChat(chatInfo);
  382. // 插入消息
  383. this.chatStore.insertMessage(msg, chatInfo);
  384. // 播放提示音
  385. if (!group.isDnd && !this.chatStore.loading &&
  386. !msg.selfSend && this.$msgType.isNormal(msg.type)
  387. && msg.status != this.$enums.MESSAGE_STATUS.READED) {
  388. this.playAudioTip();
  389. }
  390. },
  391. handleSystemMessage(msg) {
  392. // 用户被封禁
  393. if (msg.type == this.$enums.MESSAGE_TYPE.USER_BANNED) {
  394. this.$wsApi.close(3000);
  395. this.$alert("您的账号已被管理员封禁,原因:" + msg.content, "账号被封禁", {
  396. confirmButtonText: '确定',
  397. callback: () => {
  398. this.onExit();
  399. }
  400. });
  401. return;
  402. }
  403. },
  404. closeUserInfo() {
  405. this.$refs.userInfo.close();
  406. },
  407. onExit() {
  408. this.unloadStore();
  409. this.configStore.setAppInit(false);
  410. this.$wsApi.close(3000);
  411. sessionStorage.removeItem("accessToken");
  412. location.href = "/";
  413. },
  414. playAudioTip() {
  415. // 防止过于密集播放
  416. if (new Date().getTime() - this.lastPlayAudioTime > 1000) {
  417. this.lastPlayAudioTime = new Date().getTime();
  418. let audio = new Audio();
  419. let url = require(`@/assets/audio/tip.mp3`);
  420. audio.src = url;
  421. audio.play();
  422. }
  423. },
  424. showSetting() {
  425. this.showSettingDialog = true;
  426. },
  427. closeSetting() {
  428. this.showSettingDialog = false;
  429. },
  430. loadFriendInfo(id) {
  431. let friend = this.friendStore.findFriend(id);
  432. if (!friend) {
  433. friend = {
  434. id: id,
  435. showNickName: "未知用户",
  436. headImage: ""
  437. }
  438. }
  439. return friend;
  440. },
  441. loadGroupInfo(id) {
  442. let group = this.groupStore.findGroup(id);
  443. if (!group) {
  444. group = {
  445. id: id,
  446. showGroupName: "未知群聊",
  447. headImageThumb: ""
  448. }
  449. }
  450. return group;
  451. }
  452. },
  453. computed: {
  454. unreadCount() {
  455. let unreadCount = 0;
  456. let chats = this.chatStore.chats;
  457. chats.forEach(chat => {
  458. if (!chat.delete && !chat.isDnd) {
  459. unreadCount += chat.unreadCount
  460. }
  461. });
  462. return unreadCount;
  463. }
  464. },
  465. watch: {
  466. unreadCount: {
  467. handler(newCount, oldCount) {
  468. let tip = newCount > 0 ? `${newCount}条未读` : "";
  469. this.$elm.setTitleTip(tip);
  470. },
  471. immediate: true
  472. }
  473. },
  474. mounted() {
  475. this.init();
  476. },
  477. unmounted() {
  478. this.$wsApi.close();
  479. }
  480. }
  481. </script>
  482. <style scoped lang="scss">
  483. .home-page {
  484. height: 100vh;
  485. width: 100vw;
  486. display: flex;
  487. justify-content: center;
  488. align-items: center;
  489. border-radius: 4px;
  490. overflow: hidden;
  491. background: var(--im-color-primary-light-9);
  492. .app-container {
  493. width: 62vw;
  494. height: 80vh;
  495. display: flex;
  496. min-height: 600px;
  497. min-width: 970px;
  498. position: absolute;
  499. border-radius: 4px;
  500. overflow: hidden;
  501. box-shadow: var(--im-box-shadow-dark);
  502. transition: 0.2s;
  503. &.fullscreen {
  504. transition: 0.2s;
  505. width: 100vw;
  506. height: 100vh;
  507. }
  508. }
  509. .navi-bar {
  510. --icon-font-size: 22px;
  511. --width: 60px;
  512. width: var(--width);
  513. background: var(--im-color-primary-light-1);
  514. padding-top: 20px;
  515. .navi-bar-box {
  516. height: 100%;
  517. display: flex;
  518. flex-direction: column;
  519. justify-content: space-between;
  520. .botoom {
  521. margin-bottom: 30px;
  522. }
  523. }
  524. .user-head-image {
  525. display: flex;
  526. justify-content: center;
  527. }
  528. .menu {
  529. height: 200px;
  530. //margin-top: 10px;
  531. display: flex;
  532. flex-direction: column;
  533. justify-content: center;
  534. align-content: center;
  535. .link {
  536. text-decoration: none;
  537. }
  538. .router-link-active .menu-item {
  539. color: #fff;
  540. background: var(--im-color-primary-light-2);
  541. }
  542. .link:not(.router-link-active) .menu-item:hover {
  543. color: var(--im-color-primary-light-7);
  544. }
  545. .menu-item {
  546. position: relative;
  547. color: var(--im-color-primary-light-4);
  548. width: var(--width);
  549. height: 46px;
  550. display: flex;
  551. justify-content: center;
  552. align-items: center;
  553. margin-bottom: 12px;
  554. .icon {
  555. font-size: var(--icon-font-size)
  556. }
  557. .unread-text {
  558. position: absolute;
  559. background-color: var(--im-color-danger);
  560. left: 28px;
  561. top: 8px;
  562. color: white;
  563. border-radius: 30px;
  564. padding: 0 5px;
  565. font-size: var(--im-font-size-smaller);
  566. text-align: center;
  567. white-space: nowrap;
  568. border: 1px solid #f1e5e5;
  569. }
  570. }
  571. }
  572. .bottom-item {
  573. display: flex;
  574. justify-content: center;
  575. align-items: center;
  576. height: 50px;
  577. width: 100%;
  578. cursor: pointer;
  579. color: var(--im-color-primary-light-4);
  580. font-size: var(--icon-font-size);
  581. .icon {
  582. font-size: var(--icon-font-size)
  583. }
  584. &:hover {
  585. font-weight: 600;
  586. color: var(--im-color-primary-light-7);
  587. }
  588. }
  589. }
  590. .content-box {
  591. flex: 1;
  592. padding: 0;
  593. background-color: #fff;
  594. text-align: center;
  595. }
  596. }
  597. </style>