account.js 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. const {
  2. db,
  3. dbCmd,
  4. userCollection
  5. } = require('../../common/constants')
  6. const {
  7. USER_IDENTIFIER
  8. } = require('../../common/constants')
  9. const {
  10. batchFindObjctValue,
  11. getType
  12. } = require('../../common/utils')
  13. /**
  14. * 查询满足条件的用户
  15. * @param {Object} params
  16. * @param {Object} params.userQuery 用户唯一标识组成的查询条件
  17. * @param {Object} params.authorizedApp 用户允许登录的应用
  18. * @returns userMatched 满足条件的用户列表
  19. */
  20. async function findUser (params = {}) {
  21. const {
  22. userQuery,
  23. authorizedApp = []
  24. } = params
  25. const condition = getUserQueryCondition(userQuery)
  26. if (condition.length === 0) {
  27. throw new Error('Invalid user query')
  28. }
  29. const authorizedAppType = getType(authorizedApp)
  30. let appQuery = null
  31. if (authorizedAppType === 'string') {
  32. // 传入authorizedApp为单个appId时
  33. appQuery = dbCmd.or(
  34. {
  35. dcloud_appid: authorizedApp
  36. },
  37. {
  38. dcloud_appid: dbCmd.exists(false)
  39. }
  40. )
  41. } else if (authorizedAppType === 'array') {
  42. if (authorizedApp.length === 0) {
  43. // 传入空数组表示希望获取不能登录任一客户端的用户
  44. appQuery = {
  45. dcloud_appid: []
  46. }
  47. } else if (authorizedApp.length === 1) {
  48. appQuery = dbCmd.or(
  49. {
  50. dcloud_appid: authorizedApp[0]
  51. },
  52. {
  53. dcloud_appid: dbCmd.exists(false)
  54. }
  55. )
  56. } else {
  57. appQuery = dbCmd.or(
  58. {
  59. dcloud_appid: db.command.in(authorizedApp)
  60. },
  61. {
  62. dcloud_appid: dbCmd.exists(false)
  63. }
  64. )
  65. }
  66. } else {
  67. throw new Error('Invalid authorized app')
  68. }
  69. let finalQuery
  70. if (condition.length === 1) {
  71. finalQuery = dbCmd.and(condition[0], appQuery)
  72. } else {
  73. finalQuery = dbCmd.and(
  74. dbCmd.or(condition),
  75. appQuery
  76. )
  77. }
  78. const userQueryRes = await userCollection.where(finalQuery).get()
  79. return userQueryRes.data
  80. }
  81. function getUserIdentifier (userRecord = {}) {
  82. const keys = Object.keys(USER_IDENTIFIER)
  83. return batchFindObjctValue(userRecord, keys)
  84. }
  85. function getUserQueryCondition (userRecord = {}) {
  86. const userIdentifier = getUserIdentifier(userRecord)
  87. const condition = []
  88. for (const key in userIdentifier) {
  89. const value = userIdentifier[key]
  90. if (!value) {
  91. // 过滤所有value为假值的条件,在查询用户时没有意义
  92. continue
  93. }
  94. const queryItem = {
  95. [key]: value
  96. }
  97. // 为兼容用户老数据用户名及邮箱需要同时查小写及原始大小写数据
  98. if (key === 'mobile') {
  99. queryItem.mobile_confirmed = 1
  100. } else if (key === 'email') {
  101. queryItem.email_confirmed = 1
  102. const email = userIdentifier.email
  103. if (email.toLowerCase() !== email) {
  104. condition.push({
  105. email: email.toLowerCase(),
  106. email_confirmed: 1
  107. })
  108. }
  109. } else if (key === 'username') {
  110. const username = userIdentifier.username
  111. if (username.toLowerCase() !== username) {
  112. condition.push({
  113. username: username.toLowerCase()
  114. })
  115. }
  116. }
  117. condition.push(queryItem)
  118. }
  119. return condition
  120. }
  121. module.exports = {
  122. findUser,
  123. getUserIdentifier
  124. }