login-by-qq.js 3.9 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 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164
const {
  initQQ
} = require('../../lib/third-party/index')
const {
  ERROR
} = require('../../common/error')
const {
  preUnifiedLogin,
  postUnifiedLogin
} = require('../../lib/utils/unified-login')
const {
  LOG_TYPE
} = require('../../common/constants')
const {
  getQQPlatform,
  generateQQCache,
  saveQQUserKey
} = require('../../lib/utils/qq')
const url = require('url')

/**
 * QQ登录
 * @tutorial https://uniapp.dcloud.net.cn/uniCloud/uni-id-pages.html#login-by-qq
 * @param {Object} params
 * @param {String} params.code                  QQ小程序登录返回的code参数
 * @param {String} params.accessToken           App端QQ登录返回的accessToken参数
 * @param {String} params.accessTokenExpired    accessToken过期时间,由App端QQ登录返回的expires_in参数计算而来,单位:毫秒
 * @param {String} params.inviteCode            邀请码
 * @returns
 */
module.exports = async function (params = {}) {
  const schema = {
    code: {
      type: 'string',
      required: false
    },
    accessToken: {
      type: 'string',
      required: false
    },
    accessTokenExpired: {
      type: 'number',
      required: false
    },
    inviteCode: {
      type: 'string',
      required: false
    }
  }
  this.middleware.validate(params, schema)
  const {
    code,
    accessToken,
    accessTokenExpired,
    inviteCode
  } = params
  const {
    appId
  } = this.getUniversalClientInfo()
  const qqApi = initQQ.call(this)
  const qqPlatform = getQQPlatform.call(this)
  let apiName
  switch (qqPlatform) {
    case 'mp':
      apiName = 'code2Session'
      break
    case 'app':
      apiName = 'getOpenidByToken'
      break
    default:
      throw new Error('Unsupported qq platform')
  }
  let getQQAccountResult
  try {
    getQQAccountResult = await qqApi[apiName]({
      code,
      accessToken
    })
  } catch (error) {
    console.error(error)
    await this.middleware.uniIdLog({
      success: false,
      type: LOG_TYPE.LOGIN
    })
    throw {
      errCode: ERROR.GET_THIRD_PARTY_ACCOUNT_FAILED
    }
  }

  const {
    openid,
    unionid,
    // 保存下面的字段
    sessionKey // QQ小程序用户sessionKey
  } = getQQAccountResult

  const {
    type,
    user
  } = await preUnifiedLogin.call(this, {
    user: {
      qq_openid: {
        [qqPlatform]: openid
      },
      qq_unionid: unionid
    }
  })
  const extraData = {
    qq_openid: {
      [`${qqPlatform}_${appId}`]: openid
    }
  }
  if (type === 'register' && qqPlatform !== 'mp') {
    const {
      nickname,
      avatar
    } = await qqApi.getUserInfo({
      accessToken,
      openid
    })
    // eslint-disable-next-line n/no-deprecated-api
    const extName = url.parse(avatar).pathname.split('.').pop()
    const cloudPath = `user/avatar/${openid.slice(-8) + Date.now()}-avatar.${extName}`
    const getAvatarRes = await uniCloud.httpclient.request(avatar)
    if (getAvatarRes.status >= 400) {
      throw {
        errCode: ERROR.GET_THIRD_PARTY_USER_INFO_FAILED
      }
    }
    const {
      fileID
    } = await uniCloud.uploadFile({
      cloudPath,
      fileContent: getAvatarRes.data
    })
    extraData.nickname = nickname
    extraData.avatar_file = {
      name: cloudPath,
      extname: extName,
      url: fileID
    }
  }
  await saveQQUserKey.call(this, {
    openid,
    sessionKey,
    accessToken,
    accessTokenExpired
  })
  return postUnifiedLogin.call(this, {
    user,
    extraData: {
      ...extraData,
      ...generateQQCache.call(this, {
        openid,
        sessionKey, // QQ小程序用户sessionKey
        accessToken, // App端QQ用户accessToken
        accessTokenExpired // App端QQ用户accessToken过期时间
      })
    },
    isThirdParty: true,
    type,
    inviteCode
  })
}