bind-mobile-by-mp-weixin.js 1.8 KB
Newer Older
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
const {
  preBind,
  postBind
} = require('../../lib/utils/relate')
const {
  LOG_TYPE
} = require('../../common/constants')
const {
  decryptWeixinData,
  getWeixinCache
} = require('../../lib/utils/weixin')

/**
 * 通过微信绑定手机号
 * @tutorial https://uniapp.dcloud.net.cn/uniCloud/uni-id-pages.html#bind-mobile-by-mp-weixin
 * @param {Object} params
 * @param {String} params.encryptedData   微信获取手机号返回的加密信息
 * @param {String} params.iv              微信获取手机号返回的初始向量
 * @returns
 */
module.exports = async function (params = {}) {
  /**
   * 微信小程序的规则是客户端应先使用checkSession接口检测上次获取的sessionKey是否仍有效
   * 如果有效则直接使用上次存储的sessionKey即可
   * 如果无效应重新调用login接口再次刷新sessionKey
   * 因此此接口不应直接使用客户端login获取的code,只能使用缓存的sessionKey
   */
  const schema = {
    encryptedData: 'string',
    iv: 'string'
  }
  const {
    encryptedData,
    iv
  } = params
  this.middleware.validate(params, schema)
  const uid = this.authInfo.uid

  const sessionKey = await getWeixinCache.call(this, {
    uid,
    key: 'session_key'
  })
  if (!sessionKey) {
    throw new Error('Session key not found')
  }
  const {
    purePhoneNumber: mobile
  } = decryptWeixinData.call(this, {
    encryptedData,
    sessionKey,
    iv
  })

  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
  }
}