uni-im-friend.schema.ext.js 5.8 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
const { getConversationId } = require('uni-im-utils')

// schema扩展相关文档请参阅:https://uniapp.dcloud.net.cn/uniCloud/jql-schema-ext.html
const db = uniCloud.database();
module.exports = {
	trigger: {
		async afterCreate({
			addDataList,
			clientInfo
		}) {
			if (addDataList.length === 1) {
				// a加b为好友 自动 将b加a为好友
				let [data] = addDataList
				let {
					friend_uid,
					user_id
				} = data
				data.friend_uid = user_id
				data.user_id = friend_uid
				
				const dbJQL = uniCloud.databaseForJQL({
					clientInfo
				})
				
				let {data:userList} = await dbJQL.collection('uni-id-users').where({
DCloud_JSON's avatar
3.4.31  
DCloud_JSON 已提交
26
					"_id":dbJQL.command.in([friend_uid,user_id])
DCloud_JSON's avatar
DCloud_JSON 已提交
27 28 29 30 31 32 33 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
				})
				.field('_id,nickname,avatar_file')
				.get()
				
				let friendInfo = userList.filter(i=>i._id == friend_uid)[0]
				let userInfo = userList.filter(i=>i._id == user_id)[0]
				
				// console.log(12313132131,{userList,friendInfo,userInfo})
				
				// console.log('do uni-im-friend afterCreate');
				// console.log(data);
				let res = await db.collection('uni-im-friend').add(data)
				// console.log('1231', res);
				
				dbJQL.setUser({
					uid: user_id, // 建议此处使用真实uid
					role: ['admin'], // 指定当前执行用户的角色为admin。如果只希望指定为admin身份,可以删除uid和permission节点
					permission: []
				})
				let conversationId = getConversationId({
					from_uid: user_id,
					to_uid: friend_uid,
				})
				res = await db.collection('uni-im-conversation')
						.where({id:conversationId,friend_uid})
						.get()
				// 查询是否存在,防止未加好友时发起过会话重复创建
				if(res.data.length === 0){
					res = await dbJQL.collection('uni-im-conversation').add({
						id: conversationId,
						user_id,
						friend_uid,
						type: 1,
						unread_count: 0
					})
				}else{
					console.log('会话已存在')
				}
				
				
				//发消息通知 被邀请加好友的用户
				let pushParam = {
					"user_id": friend_uid, 
					"payload": {
						type: "uni-im-notification",
						subType: "uni-im-friend-add",
						// avatar_file: userInfo.avatar_file, // 头像或图标的图片地址,支持Base64
						data: {
							from_uid: user_id,
							to_uid: friend_uid,
						},
						unique:[user_id,friend_uid].sort().join('_')
					},
					title:"成功加为好友通知", 
					content:'你已成功添加"'+ userInfo.nickname +'"为好友',
				}
				// console.log(123, pushParam);
				const uniImCo = uniCloud.importObject("uni-im-co")
				res = await uniImCo.sendPushMsg(pushParam,clientInfo.appId)
				// console.log(8989989, res);
				pushParam.user_id = user_id
				pushParam.content = '你已成功添加"'+ friendInfo.nickname +'"为好友'
				res = await uniImCo.sendPushMsg(pushParam,clientInfo.appId)
			} else {
				throw new Error('非法参数')
			}
		},
		async beforeDelete({
			where,
			clientInfo
		}) {
			console.log('where',{where});
			if (where && where.friend_uid) {
				let {
					data: [friendData]
				} = await db.collection('uni-im-friend').where(where).get()
				if (friendData) {
					let {
						friend_uid,
						user_id
					} = friendData
					const dbJQL = uniCloud.databaseForJQL({
						clientInfo
					})
					let {data:userList} = await dbJQL.collection('uni-id-users').where({
DCloud_JSON's avatar
3.4.31  
DCloud_JSON 已提交
112
						"_id":dbJQL.command.in([friend_uid,user_id])
DCloud_JSON's avatar
DCloud_JSON 已提交
113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183
					})
					.field('_id,nickname,avatar_file')
					.get()
					let friendInfo = userList.filter(i=>i._id == friend_uid)[0]
					let userInfo = userList.filter(i=>i._id == user_id)[0]
					
					// console.log(12313132131,{userList,friendInfo,userInfo})
					
					let res = await db.collection('uni-im-friend')
						.where({
							friend_uid: user_id,
							user_id: friend_uid
						})
						.remove()
					console.log('同步删除好友',res);
					
					dbJQL.setUser({
						uid: user_id, // 建议此处使用真实uid
						role: ['admin'], // 指定当前执行用户的角色为admin。如果只希望指定为admin身份,可以删除uid和permission节点
						permission: []
					})
					
					let conversationId = getConversationId({
						from_uid: user_id,
						to_uid: friend_uid,
					})
					res = await dbJQL.collection('uni-im-conversation')
									.where({
										"id": conversationId
									})
									.remove()
					console.log('同步删除相关会话',res);
					//发消息通知 用户成功删除好友
					let pushParam = {
						"user_id": friend_uid, //群创建人id,后续升级为所有群管理员id
						"payload": {
							type: "uni-im-notification", // im消息通知,比如加好友请求,有用户退群等
							subType: "uni-im-friend-delete", // 通知子类型(可选)
							// avatar_file: userInfo.avatar_file, // 头像或图标的图片地址,支持Base64
							data: {
								conversationId,
								from_uid:user_id,
								to_uid:friend_uid
							},
							unique:[user_id,friend_uid].sort().join('_')
						},
						title:"好友关系解除通知",
						content:'"'+ userInfo.nickname +'"与你解除好友关系'
					}
					// console.log(123, pushParam);
					const uniImCo = uniCloud.importObject("uni-im-co")
					res = await uniImCo.sendPushMsg(pushParam,clientInfo.appId)
					// console.log(8989989, res);
					pushParam.user_id = user_id
					pushParam.content = '你已成功与"'+ friendInfo.nickname +'"解除好友关系'
					res = await uniImCo.sendPushMsg(pushParam,clientInfo.appId)
					// console.log(8989989, res);
				} else {
					throw new Error('不是好友')
				}
			} else {
				throw new Error('非法参数1')
			}
		},
		async afterRead({result}){
			result.data.forEach(item=>{
				let friendInfo = item.friend_uid[0]
			})
		}
	}
}