set-push-cid.js 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. const {
  2. deviceCollection
  3. } = require('../../common/constants')
  4. const {
  5. ERROR
  6. } = require('../../common/error')
  7. async function setOpendbDevice ({
  8. pushClientId
  9. } = {}) {
  10. // 仅新增,如果存在进行更新操作
  11. const {
  12. appId,
  13. deviceId,
  14. deviceBrand,
  15. deviceModel,
  16. osName,
  17. osVersion,
  18. osLanguage,
  19. osTheme,
  20. devicePixelRatio,
  21. windowWidth,
  22. windowHeight,
  23. screenWidth,
  24. screenHeight,
  25. romName,
  26. romVersion
  27. } = this.getClientInfo()
  28. const platform = this.clientPlatform
  29. const now = Date.now()
  30. const db = uniCloud.database()
  31. const opendbDeviceCollection = db.collection('opendb-device')
  32. const getDeviceRes = await opendbDeviceCollection.where({
  33. device_id: deviceId
  34. }).get()
  35. const data = {
  36. appid: appId,
  37. device_id: deviceId,
  38. vendor: deviceBrand,
  39. model: deviceModel,
  40. uni_platform: platform,
  41. os_name: osName,
  42. os_version: osVersion,
  43. os_language: osLanguage,
  44. os_theme: osTheme,
  45. pixel_ratio: devicePixelRatio,
  46. window_width: windowWidth,
  47. window_height: windowHeight,
  48. screen_width: screenWidth,
  49. screen_height: screenHeight,
  50. rom_name: romName,
  51. rom_version: romVersion,
  52. last_update_date: now,
  53. push_clientid: pushClientId
  54. }
  55. if (getDeviceRes.data.length > 0) {
  56. await opendbDeviceCollection.where({
  57. device_id: deviceId
  58. }).update(data)
  59. return
  60. }
  61. data.create_date = now
  62. await opendbDeviceCollection.add(data)
  63. }
  64. /**
  65. * 更新device表的push_clien_id
  66. * @tutorial https://uniapp.dcloud.net.cn/uniCloud/uni-id-pages.html#set-push-cid
  67. * @param {object} params
  68. * @param {string} params.pushClientId 客户端pushClientId
  69. * @returns
  70. */
  71. module.exports = async function (params = {}) {
  72. const schema = {
  73. pushClientId: 'string'
  74. }
  75. this.middleware.validate(params, schema)
  76. const {
  77. deviceId,
  78. appId,
  79. osName
  80. } = this.getClientInfo()
  81. let platform = this.clientPlatform
  82. if (platform === 'app') {
  83. platform += osName
  84. }
  85. const {
  86. uid,
  87. exp
  88. } = this.authInfo
  89. const { pushClientId } = params
  90. const tokenExpired = exp * 1000
  91. const getDeviceRes = await deviceCollection.where({
  92. device_id: deviceId
  93. }).get()
  94. console.log(getDeviceRes)
  95. if (getDeviceRes.data.length > 1) {
  96. return {
  97. errCode: ERROR.SYSTEM_ERROR
  98. }
  99. }
  100. const deviceRecord = getDeviceRes.data[0]
  101. await setOpendbDevice.call(this, {
  102. pushClientId
  103. })
  104. if (!deviceRecord) {
  105. await deviceCollection.add({
  106. user_id: uid,
  107. device_id: deviceId,
  108. token_expired: tokenExpired,
  109. push_clientid: pushClientId,
  110. appid: appId
  111. })
  112. return {
  113. errCode: 0
  114. }
  115. }
  116. // 同一用户允许更新token_expired,不同用户在token_expired小于Date.now()时允许更新。搭配逻辑:用户退出登录时将token_expired置0
  117. if (
  118. deviceRecord.user_id === uid ||
  119. (deviceRecord.token_expired < Date.now())
  120. ) {
  121. await deviceCollection.where({
  122. device_id: deviceId
  123. }).update({
  124. user_id: uid,
  125. token_expired: tokenExpired,
  126. push_clientid: pushClientId,
  127. appid: appId
  128. })
  129. return {
  130. errCode: 0
  131. }
  132. }
  133. return {
  134. errCode: ERROR.SYSTEM_ERROR
  135. }
  136. }