qq.js 3.8 KB
Newer Older
DCloud_JSON's avatar
DCloud_JSON 已提交
1 2 3 4 5 6 7
const {
  userCollection
} = require('../../common/constants')
const {
  ERROR
} = require('../../common/error')

study夏羽's avatar
study夏羽 已提交
8
function getQQPlatform () {
DCloud_JSON's avatar
DCloud_JSON 已提交
9 10 11 12 13 14 15 16 17 18 19 20
  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')
  }
}

study夏羽's avatar
study夏羽 已提交
21
async function saveQQUserKey ({
DCloud_JSON's avatar
DCloud_JSON 已提交
22 23 24 25 26 27 28
  openid,
  sessionKey, // QQ小程序用户sessionKey
  accessToken, // App端QQ用户accessToken
  accessTokenExpired // App端QQ用户accessToken过期时间
} = {}) {
  // 微信公众平台、开放平台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)
study夏羽's avatar
study夏羽 已提交
29
  const appId = this.getUniversalClientInfo().appId
DCloud_JSON's avatar
DCloud_JSON 已提交
30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47
  const qqPlatform = getQQPlatform.call(this)
  const keyObj = {
    dcloudAppid: appId,
    openid,
    platform: 'qq-' + qqPlatform
  }
  switch (qqPlatform) {
    case 'mp':
      await this.uniOpenBridge.setSessionKey(keyObj, {
        session_key: sessionKey
      }, 30 * 24 * 60 * 60)
      break
    case 'app':
    case 'h5':
    case 'web':
      await this.uniOpenBridge.setUserAccessToken(keyObj, {
        access_token: accessToken,
        access_token_expired: accessTokenExpired
study夏羽's avatar
study夏羽 已提交
48 49 50
      }, accessTokenExpired
        ? Math.floor((accessTokenExpired - Date.now()) / 1000)
        : 30 * 24 * 60 * 60
DCloud_JSON's avatar
DCloud_JSON 已提交
51 52 53 54 55 56 57
      )
      break
    default:
      break
  }
}

study夏羽's avatar
study夏羽 已提交
58
function generateQQCache ({
DCloud_JSON's avatar
DCloud_JSON 已提交
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
  sessionKey, // QQ小程序用户sessionKey
  accessToken, // App端QQ用户accessToken
  accessTokenExpired // App端QQ用户accessToken过期时间
} = {}) {
  const platform = getQQPlatform.call(this)
  let cache
  switch (platform) {
    case 'app':
      cache = {
        access_token: accessToken,
        access_token_expired: accessTokenExpired
      }
      break
    case 'mp':
      cache = {
        session_key: sessionKey
      }
      break
    default:
      throw new Error('Unsupported qq platform')
  }
  return {
    third_party: {
      [`${platform}_qq`]: cache
    }
  }
}

study夏羽's avatar
study夏羽 已提交
87
function getQQOpenid ({
DCloud_JSON's avatar
DCloud_JSON 已提交
88 89 90
  userRecord
} = {}) {
  const qqPlatform = getQQPlatform.call(this)
study夏羽's avatar
study夏羽 已提交
91
  const appId = this.getUniversalClientInfo().appId
DCloud_JSON's avatar
DCloud_JSON 已提交
92 93 94 95 96 97 98
  const qqOpenidObj = userRecord.qq_openid
  if (!qqOpenidObj) {
    return
  }
  return qqOpenidObj[`${qqPlatform}_${appId}`] || qqOpenidObj[qqPlatform]
}

study夏羽's avatar
study夏羽 已提交
99
async function getQQCacheFallback ({
DCloud_JSON's avatar
DCloud_JSON 已提交
100 101 102 103 104 105 106 107 108 109 110 111
  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]
}

study夏羽's avatar
study夏羽 已提交
112
async function getQQCache ({
DCloud_JSON's avatar
DCloud_JSON 已提交
113 114 115 116 117
  uid,
  userRecord,
  key
} = {}) {
  const qqPlatform = getQQPlatform.call(this)
study夏羽's avatar
study夏羽 已提交
118
  const appId = this.getUniversalClientInfo().appId
DCloud_JSON's avatar
DCloud_JSON 已提交
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

  if (!userRecord) {
    const getUserRes = await userCollection.doc(uid).get()
    userRecord = getUserRes.data[0]
  }
  if (!userRecord) {
    throw {
      errCode: ERROR.ACCOUNT_NOT_EXISTS
    }
  }
  const openid = getQQOpenid.call(this, {
    userRecord
  })
  const getCacheMethod = qqPlatform === 'mp' ? 'getSessionKey' : 'getUserAccessToken'
  const userKey = await this.uniOpenBridge[getCacheMethod]({
    dcloudAppid: appId,
    platform: 'qq-' + qqPlatform,
    openid
  })
  if (userKey) {
    return userKey[key]
  }
  return getQQCacheFallback({
    userRecord,
    key
  })
}

module.exports = {
  getQQPlatform,
  generateQQCache,
  getQQCache,
  saveQQUserKey
}