App.vue 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280
  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. // 加载离线消息
  21. this.loadPrivateMessage(store.state.chatStore.privateMsgMaxId);
  22. this.loadGroupMessage(store.state.chatStore.groupMsgMaxId);
  23. }).catch((e) => {
  24. console.log(e);
  25. this.exit();
  26. })
  27. },
  28. initWebSocket() {
  29. let loginInfo = uni.getStorageSync("loginInfo")
  30. let userId = store.state.userStore.userInfo.id;
  31. wsApi.init(process.env.WS_URL, loginInfo.accessToken);
  32. wsApi.connect();
  33. wsApi.onOpen()
  34. wsApi.onMessage((cmd, msgInfo) => {
  35. if (cmd == 2) {
  36. // 异地登录,强制下线
  37. uni.showModal({
  38. content: '您已在其他地方登陆,将被强制下线',
  39. showCancel: false,
  40. })
  41. this.exit();
  42. } else if (cmd == 3) {
  43. // 私聊消息
  44. this.handlePrivateMessage(msgInfo);
  45. } else if (cmd == 4) {
  46. // 群聊消息
  47. this.handleGroupMessage(msgInfo);
  48. }
  49. });
  50. wsApi.onClose((res) => {
  51. // 1006是服务器主动断开,3000是APP主动关闭
  52. if (res.code == 1006) {
  53. uni.showToast({
  54. title: '连接已断开,请重新登录',
  55. icon: 'none',
  56. })
  57. this.exit();
  58. } else if (res.code != 3000) {
  59. // 重新连接
  60. wsApi.connect();
  61. }
  62. })
  63. },
  64. loadPrivateMessage(minId) {
  65. store.commit("loadingPrivateMsg", true)
  66. http({
  67. url: "/message/private/loadMessage?minId=" + minId,
  68. method: 'get'
  69. }).then((msgInfos) => {
  70. msgInfos.forEach((msgInfo) => {
  71. this.handlePrivateMessage(msgInfo);
  72. })
  73. if (msgInfos.length == 100) {
  74. // 继续拉取
  75. this.loadPrivateMessage(msgInfos[99].id);
  76. } else {
  77. store.commit("loadingPrivateMsg", false)
  78. }
  79. })
  80. },
  81. loadGroupMessage(minId) {
  82. store.commit("loadingGroupMsg", true)
  83. http({
  84. url: "/message/group/loadMessage?minId=" + minId,
  85. method: 'get'
  86. }).then((msgInfos) => {
  87. msgInfos.forEach((msgInfo) => {
  88. this.handleGroupMessage(msgInfo);
  89. })
  90. if (msgInfos.length == 100) {
  91. // 继续拉取
  92. this.loadGroupMessage(msgInfos[99].id);
  93. } else {
  94. store.commit("loadingGroupMsg", false)
  95. }
  96. })
  97. },
  98. handlePrivateMessage(msg) {
  99. // 标记这条消息是不是自己发的
  100. msg.selfSend = msg.sendId == store.state.userStore.userInfo.id;
  101. // 好友id
  102. let friendId = msg.selfSend ? msg.recvId : msg.sendId;
  103. // 消息已读处理
  104. if (msg.type == enums.MESSAGE_TYPE.READED) {
  105. if (msg.selfSend) {
  106. // 我已读对方的消息,清空已读数量
  107. let chatInfo = {
  108. type: 'PRIVATE',
  109. targetId: friendId
  110. }
  111. store.commit("resetUnreadCount", chatInfo)
  112. } else {
  113. // 对方已读我的消息,修改消息状态为已读
  114. store.commit("readedMessage", friendId)
  115. }
  116. return;
  117. }
  118. this.loadFriendInfo(friendId).then((friend) => {
  119. this.insertPrivateMessage(friend, msg);
  120. })
  121. },
  122. insertPrivateMessage(friend, msg) {
  123. // webrtc 信令
  124. if (msg.type >= enums.MESSAGE_TYPE.RTC_CALL &&
  125. msg.type <= enums.MESSAGE_TYPE.RTC_CANDIDATE) {}
  126. let chatInfo = {
  127. type: 'PRIVATE',
  128. targetId: friend.id,
  129. showName: friend.nickName,
  130. headImage: friend.headImage
  131. };
  132. // 打开会话
  133. store.commit("openChat", chatInfo);
  134. // 插入消息
  135. store.commit("insertMessage", msg);
  136. // 播放提示音
  137. !msg.selfSend && this.playAudioTip();
  138. },
  139. handleGroupMessage(msg) {
  140. // 标记这条消息是不是自己发的
  141. msg.selfSend = msg.sendId == store.state.userStore.userInfo.id;
  142. let groupId = msg.groupId;
  143. // 消息已读处理
  144. if (msg.type == enums.MESSAGE_TYPE.READED) {
  145. // 我已读对方的消息,清空已读数量
  146. let chatInfo = {
  147. type: 'GROUP',
  148. targetId: groupId
  149. }
  150. store.commit("resetUnreadCount", chatInfo)
  151. return;
  152. }
  153. this.loadGroupInfo(groupId).then((group) => {
  154. // 插入群聊消息
  155. this.insertGroupMessage(group, msg);
  156. })
  157. },
  158. insertGroupMessage(group, msg) {
  159. let chatInfo = {
  160. type: 'GROUP',
  161. targetId: group.id,
  162. showName: group.remark,
  163. headImage: group.headImageThumb
  164. };
  165. // 打开会话
  166. store.commit("openChat", chatInfo);
  167. // 插入消息
  168. store.commit("insertMessage", msg);
  169. // 播放提示音
  170. !msg.selfSend && this.playAudioTip();
  171. },
  172. loadFriendInfo(id) {
  173. return new Promise((resolve, reject) => {
  174. let friend = store.state.friendStore.friends.find((f) => f.id == id);
  175. if (friend) {
  176. resolve(friend);
  177. } else {
  178. http({
  179. url: `/friend/find/${id}`,
  180. method: 'get'
  181. }).then((friend) => {
  182. store.commit("addFriend", friend);
  183. resolve(friend)
  184. })
  185. }
  186. });
  187. },
  188. loadGroupInfo(id) {
  189. return new Promise((resolve, reject) => {
  190. let group = store.state.groupStore.groups.find((g) => g.id == id);
  191. if (group) {
  192. resolve(group);
  193. } else {
  194. http({
  195. url: `/group/find/${id}`,
  196. method: 'get'
  197. }).then((group) => {
  198. resolve(group)
  199. store.commit("addGroup", group);
  200. })
  201. }
  202. });
  203. },
  204. exit() {
  205. console.log("exit");
  206. wsApi.close();
  207. uni.removeStorageSync("loginInfo");
  208. uni.reLaunch({
  209. url: "/pages/login/login"
  210. })
  211. store.dispatch("unload");
  212. },
  213. playAudioTip() {
  214. // 音频播放无法成功
  215. // this.audioTip = uni.createInnerAudioContext();
  216. // this.audioTip.src = "/static/audio/tip.wav";
  217. // this.audioTip.play();
  218. },
  219. initAudit() {
  220. console.log("initAudit")
  221. if (store.state.userStore.userInfo.type == 1) {
  222. // 显示群组功能
  223. uni.setTabBarItem({
  224. index: 2,
  225. text: "群聊"
  226. })
  227. } else {
  228. // 隐藏群组功能
  229. uni.setTabBarItem({
  230. index: 2,
  231. text: "搜索"
  232. })
  233. }
  234. }
  235. },
  236. onLaunch() {
  237. // 登录状态校验
  238. if (uni.getStorageSync("loginInfo")) {
  239. // 初始化
  240. this.init()
  241. } else {
  242. // 跳转到登录页
  243. uni.navigateTo({
  244. url: "/pages/login/login"
  245. })
  246. }
  247. }
  248. }
  249. </script>
  250. <style lang="scss">
  251. @import url('./static/icon/iconfont.css');
  252. .tab-page {
  253. // #ifdef H5
  254. height: calc(100vh - 46px - 50px); // h5平台100vh是包含了顶部和底部,需要减去
  255. // #endif
  256. // #ifndef H5
  257. height: calc(100vh);
  258. // #endif
  259. background-color: #f8f8f8;
  260. }
  261. .page {
  262. // #ifdef H5
  263. height: calc(100vh - 45px); // h5平台100vh是包含了顶部,需要减去
  264. // #endif
  265. // #ifndef H5
  266. height: calc(100vh);
  267. // #endif
  268. background-color: #f8f8f8;
  269. }
  270. </style>