uni-id-users.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 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 112 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
// schema扩展相关文档请参阅:https://uniapp.dcloud.net.cn/uniCloud/jql-schema-ext.html
const db = uniCloud.database();
const dbCmd = db.command
module.exports = {
	trigger: {
		async afterReadAsSecondaryCollection(e) {
			await afterReadAction(e, true)
		},
		async afterRead(e) {
			await afterReadAction(e)
		}
	}
}

async function afterReadAction({
	field,
	result,
	userInfo: currentUserInfo,
	primaryCollection
} = {}, asSecondaryCollection = false) {
  let isAdmin = currentUserInfo.role.includes('uni-im-admin') || currentUserInfo.role.includes('admin') || currentUserInfo.role.includes('staff')
  
  // 非管理员,只能查看自己的邮箱
  if(
    field.includes("email") && !isAdmin &&
    (result.data.length != 1 || result.data[0]._id != currentUserInfo.uid)
  ){
    result.data = result.data.map(item => {
      item.email = ""
      return item
    })
    console.log('非管理员,只能查看自己的邮箱')
  }
  
	if (result && field && field.includes("nickname")) {
		//uni-im 处理查询nickname,但值为空的情况
		let {
			data
		} = result
		// 为联查 且为副表时的字段
    // console.log('data',data);
		if (asSecondaryCollection && typeof(data[0]) == "object") {
			let foreignKeysObj = {
				"uni-im-group-member":"user_id",
				"uni-im-friend":"friend_uid"
			}
			let foreignKey = foreignKeysObj[primaryCollection]
			
			if(foreignKey in data[0]){
				data = data.map(item => item[foreignKey][0])
        data = data.filter(item => item != undefined)
			}else{
				return console.log('触发器uni-id-users.schema.ext.js,未在当前操作生效。如需应用此触发器,请补充:主表表名及其foreignKey的值,到触发器uni-id-users.schema.ext.js的变量foreignKeysObj内');
			}
		}

		// console.log('data',data);
		if (!Array.isArray(data)) {
			data = [data]
		}
		// 记录没有nickname的用户id
		let user_ids = [],
			usersInfo = {};
		data.forEach(item => {
			if (item && !item.nickname) {
				user_ids.push(item._id)
			}
		})
		if (user_ids.length) {
			console.info('注意:uni-im项目用户数据依赖nickname。有' + user_ids.length +
				`个用户数据 nickname 的值为空,已多执行一次数据库查询:将此用户的“用户名”或“邮箱”或“手机号”脱敏后,作为nickname输出。请引导用户完善nickname,减少查库次数`)
			let res = await db.collection('uni-id-users')
				.where({
					_id: dbCmd.in(user_ids)
				})
				.field({
					username: true,
					email: true,
					mobile: true
				})
				.limit(1000)
				.get()
			usersInfo = res.data.reduce((sum, current) => {
				sum[current._id] = current
				return sum
			}, {})
		}
    
		data.forEach(item => {
			if (!item.nickname) {
				let userInfo = usersInfo[item._id]
				// 管理员可以看到不打码的
				if (isAdmin) {
					item.nickname = userInfo.username || userInfo.email || userInfo.mobile
				}else{
					item.nickname = hideUsernameStr(userInfo.username) || hideEmailStr(userInfo.email) ||
											hideMobileStr(userInfo.mobile)
				}
			}else if(!isAdmin && item.nickname.includes('@')){
				// 禁止昵称用邮箱 脱敏处理
				item.nickname = hideEmailStr(item.nickname)
			}

			// 特殊处理崔博
			if (item._id == "b9839630-a479-11ea-b772-0f6ad6cf835c") {
				item.nickname = "DCloud地图服务"
				item.avatar_file = {
					url: "https://dcloud-chjh-web.oss-cn-hangzhou.aliyuncs.com/ext/uni-im/uni%402x.png"
				}
			}

		})

		function hideUsernameStr(username) {
			if (username == undefined) {
				return false
			}
			let length = username.length
			let n = parseInt(length / 2.5) * 2
			return username.substr(0, length - n) + '**' + username.substr(-1 * n / 2)
		}

		function hideEmailStr(email) {
			if (email == undefined) {
				return false
			}
			const content = email.split("@")
			return content[0].substr(0, content[0].length - 2) + '**' + content[1]
		}

		function hideMobileStr(mobile) {
			if (mobile == undefined) {
				return false
			}
			return mobile.substr(0, 3) + '****' + mobile.substr(-1 * 4)
		}
	}
}