From f9dfd6b08dc36d18bc08c6e819938a4261f28c38 Mon Sep 17 00:00:00 2001 From: Jason Date: Wed, 16 Feb 2022 11:19:53 +0800 Subject: [PATCH] =?UTF-8?q?IM=E7=99=BB=E5=BD=95=E5=AE=8C=E5=96=84=EF=BC=8C?= =?UTF-8?q?=E5=90=8C=E6=AD=A5=E5=A5=BD=E5=8F=8B=E6=95=B0=E6=8D=AE=E7=9A=84?= =?UTF-8?q?=E6=97=B6=E5=80=99=EF=BC=8C=E5=90=8C=E6=AD=A5=E7=BE=A4=E7=BB=84?= =?UTF-8?q?=E6=95=B0=E6=8D=AE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- pages/auth/auth.vue | 10 ++++++++++ public/userAuth.js | 4 +++- store/modules/im.js | 2 +- utils/im/index.js | 23 +++++++++++++---------- 4 files changed, 27 insertions(+), 12 deletions(-) diff --git a/pages/auth/auth.vue b/pages/auth/auth.vue index 05f59cf..ec3b273 100644 --- a/pages/auth/auth.vue +++ b/pages/auth/auth.vue @@ -39,6 +39,11 @@ smsAuth } from "@/apis/interfaces/auth"; import userAuth from "@/public/userAuth"; + import { + getImToken + } from '@/apis/interfaces/im.js' + import im from '@/utils/im/index.js' + export default { data() { return { @@ -59,6 +64,11 @@ "setToken", res.token_type + " " + res.access_token ); + // 在这里,登录成功,链接IM服务 + getImToken().then(res => { + console.log('在这获取IM-TOKEN', res); + im.connect(res.token, res.userInfo, () => {}) + }) this.$Router.back(); }).catch((err) => { uni.showToast({ diff --git a/public/userAuth.js b/public/userAuth.js index 0257a3a..faf4df5 100644 --- a/public/userAuth.js +++ b/public/userAuth.js @@ -105,9 +105,11 @@ class userAuth { openid: authResult.authResult.openid }).then(res => { uni.closeAuthView() - store.commit('setToken', res.token_type + ' ' + res.access_token) + store.commit('setToken', res.token_type + ' ' + res + .access_token) // 在这里,登录成功,链接IM服务 getImToken().then(res => { + console.log('在这获取IM-TOKEN', res); im.connect(res.token, res.userInfo) }) resolve() diff --git a/store/modules/im.js b/store/modules/im.js index 7d482b6..8077e66 100644 --- a/store/modules/im.js +++ b/store/modules/im.js @@ -34,7 +34,7 @@ export default { } }, mutations: { - updateContactInfo(state, contactInfo) { + updateContactInfo(state, contactInfo) { Vue.set(state.contacts, contactInfo.targetId, contactInfo) }, setSenderInfo(state, contactInfo) { diff --git a/utils/im/index.js b/utils/im/index.js index 4c22b53..7fdf5dc 100644 --- a/utils/im/index.js +++ b/utils/im/index.js @@ -6,7 +6,8 @@ import listeners from './listeners.js' import { getFriends, getUserInfo, - getImToken + getImToken, + getMyGroups } from '@/apis/interfaces/im.js' const initIm = (KEY) => { @@ -58,7 +59,7 @@ const connect = (token, userInfo, callback) => { // 设置未读消息数量 setNotifyBadge() // 首次运行获取好友列表 - const FK = 'IFT_' + userInfo.targetId + const FK = 'ZHK_' + userInfo.targetId uni.getStorage({ key: FK, @@ -70,12 +71,14 @@ const connect = (token, userInfo, callback) => { }) }) }, - fail: () => { - // 程序是首次运行,初始化加载好友信息 - getFriends().then(res => { - res.map(item => { - store.dispatch('initContact', item) - }) + fail: () => { + // 程序是首次运行,初始化加载好友和群组信息 + Promise.all([getFriends(), getMyGroups()]).then(result => { + result.map(contacts => { + contacts.map(item => { + store.dispatch('initContact', item) + }) + }) uni.setStorageSync(FK, userInfo.targetId) }) } @@ -206,8 +209,8 @@ const newMessage = (msg) => { status }) => { if (code === 0) { - if (status) { - uni.vibrateLong() + if (status) { + uni.vibrateLong() triTone() } }