relate.js 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. const {
  2. findUser
  3. } = require('./account')
  4. const {
  5. ERROR
  6. } = require('../../common/error')
  7. const {
  8. userCollection, dbCmd, USER_IDENTIFIER
  9. } = require('../../common/constants')
  10. const {
  11. getUserIdentifier
  12. } = require('../../lib/utils/account')
  13. const {
  14. batchFindObjctValue
  15. } = require('../../common/utils')
  16. const {
  17. merge
  18. } = require('../npm/index')
  19. /**
  20. *
  21. * @param {object} param
  22. * @param {string} param.uid 用户id
  23. * @param {string} param.bindAccount 要绑定的三方账户、手机号或邮箱
  24. */
  25. async function preBind ({
  26. uid,
  27. bindAccount,
  28. logType
  29. } = {}) {
  30. const {
  31. userMatched
  32. } = await findUser({
  33. userQuery: bindAccount,
  34. authorizedApp: this.getUniversalClientInfo().appId
  35. })
  36. if (userMatched.length > 0) {
  37. await this.middleware.uniIdLog({
  38. data: {
  39. user_id: uid
  40. },
  41. type: logType,
  42. success: false
  43. })
  44. throw {
  45. errCode: ERROR.BIND_CONFLICT
  46. }
  47. }
  48. }
  49. async function postBind ({
  50. uid,
  51. extraData = {},
  52. bindAccount,
  53. logType
  54. } = {}) {
  55. await userCollection.doc(uid).update(merge(bindAccount, extraData))
  56. await this.middleware.uniIdLog({
  57. data: {
  58. user_id: uid
  59. },
  60. type: logType
  61. })
  62. return {
  63. errCode: 0
  64. }
  65. }
  66. async function preUnBind ({
  67. uid,
  68. unBindAccount,
  69. logType
  70. }) {
  71. const notUnBind = ['username', 'mobile', 'email']
  72. const userIdentifier = getUserIdentifier(unBindAccount)
  73. const condition = Object.keys(userIdentifier).reduce((res, key) => {
  74. if (userIdentifier[key]) {
  75. if (notUnBind.includes(key)) {
  76. throw {
  77. errCode: ERROR.UNBIND_NOT_SUPPORTED
  78. }
  79. }
  80. res.push({
  81. [key]: userIdentifier[key]
  82. })
  83. }
  84. return res
  85. }, [])
  86. const currentUnBindAccount = Object.keys(userIdentifier).reduce((res, key) => {
  87. if (userIdentifier[key]) {
  88. res.push(key)
  89. }
  90. return res
  91. }, [])
  92. const { data: users } = await userCollection.where(dbCmd.and(
  93. { _id: uid },
  94. dbCmd.or(condition)
  95. )).get()
  96. if (users.length <= 0) {
  97. await this.middleware.uniIdLog({
  98. data: {
  99. user_id: uid
  100. },
  101. type: logType,
  102. success: false
  103. })
  104. throw {
  105. errCode: ERROR.UNBIND_FAIL
  106. }
  107. }
  108. const [user] = users
  109. const otherAccounts = batchFindObjctValue(user, Object.keys(USER_IDENTIFIER).filter(key => !notUnBind.includes(key) && !currentUnBindAccount.includes(key)))
  110. let hasOtherAccountBind = false
  111. for (const key in otherAccounts) {
  112. if (otherAccounts[key]) {
  113. hasOtherAccountBind = true
  114. break
  115. }
  116. }
  117. // 如果没有其他第三方登录方式
  118. if (!hasOtherAccountBind) {
  119. // 存在用户名或者邮箱但是没有设置过没密码就提示设置密码
  120. if ((user.username || user.email) && !user.password) {
  121. throw {
  122. errCode: ERROR.UNBIND_PASSWORD_NOT_EXISTS
  123. }
  124. }
  125. // 账号任何登录方式都没有就优先绑定手机号
  126. if (!user.mobile) {
  127. throw {
  128. errCode: ERROR.UNBIND_MOBILE_NOT_EXISTS
  129. }
  130. }
  131. }
  132. }
  133. async function postUnBind ({
  134. uid,
  135. unBindAccount,
  136. logType
  137. }) {
  138. await userCollection.doc(uid).update(unBindAccount)
  139. await this.middleware.uniIdLog({
  140. data: {
  141. user_id: uid
  142. },
  143. type: logType
  144. })
  145. return {
  146. errCode: 0
  147. }
  148. }
  149. module.exports = {
  150. preBind,
  151. postBind,
  152. preUnBind,
  153. postUnBind
  154. }