App.vue 7.6 KB

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