bind-apple.js 1.3 KB
Newer Older
study夏羽's avatar
study夏羽 已提交
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
const {
  preBind,
  postBind
} = require('../../lib/utils/relate')
const {
  LOG_TYPE
} = require('../../common/constants')
const {
  ERROR
} = require('../../common/error')
const {
  initApple
} = require('../../lib/third-party/index')

/**
 * 绑定苹果账号
 * @tutorial https://uniapp.dcloud.net.cn/uniCloud/uni-id-pages.html#bind-apple
 * @param {Object} params
 * @param {String} params.identityToken 苹果登录返回identityToken
 * @returns
 */
module.exports = async function (params = {}) {
  const schema = {
    identityToken: 'string'
  }
  this.middleware.validate(params, schema)
  const uid = this.authInfo.uid
  const {
    identityToken
  } = params
  const appleApi = initApple.call(this)
  let verifyResult
  try {
    verifyResult = await appleApi.verifyIdentityToken(identityToken)
  } catch (error) {
    await this.middleware.uniIdLog({
      success: false,
      type: LOG_TYPE.BIND_APPLE
    })
    throw {
      errCode: ERROR.GET_THIRD_PARTY_ACCOUNT_FAILED
    }
  }
  const {
    openid
  } = verifyResult

  const bindAccount = {
    apple_openid: openid
  }
  await preBind.call(this, {
    uid,
    bindAccount,
    logType: LOG_TYPE.BIND_APPLE
  })
  return postBind.call(this, {
    uid,
    bindAccount,
    extraData: {},
    logType: LOG_TYPE.BIND_APPLE
  })
}