App.vue 7.5 KB

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