bind-mobile-by-univerify.js 1.5 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 63 64 65 66 67 68 69 70
const {
  getPhoneNumber
} = require('../../lib/utils/univerify')
const {
  LOG_TYPE
} = require('../../common/constants')
const {
  preBind,
  postBind
} = require('../../lib/utils/relate')

/**
 * 通过一键登录绑定手机号
 * @tutorial https://uniapp.dcloud.net.cn/uniCloud/uni-id-pages.html#bind-mobile-by-univerify
 * @param {Object} params
 * @param {String} params.openid        APP端一键登录返回的openid
 * @param {String} params.access_token  APP端一键登录返回的access_token
 * @returns
 */
module.exports = async function (params = {}) {
  const schema = {
    openid: 'string',
    access_token: 'string'
  }
  const {
    openid,
    // eslint-disable-next-line camelcase
    access_token
  } = params
  this.middleware.validate(params, schema)
  const uid = this.authInfo.uid
  let mobile
  try {
    const phoneInfo = await getPhoneNumber.call(this, {
      // eslint-disable-next-line camelcase
      access_token,
      openid
    })
    mobile = phoneInfo.phoneNumber
  } catch (error) {
    await this.middleware.uniIdLog({
      success: false,
      data: {
        user_id: uid
      },
      type: LOG_TYPE.BIND_MOBILE
    })
    throw error
  }

  const bindAccount = {
    mobile
  }
  await preBind.call(this, {
    uid,
    bindAccount,
    logType: LOG_TYPE.BIND_MOBILE
  })
  await postBind.call(this, {
    uid,
    bindAccount,
    extraData: {
      mobile_confirmed: 1
    },
    logType: LOG_TYPE.BIND_MOBILE
  })
  return {
    errCode: 0
  }
}