friend.js 1.2 KB
Newer Older
DCloud_JSON's avatar
DCloud_JSON 已提交
1 2 3 4 5 6 7 8 9
import $state from '../state/index.js';
const db = uniCloud.database();
export default {
  get() {
    return $state.friend.dataList
  },
  async loadMore({
    friend_uid
  } = {}) {
DCloud_JSON's avatar
3.4.31  
DCloud_JSON 已提交
10
    const limit = 1000
DCloud_JSON's avatar
DCloud_JSON 已提交
11 12 13 14 15 16 17 18
    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(),
DCloud_JSON's avatar
3.4.31  
DCloud_JSON 已提交
19
        db.collection('uni-id-users').field('_id,nickname,avatar_file,realname_auth').getTemp()
DCloud_JSON's avatar
DCloud_JSON 已提交
20
      )
DCloud_JSON's avatar
3.4.31  
DCloud_JSON 已提交
21
      .limit(limit)
DCloud_JSON's avatar
DCloud_JSON 已提交
22 23 24 25 26 27 28 29 30 31
      .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]
      }
    })
DCloud_JSON's avatar
3.4.31  
DCloud_JSON 已提交
32
    $state.friend.hasMore = data.length == limit
DCloud_JSON's avatar
DCloud_JSON 已提交
33 34 35 36 37 38 39 40
    $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)
  }
}