get-invited-user.js 1.8 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 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 69 70 71 72 73 74 75 76 77 78 79 80
const {
  userCollection
} = require('../../common/constants')
const {
  coverMobile
} = require('../../common/utils')

/**
 * 获取受邀用户
 * @tutorial https://uniapp.dcloud.net.cn/uniCloud/uni-id-pages.html#get-invited-user
 * @param {Object} params
 * @param {Number} params.level       获取受邀用户的级数,1表示直接邀请的用户
 * @param {Number} params.limit       返回数据大小
 * @param {Number} params.offset      返回数据偏移
 * @param {Boolean} params.needTotal  是否需要返回总数
 * @returns
 */
module.exports = async function (params = {}) {
  const schema = {
    level: 'number',
    limit: {
      required: false,
      type: 'number'
    },
    offset: {
      required: false,
      type: 'number'
    },
    needTotal: {
      required: false,
      type: 'boolean'
    }
  }
  this.middleware.validate(params, schema)
  const {
    level,
    limit = 20,
    offset = 0,
    needTotal = false
  } = params
  const uid = this.authInfo.uid
  const query = {
    [`inviter_uid.${level - 1}`]: uid
  }
  const getUserRes = await userCollection.where(query)
    .field({
      _id: true,
      avatar: true,
      avatar_file: true,
      username: true,
      nickname: true,
      mobile: true,
      invite_time: true
    })
    .orderBy('invite_time', 'desc')
    .skip(offset)
    .limit(limit)
    .get()

  const invitedUser = getUserRes.data.map(item => {
    return {
      uid: item._id,
      username: item.username,
      nickname: item.nickname,
      mobile: coverMobile(item.mobile),
      inviteTime: item.invite_time,
      avatar: item.avatar,
      avatarFile: item.avatar_file
    }
  })
  const result = {
    errCode: 0,
    invitedUser
  }
  if (needTotal) {
    const getTotalRes = await userCollection.where(query).count()
    result.total = getTotalRes.total
  }
  return result
}