friend.js 1.1 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
import $state from '../state/index.js';
const db = uniCloud.database();
export default {
  get() {
    return $state.friend.dataList
  },
  async loadMore({
    friend_uid
  } = {}) {
    let whereString = '"user_id" == $cloudEnv_uid'
    if (friend_uid) {
      whereString += `&& "friend_uid" == "${friend_uid}"`
      // console.log('whereString',whereString);
    }
    let res = await db.collection(
        db.collection('uni-im-friend').where(whereString).field('friend_uid,mark,class_name')
        .getTemp(),
        db.collection('uni-id-users').field('_id,nickname,avatar_file').getTemp()
      )
      .limit(500)
      .get()
    let data = res.result.data
    // console.log('data',data);
    data.forEach((item, index) => {
      data[index] = item.friend_uid[0]
      let uid = data[index]._id
      if (!$state.users[uid]) {
        $state.users[uid] = item.friend_uid[0]
      }
    })
    $state.friend.hasMore = data.length == 500
    $state.friend.dataList.push(...data)
  },
  remove(friend_uid) {
    let friendList = $state.friend.dataList
    let index = friendList.findIndex(i => i._id == friend_uid)
    friendList.splice(index, 1)
  }
}