From 794a3516d2a15539396073dc00e71633b7e6b290 Mon Sep 17 00:00:00 2001 From: DCloud_JSON Date: Thu, 25 Apr 2024 17:41:37 +0800 Subject: [PATCH] =?UTF-8?q?=E6=96=B0=E5=A2=9E=20=E4=BB=85=E9=99=90?= =?UTF-8?q?=EF=BC=9A=E5=A5=BD=E5=8F=8B=E4=B9=8B=E9=97=B4=E3=80=81=E7=B3=BB?= =?UTF-8?q?=E7=BB=9F=E7=AE=A1=E7=90=86=E5=91=98=E5=8F=82=E4=B8=8E=E7=9A=84?= =?UTF-8?q?=E3=80=81=E7=BE=A4=E6=88=90=E5=91=98=E4=B8=8E=E7=BE=A4=E7=AE=A1?= =?UTF-8?q?=E7=90=86=E5=91=98=EF=BC=8C=E5=8F=91=E8=B5=B7=E7=A7=81=E8=81=8A?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- uniCloud/cloudfunctions/uni-im-co/msg.js | 82 ++++++++++++++++++++++-- 1 file changed, 75 insertions(+), 7 deletions(-) diff --git a/uniCloud/cloudfunctions/uni-im-co/msg.js b/uniCloud/cloudfunctions/uni-im-co/msg.js index 2fcf54d..7362ae9 100644 --- a/uniCloud/cloudfunctions/uni-im-co/msg.js +++ b/uniCloud/cloudfunctions/uni-im-co/msg.js @@ -16,10 +16,10 @@ const createConfig = require("uni-config-center"); const uniImConfig = createConfig({ pluginId: 'uni-im', // 插件id }) +const conversation_grade = uniImConfig.config('conversation_grade') // 发送消息方法(含:单聊和群聊) async function sendMsg(params) { - // console.error('sendMsg params~~~~~', params); await _beforeSendMsgActions.call(this, params) let { @@ -58,7 +58,8 @@ async function sendMsg(params) { appId: ["String"], about_msg_id: ['String'], action: ['String'], - is_mute: ['Boolean'] + is_mute: ['Boolean'], + chat_source: ['Object'] } }) @@ -160,7 +161,7 @@ async function sendMsg(params) { // 创建新会话或者更新已有会话。 // 拿到消息接收者的isMute状态(仅私聊有效) - let {isMute} = await _createOrUpdateConversation.call(this,conversation_id, msgData) + let {isMute} = await _createOrUpdateConversation.call(this,conversation_id, msgData,params.chat_source) // console.log({ // ...msgData, // conversation_id @@ -303,8 +304,6 @@ async function _checkConversationGrade({ * 100 - 客服 or 好友或者群成员 * 200 - 必须是好友或者群成员 **/ - const conversation_grade = uniImConfig.config('conversation_grade') - switch (conversation_grade) { case 0: //任何人可以发起会话,不校验 @@ -375,7 +374,7 @@ function _getLastMsgNote({ return last_msg_note } -async function _createOrUpdateConversation(conversation_id, msgData) { +async function _createOrUpdateConversation(conversation_id, msgData, chat_source) { // 设置会话 最后一条消息 的描述 let last_msg_note = _getLastMsgNote(msgData) @@ -412,13 +411,82 @@ async function _createOrUpdateConversation(conversation_id, msgData) { let getUserInfoRes = await db.collection('uni-id-users') .doc(this.current_uid) .field({ - mobile_confirmed:1 + email:1, + nickname:1, + role:1 }) .get() if(!getUserInfoRes.data[0].mobile_confirmed){ throw new Error('账号未绑定手机号,无法发送消息') } } + const clientInfo = this.getClientInfo() + if( conversation_grade === 300 && msgData.to_uid && clientInfo.source != 'function' && this.current_uid != 'system'){ + // console.error('createOrUpdateConversation conversation_grade 300~~~~~',clientInfo.appVersionCode); + // TODO:用于向下兼容 + if(clientInfo.appVersionCode < 24042501){ + throw new Error('你的客户端版本过旧,不能发起新会话。请刷新后重试') + } + // 仅限:系统管理员参与的 或 群成员向群管理员 发起私聊 + let check = false + // 消息发送者是系统管理员 + if(this.current_user_role.includes('staff')){ + check = true + // console.error('createOrUpdateConversation staff~~~~~'); + } else if(chat_source && chat_source.group_id){ + // console.error('createOrUpdateConversation group_id~~~~~'); + // 临时会话来源群,判断双方是否有一个是群管理员 + let { + data: [member], + } = await dbUniImGroupMember + .where({ + group_id: chat_source.group_id, + user_id: dbCmd.in([msgData.from_uid, msgData.to_uid]), + role: dbCmd.in(['admin']) + }) + .get() + if(member){ + check = true + // console.error('createOrUpdateConversation member~~~~~', member); + } + }else { + // 判断消息接收者是否为系统管理员(staff) + let { + data: [receiver], + } = await db.collection('uni-id-users') + .doc(msgData.to_uid) + .field({ + role:1, + nickname:1, + avatar_file:1, + }) + .get() + // console.error('createOrUpdateConversation receiver~~~~~', receiver); + if(receiver && receiver.role?.includes('staff')){ + // console.error('createOrUpdateConversation receiver.staff~~~~~'); + check = true + } + } + + if(!check){ + let { + data: [has] + } = await dbUniImFriend + .where({ + friend_uid: msgData.to_uid, + user_id: this.current_uid + }) + .get() + if(has){ + check = true + } + } + + if(!check){ + throw new Error('仅限:好友之间、系统管理员参与的、群成员与群管理员,发起私聊') + } + } + // 1.消息发送者 会话数据 senderConversation = { id: conversation_id, -- GitLab