register.js 4.3 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
const {
  userCollection,
  LOG_TYPE
} = require('../../common/constants')
const {
  ERROR
} = require('../../common/error')
const {
  findUser
} = require('./account')
const {
  getValidInviteCode,
  generateInviteInfo
} = require('./fission')
const {
  logout
} = require('./logout')
const PasswordUtils = require('./password')
const merge = require('lodash.merge')

study夏羽's avatar
study夏羽 已提交
21
async function realPreRegister (params = {}) {
DCloud_JSON's avatar
DCloud_JSON 已提交
22 23 24 25 26
  const {
    user
  } = params
  const userMatched = await findUser({
    userQuery: user,
study夏羽's avatar
study夏羽 已提交
27
    authorizedApp: this.getUniversalClientInfo().appId
DCloud_JSON's avatar
DCloud_JSON 已提交
28 29 30 31 32 33 34 35
  })
  if (userMatched.length > 0) {
    throw {
      errCode: ERROR.ACCOUNT_EXISTS
    }
  }
}

study夏羽's avatar
study夏羽 已提交
36
async function preRegister (params = {}) {
DCloud_JSON's avatar
DCloud_JSON 已提交
37 38 39 40 41 42 43 44 45 46 47
  try {
    await realPreRegister.call(this, params)
  } catch (error) {
    await this.middleware.uniIdLog({
      success: false,
      type: LOG_TYPE.REGISTER
    })
    throw error
  }
}

study夏羽's avatar
study夏羽 已提交
48
async function preRegisterWithPassword (params = {}) {
DCloud_JSON's avatar
DCloud_JSON 已提交
49 50 51 52 53 54 55 56
  const {
    user,
    password
  } = params
  await preRegister.call(this, {
    user
  })
  const passwordUtils = new PasswordUtils({
study夏羽's avatar
study夏羽 已提交
57
    clientInfo: this.getUniversalClientInfo(),
DCloud_JSON's avatar
DCloud_JSON 已提交
58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75
    passwordSecret: this.config.passwordSecret
  })
  const {
    passwordHash,
    version
  } = passwordUtils.generatePasswordHash({
    password
  })
  const extraData = {
    password: passwordHash,
    password_secret_version: version
  }
  return {
    user,
    extraData
  }
}

study夏羽's avatar
study夏羽 已提交
76
async function thirdPartyRegister ({
DCloud_JSON's avatar
DCloud_JSON 已提交
77 78 79 80 81 82 83 84
  user = {}
} = {}) {
  return {
    mobileConfirmed: !!(user.mobile && user.mobile_confirmed) || false,
    emailConfirmed: !!(user.email && user.email_confirmed) || false
  }
}

study夏羽's avatar
study夏羽 已提交
85
async function postRegister (params = {}) {
DCloud_JSON's avatar
DCloud_JSON 已提交
86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101
  const {
    user,
    extraData = {},
    isThirdParty = false,
    inviteCode
  } = params
  const {
    appId,
    appName,
    appVersion,
    appVersionCode,
    channel,
    scene,
    clientIP,
    osName,
    uniIdToken
study夏羽's avatar
study夏羽 已提交
102
  } = this.getUniversalClientInfo()
DCloud_JSON's avatar
DCloud_JSON 已提交
103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165

  merge(user, extraData)

  const registerChannel = channel || scene
  user.register_env = {
    appid: appId || '',
    uni_platform: this.clientPlatform || '',
    os_name: osName || '',
    app_name: appName || '',
    app_version: appVersion || '',
    app_version_code: appVersionCode || '',
    channel: registerChannel ? registerChannel + '' : '', // channel可能为数字,统一存为字符串
    client_ip: clientIP || ''
  }

  user.register_date = Date.now()
  user.dcloud_appid = [appId]

  if (user.username) {
    user.username = user.username.toLowerCase()
  }
  if (user.email) {
    user.email = user.email.toLowerCase()
  }

  const {
    autoSetInviteCode, // 注册时自动设置邀请码
    forceInviteCode // 必须有邀请码才允许注册,注意此逻辑不可对admin生效
  } = this.config
  if (autoSetInviteCode) {
    user.my_invite_code = await getValidInviteCode()
  }

  const isAdmin = user.role && user.role.includes('admin')

  if (forceInviteCode && !isAdmin && !inviteCode) {
    throw {
      errCode: ERROR.INVALID_INVITE_CODE
    }
  }

  if (inviteCode) {
    const {
      inviterUid,
      inviteTime
    } = await generateInviteInfo({
      inviteCode
    })
    user.inviter_uid = inviterUid
    user.invite_time = inviteTime
  }

  if (uniIdToken) {
    try {
      await logout.call(this)
    } catch (error) {}
  }

  const beforeRegister = this.hooks.beforeRegister
  let userRecord = user
  if (beforeRegister) {
    userRecord = await beforeRegister({
      userRecord,
study夏羽's avatar
study夏羽 已提交
166
      clientInfo: this.getUniversalClientInfo()
DCloud_JSON's avatar
DCloud_JSON 已提交
167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212
    })
  }

  const {
    id: uid
  } = await userCollection.add(userRecord)

  const {
    token,
    tokenExpired
  } = await this.uniIdCommon.createToken({
    uid
  })

  await this.middleware.uniIdLog({
    data: {
      user_id: uid
    },
    type: LOG_TYPE.REGISTER
  })

  return {
    errCode: 0,
    uid,
    newToken: {
      token,
      tokenExpired
    },
    ...(
      isThirdParty
        ? thirdPartyRegister({
          user: {
            ...userRecord,
            _id: uid
          }
        })
        : {}
    )
  }
}

module.exports = {
  preRegister,
  preRegisterWithPassword,
  postRegister
}