提交 5fb0de72 编写于 作者: 雪洛's avatar 雪洛

feat: save user key with bridge

上级 40bfc7e0
......@@ -12,6 +12,7 @@ const {
isUniIdError
} = require('./common/error')
const middleware = require('./middleware/index')
const uniOpenBridge = require('uni-open-bridge-common')
const {
registerAdmin,
......@@ -150,6 +151,7 @@ module.exports = {
// 挂载uni-captcha到this上,方便后续调用
this.uniCaptcha = uniCaptcha
this.uniOpenBridge = uniOpenBridge
// 挂载中间件
this.middleware = {}
......@@ -305,7 +307,7 @@ module.exports = {
* @param {Object} params
* @param {String} params.code QQ小程序登录返回的code参数
* @param {String} params.accessToken App端QQ登录返回的accessToken参数
* @param {String} params.accessTokenExpired 由App端QQ登录返回的expires_in参数计算而来
* @param {String} params.accessTokenExpired accessToken过期时间,由App端QQ登录返回的expires_in参数计算而来,单位:毫秒
* @param {String} params.inviteCode 邀请码
* @returns
*/
......@@ -368,6 +370,7 @@ module.exports = {
* @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参数计算而来,单位:毫秒
* @returns
*/
bindQQ,
......
......@@ -56,7 +56,7 @@ module.exports = class Auth {
}
})
if (result.expiresIn) {
result.expired = Date.now() + result.expiresIn
result.expired = Date.now() + result.expiresIn * 1000
// delete result.expiresIn
}
return result
......
......@@ -18,26 +18,56 @@ function getQQPlatform () {
}
}
function generateQQCache ({
async function saveQQUserKey ({
openid,
sessionKey, // QQ小程序用户sessionKey
accessToken, // App端QQ用户accessToken
accessTokenExpired // App端QQ用户accessToken过期时间
} = {}) {
const platform = getQQPlatform.call(this)
// 微信公众平台、开放平台refreshToken有效期均为30天(微信没有在网络请求里面返回30天这个值,务必注意未来可能出现调整,需及时更新此处逻辑)。
// 此前QQ开放平台有调整过accessToken的过期时间:[access_token有效期由90天缩短至30天](https://wiki.connect.qq.com/%E3%80%90qq%E4%BA%92%E8%81%94%E3%80%91access_token%E6%9C%89%E6%95%88%E6%9C%9F%E8%B0%83%E6%95%B4)
const appId = this.getClientInfo().appId
const qqPlatform = getQQPlatform.call(this)
const keyObj = {
dcloudAppid: appId,
openid,
platform: qqPlatform + '-weixin'
}
switch (qqPlatform) {
case 'mp':
await this.uniOpenBridge.setUserKey(keyObj, {
session_key: sessionKey
})
break
case 'app':
case 'h5':
case 'web':
await this.uniOpenBridge.setUserKey(keyObj, {
access_token: accessToken,
access_token_expired: accessTokenExpired
}, Math.floor((accessTokenExpired - Date.now()) / 1000))
break
default:
break
}
}
function generateQQCache ({
sessionKey, // QQ小程序用户sessionKey
accessToken, // App端QQ用户accessToken
accessTokenExpired // App端QQ用户accessToken过期时间
} = {}) {
const platform = getQQPlatform.call(this)
let cache
switch (platform) {
case 'app':
cache = {
openid,
access_token: accessToken,
access_token_expired: accessTokenExpired
}
break
case 'mp':
cache = {
openid,
session_key: sessionKey
}
break
......@@ -46,19 +76,44 @@ function generateQQCache ({
}
return {
third_party: {
[`${appId}_${platform}_qq`]: cache,
[`${platform}_qq`]: cache
}
}
}
function getQQOpenid ({
userRecord
} = {}) {
const qqPlatform = getQQPlatform.call(this)
const appId = this.getClientInfo().appId
const qqOpenidObj = userRecord.qq_openid
if (!qqOpenidObj) {
return
}
return qqOpenidObj[`${qqPlatform}_${appId}`] || qqOpenidObj[qqPlatform]
}
async function getQQCacheFallback ({
userRecord,
key
} = {}) {
const platform = getQQPlatform.call(this)
const thirdParty = userRecord && userRecord.third_party
if (!thirdParty) {
return
}
const qqCache = thirdParty[`${platform}_qq`]
return qqCache && qqCache[key]
}
async function getQQCache ({
uid,
userRecord,
key
} = {}) {
const platform = getQQPlatform.call(this)
const qqPlatform = getQQPlatform.call(this)
const appId = this.getClientInfo().appId
if (!userRecord) {
const getUserRes = await userCollection.doc(uid).get()
userRecord = getUserRes.data[0]
......@@ -68,17 +123,26 @@ async function getQQCache ({
errCode: ERROR.ACCOUNT_NOT_EXISTS
}
}
const thirdParty = userRecord && userRecord.third_party
if (!thirdParty) {
return
const openid = getQQOpenid.call(this, {
userRecord
})
const userKey = await this.uniOpenBridge.getUserKey({
dcloudAppid: appId,
platform: qqPlatform + '-qq',
openid
})
if (userKey) {
return userKey[key]
}
const qqCacheOld = thirdParty[`${platform}_qq`]
const qqCache = thirdParty[`${appId}_${platform}_qq`]
return (qqCache && qqCache[key]) || (qqCacheOld && qqCacheOld[key])
return getQQCacheFallback({
userRecord,
key
})
}
module.exports = {
getQQPlatform,
generateQQCache,
getQQCache
getQQCache,
saveQQUserKey
}
......@@ -13,6 +13,7 @@ const {
generateInviteInfo
} = require('./fission')
const PasswordUtils = require('./password')
const merge = require('lodash.merge')
async function realPreRegister (params = {}) {
const {
......@@ -93,7 +94,7 @@ async function postRegister (params = {}) {
osName
} = this.getClientInfo()
Object.assign(user, extraData)
merge(user, extraData)
const registerChannel = channel || scene
user.register_env = {
......
......@@ -7,6 +7,7 @@ const {
const {
userCollection
} = require('../../common/constants')
const merge = require('lodash.merge')
/**
*
......@@ -43,10 +44,7 @@ async function postBind ({
bindAccount,
logType
} = {}) {
await userCollection.doc(uid).update({
...bindAccount,
...extraData
})
await userCollection.doc(uid).update(merge(bindAccount, extraData))
await this.middleware.uniIdLog({
data: {
user_id: uid
......
......@@ -5,6 +5,7 @@ const {
const {
ERROR
} = require('../../common/error')
function decryptWeixinData ({
encryptedData,
sessionKey,
......@@ -47,22 +48,56 @@ function getWeixinPlatform () {
}
}
function generateWeixinCache ({
async function saveWeixinUserKey ({
openid,
sessionKey, // 微信小程序用户sessionKey
accessToken, // App端微信用户accessToken
refreshToken, // App端微信用户refreshToken
accessTokenExpired // App端微信用户accessToken过期时间
} = {}) {
const platform = getWeixinPlatform.call(this)
// 微信公众平台、开放平台refreshToken有效期均为30天(微信没有在网络请求里面返回30天这个值,务必注意未来可能出现调整,需及时更新此处逻辑)。
// 此前QQ开放平台有调整过accessToken的过期时间:[access_token有效期由90天缩短至30天](https://wiki.connect.qq.com/%E3%80%90qq%E4%BA%92%E8%81%94%E3%80%91access_token%E6%9C%89%E6%95%88%E6%9C%9F%E8%B0%83%E6%95%B4)
const appId = this.getClientInfo().appId
const weixinPlatform = getWeixinPlatform.call(this)
const keyObj = {
dcloudAppid: appId,
openid,
platform: weixinPlatform + '-weixin'
}
switch (weixinPlatform) {
case 'mp':
await this.uniOpenBridge.setUserKey(keyObj, {
session_key: sessionKey
})
break
case 'app':
case 'h5':
case 'web':
await this.uniOpenBridge.setUserKey(keyObj, {
access_token: accessToken,
refresh_token: refreshToken,
access_token_expired: accessTokenExpired
}, 30 * 24 * 3600)
break
default:
break
}
}
function generateWeixinCache ({
sessionKey, // 微信小程序用户sessionKey
accessToken, // App端微信用户accessToken
refreshToken, // App端微信用户refreshToken
accessTokenExpired // App端微信用户accessToken过期时间
} = {}) {
const platform = getWeixinPlatform.call(this)
let cache
switch (platform) {
case 'app':
case 'h5':
case 'web':
cache = {
openid,
access_token: accessToken,
refresh_token: refreshToken,
access_token_expired: accessTokenExpired
......@@ -70,7 +105,6 @@ function generateWeixinCache ({
break
case 'mp':
cache = {
openid,
session_key: sessionKey
}
break
......@@ -79,18 +113,42 @@ function generateWeixinCache ({
}
return {
third_party: {
[`${appId}_${platform}_weixin`]: cache,
[`${platform}_weixin`]: cache
}
}
}
function getWeixinOpenid ({
userRecord
} = {}) {
const weixinPlatform = getWeixinPlatform.call(this)
const appId = this.getClientInfo().appId
const wxOpenidObj = userRecord.wx_openid
if (!wxOpenidObj) {
return
}
return wxOpenidObj[`${weixinPlatform}_${appId}`] || wxOpenidObj[weixinPlatform]
}
async function getWeixinCacheFallback ({
userRecord,
key
} = {}) {
const platform = getWeixinPlatform.call(this)
const thirdParty = userRecord && userRecord.third_party
if (!thirdParty) {
return
}
const weixinCache = thirdParty[`${platform}_weixin`]
return weixinCache && weixinCache[key]
}
async function getWeixinCache ({
uid,
userRecord,
key
} = {}) {
const platform = getWeixinPlatform.call(this)
const weixinPlatform = getWeixinPlatform.call(this)
const appId = this.getClientInfo().appId
if (!userRecord) {
const getUserRes = await userCollection.doc(uid).get()
......@@ -101,18 +159,27 @@ async function getWeixinCache ({
errCode: ERROR.ACCOUNT_NOT_EXISTS
}
}
const thirdParty = userRecord && userRecord.third_party
if (!thirdParty) {
return
const openid = getWeixinOpenid.call(this, {
userRecord
})
const userKey = await this.uniOpenBridge.getUserKey({
dcloudAppid: appId,
platform: weixinPlatform + '-weixin',
openid
})
if (userKey) {
return userKey[key]
}
const weixinCacheOld = thirdParty[`${platform}_weixin`]
const weixinCache = thirdParty[`${appId}_${platform}_weixin`]
return (weixinCache && weixinCache[key]) || (weixinCacheOld && weixinCacheOld[key])
return getWeixinCacheFallback({
userRecord,
key
})
}
module.exports = {
decryptWeixinData,
getWeixinPlatform,
generateWeixinCache,
getWeixinCache
getWeixinCache,
saveWeixinUserKey
}
......@@ -13,7 +13,8 @@ const {
} = require('../../common/constants')
const {
getQQPlatform,
generateQQCache
generateQQCache,
saveQQUserKey
} = require('../../lib/utils/qq')
const url = require('url')
......@@ -23,7 +24,7 @@ const url = require('url')
* @param {Object} params
* @param {String} params.code QQ小程序登录返回的code参数
* @param {String} params.accessToken App端QQ登录返回的accessToken参数
* @param {String} params.accessTokenExpired 由App端QQ登录返回的expires_in参数计算而来
* @param {String} params.accessTokenExpired accessToken过期时间,由App端QQ登录返回的expires_in参数计算而来,单位:毫秒
* @param {String} params.inviteCode 邀请码
* @returns
*/
......@@ -53,6 +54,9 @@ module.exports = async function (params = {}) {
accessTokenExpired,
inviteCode
} = params
const {
appId
} = this.getClientInfo()
const qqApi = initQQ.call(this)
const qqPlatform = getQQPlatform.call(this)
let apiName
......@@ -101,7 +105,11 @@ module.exports = async function (params = {}) {
qq_unionid: unionid
}
})
const extraData = {}
const extraData = {
qq_openid: {
[`${qqPlatform}_${appId}`]: openid
}
}
if (type === 'register' && qqPlatform !== 'mp') {
const {
nickname,
......@@ -132,6 +140,12 @@ module.exports = async function (params = {}) {
url: fileID
}
}
await saveQQUserKey.call(this, {
openid,
sessionKey,
accessToken,
accessTokenExpired
})
return postUnifiedLogin.call(this, {
user,
extraData: {
......
......@@ -10,7 +10,8 @@ const {
} = require('../../lib/utils/unified-login')
const {
generateWeixinCache,
getWeixinPlatform
getWeixinPlatform,
saveWeixinUserKey
} = require('../../lib/utils/weixin')
const {
LOG_TYPE
......@@ -38,6 +39,9 @@ module.exports = async function (params = {}) {
code,
inviteCode
} = params
const {
appId
} = this.getClientInfo()
const weixinApi = initWeixin.call(this)
const weixinPlatform = getWeixinPlatform.call(this)
let apiName
......@@ -88,7 +92,11 @@ module.exports = async function (params = {}) {
wx_unionid: unionid
}
})
const extraData = { }
const extraData = {
wx_openid: {
[`${weixinPlatform}_${appId}`]: openid
}
}
if (type === 'register' && weixinPlatform !== 'mp') {
const {
nickname,
......@@ -119,6 +127,13 @@ module.exports = async function (params = {}) {
url: fileID
}
}
await saveWeixinUserKey.call(this, {
openid,
sessionKey,
accessToken,
refreshToken,
accessTokenExpired
})
return postUnifiedLogin.call(this, {
user,
extraData: {
......
......@@ -12,7 +12,9 @@ const {
initQQ
} = require('../../lib/third-party/index')
const {
generateQQCache
generateQQCache,
getQQPlatform,
saveQQUserKey
} = require('../../lib/utils/qq')
/**
......@@ -21,6 +23,7 @@ const {
* @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参数计算而来,单位:毫秒
* @returns
*/
module.exports = async function (params = {}) {
......@@ -32,14 +35,21 @@ module.exports = async function (params = {}) {
accessToken: {
type: 'string',
required: false
},
accessTokenExpired: {
type: 'number',
required: false
}
}
this.middleware.validate(params, schema)
const uid = this.authInfo.uid
const {
code,
accessToken
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'
......@@ -77,10 +87,19 @@ module.exports = async function (params = {}) {
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
......
......@@ -6,7 +6,9 @@ const {
LOG_TYPE
} = require('../../common/constants')
const {
generateWeixinCache
generateWeixinCache,
saveWeixinUserKey,
getWeixinPlatform
} = require('../../lib/utils/weixin')
const {
initWeixin
......@@ -31,6 +33,9 @@ module.exports = async function (params = {}) {
const {
code
} = params
const weixinPlatform = getWeixinPlatform.call(this)
const appId = this.getClientInfo().appId
const weixinApi = initWeixin.call(this)
const clientPlatform = this.clientPlatform
const apiName = clientPlatform === 'mp-weixin' ? 'code2Session' : 'getOauthAccessToken'
......@@ -68,10 +73,20 @@ module.exports = async function (params = {}) {
bindAccount,
logType: LOG_TYPE.BIND_WEIXIN
})
await saveWeixinUserKey.call(this, {
openid,
sessionKey,
accessToken,
refreshToken,
accessTokenExpired
})
return postBind.call(this, {
uid,
bindAccount,
extraData: {
wx_openid: {
[`${weixinPlatform}_${appId}`]: openid
},
...generateWeixinCache.call(this, {
openid,
sessionKey, // 微信小程序用户sessionKey
......
......@@ -24,15 +24,18 @@ module.exports = async function (params) {
const oauthConfig = this.configUtils.getOauthConfig({
provider: 'weixin'
})
const bridge = require('uni-open-bridge-common')
const {
appId
} = this.getClientInfo()
const weixinPlatform = getWeixinPlatform.call(this)
const ticket = await bridge.getWeixinTicket({
const getTicketRes = await this.uniOpenBridge.getWeixinTicket({
appId,
platform: weixinPlatform
})
if (!getTicketRes) {
throw new Error('Wechat official account ticket not found, please referer to: https://uniapp.dcloud.net.cn/uniCloud/uni-id-pages.html#login-by-weixin-h5')
}
const ticket = getTicketRes.ticket
const signContent = {
jsapi_ticket: ticket,
noncestr: getNonceStr(),
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册