GroupMember.class.js 2.3 KB
Newer Older
DCloud_JSON's avatar
3.4.31  
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
import CloudData from '@/uni_modules/uni-im/sdk/ext/CloudData.class.js'
import $users from '@/uni_modules/uni-im/sdk/methods/users.js';
import $extensions from '@/uni_modules/uni-im/sdk/methods/extensions.js';
const dbJQL = uniCloud.databaseForJQL()
const dbJQLCommand = dbJQL.command
const $ = dbJQL.command.aggregate

export default class GroupMember extends CloudData {
  constructor({group_id}={}) {
    super()
    this.group_id = group_id
    this.indexKey = 'users._id'
    this.needLoadOnce = true
  }
  __beforeAdd(datas){
    // console.log('GroupMember beforeAdd',datas);
    // 调用扩展点中的方法
    // $extensions.invokeExts('before-add-group-member', datas)
    
    // 为了方便使用,将用户信息缓存到全局
    const userInfoObj = datas.reduce((obj,item) => {
      obj[item.users._id] = item.users
      return obj
    },{})
    $users.merge(userInfoObj)
  }
  findByUid(uid) {
    console.warn('member.findByUid 已经过期,去直接member.find(uid)查找');
    return this.dataList.find(item => item.users._id === uid)
  }
  __beforeFind(param){
    if (typeof param === 'string'){
      // 设置为默认按users._id查找群成员,而不是按_id查找
      return {
        users:{
          _id:param
        }
      }
    }else{
      return param
    }
  }
  async __get() {
    // console.error('pull GroupMember Data param',this.group_id);
    let _where = `"group_id" == "${this.group_id}"`
    if (this.lastItem) {
      const {active_time,_id} = this.lastItem
      _where += ` && !(role in ["admin"]) && ("active_time" < ${active_time} || ("active_time" == ${active_time} && "_id" < "${_id}"))`
    }
    let res = await dbJQL.collection(
        dbJQL.collection('uni-im-group-member')
            .where(_where)
            .orderBy("role desc,active_time desc,_id desc")
            .limit(this.loadLimit)
            .getTemp(),
        dbJQL.collection('uni-id-users').field('_id,nickname,avatar_file,realname_auth').getTemp()
      )
      .get()
    // console.error('拉取到群成员数据',_where,res.data.length,length);
    this.lastItem = res.data[res.data.length - 1]
    return res.data.map(item => {
      const usersInfo = item.user_id[0];
      delete item.user_id
      item.users = usersInfo
      return item
    })
  }
}