get-auth-result.js 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. const { userCollection, REAL_NAME_STATUS, frvLogsCollection } = require('../../common/constants')
  2. const { dataDesensitization, catchAwait } = require('../../common/utils')
  3. const { encryptData, decryptData } = require('../../common/sensitive-aes-cipher')
  4. const { ERROR } = require('../../common/error')
  5. /**
  6. * 查询认证结果
  7. * @tutorial https://uniapp.dcloud.net.cn/uniCloud/uni-id-pages.html#get-frv-auth-result
  8. * @param {Object} params
  9. * @param {String} params.certifyId 认证ID
  10. * @returns
  11. */
  12. module.exports = async function (params) {
  13. const schema = {
  14. certifyId: 'string'
  15. }
  16. this.middleware.validate(params, schema)
  17. const { uid } = this.authInfo // 从authInfo中取出uid属性
  18. const { certifyId } = params // 从params中取出certifyId属性
  19. const user = await userCollection.doc(uid).get() // 根据uid查询用户信息
  20. const userInfo = user.data && user.data[0] // 从查询结果中获取userInfo对象
  21. // 如果用户不存在,抛出账户不存在的错误
  22. if (!userInfo) {
  23. throw {
  24. errCode: ERROR.ACCOUNT_NOT_EXISTS
  25. }
  26. }
  27. const { realname_auth: realNameAuth = {} } = userInfo
  28. // 如果用户已经实名认证,抛出已实名认证的错误
  29. if (realNameAuth.auth_status === REAL_NAME_STATUS.CERTIFIED) {
  30. throw {
  31. errCode: ERROR.REAL_NAME_VERIFIED
  32. }
  33. }
  34. // 初始化实人认证服务
  35. const frvManager = uniCloud.getFacialRecognitionVerifyManager({
  36. requestId: this.getUniCloudRequestId()
  37. })
  38. // 调用frvManager的getAuthResult方法,获取认证结果
  39. const [error, res] = await catchAwait(frvManager.getAuthResult({
  40. certifyId
  41. }))
  42. // 如果出现错误,抛出未知错误并打印日志
  43. if (error) {
  44. console.log(ERROR.UNKNOWN_ERROR, 'error: ', error)
  45. throw error
  46. }
  47. // 如果认证状态为“PROCESSING”,抛出认证正在处理中的错误
  48. if (res.authState === 'PROCESSING') {
  49. throw {
  50. errCode: ERROR.FRV_PROCESSING
  51. }
  52. }
  53. // 如果认证状态为“FAIL”,更新认证日志的状态并抛出认证失败的错误
  54. if (res.authState === 'FAIL') {
  55. await frvLogsCollection.where({
  56. certify_id: certifyId
  57. }).update({
  58. status: REAL_NAME_STATUS.CERTIFY_FAILED
  59. })
  60. console.log(ERROR.FRV_FAIL, 'error: ', res)
  61. throw {
  62. errCode: ERROR.FRV_FAIL
  63. }
  64. }
  65. // 如果认证状态不为“SUCCESS”,抛出未知错误并打印日志
  66. if (res.authState !== 'SUCCESS') {
  67. console.log(ERROR.UNKNOWN_ERROR, 'source res: ', res)
  68. throw {
  69. errCode: ERROR.UNKNOWN_ERROR
  70. }
  71. }
  72. // 根据certifyId查询认证记录
  73. const frvLogs = await frvLogsCollection.where({
  74. certify_id: certifyId
  75. }).get()
  76. const log = frvLogs.data && frvLogs.data[0]
  77. const updateData = {
  78. realname_auth: {
  79. auth_status: REAL_NAME_STATUS.CERTIFIED,
  80. real_name: log.real_name,
  81. identity: log.identity,
  82. auth_date: Date.now(),
  83. type: 0
  84. }
  85. }
  86. // 如果获取到了认证照片的地址,则会对其进行下载,并使用uniCloud.uploadFile方法将其上传到云存储,并将上传后的fileID保存起来。
  87. if (res.pictureUrl) {
  88. const pictureRes = await uniCloud.httpclient.request(res.pictureUrl)
  89. if (pictureRes.status < 400) {
  90. const {
  91. fileID
  92. } = await uniCloud.uploadFile({
  93. cloudPath: `user/id-card/${uid}.b64`,
  94. cloudPathAsRealPath: true,
  95. fileContent: Buffer.from(encryptData.call(this, pictureRes.data.toString('base64')))
  96. })
  97. updateData.realname_auth.in_hand = fileID
  98. }
  99. }
  100. await Promise.all([
  101. // 更新用户认证状态
  102. userCollection.doc(uid).update(updateData),
  103. // 更新实人认证记录状态
  104. frvLogsCollection.where({
  105. certify_id: certifyId
  106. }).update({
  107. status: REAL_NAME_STATUS.CERTIFIED
  108. })
  109. ])
  110. return {
  111. errCode: 0,
  112. authStatus: REAL_NAME_STATUS.CERTIFIED,
  113. realName: dataDesensitization(decryptData.call(this, log.real_name), { onlyLast: true }), // 对姓名进行脱敏处理
  114. identity: dataDesensitization(decryptData.call(this, log.identity)) // 对身份证号进行脱敏处理
  115. }
  116. }