diff --git a/apis/interfaces/im.js b/apis/interfaces/im.js index b179e16..b6ff4d4 100644 --- a/apis/interfaces/im.js +++ b/apis/interfaces/im.js @@ -22,14 +22,14 @@ const getImToken = () => { url: 'im/token', }, true) } - -// 删除好友 -const deleteFriend = (targetId) => { - return request({ - method: 'DELETE', - url: 'im/friends/' + targetId, - }) -} + +// 删除好友 +const deleteFriend = (targetId) => { + return request({ + method: 'DELETE', + url: 'im/friends/' + targetId, + }) +} // 获取用户信息 const getUserInfo = (targetId) => { @@ -37,9 +37,9 @@ const getUserInfo = (targetId) => { url: 'im/userInfo/' + targetId, }) } - -/** - * 查看好友资料,附带好友关系 + +/** + * 查看好友资料,附带好友关系 */ const getFriendInfo = (userId) => { return request({ @@ -84,16 +84,26 @@ const pedingFriend = (recipient) => { url: 'im/friends/' + recipient }) } + +/** + * 好友申请数量 + */ +const getPendingCount = () => { + return request({ + url: 'im/friends/pending/count' + }) +} export { - getImToken, + getImToken, deleteFriend, getFriends, - getUserInfo, + getUserInfo, getFriendInfo, getPedings, resolveFriend, rejectFriend, searchFriend, - pedingFriend + pedingFriend, + getPendingCount } diff --git a/components/im/imAUDIO.vue b/components/im/imAUDIO.vue index 4f42250..4cf74ad 100644 --- a/components/im/imAUDIO.vue +++ b/components/im/imAUDIO.vue @@ -1,8 +1,10 @@ @@ -11,8 +13,13 @@ name:"im", props:{ msg : { - type : String, - default: 'https://images.pexels.com/photos/10266655/pexels-photo-10266655.jpeg' + type : Object, + default: () => { + return { + src: "", + time: "20" + } + } }, guest: { type: Boolean, @@ -20,10 +27,18 @@ } }, methods:{ - openImg(){ - uni.previewImage({ - urls : [this.msg], - current : 1 + // 播放语音消息 + onPlayMsg() { + let innerAudioContext = uni.createInnerAudioContext() + innerAudioContext.src = this.audioSrc + if (this.audioContextPaused) { + innerAudioContext.play() + this.audioContextPaused = false + return + } + innerAudioContext.stop() + innerAudioContext.onStop(resStop => { + this.audioContextPaused = true }) } } diff --git a/components/im/imTXT.vue b/components/im/imTXT.vue index fe31273..5de8135 100644 --- a/components/im/imTXT.vue +++ b/components/im/imTXT.vue @@ -15,7 +15,7 @@ props:{ msg : { type : String, - default: '文字消息' + default: '' }, guest: { type: Boolean, @@ -31,8 +31,6 @@ padding: 20rpx; font-size: 28rpx; line-height: 40rpx; - min-height: 40rpx; - min-width: 50rpx; } .im--text.left{ diff --git a/manifest.json b/manifest.json index c09ce08..c681adb 100644 --- a/manifest.json +++ b/manifest.json @@ -32,6 +32,7 @@ "", "", "", + "", "", "", "", diff --git a/pages.json b/pages.json index 0b02e9b..c807204 100644 --- a/pages.json +++ b/pages.json @@ -453,7 +453,9 @@ "path": "pages/im/friends/mine", "name": "imFriendsMine", "style": { - "navigationBarTitleText": "我的资料" + "navigationBarTitleText": "我的二维码", + "navigationBarBackgroundColor": "#34CE98", + "navigationBarTextStyle": "white" } }, { @@ -573,14 +575,14 @@ "navigationBarBackgroundColor": "#34CE98", "navigationBarTextStyle": "white" } - },{ - "path" : "pages/im/private/chat", - "style" : { - "disableScroll": true, - "navigationBarTitleText": "聊天", - "enablePullDownRefresh": false, - "navigationBarBackgroundColor":"#F3F6FB" - } + }, { + "path": "pages/im/private/chat", + "style": { + "disableScroll": true, + "navigationBarTitleText": "聊天", + "enablePullDownRefresh": false, + "navigationBarBackgroundColor": "#F3F6FB" + } } ], "tabBar": { diff --git a/pages/im/friends/index.vue b/pages/im/friends/index.vue index 1811ed5..a053b62 100644 --- a/pages/im/friends/index.vue +++ b/pages/im/friends/index.vue @@ -4,7 +4,9 @@ - 新的朋友 + + 新的朋友 ({{ pendingCount }}) + @@ -22,7 +24,8 @@ - import { - getFriends + getFriends, + getPendingCount } from '@/apis/interfaces/im'; + export default { data() { return { indexs: [], - friends: [] + friends: [], + pendingCount: 0 }; }, onShow() { getFriends().then(res => { - this.indexs = res.indexList; - this.friends = res.itemArr; - }); + this.indexs = res.indexList + this.friends = res.itemArr + }) + getPendingCount().then(res => { + console.log(res); + this.pendingCount = res + }) }, - computed: {}, methods: { - // 扫码提示 showToast() { uni.showToast({ title: '群聊功能暂未开放,敬请期待', @@ -104,7 +112,9 @@ .name { flex: 1; padding-left: $padding; - font-size: $title-size; + font-size: $title-size + 2; + font-size: $title-size + 2; + color: #454545 !important; @extend .nowrap; } } diff --git a/pages/im/friends/info.vue b/pages/im/friends/info.vue index 5e4a375..a75aa3f 100644 --- a/pages/im/friends/info.vue +++ b/pages/im/friends/info.vue @@ -2,7 +2,7 @@ - + {{userInfo.name}} 地址:{{userInfo.address}} @@ -22,11 +22,11 @@ - + - + @@ -40,21 +40,21 @@ - + 删除好友 - - + + 发送消息 - + - 视频通话 + 音/视频通话 @@ -62,7 +62,7 @@ - + 申请好友 @@ -104,7 +104,8 @@ callShow: false } }, - onLoad(e) { + onLoad(e) { + console.log(e); this.targetId = e.targetId getFriendInfo(e.targetId).then(res => { this.userInfo = res @@ -126,123 +127,99 @@ } }); }, - toPrivate() { - uni.redirectTo({ - url: '/pages/im/private/index?conversationType=1&targetId=' + this.targetId - }); - }, - setRemark() { - uni.showToast({ - title: '开发中', - icon: 'none' - }); - }, - deleteFriend() { - uni.showModal({ - title: '删除确认', - content: '确认删除后不可恢复', - success: e => { - if (e.confirm) { - deleteFriend(this.targetId).then(res => { - // 删除聊天记录 - RongIMLib.deleteMessages(1, this.targetId); - RongIMLib.removeConversation(1, this.targetId); - uni.showToast({ - icon: 'none', - title: '好友删除成功', - success() { - uni.switchTab({ - url: '/pages/im/index' - }); - } - }); - }); - } + methods: { + copyAddress() { + uni.setClipboardData({ + data: this.userInfo.address, + success: () => { + uni.showToast({ + icon: 'none', + title: '复制成功' }) - }, - toPrivate() { - uni.redirectTo({ - url: '/pages/im/private/index?conversationType=1&targetId=' + this.targetId - }) - }, - setRemark() { - uni.showToast({ - title: '开发中', - icon: 'none' - }) - }, - deleteFriend() { - uni.showModal({ - title: '删除确认', - content: '确认删除后不可恢复', - success: (e) => { - if (e.confirm) { - deleteFriend(this.targetId).then(res => { - // 删除聊天记录 - RongIMLib.deleteMessages(1, this.targetId) - RongIMLib.removeConversation(1, this.targetId) - uni.showToast({ - icon: 'none', - title: '好友删除成功', - success() { - uni.switchTab({ - url: '/pages/im/index' - }) - } - }) + } + }) + }, + toPrivate() { + uni.redirectTo({ + url: '/pages/im/private/chat?conversationType=1&targetId=' + this.targetId + }); + }, + setRemark() { + uni.showToast({ + title: '开发中', + icon: 'none' + }); + }, + deleteFriend() { + uni.showModal({ + title: '删除确认', + content: '确认删除后不可恢复', + success: e => { + if (e.confirm) { + deleteFriend(this.targetId).then(res => { + // 删除聊天记录 + RongIMLib.deleteMessages(1, this.targetId); + RongIMLib.removeConversation(1, this.targetId); + uni.showToast({ + icon: 'none', + title: '好友删除成功', + success() { + uni.switchTab({ + url: '/pages/im/index' + }) + } }) - } + }) } - }) - }, - setStatus() { - RongIMLib.setConversationNotificationStatus(this.conversationType, this.targetId, this.status, ({ + } + }) + }, + setStatus() { + RongIMLib.setConversationNotificationStatus(this.conversationType, this.targetId, this.status, + ({ status }) => { this.status = !Boolean(status) }) - }, - setTop() { - RongIMLib.setConversationToTop(this.conversationType, this.targetId, this.isTop, (res) => { - RongIMLib.getConversation(this.conversationType, this.targetId, ({ - conversation - }) => { - this.isTop = conversation.isTop - }) + }, + setTop() { + RongIMLib.setConversationToTop(this.conversationType, this.targetId, this.isTop, (res) => { + RongIMLib.getConversation(this.conversationType, this.targetId, ({ + conversation + }) => { + this.isTop = conversation.isTop }) - }, - // 申请好友 - toBeFriend() { - pedingFriend(this.targetId).then(res => { - uni.showToast({ - title: '申请成功', - icon: 'none' - }); + }) + }, + // 申请好友 + toBeFriend() { + pedingFriend(this.targetId).then(res => { + uni.showToast({ + title: '申请成功', + icon: 'none' + }); - }) - .catch(err => { - uni.showToast({ - icon: 'error', - title: err.message, - duration: 2000 - }) - }) - }, - singleCall(e) { - CallLib.startSingleCall(this.targetId, e.type, ''); - uni.redirectTo({ - url: '/pages/im/private/call', - success: (err) => { - console.log('跳转视频通话成功'); - }, - fail: (err) => { - console.log('跳转视频页失败', err); - } }) - }, + .catch(err => { + uni.showToast({ + icon: 'error', + title: err.message, + duration: 2000 + }) + }) + }, + singleCall(e) { + uni.showToast({ + icon: 'none', + title: '功能正在开发中' + }) + // CallLib.startSingleCall(this.targetId, e.type, ''); + // uni.redirectTo({ + // url: '/pages/im/private/call?targetId=' + this.targetId + '&mediaType=' + e.type + // }) + } } } - }; diff --git a/pages/im/index.vue b/pages/im/index.vue index 9a40fc2..c9f48ad 100644 --- a/pages/im/index.vue +++ b/pages/im/index.vue @@ -29,17 +29,14 @@ - + - - + @@ -56,7 +53,7 @@ {{item}} - + @@ -74,7 +71,9 @@ import * as RongIMLib from "@/uni_modules/RongCloud-IMWrapper/js_sdk/index" import im from '@/utils/im/index.js' import userAuth from '@/public/userAuth' - import { getImToken } from '@/apis/interfaces/im.js' + import { + getImToken + } from '@/apis/interfaces/im.js' export default { data() { return { @@ -99,13 +98,20 @@ } } }, + onLoad() { + if (this.isShown) { + uni.$on('onReceiveMessage', (msg) => { + console.log(msg); + this.getConversationList() + }) + uni.$on('onConnectionStatusChange', (status) => { + this.connection = status + }) + } + }, onShow() { if (this.$store.state.token !== '') { - getImToken().then(res => { - im.connect(res.token, res.userInfo, () => { - this.getConversationList() - }) - }) + this.getConversationList() } this.isShown = true }, @@ -127,16 +133,6 @@ } } }, - watch: { - '$store.getters.newMessage': function(n, o) { - if (this.isShown) { - this.getConversationList() - } - }, - '$store.getters.connection': function(n, o) { - this.connection = n - } - }, methods: { hidePop() { this.showPop = false @@ -203,9 +199,10 @@ toDetail(item) { this.hidePop() uni.navigateTo({ - url: '/pages/im/private/index?targetId=' + item.targetId + '&conversationType=' + item - .conversationType - }) + url: '/pages/im/private/chat?targetId=' + item.targetId + '&conversationType=' + item.conversationType + }) + + // url: '/pages/im/private/index?targetId=' + item.targetId + '&conversationType=' + item.conversationType }, // 点击按钮 onNav(name, params) { @@ -226,10 +223,8 @@ scanQrCode() { uni.scanCode({ success: (res) => { - // "result": "ADDFRIEND|10010", if (res.scanType == 'QR_CODE') { res.result.substr(0, 10) == 'ADDFRIEND|' - // 跳转到添加好友页面 uni.navigateTo({ url: '/pages/im/friends/info?targetId=' + res.result.substr(10) }) @@ -293,7 +288,7 @@ .contents { background-color: $window-color; min-height: 100vh; - padding-top: 90rpx; + padding-top: 90rpx + 20rpx; box-sizing: border-box; .null-list { @@ -314,14 +309,15 @@ .message { background: white; - padding: 20rpx 30rpx; + padding: 30rpx 0 0 30rpx; position: relative; display: flex; - &.u-border-bottom {} &.is-top { background: $window-color; + border-bottom: #e8e8e8; + // background-color: rgba($color: $main-color, $alpha: 0.02); } .avatar { @@ -333,28 +329,39 @@ } .content { - padding-left: 30rpx; - width: calc(100% - 44px); + margin-left: 30rpx; + width: calc(100% - 46px); box-sizing: border-box; + border-bottom: solid 1rpx #f3f3f3; + position: relative; .header { display: flex; justify-content: space-between; .name { - font-size: $title-size + 4; + font-size: $title-size + 2; + color: #454545; + color: #454545; } .time { font-size: $title-size-sm; - color: $text-gray; + color: $text-gray-m; + position: absolute; + right: 30rpx; } } .preview { word-break: break-all; - color: $text-gray; - font-size: $title-size-sm; + color: $text-gray-m; + padding-top: $padding - 20; + padding-bottom: $padding; + font-size: $title-size-m; + height: 40rpx; + line-height: 40rpx; + width: 500rpx; @extend .nowrap; } } @@ -410,4 +417,7 @@ } } } + .u-border-bottom{ + border-bottom: solid 1rpx #f9f9f9 !important; + } diff --git a/pages/im/private/call.nvue b/pages/im/private/call.nvue index 439c555..47926d6 100644 --- a/pages/im/private/call.nvue +++ b/pages/im/private/call.nvue @@ -8,20 +8,43 @@ - + - - {{userInfo.name}} - 等待对方接听 - 已接通 {{ connected }} - {{ mediaType == 0 ? '邀请您语音通话' : '邀请您视频通话' }} + + + {{userInfo.name}} + 等待对方接听 + 已接通 + {{ mediaType == 0 ? '邀请您语音通话' : '邀请您视频通话' }} - 麦克风 - 挂断 - 接听 - 扬声器 + + + + + + 麦克风 + + + + + + 挂断 + + + + + + 接听 + + + + + + + 扬声器 + @@ -37,50 +60,32 @@ return { targetId: '', mediaType: 0, // 0 语音 1 视频 - users: [], userInfo: {}, isOut: false, connected: false, micStatus: false, speStatus: false, remoteRinging: false, - innerAudioContext: null + ring: null } }, onLoad(e) { - this.targetId = e.targetId || 10051 + this.targetId = e.targetId this.mediaType = e.mediaType getFriendInfo(this.targetId).then(res => { this.userInfo = res }) this.startRing() // 监听通话链接状态 - - setTimeout(() => { - CallLib.setVideoView(10047, this.$refs.bigVideoView.ref, 0, false) - }, 200) - - uni.$on('onCallConnected', () => { - console.log('OnCallConnected'); - this.connected = true - this.stopRing() - }) - uni.$on('onCallDisconnected', () => { - this.hangup() - }) - uni.$on('onRemoteUserRinging', () => { + uni.$once('onCallConnected', this.onCallConnected) + uni.$once('onCallDisconnected', this.hangup) + uni.$once('onRemoteUserRinging', () => { this.remoteRinging = true }) - uni.$on('onRemoteUserJoined', () => { + uni.$once('onRemoteUserJoined', () => { this.remoteRinging = false }) }, - beforeDestroy() { - uni.$off('onCallConnected') - uni.$off('onCallDisconnected') - uni.$off('onRemoteUserRinging') - uni.$off('onRemoteUserJoined') - }, computed: { windowWidth() { return uni.getSystemInfoSync().windowWidth @@ -100,10 +105,15 @@ }, accept() { CallLib.accept() + }, + onCallConnected() { + // 关掉铃声 + this.stopRing() + // 设置链接状态 + this.connected = true // 视频通话,才开摄像头 if (this.mediaType == 1) { const session = CallLib.getCurrentCallSession() - this.users = session.users setTimeout(() => { CallLib.setVideoView(session.targetId, this.$refs.bigVideoView.ref, 0, false) @@ -118,11 +128,10 @@ setTimeout(() => { this.downRing() - uni.navigateBack() - // uni.redirectTo({ - // url: '/pages/im/private/index?targetId=' + this.targetId + '&conversationType=1' - // }) - }, 500); + uni.switchTab({ + url: '/pages/im/index' + }) + }, 200); }, toHome() { uni.switchTab({ @@ -130,24 +139,24 @@ }) }, startRing() { - const innerAudioContext = uni.createInnerAudioContext() - this.innerAudioContext = innerAudioContext - innerAudioContext.autoplay = true - innerAudioContext.loop = true - innerAudioContext.src = '/static/im/sounds/call-ring.mp3' - innerAudioContext.onEnded(() => { - innerAudioContext.destroy() + const ring = uni.createInnerAudioContext() + this.ring = ring + ring.autoplay = true + ring.loop = true + ring.src = '/static/im/sounds/call-ring.mp3' + ring.onEnded(() => { + ring.destroy() }) }, stopRing() { - this.innerAudioContext.stop() + this.ring.stop() }, downRing() { - const innerAudioContext = uni.createInnerAudioContext() - innerAudioContext.autoplay = true - innerAudioContext.src = '/static/im/sounds/call-down.mp3' - innerAudioContext.onEnded(() => { - innerAudioContext.destroy() + const ding = uni.createInnerAudioContext() + ding.autoplay = true + ding.src = '/static/im/sounds/call-down.mp3' + ding.onEnded(() => { + ding.destroy() }) } } @@ -156,17 +165,18 @@ diff --git a/pages/im/private/chat.nvue b/pages/im/private/chat.nvue index abfb703..87a9b25 100644 --- a/pages/im/private/chat.nvue +++ b/pages/im/private/chat.nvue @@ -2,12 +2,20 @@ - - - - - - + + + {{ item.sentTime|timeCustomCN }} + + + + + + + + + {{ item.sentStatus == 50 ? '已读': '未读'}} + + @@ -26,46 +34,255 @@ - 发送 + 发送 + + + + + 录音中 {{transcribeTime}} s diff --git a/public/userAuth.js b/public/userAuth.js index a2464cf..0257a3a 100644 --- a/public/userAuth.js +++ b/public/userAuth.js @@ -1,115 +1,136 @@ -/** - * Web唐明明 - * 匆匆数载恍如梦,岁月迢迢华发增。 - * 碌碌无为枉半生,一朝惊醒万事空。 - * moduleName: 登录 - */ - -import { router } from '../router' -import { keyAuth } from '../apis/interfaces/auth' -import store from '../store' - -class userAuth { - constructor() { - this.univerfyConfig = { - fullScreen: true, - authButton: { - 'title': '一键登录', - 'normalColor': '#34CE98', - 'highlightColor': '#16b17a', - 'disabledColor': '#aae4cc', - }, - otherLoginButton: { - 'title': '其他手机号码', - 'borderColor': '#34CE98', - 'textColor': '#34CE98' - }, - privacyTerms: { - 'checkedImage': '/static/icon/checked-icon.png', - 'uncheckedImage': '/static/icon/unchecked-icon.png', - 'textColor': '#999', - 'termsColor': '#34CE98', - 'suffix': '并使用本机号码登录/注册', - 'privacyItems': [{ - 'url': 'https://www.baidu.com', - 'title': '隐私协议' - }, { - 'url': 'https://www.baidu.com', - 'title': '服务协议' - }] - }, - buttons: { - 'iconWidth': '45px', - 'list': [{ - "provider": '微信登录', - "iconPath": '/static/icons/wechat.png', - }] - } - } - } - // 预登录 - Login() { - //#ifdef H5 - router.push({ name: 'Auth' }) - return - //#endif - - return new Promise((resolve, reject) => { - uni.showLoading({ - title: '加载中', - mask: true - }) - uni.preLogin({ - provider: 'univerify', - success: res => { - this.keyLogin().then(() => { - resolve({ - auth: true - }) - }).catch(errMsg => { - reject(errMsg) - }) - }, - fail: err => { - router.push({ name: 'Auth' }) - }, - complete() { - uni.hideLoading() - } - }) - }) - } - // 一键登录 - keyLogin() { - return new Promise((resolve, reject) => { - uni.login({ - provider: 'univerify', - univerifyStyle: { - ...this.univerfyConfig - }, - success: authResult => { - keyAuth({ - access_token: authResult.authResult.access_token, - openid: authResult.authResult.openid - }).then(res => { - uni.closeAuthView() - store.commit('setToken', res.token_type + ' ' + res.access_token) - resolve() - }).catch(err => { - reject(err) - }) - }, - fail: err => { - uni.closeAuthView() - switch (err.code) { - case 30002: - router.push({ name: 'Auth', params: { keyPhone: 1 }}) - break - } - } - }) - }) - } -} - +/** + * Web唐明明 + * 匆匆数载恍如梦,岁月迢迢华发增。 + * 碌碌无为枉半生,一朝惊醒万事空。 + * moduleName: 登录 + */ + +import { + router +} from '../router' +import { + keyAuth +} from '../apis/interfaces/auth' +import store from '../store' +import { + getImToken +} from '@/apis/interfaces/im.js' +import im from '@/utils/im/index.js' + +class userAuth { + constructor() { + this.univerfyConfig = { + fullScreen: true, + authButton: { + 'title': '一键登录', + 'normalColor': '#34CE98', + 'highlightColor': '#16b17a', + 'disabledColor': '#aae4cc', + }, + otherLoginButton: { + 'title': '其他手机号码', + 'borderColor': '#34CE98', + 'textColor': '#34CE98' + }, + privacyTerms: { + 'checkedImage': '/static/icon/checked-icon.png', + 'uncheckedImage': '/static/icon/unchecked-icon.png', + 'textColor': '#999', + 'termsColor': '#34CE98', + 'suffix': '并使用本机号码登录/注册', + 'privacyItems': [{ + 'url': 'https://www.baidu.com', + 'title': '隐私协议' + }, { + 'url': 'https://www.baidu.com', + 'title': '服务协议' + }] + }, + buttons: { + 'iconWidth': '45px', + 'list': [{ + "provider": '微信登录', + "iconPath": '/static/icons/wechat.png', + }] + } + } + } + // 预登录 + Login() { + //#ifdef H5 + router.push({ + name: 'Auth' + }) + return + //#endif + + return new Promise((resolve, reject) => { + uni.showLoading({ + title: '加载中', + mask: true + }) + uni.preLogin({ + provider: 'univerify', + success: res => { + this.keyLogin().then(() => { + resolve({ + auth: true + }) + }).catch(errMsg => { + reject(errMsg) + }) + }, + fail: err => { + router.push({ + name: 'Auth' + }) + }, + complete() { + uni.hideLoading() + } + }) + }) + } + // 一键登录 + keyLogin() { + return new Promise((resolve, reject) => { + uni.login({ + provider: 'univerify', + univerifyStyle: { + ...this.univerfyConfig + }, + success: authResult => { + keyAuth({ + access_token: authResult.authResult.access_token, + openid: authResult.authResult.openid + }).then(res => { + uni.closeAuthView() + store.commit('setToken', res.token_type + ' ' + res.access_token) + // 在这里,登录成功,链接IM服务 + getImToken().then(res => { + im.connect(res.token, res.userInfo) + }) + resolve() + }).catch(err => { + reject(err) + }) + }, + fail: err => { + uni.closeAuthView() + switch (err.code) { + case 30002: + router.push({ + name: 'Auth', + params: { + keyPhone: 1 + } + }) + break + } + } + }) + }) + } +} + export default userAuth diff --git a/router/index.js b/router/index.js index 892961e..b3b5465 100644 --- a/router/index.js +++ b/router/index.js @@ -10,6 +10,9 @@ import store from '@/store/index' const ROUTES = [{ 'path': '/pages/im/private/call', 'name': 'imPrivateCall' +},{ + 'path': '/pages/im/private/chat', + 'name': 'imPrivateChat' }] // #endif diff --git a/store/modules/im.js b/store/modules/im.js index c9db26e..e72b86e 100644 --- a/store/modules/im.js +++ b/store/modules/im.js @@ -2,19 +2,10 @@ import im from "@/utils/im/index.js" export default { state: { - newMsg: {}, friends: {}, sender: {}, - // IM连接状态,0是正常 - connection: 0 }, getters: { - newMessage(state) { - return state.newMsg - }, - connection(state) { - return state.connection - }, friends(state) { return state.friends }, @@ -40,28 +31,14 @@ export default { } }, mutations: { - newMessage(state, msg) { - Vue.set(state, 'newMsg', msg) - }, updateFriends(state, userInfo) { Vue.set(state.friends, userInfo.userId, userInfo) }, SET_state_sender(state, userInfo) { state.sender = userInfo - }, - SET_connection_status(state, status) { - state.connection = status } }, - actions: { - updateConnectionStatus({commit}, status) { - commit('SET_connection_status', status) - }, - newMessage({ - commit - }, msg) { - commit('newMessage', msg) - }, + actions: { setSenderInfo({ commit }, userInfo) { diff --git a/utils/im/index.js b/utils/im/index.js index 4a864ea..2131e2c 100644 --- a/utils/im/index.js +++ b/utils/im/index.js @@ -3,13 +3,20 @@ import * as CallLib from '@/uni_modules/RongCloud-CallWrapper/lib/index' import store from '@/store/index.js' import { getFriends, - getUserInfo + getUserInfo, + getImToken } from '@/apis/interfaces/im.js' const initIm = (KEY) => { RongIMLib.init(KEY) CallLib.init() addListeners() + // 初始化的时候 自动链接 + if (store.getters.getToken !== '') { + getImToken().then(res => { + connect(res.token, res.userInfo, () => {}) + }) + } } const setNotifyBadge = () => { @@ -19,6 +26,9 @@ const setNotifyBadge = () => { count }) => { if (code === 0) { + // #ifdef APP-PLUS + plus.runtime.setBadgeNumber(count) + // #endif if (count > 0) { uni.setTabBarBadge({ index: 3, @@ -52,14 +62,25 @@ const connect = (token, userInfo, callback) => { const model = uni.model.friendModel model.find((err, results) => { + console.log('好友列表', results); results.map(item => { store.dispatch('updateFriends', item) }) }) } +/** + * 断开链接 + */ const disconnect = () => { RongIMLib.disconnect() + // 移除提醒数量 + // #ifdef APP-PLUS + plus.runtime.setBadgeNumber(0) + // #endif + uni.removeTabBarBadge({ + index: 3 + }) } // 允许通知的消息类型,触发更新消息列表操作 @@ -89,8 +110,8 @@ function inArray(search, array) { const addListeners = () => { // 添加连接状态监听函数 RongIMLib.addConnectionStatusListener((res) => { - console.log('连接状态监', res.data.status); - store.dispatch('updateConnectionStatus', res.data.status) + console.log('连接状态监听', res.data.status); + uni.$emit('onConnectionStatusChange', res.data.status) }) // 添加消息监听函数 RongIMLib.addReceiveMessageListener((res) => { @@ -101,39 +122,39 @@ const addListeners = () => { } }) // 监听通话呼入 - CallLib.onCallReceived((res) => { - console.log("Engine:OnCallReceived=>" + "监听通话呼入, 目标id=>", res.data.targetId); - console.log('RES', res); + CallLib.onCallReceived(({ + data + }) => { + console.log('onCallReceived', data) + uni.navigateTo({ - url: '/pages/im/private/call?targetId=' + res.data.targetId + '&mediaType=' + - res.data.mediaType, - success: (err) => { - console.log('跳转视频通话成功'); - }, - fail: (err) => { - console.log('跳转视频页失败', err); - } + url: '/pages/im/private/call?targetId=' + data.targetId + '&mediaType=' + + data.mediaType }) - }) - - CallLib.onCallConnected((res) => { - uni.$emit('onCallConnected'); - }) - CallLib.onCallOutgoing((res) => { - uni.$emit('onCallOutgoing'); - }) - CallLib.onRemoteUserRinging((res) => { - uni.$emit('onRemoteUserRinging'); - }) - CallLib.onRemoteUserJoined((res) => { - uni.$emit('onRemoteUserJoined'); - }) - CallLib.onCallDisconnected((res) => { - uni.$emit('onCallDisconnected'); - }) - CallLib.onRemoteUserLeft((res) => { - uni.$emit('onCallDisconnected'); - }) + }) + + // 通话建立成功 + CallLib.onCallConnected(() => { + uni.$emit('onCallConnected'); + }) + + CallLib.onCallOutgoing((res) => { + uni.$emit('onCallOutgoing'); + }) + CallLib.onRemoteUserRinging((res) => { + uni.$emit('onRemoteUserRinging'); + }) + CallLib.onRemoteUserJoined((res) => { + uni.$emit('onRemoteUserJoined'); + }) + CallLib.onCallDisconnected((res) => { + console.log('断开链接', res); + uni.$emit('onCallDisconnected'); + }) + CallLib.onRemoteUserLeft((res) => { + console.log('远端离开', res); + uni.$emit('onRemoteUserLeft'); + }) } // 维护消息列表 @@ -155,7 +176,8 @@ const newMessage = (msg) => { syncUserInfo(msg.targetId) } - store.dispatch('newMessage', msg) + uni.$emit('onReceiveMessage', msg); + // store.dispatch('newMessage', msg) } function syncUserInfo(targetId) {