IdentityAuthService.java 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. package com.izouma.nineth.service;
  2. import com.alibaba.fastjson.JSONObject;
  3. import com.github.kevinsawicki.http.HttpRequest;
  4. import com.izouma.nineth.domain.IdentityAuth;
  5. import com.izouma.nineth.domain.User;
  6. import com.izouma.nineth.dto.PageQuery;
  7. import com.izouma.nineth.enums.AuthStatus;
  8. import com.izouma.nineth.exception.BusinessException;
  9. import com.izouma.nineth.repo.IdentityAuthRepo;
  10. import com.izouma.nineth.repo.UserRepo;
  11. import com.izouma.nineth.utils.JpaUtils;
  12. import lombok.AllArgsConstructor;
  13. import lombok.extern.slf4j.Slf4j;
  14. import org.springframework.data.domain.Page;
  15. import org.springframework.data.redis.core.BoundValueOperations;
  16. import org.springframework.data.redis.core.RedisTemplate;
  17. import org.springframework.scheduling.annotation.Scheduled;
  18. import org.springframework.stereotype.Service;
  19. import java.util.List;
  20. import java.util.concurrent.TimeUnit;
  21. import java.util.stream.Collectors;
  22. @Service
  23. @AllArgsConstructor
  24. @Slf4j
  25. public class IdentityAuthService {
  26. private IdentityAuthRepo identityAuthRepo;
  27. private UserRepo userRepo;
  28. private AdapayService adapayService;
  29. private RedisTemplate<String, Object> redisTemplate;
  30. public Page<IdentityAuth> all(PageQuery pageQuery) {
  31. return identityAuthRepo.findAll(JpaUtils.toSpecification(pageQuery, IdentityAuth.class), JpaUtils.toPageRequest(pageQuery));
  32. }
  33. public void apply(IdentityAuth identityAuth) {
  34. if (identityAuth.getUserId() == null) {
  35. throw new BusinessException("用户不存在");
  36. }
  37. User user = userRepo.findByIdAndDelFalse(identityAuth.getUserId()).orElseThrow(new BusinessException("用户不存在"));
  38. List<IdentityAuth> auths = identityAuthRepo.findByUserIdAndDelFalse(identityAuth.getUserId());
  39. auths.stream().filter(auth -> auth.getStatus() == AuthStatus.PENDING).findAny().ifPresent(a -> {
  40. throw new BusinessException("正在审核中,请勿重复提交");
  41. });
  42. auths.stream().filter(auth -> auth.getStatus() == AuthStatus.SUCCESS).findAny().ifPresent(a -> {
  43. throw new BusinessException("已认证,请勿重复提交");
  44. });
  45. identityAuth.setStatus(AuthStatus.PENDING);
  46. identityAuthRepo.save(identityAuth);
  47. user.setAuthStatus(AuthStatus.PENDING);
  48. userRepo.save(user);
  49. }
  50. public void audit(Long id, AuthStatus status) {
  51. IdentityAuth auth = identityAuthRepo.findByIdAndDelFalse(id).orElseThrow(new BusinessException("申请不存在"));
  52. if (auth.getStatus() != AuthStatus.PENDING) {
  53. throw new BusinessException("已经审核过");
  54. }
  55. User user = userRepo.findByIdAndDelFalse(auth.getUserId()).orElseThrow(new BusinessException("用户不存在"));
  56. if (status == AuthStatus.SUCCESS) {
  57. user.setAuthId(auth.getId());
  58. }
  59. auth.setStatus(status);
  60. identityAuthRepo.save(auth);
  61. user.setAuthStatus(status);
  62. userRepo.save(user);
  63. }
  64. public List<User> repeat(String idNo, Long userId) {
  65. List<IdentityAuth> auths = identityAuthRepo.findAllByIdNoAndUserIdIsNotAndDelFalse(idNo, userId);
  66. if (auths.isEmpty()) {
  67. return null;
  68. }
  69. List<Long> userIds = auths.stream().map(IdentityAuth::getUserId).distinct().collect(Collectors.toList());
  70. return userRepo.findByIdInAndDelFalse(userIds);
  71. }
  72. public void validate(String name, String phone, String idno) {
  73. String body = HttpRequest.post("https://jubrige.market.alicloudapi.com/mobile/3-validate-transfer")
  74. .header("Authorization", "APPCODE b48bc8f6759345a79ae20a951f03dabe")
  75. .contentType(HttpRequest.CONTENT_TYPE_FORM)
  76. .form("idCardNo", idno)
  77. .form("mobile", phone)
  78. .form("name", name)
  79. .body();
  80. JSONObject jsonObject = JSONObject.parseObject(body);
  81. if (jsonObject.getInteger("code") != 200) {
  82. String msg = jsonObject.getString("msg");
  83. throw new BusinessException(msg);
  84. } else {
  85. JSONObject data = jsonObject.getJSONObject("data");
  86. int result = data.getIntValue("result");
  87. String desc = data.getString("desc");
  88. if (result != 0) {
  89. throw new BusinessException(desc);
  90. } else {
  91. log.info("{} {} {} 实名认证通过", name, phone, idno);
  92. }
  93. }
  94. }
  95. @Scheduled(fixedRate = 60000)
  96. public void autoValidate() {
  97. BoundValueOperations<String, Object> ops = redisTemplate.boundValueOps("autoValidate");
  98. Boolean b = ops.setIfAbsent(1, 1, TimeUnit.HOURS);
  99. if (Boolean.TRUE.equals(b)) {
  100. try {
  101. List<IdentityAuth> list = identityAuthRepo.findByStatusAndAutoValidated(AuthStatus.PENDING, false);
  102. list.parallelStream().forEach(identityAuth -> {
  103. int count = identityAuthRepo.countByIdNoAndStatus(identityAuth.getIdNo(), AuthStatus.SUCCESS);
  104. boolean success = false;
  105. String reason = null;
  106. if (count >= 3) {
  107. success = false;
  108. reason = "同一身份证注册超过3个";
  109. } else {
  110. try {
  111. User user = userRepo.findById(identityAuth.getUserId())
  112. .orElseThrow(new BusinessException("用户不存在"));
  113. validate(identityAuth.getRealName(), user.getPhone(), identityAuth.getIdNo());
  114. success = true;
  115. } catch (Exception e) {
  116. reason = e.getMessage();
  117. }
  118. }
  119. identityAuth.setAutoValidated(true);
  120. identityAuth.setReason(reason);
  121. identityAuth.setStatus(success ? AuthStatus.SUCCESS : AuthStatus.PENDING);
  122. identityAuthRepo.save(identityAuth);
  123. });
  124. } catch (Exception ignored) {
  125. }
  126. redisTemplate.delete("autoValidate");
  127. }
  128. }
  129. }