uni-im-group.schema.ext.js 4.0 KB
Newer Older
DCloud_JSON's avatar
DCloud_JSON 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
// schema扩展相关文档请参阅:https://uniapp.dcloud.net.cn/uniCloud/jql-schema-ext.html
const db = uniCloud.database();
const uniImCo = uniCloud.importObject("uni-im-co")
module.exports = {
	trigger: {
    async beforeUpdate({where,updateData,clientInfo,docId,userInfo}){
      const group_id = where._id || docId;
      if(!group_id){
        throw new Error('jql方式更新uni-im-group,️必须传入群ID')
      }
      
      // 判断是不是此群的管理员
      let getGroupMemberRes = await db.collection('uni-im-group-member').where({
        group_id,
        user_id:userInfo.uid
      }).get()
      console.log('getGroupMemberRes',getGroupMemberRes)
      const [groupMember] = getGroupMemberRes.data
      if(!groupMember){
        throw Error('非法操作,不是群成员')
      }else if(!groupMember.role.includes('admin')){
        throw Error('非法操作,不是管理员')
      }
     
      let field = Object.keys(updateData)[0]
      if(field == 'notification'){
        // 如果是群公告,增加一个公告的创建时间
        updateData[field].create_time = Date.now()
29 30 31 32 33
        await db.collection('uni-im-conversation').where({
          group_id,
        }).update({
          has_unread_group_notification: true
        })
DCloud_JSON's avatar
DCloud_JSON 已提交
34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115
      }
    },
    async afterUpdate({where,updateData,clientInfo,userInfo}){
      let field = Object.keys(updateData)[0]
      let fieldsName = {
        "avatar_file":"群聊头像",
        "name":"群聊名称",
        "introduction":"群简介",
        "notification":"群公告",
        "mute_all_members":"全群禁言"
      }[field];
      if(!fieldsName){
        return console.log('字段:'+field+'已更新,未配置为推送绑定字段。无需通知所有群成员');
      }
      const group_id = where._id
      if(field === 'mute_all_members'){
        // 群成员表中增加一个字段,记录禁言状态,这样就不需要再查群信息表了
        await db.collection('uni-im-group-member').where({
          group_id,
        }).update({
          // 禁言程度:0-正常,1-被单独禁言或被全群禁言 2-被单独禁言且被全群禁言
          mute_type: updateData[field] ? 1 : 0 // db.command.inc(updateData[field] ? 1 : -1) // TODO未知原因,暂时先用1和0代替
        })
      }
      
      // 通知所有群成员,群资料更新
      const msgData = {
        action:"update-group-info-"+field,
        body:{updateData,fieldsName},
      	type:"system",
      	create_time: Date.now(),
      	conversation_id:"group_"+group_id,
      	group_id,
        appId:clientInfo.appId
      }
      await uniImCo.sendMsg(msgData,userInfo.uid)
    },
		async beforeDelete({where,userInfo,triggerContext}){
			if(!triggerContext){
				throw new Error('执行删除群失败!你的HBuilderX版本过低,请使用3.6.16及以上版本')
			}
			
			let {data:[groupInfo]} = await db.collection('uni-im-group').doc(where._id).get()
			triggerContext.groupInfo = groupInfo
			console.log('beforeDelete',where,groupInfo);
			let {data:[has]} = await db.collection('uni-im-conversation')
										.where({
											group_id:where._id,
											user_id:userInfo._id
										})
										.get()
			if(!has||!groupInfo){
				throw new Error('限群创建者操作')
			}
		},
		async afterDelete({where,clientInfo,userInfo,triggerContext}){
			console.log('beforeDelete',where,triggerContext);
			const group_id = where._id
      
      //使用软删除,否则用户就收不到解散群的通知
      let res = await db.collection('uni-im-group-member')
        .where({group_id})
        .update({"is_delete":true})
        
      console.log('beforeDelete',res);

      // 通知所有群成员,有用户离开群
      const msgData = {
        action:"group-dissolved",
      	type:"system",
      	create_time: Date.now(),
      	conversation_id:"group_"+group_id,
      	group_id,
        appId:clientInfo.appId,
        body:{
          groupName:triggerContext.groupInfo.name,
        }
      }
      await uniImCo.sendMsg(msgData,userInfo.uid)
		}
	}
}