App.vue 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. <script>
  2. import store from './store';
  3. import http from './common/request';
  4. import * as enums from './common/enums';
  5. import * as wsApi from './common/wssocket';
  6. export default {
  7. data() {
  8. return {
  9. audioTip: null
  10. }
  11. },
  12. methods: {
  13. init() {
  14. // 加载数据
  15. store.dispatch("load").then(() => {
  16. // 初始化websocket
  17. this.initWebSocket();
  18. }).catch((e) => {
  19. console.log(e);
  20. this.exit();
  21. })
  22. },
  23. initWebSocket() {
  24. let loginInfo = uni.getStorageSync("loginInfo")
  25. let userId = store.state.userStore.userInfo.id;
  26. wsApi.createWebSocket(process.env.WS_URL, loginInfo.accessToken);
  27. wsApi.onopen(() => {
  28. this.pullUnreadMessage();
  29. });
  30. wsApi.onmessage((cmd, msgInfo) => {
  31. if (cmd == 2) {
  32. // 异地登录,强制下线
  33. uni.showModal({
  34. content: '您已在其他地方登陆,将被强制下线',
  35. showCancel: false,
  36. })
  37. this.exit();
  38. } else if (cmd == 3) {
  39. // 标记这条消息是不是自己发的
  40. msgInfo.selfSend = userId == msgInfo.sendId;
  41. // 插入私聊消息
  42. this.handlePrivateMessage(msgInfo);
  43. } else if (cmd == 4) {
  44. // 标记这条消息是不是自己发的
  45. msgInfo.selfSend = userId == msgInfo.sendId;
  46. // 插入群聊消息
  47. this.handleGroupMessage(msgInfo);
  48. }
  49. })
  50. },
  51. pullUnreadMessage() {
  52. // 拉取未读私聊消息
  53. http({
  54. url: "/message/private/pullUnreadMessage",
  55. method: 'POST'
  56. });
  57. // 拉取未读群聊消息
  58. http({
  59. url: "/message/group/pullUnreadMessage",
  60. method: 'POST'
  61. });
  62. },
  63. handlePrivateMessage(msg) {
  64. let friendId = msg.selfSend ? msg.recvId : msg.sendId;
  65. let friend = store.state.friendStore.friends.find((f) => f.id == friendId);
  66. if (!friend) {
  67. http({
  68. url: `/friend/find/${msg.sendId}`,
  69. method: 'GET'
  70. }).then((friend) => {
  71. this.insertPrivateMessage(friend, msg);
  72. store.commit("addFriend", friend);
  73. })
  74. } else {
  75. // 好友列表不存在好友信息,则发请求获取好友信息
  76. this.insertPrivateMessage(friend, msg);
  77. }
  78. },
  79. insertPrivateMessage(friend, msg) {
  80. // webrtc 信令
  81. if (msg.type >= enums.MESSAGE_TYPE.RTC_CALL &&
  82. msg.type <= enums.MESSAGE_TYPE.RTC_CANDIDATE) {}
  83. let chatInfo = {
  84. type: 'PRIVATE',
  85. targetId: friend.id,
  86. showName: friend.nickName,
  87. headImage: friend.headImage
  88. };
  89. // 打开会话
  90. store.commit("openChat", chatInfo);
  91. // 插入消息
  92. store.commit("insertMessage", msg);
  93. // 播放提示音
  94. !msg.selfSend && this.playAudioTip();
  95. },
  96. handleGroupMessage(msg) {
  97. let group = store.state.groupStore.groups.find((g) => g.id == msg.groupId);
  98. if (!group) {
  99. http({
  100. url: `/group/find/${msg.groupId}`,
  101. method: 'get'
  102. }).then((group) => {
  103. this.insertGroupMessage(group, msg);
  104. store.commit("addGroup", group);
  105. })
  106. } else {
  107. // 群聊缓存存在,直接插入群聊消息
  108. this.insertGroupMessage(group, msg);
  109. }
  110. },
  111. insertGroupMessage(group, msg) {
  112. let chatInfo = {
  113. type: 'GROUP',
  114. targetId: group.id,
  115. showName: group.remark,
  116. headImage: group.headImageThumb
  117. };
  118. // 打开会话
  119. store.commit("openChat", chatInfo);
  120. // 插入消息
  121. store.commit("insertMessage", msg);
  122. // 播放提示音
  123. !msg.selfSend && this.playAudioTip();
  124. },
  125. exit() {
  126. console.log("exit");
  127. wsApi.closeWebSocket();
  128. uni.removeStorageSync("loginInfo");
  129. uni.reLaunch({
  130. url: "/pages/login/login"
  131. })
  132. },
  133. playAudioTip() {
  134. // 音频播放无法成功
  135. // this.audioTip = uni.createInnerAudioContext();
  136. // this.audioTip.src = "/static/audio/tip.wav";
  137. // this.audioTip.play();
  138. }
  139. },
  140. onLaunch() {
  141. // 登录状态校验
  142. if (uni.getStorageSync("loginInfo")) {
  143. // 初始化
  144. this.init()
  145. } else {
  146. // 跳转到登录页
  147. uni.navigateTo({
  148. url: "/pages/login/login"
  149. })
  150. }
  151. }
  152. }
  153. </script>
  154. <style lang="scss">
  155. @import url('./static/icon/iconfont.css');
  156. .tab-page {
  157. // #ifdef H5
  158. height: calc(100vh - 46px - 50px); // h5平台100vh是包含了顶部和底部,需要减去
  159. // #endif
  160. // #ifndef H5
  161. height: calc(100vh);
  162. // #endif
  163. background-color: #f8f8f8;
  164. }
  165. .page {
  166. // #ifdef H5
  167. height: calc(100vh - 45px); // h5平台100vh是包含了顶部,需要减去
  168. // #endif
  169. // #ifndef H5
  170. height: calc(100vh);
  171. // #endif
  172. background-color: #f8f8f8;
  173. }
  174. </style>