users.js 2.7 KB
Newer Older
DCloud_JSON's avatar
DCloud_JSON 已提交
1
import $state from '../state/index.js';
DCloud_JSON's avatar
3.4.31  
DCloud_JSON 已提交
2 3 4 5 6 7
let $users = {
  system: {
    _id: 'system',
    nickname: '系统消息',
    avatar_file: '/static/uni-im/avatar/system.png',
  },
DCloud_JSON's avatar
DCloud_JSON 已提交
8 9 10 11 12 13 14 15
  merge(usersInfo) {
    if (Array.isArray(usersInfo)) {
      let obj = {}
      usersInfo.forEach(item => {
        obj[item._id] = item
      })
      usersInfo = obj
    }
DCloud_JSON's avatar
3.4.31  
DCloud_JSON 已提交
16 17 18 19 20 21 22 23 24 25 26 27
    Object.assign($state.users, usersInfo)
  },
  find(param) {
    param = Array.isArray(param) ? param : [param]
    let usersInfo = []
    param.forEach(uid => {
      const userInfo = $state.users[uid]
      if (userInfo) {
        usersInfo.push(userInfo)
      }
    })
    return usersInfo
DCloud_JSON's avatar
DCloud_JSON 已提交
28 29 30 31 32 33 34
  },
  async get(param) {
    const uid_list = Array.isArray(param) ? param : [param]
    // 信息在本地不存在的用户id
    let new_uid_list = [];
    let userInfoList = [];
    uid_list.forEach(uid => {
DCloud_JSON's avatar
3.4.31  
DCloud_JSON 已提交
35 36 37
      let userInfo = $state.users[uid]
      if (userInfo && !this.loadTask.list.includes(uid)) {
        userInfoList.push(userInfo)
DCloud_JSON's avatar
DCloud_JSON 已提交
38 39 40 41 42 43 44 45 46 47
      } else {
        new_uid_list.push(uid)
      }
    })
    if (new_uid_list.length) {
      // 从云端加载本地不存在的用户数据
      const db = uniCloud.database();
      let res = await db.collection('uni-id-users').where({
          "_id": db.command.in(new_uid_list)
        })
DCloud_JSON's avatar
3.4.31  
DCloud_JSON 已提交
48
        .field('_id,nickname,avatar_file,realname_auth')
DCloud_JSON's avatar
DCloud_JSON 已提交
49 50 51
        .get()
      userInfoList.push(...res.result.data)
      $state.users.merge(res.result.data)
DCloud_JSON's avatar
3.4.31  
DCloud_JSON 已提交
52
      // console.log('从云端加载本地不存在的用户数据',new_uid_list.length,res.result.data, res,$state.users)
DCloud_JSON's avatar
DCloud_JSON 已提交
53 54
    }
    return Array.isArray(param) ? userInfoList : userInfoList[0]
DCloud_JSON's avatar
3.4.31  
DCloud_JSON 已提交
55
  },
DCloud_JSON's avatar
DCloud_JSON 已提交
56
  getNickname(user_id,tmpNickname = '[昵称加载中...]'){
DCloud_JSON's avatar
3.4.31  
DCloud_JSON 已提交
57 58 59 60 61 62 63 64
    const nickname = $state.users[user_id]?.nickname
    if(nickname){
      return nickname
    } else {
      // 如果用户信息不存在,先加入队列
      $users.loadTask.add(user_id)
      
      $state.users[user_id] = {
DCloud_JSON's avatar
DCloud_JSON 已提交
65
        nickname:tmpNickname,
DCloud_JSON's avatar
3.4.31  
DCloud_JSON 已提交
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
        _id:user_id,
        __loading:true
      }
      return $state.users[user_id]?.nickname
    }
  },
  // 设置一个队列加载用户信息避免频繁请求
  loadTask:{
    list:[],
    add(uids){
      uids = Array.isArray(uids) ? uids : [uids],
      this.list.push(...uids)
      this.run()
    },
    run(){
      // console.log('run-loadTask0')
      if(this.timer){
        clearTimeout(this.timer)
      }
      // console.log('run-loadTask1')
      this.timer = setTimeout(()=>{
        this.timer = null
        if(this.list.length){
          // console.log('run-loadTask2')
          // 去重
          this.list = [...new Set(this.list)]
          $users.get(this.list)
          this.list = []
        }
      },300)
    }
DCloud_JSON's avatar
DCloud_JSON 已提交
97
  }
DCloud_JSON's avatar
3.4.31  
DCloud_JSON 已提交
98 99
}
export default $users;