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/pages/im/group/chat.nvue b/pages/im/group/chat.nvue index a06fb9f..f2522e6 100644 --- a/pages/im/group/chat.nvue +++ b/pages/im/group/chat.nvue @@ -3,7 +3,8 @@ - + @@ -43,12 +44,17 @@ data() { return { targetId: '', - conversationType: 3, + conversationType: RongIMLib.ConversationType.GROUP, messages: [], groupInfo: { name: '' } } + }, + computed: { + latestMessage() { + return this.messages[this.messages.length - 1] + } }, onLoad(e) { this.targetId = e.targetId @@ -61,51 +67,47 @@ if (msg.targetId == this.targetId) { this.getNewMessage() } - }) - uni.$once('cleanGroupMessage', this.getMessageList) - }, - onBackPress() { - uni.$off('onReceiveMessage') - console.log('Off onReceiveMessage'); + }) + uni.$once('cleanGroupMessage', this.getMessageList) }, onNavigationBarButtonTap() { uni.navigateTo({ url: '/pages/im/group/info?targetId=' + this.targetId }) }, - methods: { - toUser(item) { - if (item.senderUserId == '__system__') { - return - } - if (item.messageDirection == 1) { - uni.navigateTo({ - url: '/pages/im/friends/mine?targetId=' + item.senderUserId - }) - } else{ - uni.navigateTo({ - url: '/pages/im/friends/info?targetId=' + item.senderUserId - }) - } - }, - getNewMessage() { - im.getMessageList( - this.conversationType, - this.targetId, - new Date().getTime(), - 10, - false, - (messages) => { - this.messages = this.messages.concat(messages) - this.scrollBottom() - }) + methods: { + toUser(item) { + if (item.senderUserId == '__system__') { + return + } + if (item.messageDirection == 1) { + uni.navigateTo({ + url: '/pages/im/friends/mine?targetId=' + item.senderUserId + }) + } else { + uni.navigateTo({ + url: '/pages/im/friends/info?targetId=' + item.senderUserId + }) + } + }, + getNewMessage() { + im.getMessageList( + this.conversationType, + this.targetId, + this.latestMessage.sentTime, + 1, + false, + (messages) => { + this.messages = this.messages.concat(messages) + this.scrollBottom() + }) }, // 获取消息列表 getMessageList() { im.getMessageList( this.conversationType, this.targetId, - new Date().getTime(), + 0, 100, true, (messages) => { @@ -115,12 +117,15 @@ }, // 滚动到底部 scrollBottom(type) { - // 清理当前会话,未读消息数量 - RongIMLib.clearMessagesUnreadStatus(this.conversationType, this.targetId, new Date().getTime() + 1100) - // 发送消息已读状态给对方 - RongIMLib.sendReadReceiptMessage(this.conversationType, this.targetId, new Date().getTime()) - // 更新badge提醒数量 - im.setNotifyBadge() + if (this.latestMessage) { + // 清理当前会话,未读消息数量 + RongIMLib.clearMessagesUnreadStatus(this.conversationType, this.targetId, this.latestMessage + .sentTime) + // // 发送消息已读状态给对方 + // RongIMLib.sendReadReceiptMessage(this.conversationType, this.targetId, this.latestMessage.sentTime) + // 更新badge提醒数量 + im.setNotifyBadge() + } setTimeout(() => { let el = this.$refs.chatBottom diff --git a/pages/im/private/chat.nvue b/pages/im/private/chat.nvue index c1ec122..1fb5400 100644 --- a/pages/im/private/chat.nvue +++ b/pages/im/private/chat.nvue @@ -42,6 +42,12 @@ const ChatList = uni.requireNativePlugin('dom') export default { + components: { + sentMessageBar, + showVoice, + showImage, + showText + }, data() { return { targetId: '', @@ -54,11 +60,10 @@ } } }, - components: { - sentMessageBar, - showVoice, - showImage, - showText + computed: { + latestMessage() { + return this.messages[this.messages.length - 1] + } }, onLoad(e) { this.targetId = e.targetId @@ -94,24 +99,24 @@ customCN(val) { return timeCustomCN(val) }, - getNewMessage() { - im.getMessageList( - this.conversationType, - this.targetId, - new Date().getTime(), - 10, - false, - (messages) => { - this.messages = this.messages.concat(messages) - this.scrollBottom() - }) + getNewMessage() { + im.getMessageList( + this.conversationType, + this.targetId, + this.latestMessage.sentTime, + 1, + false, + (messages) => { + this.messages = this.messages.concat(messages) + this.scrollBottom() + }) }, // 获取消息列表 getMessageList() { im.getMessageList( this.conversationType, this.targetId, - new Date().getTime(), + 0, 100, true, (messages) => { @@ -128,12 +133,15 @@ }, // 滚动到底部 scrollBottom(type) { - // 清理当前会话,未读消息数量 - RongIMLib.clearMessagesUnreadStatus(this.conversationType, this.targetId, new Date().getTime()) - // 发送消息已读状态给对方 - RongIMLib.sendReadReceiptMessage(this.conversationType, this.targetId, new Date().getTime()) - // 更新badge提醒数量 - im.setNotifyBadge() + if (this.latestMessage) { + // 清理当前会话,未读消息数量 + RongIMLib.clearMessagesUnreadStatus(this.conversationType, this.targetId, this.latestMessage + .sentTime) + // // 发送消息已读状态给对方 + RongIMLib.sendReadReceiptMessage(this.conversationType, this.targetId, this.latestMessage.sentTime) + // 更新badge提醒数量 + im.setNotifyBadge() + } setTimeout(() => { let el = this.$refs.chatBottom ChatList.scrollToElement(el, { 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() } }