const { userCollection } = require('../../common/constants') const { ERROR } = require('../../common/error') function getQQPlatform () { const platform = this.clientPlatform switch (platform) { case 'app': case 'app-plus': return 'app' case 'mp-qq': return 'mp' default: throw new Error('Unsupported qq platform') } } function generateQQCache ({ openid, sessionKey, // QQ小程序用户sessionKey accessToken, // App端QQ用户accessToken accessTokenExpired // App端QQ用户accessToken过期时间 } = {}) { const platform = getQQPlatform.call(this) const appId = this.getClientInfo().appId let cache switch (platform) { case 'app': cache = { openid, access_token: accessToken, access_token_expired: accessTokenExpired } break case 'mp': cache = { openid, session_key: sessionKey } break default: throw new Error('Unsupported qq platform') } return { third_party: { [`${appId}_${platform}_qq`]: cache, [`${platform}_qq`]: cache } } } async function getQQCache ({ uid, userRecord, key } = {}) { const platform = getQQPlatform.call(this) const appId = this.getClientInfo().appId if (!userRecord) { const getUserRes = await userCollection.doc(uid).get() userRecord = getUserRes.data[0] } if (!userRecord) { throw { errCode: ERROR.ACCOUNT_NOT_EXISTS } } const thirdParty = userRecord && userRecord.third_party if (!thirdParty) { return } const qqCacheOld = thirdParty[`${platform}_qq`] const qqCache = thirdParty[`${appId}_${platform}_qq`] return (qqCache && qqCache[key]) || (qqCacheOld && qqCacheOld[key]) } module.exports = { getQQPlatform, generateQQCache, getQQCache }