App.vue 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  1. <script>
  2. import {
  3. EMClient
  4. } from "@/EaseIM";
  5. import {
  6. emConnectListener,
  7. emMountGlobalListener
  8. } from "@/EaseIM/emListener";
  9. import {
  10. emConnect,
  11. emUserInfos,
  12. emConversation
  13. } from "@/EaseIM/emApis";
  14. import {
  15. CONNECT_CALLBACK_TYPE,
  16. HANDLER_EVENT_NAME
  17. } from "@/EaseIM/constant";
  18. import {
  19. emHandleReconnect
  20. } from "@/EaseIM/utils";
  21. // import readAppInfo from './utils/readAppInfo.js';
  22. const {
  23. fetchUserInfoWithLoginId,
  24. fetchOtherInfoFromServer
  25. } =
  26. emUserInfos();
  27. //获取会话列表
  28. const {
  29. fetchConversationFromServer,
  30. fetchPinConversationFromServer,
  31. getLocalConversations
  32. } = emConversation();
  33. export default {
  34. onLaunch() {
  35. console.log('App Launch')
  36. //传给监听callback回调
  37. return;
  38. const connectedCallback = (type) => {
  39. console.log(">>>>>IM连接回调", type);
  40. if (type === CONNECT_CALLBACK_TYPE.CONNECT_CALLBACK) {
  41. this.onConnectedSuccess();
  42. }
  43. if (type === CONNECT_CALLBACK_TYPE.DISCONNECT_CALLBACK) {
  44. this.onDisconnect();
  45. }
  46. if (type === CONNECT_CALLBACK_TYPE.RECONNECTING_CALLBACK) {
  47. this.onReconnecting();
  48. }
  49. };
  50. /* 链接所需监听回调 */
  51. emConnectListener(connectedCallback);
  52. /* 全局类型监听集合、消息、联系人、群组等... */
  53. emMountGlobalListener();
  54. this.handleAutoLoginEaseIM();
  55. },
  56. computed: {
  57. loginStoreStatus() {
  58. return this.$store.state.LoginStore.loginStatus;
  59. },
  60. loginStoreUserBaseInfos() {
  61. return this.$store.state.LoginStore.loginUserBaseInfos;
  62. },
  63. },
  64. onShow: function() {
  65. console.log('appshow===>>>>>>')
  66. // 读取url参数信息,存储app传递的用户相关信息
  67. // readAppInfo(() => {
  68. // // uni.redirectTo({
  69. // // url: "/pages/my/index"
  70. // // });
  71. // });
  72. // uni.redirectTo({
  73. // url: "/pages/my/index"
  74. // });
  75. },
  76. methods: {
  77. //获取会话列表
  78. getConversationFromServer() {
  79. const uIds = [];
  80. EMClient.getServerConversations({
  81. pageSize: 100,
  82. cursor: ''
  83. }).then((res) => {
  84. res.data.conversations && res.data.conversations.forEach((item) => {
  85. console.log('会话item', item)
  86. uIds.push(item.conversationId)
  87. })
  88. console.log('用户列表id', uIds);
  89. uni.setStorage({
  90. key: 'uIds',
  91. data: uIds
  92. })
  93. // EMClient.fetchUserInfoById(uIds).then((resData)=>{
  94. // console.log('用户列表',resData.data);
  95. // })
  96. }).catch((error) => {
  97. console.log(error);
  98. })
  99. },
  100. //登录环信
  101. async loginWithUserId() {
  102. try {
  103. const res = await loginWithPassword(
  104. uni.getStorageSync('im_userid'),
  105. uni.getStorageSync('im_pass')
  106. );
  107. this.$store.commit('SET_LOGIN_USER_BASE_INFOS', {
  108. loginUserId: uni.getStorageSync('im_userid'),
  109. });
  110. this.setEMUserLoginInfosToStorage(
  111. uni.getStorageSync('im_userid').toLowerCase(),
  112. res.accessToken
  113. );
  114. } catch (error) {
  115. console.log('>>>>>>环信登录失败', error);
  116. uni.showToast({
  117. title: '登录失败',
  118. icon: 'none',
  119. });
  120. } finally {
  121. }
  122. },
  123. setEMUserLoginInfosToStorage(userId, token) {
  124. const params = {
  125. key: `EM_LOGIN_INFOS`,
  126. data: {
  127. userId: userId,
  128. token: token
  129. },
  130. };
  131. uni.setStorage({
  132. ...params
  133. });
  134. },
  135. //自动登录
  136. handleAutoLoginEaseIM() {
  137. const {
  138. actionEMReconnect
  139. } = emHandleReconnect();
  140. const loginInfos = uni.getStorageSync(`EM_LOGIN_INFOS`);
  141. console.log('loginInfos', loginInfos)
  142. if (!loginInfos) return;
  143. actionEMReconnect();
  144. },
  145. onConnectedSuccess() {
  146. const {
  147. loginUserId
  148. } = this.loginStoreUserBaseInfos || {};
  149. const finalLoginUserId = loginUserId || EMClient.user;
  150. if (!this.loginStoreStatus) {
  151. this.fetchLoginUserNeedData();
  152. uni.hideLoading();
  153. console.log(">>>>>>开始跳转至会话列表页面");
  154. //
  155. this.getConversationFromServer();
  156. uni.redirectTo({
  157. url: "../home/home?myName=" + finalLoginUserId,
  158. });
  159. }
  160. this.$store.commit("SET_LOGIN_USER_BASE_INFOS", {
  161. loginUserId: finalLoginUserId,
  162. });
  163. this.$store.commit("SET_LOGIN_STATUS", true);
  164. },
  165. onDisconnect() {
  166. const {
  167. closeEaseIM
  168. } = emConnect();
  169. const {
  170. actionEMReconnect
  171. } = emHandleReconnect();
  172. //断开回调触发后,如果业务登录状态为true则说明异常断开需要重新登录
  173. if (!this.loginStatus) {
  174. uni.showToast({
  175. title: "退出登录",
  176. icon: "none",
  177. duration: 2000,
  178. });
  179. uni.redirectTo({
  180. url: "../login/login",
  181. });
  182. closeEaseIM();
  183. } else {
  184. console.log(">>>>>需执行重登逻辑");
  185. //执行通过token进行重新登录
  186. actionEMReconnect();
  187. }
  188. },
  189. onReconnecting() {
  190. uni.showToast({
  191. title: "IM 重连中...",
  192. icon: "none",
  193. });
  194. },
  195. //获取登录所需基础参数
  196. async fetchLoginUserNeedData() {
  197. await this.$store.dispatch("fetchFriendList");
  198. //获取好友用户属性
  199. await this.$store.dispatch("fetchFriendUserInfoCollection");
  200. //获取当前登录用户好友信息
  201. await this.$store.dispatch("fetchLoginUserProfile");
  202. await this.$store.dispatch("fetchBlockUserList");
  203. // 在线状态订阅
  204. await this.$store.dispatch("subscribePresenceStatus");
  205. this.fetchJoinedGroupList();
  206. //初始化缓存本地的新邀请列表
  207. this.$store.commit("INIT_RECEIVE_INVITE_LIST");
  208. },
  209. //获取加入的群组列表
  210. async fetchJoinedGroupList() {
  211. //获取群组列表
  212. await this.$store.dispatch("fetchJoinedGroupList", {
  213. isInit: true,
  214. });
  215. },
  216. },
  217. onHide: function() {
  218. console.log('App Hide')
  219. },
  220. }
  221. </script>
  222. <style>
  223. /*每个页面公共css */
  224. * {
  225. padding: 0;
  226. margin: 0;
  227. /* background-color: rgb(247, 248, 252); */
  228. /* background-color: green; */
  229. }
  230. @import "@/uni_modules/uview-ui/index.scss";
  231. </style>