App.vue 4.8 KB

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