get-certify-id.js 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. const { userCollection, REAL_NAME_STATUS, frvLogsCollection, dbCmd } = require('../../common/constants')
  2. const { ERROR } = require('../../common/error')
  3. const { encryptData } = require('../../common/sensitive-aes-cipher')
  4. const { getCurrentDateTimestamp } = require('../../common/utils')
  5. // const CertifyIdExpired = 25 * 60 * 1000 // certifyId 过期时间为30分钟,在25分时置为过期
  6. /**
  7. * 获取认证ID
  8. * @tutorial https://uniapp.dcloud.net.cn/uniCloud/uni-id-pages.html#get-frv-certify-id
  9. * @param {Object} params
  10. * @param {String} params.realName 真实姓名
  11. * @param {String} params.idCard 身份证号码
  12. * @param {String} params.metaInfo 客户端初始化时返回的metaInfo
  13. * @returns
  14. */
  15. module.exports = async function (params) {
  16. const schema = {
  17. realName: 'realName',
  18. idCard: 'idCard',
  19. metaInfo: 'string'
  20. }
  21. this.middleware.validate(params, schema)
  22. const { realName: originalRealName, idCard: originalIdCard, metaInfo } = params // 解构出传入参数的真实姓名、身份证号码、其他元数据
  23. const realName = encryptData.call(this, originalRealName) // 对真实姓名进行加密处理
  24. const idCard = encryptData.call(this, originalIdCard) // 对身份证号码进行加密处理
  25. const { uid } = this.authInfo // 获取当前用户的 ID
  26. const idCardCertifyLimit = this.config.idCardCertifyLimit || 1 // 获取身份证认证限制次数,默认为1次
  27. const realNameCertifyLimit = this.config.realNameCertifyLimit || 5 // 获取实名认证限制次数,默认为5次
  28. const frvNeedAlivePhoto = this.config.frvNeedAlivePhoto || false // 是否需要拍摄活体照片,默认为 false
  29. const user = await userCollection.doc(uid).get() // 获取用户信息
  30. const userInfo = user.data && user.data[0] // 获取用户信息对象中的实名认证信息
  31. const { realname_auth: realNameAuth = {} } = userInfo // 解构出实名认证信息中的认证状态对象,默认为空对象
  32. // 如果用户已经实名认证过,不能再次认证
  33. if (realNameAuth.auth_status === REAL_NAME_STATUS.CERTIFIED) {
  34. throw {
  35. errCode: ERROR.REAL_NAME_VERIFIED
  36. }
  37. }
  38. // 查询已经使用同一个身份证认证的账号数量,如果超过限制则不能认证
  39. const idCardAccount = await userCollection.where({
  40. realname_auth: {
  41. type: 0, // 用户认证状态是个人
  42. auth_status: REAL_NAME_STATUS.CERTIFIED, // 认证状态为已认证
  43. identity: idCard // 身份证号码和传入参数的身份证号码相同
  44. }
  45. }).get()
  46. if (idCardAccount.data.length >= idCardCertifyLimit) {
  47. throw {
  48. errCode: ERROR.ID_CARD_EXISTS
  49. }
  50. }
  51. // 查询用户今天已经进行的实名认证次数,如果超过限制则不能认证
  52. const userFrvLogs = await frvLogsCollection.where({
  53. user_id: uid,
  54. created_date: dbCmd.gt(getCurrentDateTimestamp()) // 查询今天的认证记录
  55. }).get()
  56. // 限制用户每日认证次数
  57. if (userFrvLogs.data && userFrvLogs.data.length >= realNameCertifyLimit) {
  58. throw {
  59. errCode: ERROR.REAL_NAME_VERIFY_UPPER_LIMIT
  60. }
  61. }
  62. // 初始化实人认证服务
  63. const frvManager = uniCloud.getFacialRecognitionVerifyManager({
  64. requestId: this.getUniCloudRequestId() // 获取当前
  65. })
  66. // 调用实人认证服务,获取认证 ID
  67. const res = await frvManager.getCertifyId({
  68. realName: originalRealName,
  69. idCard: originalIdCard,
  70. needPicture: frvNeedAlivePhoto,
  71. metaInfo
  72. })
  73. // 将认证记录插入到实名认证日志中
  74. await frvLogsCollection.add({
  75. user_id: uid,
  76. certify_id: res.certifyId,
  77. real_name: realName,
  78. identity: idCard,
  79. status: REAL_NAME_STATUS.WAITING_CERTIFIED,
  80. created_date: Date.now()
  81. })
  82. // 返回认证ID
  83. return {
  84. certifyId: res.certifyId
  85. }
  86. }