首次完整推送,
V:1.20240808.006
This commit is contained in:
97
uni_modules/uni-id-pages/uniCloud/cloudfunctions/uni-id-co/lib/third-party/qq/account/index.js
vendored
Normal file
97
uni_modules/uni-id-pages/uniCloud/cloudfunctions/uni-id-co/lib/third-party/qq/account/index.js
vendored
Normal file
@ -0,0 +1,97 @@
|
||||
const {
|
||||
UniCloudError
|
||||
} = require('../../../../common/error')
|
||||
const {
|
||||
resolveUrl
|
||||
} = require('../../../../common/utils')
|
||||
const {
|
||||
callQQOpenApi
|
||||
} = require('../normalize')
|
||||
|
||||
module.exports = class Auth {
|
||||
constructor (options) {
|
||||
this.options = Object.assign({
|
||||
baseUrl: 'https://graph.qq.com',
|
||||
timeout: 5000
|
||||
}, options)
|
||||
}
|
||||
|
||||
async _requestQQOpenapi ({ name, url, data, options }) {
|
||||
const defaultOptions = {
|
||||
method: 'GET',
|
||||
dataType: 'json',
|
||||
dataAsQueryString: true,
|
||||
timeout: this.options.timeout
|
||||
}
|
||||
const result = await callQQOpenApi({
|
||||
name: `auth.${name}`,
|
||||
url: resolveUrl(this.options.baseUrl, url),
|
||||
data,
|
||||
options,
|
||||
defaultOptions
|
||||
})
|
||||
return result
|
||||
}
|
||||
|
||||
async getUserInfo ({
|
||||
accessToken,
|
||||
openid
|
||||
} = {}) {
|
||||
const url = '/user/get_user_info'
|
||||
const result = await this._requestQQOpenapi({
|
||||
name: 'getUserInfo',
|
||||
url,
|
||||
data: {
|
||||
oauthConsumerKey: this.options.appId,
|
||||
accessToken,
|
||||
openid
|
||||
}
|
||||
})
|
||||
return {
|
||||
nickname: result.nickname,
|
||||
avatar: result.figureurl_qq_1
|
||||
}
|
||||
}
|
||||
|
||||
async getOpenidByToken ({
|
||||
accessToken
|
||||
} = {}) {
|
||||
const url = '/oauth2.0/me'
|
||||
const result = await this._requestQQOpenapi({
|
||||
name: 'getOpenidByToken',
|
||||
url,
|
||||
data: {
|
||||
accessToken,
|
||||
unionid: 1,
|
||||
fmt: 'json'
|
||||
}
|
||||
})
|
||||
if (result.clientId !== this.options.appId) {
|
||||
throw new UniCloudError({
|
||||
code: 'APPID_NOT_MATCH',
|
||||
message: 'appid not match'
|
||||
})
|
||||
}
|
||||
return {
|
||||
openid: result.openid,
|
||||
unionid: result.unionid
|
||||
}
|
||||
}
|
||||
|
||||
async code2Session ({
|
||||
code
|
||||
} = {}) {
|
||||
const url = 'https://api.q.qq.com/sns/jscode2session'
|
||||
const result = await this._requestQQOpenapi({
|
||||
name: 'getOpenidByToken',
|
||||
url,
|
||||
data: {
|
||||
grant_type: 'authorization_code',
|
||||
appid: this.options.appId,
|
||||
secret: this.options.secret,
|
||||
js_code: code
|
||||
}
|
||||
})
|
||||
return result
|
||||
}
|
||||
}
|
85
uni_modules/uni-id-pages/uniCloud/cloudfunctions/uni-id-co/lib/third-party/qq/normalize.js
vendored
Normal file
85
uni_modules/uni-id-pages/uniCloud/cloudfunctions/uni-id-co/lib/third-party/qq/normalize.js
vendored
Normal file
@ -0,0 +1,85 @@
|
||||
const {
|
||||
UniCloudError
|
||||
} = require('../../../common/error')
|
||||
const {
|
||||
camel2snakeJson,
|
||||
snake2camelJson
|
||||
} = require('../../../common/utils')
|
||||
|
||||
function generateApiResult (apiName, data) {
|
||||
if (data.ret || data.error) {
|
||||
// 这三种都是qq的错误码规范
|
||||
const code = data.ret || data.error || data.errcode || -2
|
||||
const message = data.msg || data.error_description || data.errmsg || `${apiName} fail`
|
||||
throw new UniCloudError({
|
||||
code,
|
||||
message
|
||||
})
|
||||
} else {
|
||||
delete data.ret
|
||||
delete data.msg
|
||||
delete data.error
|
||||
delete data.error_description
|
||||
delete data.errcode
|
||||
delete data.errmsg
|
||||
return {
|
||||
...data,
|
||||
errMsg: `${apiName} ok`,
|
||||
errCode: 0
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
function nomalizeError (apiName, error) {
|
||||
throw new UniCloudError({
|
||||
code: error.code || -2,
|
||||
message: error.message || `${apiName} fail`
|
||||
})
|
||||
}
|
||||
|
||||
async function callQQOpenApi ({
|
||||
name,
|
||||
url,
|
||||
data,
|
||||
options,
|
||||
defaultOptions
|
||||
}) {
|
||||
options = Object.assign({}, defaultOptions, options, { data: camel2snakeJson(Object.assign({}, data)) })
|
||||
let result
|
||||
try {
|
||||
result = await uniCloud.httpclient.request(url, options)
|
||||
} catch (e) {
|
||||
return nomalizeError(name, e)
|
||||
}
|
||||
let resData = result.data
|
||||
const contentType = result.headers['content-type']
|
||||
if (
|
||||
Buffer.isBuffer(resData) &&
|
||||
(contentType.indexOf('text/plain') === 0 ||
|
||||
contentType.indexOf('application/json') === 0)
|
||||
) {
|
||||
try {
|
||||
resData = JSON.parse(resData.toString())
|
||||
} catch (e) {
|
||||
resData = resData.toString()
|
||||
}
|
||||
} else if (Buffer.isBuffer(resData)) {
|
||||
resData = {
|
||||
buffer: resData,
|
||||
contentType
|
||||
}
|
||||
}
|
||||
return snake2camelJson(
|
||||
generateApiResult(
|
||||
name,
|
||||
resData || {
|
||||
errCode: -2,
|
||||
errMsg: 'Request failed'
|
||||
}
|
||||
)
|
||||
)
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
callQQOpenApi
|
||||
}
|
Reference in New Issue
Block a user