This commit is contained in:
2022-02-16 13:29:42 +08:00
12 changed files with 573 additions and 301 deletions

View File

@@ -117,9 +117,10 @@ const getGroupBase = (groupId) => {
}) })
} }
const getGroupUsers = (groupId) => { const getGroupUsers = (groupId, limit) => {
limit = limit || 0
return request({ return request({
url: 'im/groups/' + groupId + '/users' url: 'im/groups/' + groupId + '/users?limit=' + limit
}) })
} }
@@ -195,7 +196,7 @@ const joinGroup = (groupId, message) => {
const quitGroup = (groupId) => { const quitGroup = (groupId) => {
return request({ return request({
method: 'POST', method: 'DELETE',
url: 'im/groups/' + groupId + '/quit' url: 'im/groups/' + groupId + '/quit'
}) })
} }
@@ -210,6 +211,59 @@ const dismissGroup = (groupId) => {
}) })
} }
/**
* 移除群成员
*/
const removeGroupUser = (groupId, userId) => {
return request({
method: 'DELETE',
url: 'im/groups/' + groupId + '/users/' + userId
})
}
/**
* 邀请群成员
*/
const inviteGroupUser = (groupId, userIds) => {
return request({
method: 'POST',
url: 'im/groups/' + groupId + '/invite',
data: {
userIds
}
})
}
/**
* 设置群管理
*/
const setGroupAdmin = (groupId, userId) => {
return request({
method: 'POST',
url: 'im/groups/' + groupId + '/admin/' + userId
})
}
/**
* 移除群管理
*/
const removeGroupAdmin = (groupId, userId) => {
return request({
method: 'DELETE',
url: 'im/groups/' + groupId + '/admin/' + userId
})
}
/**
* 转移群主
*/
const transferGroupOwner = (groupId, userId) => {
return request({
method: 'DELETE',
url: 'im/groups/' + groupId + '/owner/' + userId
})
}
export { export {
getImToken, getImToken,
deleteFriend, deleteFriend,
@@ -235,5 +289,10 @@ export {
joinGroupPre, joinGroupPre,
joinGroup, joinGroup,
quitGroup, quitGroup,
dismissGroup dismissGroup,
inviteGroupUser,
removeGroupUser,
setGroupAdmin,
removeGroupAdmin,
transferGroupOwner
} }

View File

@@ -482,6 +482,13 @@
"navigationBarTitleText": "群信息" "navigationBarTitleText": "群信息"
} }
}, },
{
"path": "pages/im/group/invite",
"name": "imGroupInvite",
"style": {
"navigationBarTitleText": "邀请好友"
}
},
{ {
"path": "pages/im/group/users", "path": "pages/im/group/users",
"name": "imGroupUsers", "name": "imGroupUsers",

View File

@@ -39,6 +39,11 @@
smsAuth smsAuth
} from "@/apis/interfaces/auth"; } from "@/apis/interfaces/auth";
import userAuth from "@/public/userAuth"; import userAuth from "@/public/userAuth";
import {
getImToken
} from '@/apis/interfaces/im.js'
import im from '@/utils/im/index.js'
export default { export default {
data() { data() {
return { return {
@@ -59,6 +64,11 @@
"setToken", "setToken",
res.token_type + " " + res.access_token res.token_type + " " + res.access_token
); );
// 在这里登录成功链接IM服务
getImToken().then(res => {
console.log('在这获取IM-TOKEN', res);
im.connect(res.token, res.userInfo, () => {})
})
this.$Router.back(); this.$Router.back();
}).catch((err) => { }).catch((err) => {
uni.showToast({ uni.showToast({

View File

@@ -3,7 +3,8 @@
<list class="body" :show-scrollbar="false"> <list class="body" :show-scrollbar="false">
<cell class="cell" v-for="(item, index) in messages" :key="index"> <cell class="cell" v-for="(item, index) in messages" :key="index">
<view class="cell-item" :class="item.messageDirection == 1 ? 'right' : 'left'"> <view class="cell-item" :class="item.messageDirection == 1 ? 'right' : 'left'">
<u-avatar class="avatar" @click="toUser(item)" size="36" shape="square" :src="item.content.userInfo.portraitUrl" /> <u-avatar class="avatar" @click="toUser(item)" size="36" shape="square"
:src="item.content.userInfo.portraitUrl" />
<view class="msg"> <view class="msg">
<show-voice v-if="item.objectName === 'RC:HQVCMsg'" :guest="item.messageDirection == 1" <show-voice v-if="item.objectName === 'RC:HQVCMsg'" :guest="item.messageDirection == 1"
:msg="item.content" :name="item.content.userInfo.name" /> :msg="item.content" :name="item.content.userInfo.name" />
@@ -16,7 +17,7 @@
</cell> </cell>
<cell class="cell-footer" ref="chatBottom"></cell> <cell class="cell-footer" ref="chatBottom"></cell>
</list> </list>
<sent-message-bar :conversationType="conversationType" :targetId="targetId" @onSuccess="getMessageList()" /> <sent-message-bar :conversationType="conversationType" :targetId="targetId" @onSuccess="getNewMessage()" />
</view> </view>
</template> </template>
@@ -43,13 +44,24 @@
data() { data() {
return { return {
targetId: '', targetId: '',
conversationType: 3, conversationType: RongIMLib.ConversationType.GROUP,
messages: [], messages: [],
groupInfo: { groupInfo: {
name: '' name: ''
} }
} }
}, },
computed: {
latestMessage() {
if (this.messages.length > 1) {
return this.messages[this.messages.length - 1]
} else {
return {
sentTime: 0
}
}
}
},
onLoad(e) { onLoad(e) {
this.targetId = e.targetId this.targetId = e.targetId
this.groupInfo = this.$store.getters.contactInfo(this.targetId) this.groupInfo = this.$store.getters.contactInfo(this.targetId)
@@ -59,15 +71,11 @@
this.getMessageList() this.getMessageList()
uni.$on('onReceiveMessage', (msg) => { uni.$on('onReceiveMessage', (msg) => {
if (msg.targetId == this.targetId) { if (msg.targetId == this.targetId) {
this.getMessageList() this.getNewMessage()
} }
}) })
uni.$once('cleanGroupMessage', this.getMessageList) uni.$once('cleanGroupMessage', this.getMessageList)
}, },
onBackPress() {
uni.$off('onReceiveMessage')
console.log('Off onReceiveMessage');
},
onNavigationBarButtonTap() { onNavigationBarButtonTap() {
uni.navigateTo({ uni.navigateTo({
url: '/pages/im/group/info?targetId=' + this.targetId url: '/pages/im/group/info?targetId=' + this.targetId
@@ -82,19 +90,31 @@
uni.navigateTo({ uni.navigateTo({
url: '/pages/im/friends/mine?targetId=' + item.senderUserId url: '/pages/im/friends/mine?targetId=' + item.senderUserId
}) })
} else{ } else {
uni.navigateTo({ uni.navigateTo({
url: '/pages/im/friends/info?targetId=' + item.senderUserId 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() { getMessageList() {
im.getMessageList( im.getMessageList(
this.conversationType, this.conversationType,
this.targetId, this.targetId,
new Date().getTime(), 0,
20, 100,
true, true,
(messages) => { (messages) => {
this.messages = messages.reverse() this.messages = messages.reverse()
@@ -103,12 +123,15 @@
}, },
// 滚动到底部 // 滚动到底部
scrollBottom(type) { scrollBottom(type) {
if (this.latestMessage) {
// 清理当前会话,未读消息数量 // 清理当前会话,未读消息数量
RongIMLib.clearMessagesUnreadStatus(this.conversationType, this.targetId, new Date().getTime() + 1100) RongIMLib.clearMessagesUnreadStatus(this.conversationType, this.targetId, this.latestMessage
// 发送消息已读状态给对方 .sentTime)
RongIMLib.sendReadReceiptMessage(this.conversationType, this.targetId, new Date().getTime()) // // 发送消息已读状态给对方
// RongIMLib.sendReadReceiptMessage(this.conversationType, this.targetId, this.latestMessage.sentTime)
// 更新badge提醒数量 // 更新badge提醒数量
im.setNotifyBadge() im.setNotifyBadge()
}
setTimeout(() => { setTimeout(() => {
let el = this.$refs.chatBottom let el = this.$refs.chatBottom

View File

@@ -2,8 +2,11 @@
<view class="container"> <view class="container">
<view class="members u-border-bottom"> <view class="members u-border-bottom">
<view class="users"> <view class="users">
<view class="user" v-for="(item, index) in users" :key="index" @click="toUser(item)"> <view :class="['user', {'active': item.targetId == currentUser.targetId}]"
<u-avatar size="44" shape="square" :src="item.portraitUrl"></u-avatar> v-for="(item, index) in users" :key="index" @click="toUser(item)"
@longpress="showUserActionSheet(item)">
<u-avatar size="44" shape="square" :src="item.portraitUrl">
</u-avatar>
<view class="name">{{ item.name }}</view> <view class="name">{{ item.name }}</view>
</view> </view>
<view class="user" v-if="group.can_invite"> <view class="user" v-if="group.can_invite">
@@ -57,6 +60,10 @@
<u-action-sheet @select="doAction" :actions="joinTypeMap" cancelText="取消" :show="showActions" <u-action-sheet @select="doAction" :actions="joinTypeMap" cancelText="取消" :show="showActions"
@close="showActions=false"> @close="showActions=false">
</u-action-sheet> </u-action-sheet>
<u-action-sheet @select="handleUserAction" :actions="userActionMap" cancelText="取消" :show="showUserAction"
@close="hideUserAction">
</u-action-sheet>
</view> </view>
</template> </template>
@@ -65,7 +72,12 @@
getGroupInfo, getGroupInfo,
updateGroup, updateGroup,
quitGroup, quitGroup,
dismissGroup removeGroupUser,
setGroupAdmin,
removeGroupAdmin,
dismissGroup,
transferGroupOwner,
getGroupUsers
} from '@/apis/interfaces/im.js' } from '@/apis/interfaces/im.js'
import { import {
uploads uploads
@@ -90,7 +102,24 @@
qrContent: 'JOINGROUP|', qrContent: 'JOINGROUP|',
joinType: '', joinType: '',
joinTypeMap: [], joinTypeMap: [],
showActions: false showActions: false,
showUserAction: false,
userActionMap: [{
key: 0,
name: '移除成员',
disabled: false
}, {
key: 1,
name: '设置管理',
disabled: true
},
{
key: 2,
name: '转移群主',
disabled: true
}
],
currentUser: {}
} }
}, },
onLoad(e) { onLoad(e) {
@@ -110,18 +139,23 @@
} }
}) })
this.initData() this.initData()
this.initUsers()
uni.$on('groupAnnouncementCreated', this.initData) uni.$on('groupAnnouncementCreated', this.initData)
}, },
onUnload() { onUnload() {
uni.$off('groupAnnouncementCreated') uni.$off('groupAnnouncementCreated')
}, },
methods: { methods: {
initUsers() {
getGroupUsers(this.targetId, 14).then(res => {
this.users = res
})
},
initData() { initData() {
getGroupInfo(this.targetId).then(res => { getGroupInfo(this.targetId).then(res => {
this.group = res.group this.group = res.group
this.groupName = res.group.name this.groupName = res.group.name
this.announcement = res.announcement this.announcement = res.announcement
this.users = res.users
this.members = res.members this.members = res.members
this.loaded = true this.loaded = true
this.joinTypeMap = res.join_type_map.map(item => { this.joinTypeMap = res.join_type_map.map(item => {
@@ -132,6 +166,7 @@
return item.key == res.join_type return item.key == res.join_type
})[0].name })[0].name
}).catch(err => { }).catch(err => {
console.log('getGroupInfo ERR', err);
uni.showToast({ uni.showToast({
icon: 'none', icon: 'none',
title: '群不存在' title: '群不存在'
@@ -167,7 +202,9 @@
} }
}, },
inviteUser() { inviteUser() {
uni.navigateTo({
url: '/pages/im/group/invite?targetId=' + this.targetId
})
}, },
loadMore() { loadMore() {
uni.navigateTo({ uni.navigateTo({
@@ -209,7 +246,6 @@
}, },
// 修改群头像 // 修改群头像
onGroupAvatar() { onGroupAvatar() {
console.log('onGroupAvatar');
uni.chooseImage({ uni.chooseImage({
count: 1, count: 1,
sourceType: ['album'], sourceType: ['album'],
@@ -325,13 +361,85 @@
icon: 'none', icon: 'none',
title: '退出群聊成功' title: '退出群聊成功'
}) })
// 移除指定的会话
RongIMLib.removeConversation(this.conversationType, this.targetId)
uni.switchTab({ uni.switchTab({
url: '/pages/im/index' url: '/pages/im/index'
}) })
}).catch(err => {
console.log(err);
}) })
} }
} }
}) })
},
showUserActionSheet(item) {
this.currentUser = item
// 只有管理员以上才会弹窗
if (this.group.is_admin) {
if (this.group.is_owner) {
this.userActionMap.map((item) => {
item.disabled = false
return item
})
}
this.showUserAction = true
}
},
hideUserAction() {
this.currentUser = {}
this.showUserAction = false
},
handleUserAction(e) {
switch (e.key) {
case 0:
removeGroupUser(this.targetId, this.currentUser.targetId).then(res => {
uni.showToast({
icon: 'none',
title: '成员移除成功'
})
}).catch(err => {
uni.showToast({
icon: 'none',
title: err.message
})
}).finally(() => {
this.initUsers()
})
break;
case 1:
setGroupAdmin(this.targetId, this.currentUser.targetId).then(res => {
uni.showToast({
icon: 'none',
title: '设置管理成功'
})
}).catch(err => {
uni.showToast({
icon: 'none',
title: err.message
})
}).finally(() => {
this.initUsers()
})
break;
case 2:
transferGroupOwner(this.targetId, this.currentUser.targetId).then(res => {
uni.showToast({
icon: 'none',
title: '群主转让成功'
})
}).catch(err => {
uni.showToast({
icon: 'none',
title: err.message
})
}).finally(() => {
this.initUsers()
this.initData()
})
break;
}
} }
} }
} }
@@ -367,6 +475,10 @@
justify-content: center; justify-content: center;
align-items: center; align-items: center;
&.active {
background-color: $window-color;
}
.name { .name {
color: $text-gray-m; color: $text-gray-m;
width: 126rpx; width: 126rpx;

21
pages/im/group/invite.vue Normal file
View File

@@ -0,0 +1,21 @@
<template>
<view class="">
</view>
</template>
<script>
export default {
data() {
return {
targetId: ''
}
},
onLoad(e) {
this.targetId = e.targetId
}
}
</script>
<style>
</style>

View File

@@ -7,7 +7,8 @@
<text class="text">{{ customCN(item.sentTime) }}</text> <text class="text">{{ customCN(item.sentTime) }}</text>
</view> </view>
<view class="cell-item" :class="item.messageDirection == 1 ? 'right' : 'left'"> <view class="cell-item" :class="item.messageDirection == 1 ? 'right' : 'left'">
<u-avatar class="avatar" size="36" shape="square" @click="showUser(targetId, item.messageDirection)" :src="userInfo.portraitUrl" /> <u-avatar class="avatar" size="36" shape="square" @click="showUser(targetId, item.messageDirection)"
:src="userInfo.portraitUrl" />
<view class="msg"> <view class="msg">
<show-voice v-if="item.objectName === 'RC:HQVCMsg'" :guest="item.messageDirection == 1" <show-voice v-if="item.objectName === 'RC:HQVCMsg'" :guest="item.messageDirection == 1"
:msg="item.content" /> :msg="item.content" />
@@ -23,7 +24,7 @@
</cell> </cell>
<cell class="cell-footer" ref="chatBottom"></cell> <cell class="cell-footer" ref="chatBottom"></cell>
</list> </list>
<sent-message-bar :conversationType="conversationType" :targetId="targetId" @onSuccess="getMessageList()" /> <sent-message-bar :conversationType="conversationType" :targetId="targetId" @onSuccess="getNewMessage()" />
</view> </view>
</template> </template>
@@ -41,6 +42,12 @@
const ChatList = uni.requireNativePlugin('dom') const ChatList = uni.requireNativePlugin('dom')
export default { export default {
components: {
sentMessageBar,
showVoice,
showImage,
showText
},
data() { data() {
return { return {
targetId: '', targetId: '',
@@ -53,11 +60,16 @@
} }
} }
}, },
components: { computed: {
sentMessageBar, latestMessage() {
showVoice, if (this.messages.length > 1) {
showImage, return this.messages[this.messages.length - 1]
showText } else {
return {
sentTime: 0
}
}
}
}, },
onLoad(e) { onLoad(e) {
this.targetId = e.targetId this.targetId = e.targetId
@@ -70,30 +82,48 @@
// 监听消息已读状态 // 监听消息已读状态
uni.$on('onReadReceiptReceived', (data) => { uni.$on('onReadReceiptReceived', (data) => {
if (data.targetId == this.targetId) { if (data.targetId == this.targetId) {
this.getMessageList() this.messages = this.messages.map((item) => {
if (item.messageDirection == 1 && item.sentStatus == 30 && item.receivedTime <
data.messageTime + 1000) {
item.sentStatus = 50
}
return item
})
} }
}) })
// 监听收到新消息,判断是否是当前会话,更新会话内容 // 监听收到新消息,判断是否是当前会话,更新会话内容
uni.$on('onReceiveMessage', (msg) => { uni.$on('onReceiveMessage', (msg) => {
if (msg.targetId == this.targetId) { if (msg.targetId == this.targetId) {
this.getMessageList() this.getNewMessage()
} }
}) })
}, },
onBackPress() { onUnload() {
uni.$off('onReceiveMessage') uni.$off('onReadReceiptReceived')
}, },
methods: { methods: {
customCN(val) { customCN(val) {
return timeCustomCN(val) return timeCustomCN(val)
}, },
getNewMessage() {
im.getMessageList(
this.conversationType,
this.targetId,
this.latestMessage.sentTime || 0,
1,
false,
(messages) => {
this.messages = this.messages.concat(messages)
this.scrollBottom()
})
},
// 获取消息列表 // 获取消息列表
getMessageList() { getMessageList() {
im.getMessageList( im.getMessageList(
this.conversationType, this.conversationType,
this.targetId, this.targetId,
new Date().getTime(), 0,
10, 100,
true, true,
(messages) => { (messages) => {
this.messages = messages.reverse() this.messages = messages.reverse()
@@ -109,12 +139,15 @@
}, },
// 滚动到底部 // 滚动到底部
scrollBottom(type) { scrollBottom(type) {
if (this.latestMessage) {
// 清理当前会话,未读消息数量 // 清理当前会话,未读消息数量
RongIMLib.clearMessagesUnreadStatus(this.conversationType, this.targetId, new Date().getTime() + 1100) RongIMLib.clearMessagesUnreadStatus(this.conversationType, this.targetId, this.latestMessage
// 发送消息已读状态给对方 .sentTime)
RongIMLib.sendReadReceiptMessage(this.conversationType, this.targetId, new Date().getTime()) // // 发送消息已读状态给对方
RongIMLib.sendReadReceiptMessage(this.conversationType, this.targetId, this.latestMessage.sentTime)
// 更新badge提醒数量 // 更新badge提醒数量
im.setNotifyBadge() im.setNotifyBadge()
}
setTimeout(() => { setTimeout(() => {
let el = this.$refs.chatBottom let el = this.$refs.chatBottom
ChatList.scrollToElement(el, { ChatList.scrollToElement(el, {

View File

@@ -4,7 +4,7 @@
<view class="list"> <view class="list">
<view class="list-item" @click="updImgs"> <view class="list-item" @click="updImgs">
<view class="list-item-left"> <span>修改头像</span> </view> <view class="list-item-left"> <span>修改头像</span> </view>
<view class="avatar" > <view class="avatar">
<image :src="avatar.showPath || require('@/static/user/cover.png')" mode="aspectFill" /> <image :src="avatar.showPath || require('@/static/user/cover.png')" mode="aspectFill" />
<u-icon name="arrow-right" color="#999" size="20"></u-icon> <u-icon name="arrow-right" color="#999" size="20"></u-icon>
</view> </view>
@@ -67,8 +67,10 @@
updImgs(type) { updImgs(type) {
uni.chooseImage({ uni.chooseImage({
crop: { crop: {
width: 80, quality: 100,
height: 80 width: 128,
height: 128,
resize: true
}, },
success: res => { success: res => {
let path = res.tempFiles.map((val, index) => { let path = res.tempFiles.map((val, index) => {
@@ -80,7 +82,7 @@
uploads(path).then(pathRes => { uploads(path).then(pathRes => {
this.avatar.path = pathRes.path[0] this.avatar.path = pathRes.path[0]
this.avatar.showPath = pathRes.url[0] this.avatar.showPath = pathRes.url[0]
this.resetUserInfo('avatar',pathRes.url[0]) this.resetUserInfo('avatar', pathRes.url[0])
}).catch(err => { }).catch(err => {
uni.showToast({ uni.showToast({
title: err.message, title: err.message,
@@ -93,19 +95,19 @@
}) })
}, },
// 修改姓名 // 修改姓名
blur(e){ blur(e) {
let value = e.detail.value let value = e.detail.value
if(value !== this.nickname){ if (value !== this.nickname) {
this.resetUserInfo('nickname',value) this.resetUserInfo('nickname', value)
} }
}, },
// 修改头像或昵称 // 修改头像或昵称
resetUserInfo(key, value) { resetUserInfo(key, value) {
let data ={ let data = {
key:key, key: key,
value:value value: value
} }
resetUserInfo(data).then(res=>{ resetUserInfo(data).then(res => {
uni.showToast({ uni.showToast({
title: res, title: res,
icon: 'none' icon: 'none'

View File

@@ -105,9 +105,11 @@ class userAuth {
openid: authResult.authResult.openid openid: authResult.authResult.openid
}).then(res => { }).then(res => {
uni.closeAuthView() uni.closeAuthView()
store.commit('setToken', res.token_type + ' ' + res.access_token) store.commit('setToken', res.token_type + ' ' + res
.access_token)
// 在这里登录成功链接IM服务 // 在这里登录成功链接IM服务
getImToken().then(res => { getImToken().then(res => {
console.log('在这获取IM-TOKEN', res);
im.connect(res.token, res.userInfo) im.connect(res.token, res.userInfo)
}) })
resolve() resolve()

View File

@@ -6,7 +6,8 @@ import listeners from './listeners.js'
import { import {
getFriends, getFriends,
getUserInfo, getUserInfo,
getImToken getImToken,
getMyGroups
} from '@/apis/interfaces/im.js' } from '@/apis/interfaces/im.js'
const initIm = (KEY) => { const initIm = (KEY) => {
@@ -58,7 +59,7 @@ const connect = (token, userInfo, callback) => {
// 设置未读消息数量 // 设置未读消息数量
setNotifyBadge() setNotifyBadge()
// 首次运行获取好友列表 // 首次运行获取好友列表
const FK = 'IFT_' + userInfo.targetId const FK = 'ZHK_' + userInfo.targetId
uni.getStorage({ uni.getStorage({
key: FK, key: FK,
@@ -71,11 +72,13 @@ const connect = (token, userInfo, callback) => {
}) })
}, },
fail: () => { fail: () => {
// 程序是首次运行,初始化加载好友信息 // 程序是首次运行,初始化加载好友和群组信息
getFriends().then(res => { Promise.all([getFriends(), getMyGroups()]).then(result => {
res.map(item => { result.map(contacts => {
contacts.map(item => {
store.dispatch('initContact', item) store.dispatch('initContact', item)
}) })
})
uni.setStorageSync(FK, userInfo.targetId) uni.setStorageSync(FK, userInfo.targetId)
}) })
} }

View File

@@ -22,7 +22,7 @@ const getMessageList = (conversationType, targetId, timeStamp, count, isForward,
conversationType, conversationType,
targetId, targetId,
objectNames, objectNames,
timeStamp + 1000, timeStamp,
count, count,
isForward, isForward,
({ ({