| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248 |
- <script>
- import {
- EMClient
- } from "@/EaseIM";
- import {
- emConnectListener,
- emMountGlobalListener
- } from "@/EaseIM/emListener";
- import {
- emConnect,
- emUserInfos,
- emConversation
- } from "@/EaseIM/emApis";
- import {
- CONNECT_CALLBACK_TYPE,
- HANDLER_EVENT_NAME
- } from "@/EaseIM/constant";
- import {
- emHandleReconnect
- } from "@/EaseIM/utils";
- // import readAppInfo from './utils/readAppInfo.js';
- const {
- fetchUserInfoWithLoginId,
- fetchOtherInfoFromServer
- } =
- emUserInfos();
- //获取会话列表
- const {
- fetchConversationFromServer,
- fetchPinConversationFromServer,
- getLocalConversations
- } = emConversation();
-
-
- export default {
- onLaunch() {
- console.log('App Launch')
- //传给监听callback回调
- return;
-
-
- const connectedCallback = (type) => {
- console.log(">>>>>IM连接回调", type);
- if (type === CONNECT_CALLBACK_TYPE.CONNECT_CALLBACK) {
- this.onConnectedSuccess();
- }
- if (type === CONNECT_CALLBACK_TYPE.DISCONNECT_CALLBACK) {
- this.onDisconnect();
- }
- if (type === CONNECT_CALLBACK_TYPE.RECONNECTING_CALLBACK) {
- this.onReconnecting();
- }
- };
-
- /* 链接所需监听回调 */
- emConnectListener(connectedCallback);
- /* 全局类型监听集合、消息、联系人、群组等... */
- emMountGlobalListener();
- this.handleAutoLoginEaseIM();
-
- },
- computed: {
- loginStoreStatus() {
- return this.$store.state.LoginStore.loginStatus;
- },
- loginStoreUserBaseInfos() {
- return this.$store.state.LoginStore.loginUserBaseInfos;
- },
- },
- onShow: function() {
- console.log('appshow===>>>>>>')
- // 读取url参数信息,存储app传递的用户相关信息
- // readAppInfo(() => {
- // // uni.redirectTo({
- // // url: "/pages/my/index"
- // // });
- // });
- // uni.redirectTo({
-
- // url: "/pages/my/index"
- // });
-
-
-
- },
- methods: {
- //获取会话列表
- getConversationFromServer() {
-
- const uIds = [];
- EMClient.getServerConversations({
- pageSize: 100,
- cursor: ''
- }).then((res) => {
- res.data.conversations && res.data.conversations.forEach((item) => {
- console.log('会话item', item)
- uIds.push(item.conversationId)
- })
- console.log('用户列表id', uIds);
- uni.setStorage({
- key: 'uIds',
- data: uIds
- })
- // EMClient.fetchUserInfoById(uIds).then((resData)=>{
- // console.log('用户列表',resData.data);
- // })
- }).catch((error) => {
- console.log(error);
- })
- },
- //登录环信
- async loginWithUserId() {
-
- try {
- const res = await loginWithPassword(
- uni.getStorageSync('im_userid'),
- uni.getStorageSync('im_pass')
- );
- this.$store.commit('SET_LOGIN_USER_BASE_INFOS', {
- loginUserId: uni.getStorageSync('im_userid'),
- });
- this.setEMUserLoginInfosToStorage(
- uni.getStorageSync('im_userid').toLowerCase(),
- res.accessToken
- );
- } catch (error) {
- console.log('>>>>>>环信登录失败', error);
- uni.showToast({
- title: '登录失败',
- icon: 'none',
- });
- } finally {
-
- }
- },
- setEMUserLoginInfosToStorage(userId, token) {
- const params = {
- key: `EM_LOGIN_INFOS`,
- data: {
- userId: userId,
- token: token
- },
- };
- uni.setStorage({
- ...params
- });
- },
- //自动登录
- handleAutoLoginEaseIM() {
- const {
- actionEMReconnect
- } = emHandleReconnect();
- const loginInfos = uni.getStorageSync(`EM_LOGIN_INFOS`);
- console.log('loginInfos', loginInfos)
- if (!loginInfos) return;
- actionEMReconnect();
- },
- onConnectedSuccess() {
- const {
- loginUserId
- } = this.loginStoreUserBaseInfos || {};
- const finalLoginUserId = loginUserId || EMClient.user;
- if (!this.loginStoreStatus) {
- this.fetchLoginUserNeedData();
- uni.hideLoading();
- console.log(">>>>>>开始跳转至会话列表页面");
- //
- this.getConversationFromServer();
- uni.redirectTo({
- url: "../home/home?myName=" + finalLoginUserId,
- });
- }
- this.$store.commit("SET_LOGIN_USER_BASE_INFOS", {
- loginUserId: finalLoginUserId,
- });
- this.$store.commit("SET_LOGIN_STATUS", true);
-
- },
- onDisconnect() {
- const {
- closeEaseIM
- } = emConnect();
- const {
- actionEMReconnect
- } = emHandleReconnect();
- //断开回调触发后,如果业务登录状态为true则说明异常断开需要重新登录
- if (!this.loginStatus) {
- uni.showToast({
- title: "退出登录",
- icon: "none",
- duration: 2000,
- });
- uni.redirectTo({
- url: "../login/login",
- });
- closeEaseIM();
- } else {
- console.log(">>>>>需执行重登逻辑");
- //执行通过token进行重新登录
- actionEMReconnect();
- }
- },
- onReconnecting() {
- uni.showToast({
- title: "IM 重连中...",
- icon: "none",
- });
- },
- //获取登录所需基础参数
- async fetchLoginUserNeedData() {
- await this.$store.dispatch("fetchFriendList");
- //获取好友用户属性
- await this.$store.dispatch("fetchFriendUserInfoCollection");
- //获取当前登录用户好友信息
- await this.$store.dispatch("fetchLoginUserProfile");
- await this.$store.dispatch("fetchBlockUserList");
- // 在线状态订阅
- await this.$store.dispatch("subscribePresenceStatus");
- this.fetchJoinedGroupList();
- //初始化缓存本地的新邀请列表
- this.$store.commit("INIT_RECEIVE_INVITE_LIST");
- },
- //获取加入的群组列表
- async fetchJoinedGroupList() {
- //获取群组列表
- await this.$store.dispatch("fetchJoinedGroupList", {
- isInit: true,
- });
- },
- },
-
- onHide: function() {
- console.log('App Hide')
- },
- }
- </script>
-
- <style>
- /*每个页面公共css */
- * {
- padding: 0;
- margin: 0;
- /* background-color: rgb(247, 248, 252); */
- /* background-color: green; */
- }
-
- @import "@/uni_modules/uview-ui/index.scss";
- </style>
|