relate.js 3.4 KB
Newer Older
DCloud_JSON's avatar
DCloud_JSON 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164
const {
  findUser
} = require('./account')
const {
  ERROR
} = require('../../common/error')
const {
  userCollection, dbCmd, USER_IDENTIFIER
} = require('../../common/constants')
const {
  getUserIdentifier
} = require('../../lib/utils/account')

const {
  batchFindObjctValue
} = require('../../common/utils')
const merge = require('lodash.merge')

/**
 *
 * @param {object} param
 * @param {string} param.uid 用户id
 * @param {string} param.bindAccount 要绑定的三方账户、手机号或邮箱
 */
async function preBind ({
  uid,
  bindAccount,
  logType
} = {}) {
  const {
    userMatched
  } = await findUser({
    userQuery: bindAccount,
    authorizedApp: this.getUniversalClientInfo().appId
  })
  if (userMatched.length > 0) {
    await this.middleware.uniIdLog({
      data: {
        user_id: uid
      },
      type: logType,
      success: false
    })
    throw {
      errCode: ERROR.BIND_CONFLICT
    }
  }
}

async function postBind ({
  uid,
  extraData = {},
  bindAccount,
  logType
} = {}) {
  await userCollection.doc(uid).update(merge(bindAccount, extraData))
  await this.middleware.uniIdLog({
    data: {
      user_id: uid
    },
    type: logType
  })
  return {
    errCode: 0
  }
}

async function preUnBind ({
  uid,
  unBindAccount,
  logType
}) {
  const notUnBind = ['username', 'mobile', 'email']
  const userIdentifier = getUserIdentifier(unBindAccount)
  const condition = Object.keys(userIdentifier).reduce((res, key) => {
    if (userIdentifier[key]) {
      if (notUnBind.includes(key)) {
        throw {
          errCode: ERROR.UNBIND_NOT_SUPPORTED
        }
      }

      res.push({
        [key]: userIdentifier[key]
      })
    }

    return res
  }, [])
  const currentUnBindAccount = Object.keys(userIdentifier).reduce((res, key) => {
    if (userIdentifier[key]) {
      res.push(key)
    }
    return res
  }, [])
  const { data: users } = await userCollection.where(dbCmd.and(
    { _id: uid },
    dbCmd.or(condition)
  )).get()

  if (users.length <= 0) {
    await this.middleware.uniIdLog({
      data: {
        user_id: uid
      },
      type: logType,
      success: false
    })
    throw {
      errCode: ERROR.UNBIND_FAIL
    }
  }

  const [user] = users
  const otherAccounts = batchFindObjctValue(user, Object.keys(USER_IDENTIFIER).filter(key => !notUnBind.includes(key) && !currentUnBindAccount.includes(key)))
  let hasOtherAccountBind = false

  for (const key in otherAccounts) {
    if (otherAccounts[key]) {
      hasOtherAccountBind = true
      break
    }
  }

  // 如果没有其他第三方登录方式
  if (!hasOtherAccountBind) {
    // 存在用户名或者邮箱但是没有设置过没密码就提示设置密码
    if ((user.username || user.email) && !user.password) {
      throw {
        errCode: ERROR.UNBIND_PASSWORD_NOT_EXISTS
      }
    }
    // 账号任何登录方式都没有就优先绑定手机号
    if (!user.mobile) {
      throw {
        errCode: ERROR.UNBIND_MOBILE_NOT_EXISTS
      }
    }
  }
}

async function postUnBind ({
  uid,
  unBindAccount,
  logType
}) {
  await userCollection.doc(uid).update(unBindAccount)
  await this.middleware.uniIdLog({
    data: {
      user_id: uid
    },
    type: logType
  })
  return {
    errCode: 0
  }
}

module.exports = {
  preBind,
  postBind,
  preUnBind,
  postUnBind
}