bind-weixin.js 2.4 KB
Newer Older
DCloud_JSON's avatar
DCloud_JSON 已提交
1 2 3 4 5 6 7 8
const {
  preBind,
  postBind
} = require('../../lib/utils/relate')
const {
  LOG_TYPE
} = require('../../common/constants')
const {
雪洛's avatar
雪洛 已提交
9 10 11
  generateWeixinCache,
  saveWeixinUserKey,
  getWeixinPlatform
DCloud_JSON's avatar
DCloud_JSON 已提交
12 13 14 15 16 17 18 19 20 21
} = require('../../lib/utils/weixin')
const {
  initWeixin
} = require('../../lib/third-party/index')
const {
  ERROR
} = require('../../common/error')

/**
 * 绑定微信
雪洛's avatar
雪洛 已提交
22
 * @tutorial https://uniapp.dcloud.net.cn/uniCloud/uni-id-pages.html#bind-weixin
DCloud_JSON's avatar
DCloud_JSON 已提交
23 24 25 26 27 28 29 30 31 32 33 34 35
 * @param {Object} params
 * @param {String} params.code  微信登录返回的code
 * @returns
 */
module.exports = async function (params = {}) {
  const schema = {
    code: 'string'
  }
  this.middleware.validate(params, schema)
  const uid = this.authInfo.uid
  const {
    code
  } = params
雪洛's avatar
雪洛 已提交
36 37 38
  const weixinPlatform = getWeixinPlatform.call(this)
  const appId = this.getClientInfo().appId

DCloud_JSON's avatar
DCloud_JSON 已提交
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 74 75
  const weixinApi = initWeixin.call(this)
  const clientPlatform = this.clientPlatform
  const apiName = clientPlatform === 'mp-weixin' ? 'code2Session' : 'getOauthAccessToken'
  let getWeixinAccountResult
  try {
    getWeixinAccountResult = await weixinApi[apiName](code)
  } catch (error) {
    await this.middleware.uniIdLog({
      success: false,
      type: LOG_TYPE.BIND_WEIXIN
    })
    throw {
      errCode: ERROR.GET_THIRD_PARTY_ACCOUNT_FAILED
    }
  }

  const {
    openid,
    unionid,
    // 保存下面四个字段
    sessionKey, // 微信小程序用户sessionKey
    accessToken, // App端微信用户accessToken
    refreshToken, // App端微信用户refreshToken
    expired: accessTokenExpired // App端微信用户accessToken过期时间
  } = getWeixinAccountResult

  const bindAccount = {
    wx_openid: {
      [clientPlatform]: openid
    },
    wx_unionid: unionid
  }
  await preBind.call(this, {
    uid,
    bindAccount,
    logType: LOG_TYPE.BIND_WEIXIN
  })
雪洛's avatar
雪洛 已提交
76 77 78 79 80 81 82
  await saveWeixinUserKey.call(this, {
    openid,
    sessionKey,
    accessToken,
    refreshToken,
    accessTokenExpired
  })
DCloud_JSON's avatar
DCloud_JSON 已提交
83 84 85 86
  return postBind.call(this, {
    uid,
    bindAccount,
    extraData: {
雪洛's avatar
雪洛 已提交
87 88 89
      wx_openid: {
        [`${weixinPlatform}_${appId}`]: openid
      },
DCloud_JSON's avatar
DCloud_JSON 已提交
90
      ...generateWeixinCache.call(this, {
雪洛's avatar
雪洛 已提交
91
        openid,
DCloud_JSON's avatar
DCloud_JSON 已提交
92 93 94 95 96 97 98 99 100
        sessionKey, // 微信小程序用户sessionKey
        accessToken, // App端微信用户accessToken
        refreshToken, // App端微信用户refreshToken
        accessTokenExpired // App端微信用户accessToken过期时间
      })
    },
    logType: LOG_TYPE.BIND_WEIXIN
  })
}