Friend.class.js 1.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
import CloudData from '@/uni_modules/uni-im/sdk/ext/CloudData.class.js'
import $state from '../state/index.js';
const dbJQL = uniCloud.databaseForJQL()
const dbJQLCommand = dbJQL.command
const $ = dbJQL.command.aggregate
export default class Friend extends CloudData {
  __beforeAdd(friendList) {
    // 将好友信息存储到users中
    friendList.forEach(item => $state.users[item._id] = item)
    return friendList
  }
  async __get(param = {}) {
    const {friend_uid} = param
    const limit = this.loadLimit
    let whereString = '"user_id" == $cloudEnv_uid'
    if (friend_uid) {
      whereString += `&& "friend_uid" == "${friend_uid}"`
      // console.log('whereString',whereString);
    }
    let res = await dbJQL.collection(
        dbJQL.collection('uni-im-friend').where(whereString).field('friend_uid,mark,class_name')
        .getTemp(),
        dbJQL.collection('uni-id-users').field('_id,nickname,avatar_file,realname_auth').getTemp()
      )
      .limit(limit)
      .get()
    const usersList = res.data.map(item => item.friend_uid[0])
    $state.users.merge(usersList.reduce((obj, item) => {
      obj[item._id] = item
      return obj
    }, {}))
    return usersList 
  }
  remove(friend_uid) {
    let friendList = $state.friend.dataList
    let index = friendList.findIndex(i => i._id == friend_uid)
    friendList.splice(index, 1)
  }
}