123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110 |
- const {
- preBind,
- postBind
- } = require('../../lib/utils/relate')
- const {
- LOG_TYPE
- } = require('../../common/constants')
- const {
- ERROR
- } = require('../../common/error')
- const {
- initQQ
- } = require('../../lib/third-party/index')
- const {
- generateQQCache,
- getQQPlatform,
- saveQQUserKey
- } = require('../../lib/utils/qq')
- module.exports = async function (params = {}) {
- const schema = {
- code: {
- type: 'string',
- required: false
- },
- accessToken: {
- type: 'string',
- required: false
- },
- accessTokenExpired: {
- type: 'number',
- required: false
- }
- }
- this.middleware.validate(params, schema)
- const uid = this.authInfo.uid
- const {
- code,
- accessToken,
- accessTokenExpired
- } = params
- const qqPlatform = getQQPlatform.call(this)
- const appId = this.getClientInfo().appId
- const qqApi = initQQ.call(this)
- const clientPlatform = this.clientPlatform
- const apiName = clientPlatform === 'mp-qq' ? 'code2Session' : 'getOpenidByToken'
- let getQQAccountResult
- try {
- getQQAccountResult = await qqApi[apiName]({
- code,
- accessToken
- })
- } catch (error) {
- await this.middleware.uniIdLog({
- success: false,
- type: LOG_TYPE.BIND_QQ
- })
- throw {
- errCode: ERROR.GET_THIRD_PARTY_ACCOUNT_FAILED
- }
- }
- const {
- openid,
- unionid,
-
- sessionKey
- } = getQQAccountResult
- const bindAccount = {
- qq_openid: {
- [clientPlatform]: openid
- },
- qq_unionid: unionid
- }
- await preBind.call(this, {
- uid,
- bindAccount,
- logType: LOG_TYPE.BIND_QQ
- })
- await saveQQUserKey.call(this, {
- openid,
- sessionKey,
- accessToken,
- accessTokenExpired
- })
- return postBind.call(this, {
- uid,
- bindAccount,
- extraData: {
- qq_openid: {
- [`${qqPlatform}_${appId}`]: openid
- },
- ...generateQQCache.call(this, {
- openid,
- sessionKey
- })
- },
- logType: LOG_TYPE.BIND_QQ
- })
- }
|