relate.js 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. const {
  2. findUser
  3. } = require('./account')
  4. const {
  5. ERROR
  6. } = require('../../common/error')
  7. const {
  8. userCollection
  9. } = require('../../common/constants')
  10. const merge = require('lodash.merge')
  11. /**
  12. *
  13. * @param {object} param
  14. * @param {string} param.uid 用户id
  15. * @param {string} param.bindAccount 要绑定的三方账户、手机号或邮箱
  16. */
  17. async function preBind ({
  18. uid,
  19. bindAccount,
  20. logType
  21. } = {}) {
  22. const userMatched = await findUser({
  23. userQuery: bindAccount,
  24. authorizedApp: this.getClientInfo().appId
  25. })
  26. if (userMatched.length > 0) {
  27. await this.middleware.uniIdLog({
  28. data: {
  29. user_id: uid
  30. },
  31. type: logType,
  32. success: false
  33. })
  34. throw {
  35. errCode: ERROR.BIND_CONFLICT
  36. }
  37. }
  38. }
  39. async function postBind ({
  40. uid,
  41. extraData = {},
  42. bindAccount,
  43. logType
  44. } = {}) {
  45. await userCollection.doc(uid).update(merge(bindAccount, extraData))
  46. await this.middleware.uniIdLog({
  47. data: {
  48. user_id: uid
  49. },
  50. type: logType
  51. })
  52. return {
  53. errCode: 0
  54. }
  55. }
  56. module.exports = {
  57. preBind,
  58. postBind
  59. }