App.vue 4.8 KB

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