register.js 1.5 KB
Newer Older
DCloud_JSON's avatar
DCloud_JSON 已提交
1
const { preRegister, postRegister } = require('../../lib/utils/register')
DCloud_JSON's avatar
2.1.2  
DCloud_JSON 已提交
2
const { EXTERNAL_DIRECT_CONNECT_PROVIDER } = require('../../common/constants')
DCloud_JSON's avatar
DCloud_JSON 已提交
3

DCloud_JSON's avatar
2.1.2  
DCloud_JSON 已提交
4 5 6 7 8 9 10 11 12 13
/**
 * 外部注册用户
 * @tutorial https://uniapp.dcloud.net.cn/uniCloud/uni-id-pages.html#external-register
 * @param {object} params
 * @param {string} params.externalUid   业务系统的用户id
 * @param {string} params.nickname  昵称
 * @param {string} params.gender  性别
 * @param {string} params.avatar  头像
 * @returns {object}
 */
DCloud_JSON's avatar
DCloud_JSON 已提交
14 15
module.exports = async function (params = {}) {
  const schema = {
DCloud_JSON's avatar
2.1.2  
DCloud_JSON 已提交
16
    externalUid: 'string',
DCloud_JSON's avatar
DCloud_JSON 已提交
17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33
    nickname: {
      required: false,
      type: 'nickname'
    },
    gender: {
      required: false,
      type: 'number'
    },
    avatar: {
      required: false,
      type: 'string'
    }
  }

  this.middleware.validate(params, schema)

  const {
DCloud_JSON's avatar
2.1.2  
DCloud_JSON 已提交
34
    externalUid,
DCloud_JSON's avatar
DCloud_JSON 已提交
35 36 37 38 39 40 41
    avatar,
    gender,
    nickname
  } = params

  await preRegister.call(this, {
    user: {
DCloud_JSON's avatar
2.1.2  
DCloud_JSON 已提交
42 43 44 45
      identities: {
        provider: EXTERNAL_DIRECT_CONNECT_PROVIDER,
        uid: externalUid
      }
DCloud_JSON's avatar
DCloud_JSON 已提交
46 47 48 49 50 51 52
    }
  })

  const result = await postRegister.call(this, {
    user: {
      avatar,
      gender,
DCloud_JSON's avatar
2.1.2  
DCloud_JSON 已提交
53 54 55 56 57 58 59 60 61 62 63 64
      nickname,
      identities: [
        {
          provider: EXTERNAL_DIRECT_CONNECT_PROVIDER,
          userInfo: {
            avatar,
            gender,
            nickname
          },
          uid: externalUid
        }
      ]
DCloud_JSON's avatar
DCloud_JSON 已提交
65 66 67 68 69 70
    }
  })

  return {
    errCode: result.errCode,
    newToken: result.newToken,
DCloud_JSON's avatar
2.1.2  
DCloud_JSON 已提交
71
    externalUid,
DCloud_JSON's avatar
DCloud_JSON 已提交
72 73
    avatar,
    gender,
DCloud_JSON's avatar
2.1.2  
DCloud_JSON 已提交
74 75
    nickname,
    uid: result.uid
DCloud_JSON's avatar
DCloud_JSON 已提交
76 77
  }
}