qq.js 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. const {
  2. userCollection
  3. } = require('../../common/constants')
  4. const {
  5. ERROR
  6. } = require('../../common/error')
  7. function getQQPlatform() {
  8. const platform = this.clientPlatform
  9. switch (platform) {
  10. case 'app':
  11. case 'app-plus':
  12. return 'app'
  13. case 'mp-qq':
  14. return 'mp'
  15. default:
  16. throw new Error('Unsupported qq platform')
  17. }
  18. }
  19. async function saveQQUserKey({
  20. openid,
  21. sessionKey, // QQ小程序用户sessionKey
  22. accessToken, // App端QQ用户accessToken
  23. accessTokenExpired // App端QQ用户accessToken过期时间
  24. } = {}) {
  25. // 微信公众平台、开放平台refreshToken有效期均为30天(微信没有在网络请求里面返回30天这个值,务必注意未来可能出现调整,需及时更新此处逻辑)。
  26. // 此前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)
  27. const appId = this.getClientInfo().appId
  28. const qqPlatform = getQQPlatform.call(this)
  29. const keyObj = {
  30. dcloudAppid: appId,
  31. openid,
  32. platform: 'qq-' + qqPlatform
  33. }
  34. switch (qqPlatform) {
  35. case 'mp':
  36. await this.uniOpenBridge.setSessionKey(keyObj, {
  37. session_key: sessionKey
  38. }, 30 * 24 * 60 * 60)
  39. break
  40. case 'app':
  41. case 'h5':
  42. case 'web':
  43. await this.uniOpenBridge.setUserAccessToken(keyObj, {
  44. access_token: accessToken,
  45. access_token_expired: accessTokenExpired
  46. }, accessTokenExpired ?
  47. Math.floor((accessTokenExpired - Date.now()) / 1000) :
  48. 30 * 24 * 60 * 60
  49. )
  50. break
  51. default:
  52. break
  53. }
  54. }
  55. function generateQQCache({
  56. sessionKey, // QQ小程序用户sessionKey
  57. accessToken, // App端QQ用户accessToken
  58. accessTokenExpired // App端QQ用户accessToken过期时间
  59. } = {}) {
  60. const platform = getQQPlatform.call(this)
  61. let cache
  62. switch (platform) {
  63. case 'app':
  64. cache = {
  65. access_token: accessToken,
  66. access_token_expired: accessTokenExpired
  67. }
  68. break
  69. case 'mp':
  70. cache = {
  71. session_key: sessionKey
  72. }
  73. break
  74. default:
  75. throw new Error('Unsupported qq platform')
  76. }
  77. return {
  78. third_party: {
  79. [`${platform}_qq`]: cache
  80. }
  81. }
  82. }
  83. function getQQOpenid({
  84. userRecord
  85. } = {}) {
  86. const qqPlatform = getQQPlatform.call(this)
  87. const appId = this.getClientInfo().appId
  88. const qqOpenidObj = userRecord.qq_openid
  89. if (!qqOpenidObj) {
  90. return
  91. }
  92. return qqOpenidObj[`${qqPlatform}_${appId}`] || qqOpenidObj[qqPlatform]
  93. }
  94. async function getQQCacheFallback({
  95. userRecord,
  96. key
  97. } = {}) {
  98. const platform = getQQPlatform.call(this)
  99. const thirdParty = userRecord && userRecord.third_party
  100. if (!thirdParty) {
  101. return
  102. }
  103. const qqCache = thirdParty[`${platform}_qq`]
  104. return qqCache && qqCache[key]
  105. }
  106. async function getQQCache({
  107. uid,
  108. userRecord,
  109. key
  110. } = {}) {
  111. const qqPlatform = getQQPlatform.call(this)
  112. const appId = this.getClientInfo().appId
  113. if (!userRecord) {
  114. const getUserRes = await userCollection.doc(uid).get()
  115. userRecord = getUserRes.data[0]
  116. }
  117. if (!userRecord) {
  118. throw {
  119. errCode: ERROR.ACCOUNT_NOT_EXISTS
  120. }
  121. }
  122. const openid = getQQOpenid.call(this, {
  123. userRecord
  124. })
  125. const getCacheMethod = qqPlatform === 'mp' ? 'getSessionKey' : 'getUserAccessToken'
  126. const userKey = await this.uniOpenBridge[getCacheMethod]({
  127. dcloudAppid: appId,
  128. platform: 'qq-' + qqPlatform,
  129. openid
  130. })
  131. if (userKey) {
  132. return userKey[key]
  133. }
  134. return getQQCacheFallback({
  135. userRecord,
  136. key
  137. })
  138. }
  139. module.exports = {
  140. getQQPlatform,
  141. generateQQCache,
  142. getQQCache,
  143. saveQQUserKey
  144. }