Browse Source

自动认证

xiongzhu 3 years ago
parent
commit
b54a77251c

+ 5 - 1
src/main/java/com/izouma/nineth/domain/IdentityAuth.java

@@ -12,7 +12,7 @@ import javax.persistence.*;
 
 @Data
 @Entity
-@Table(indexes = {@Index(columnList = "userId")})
+@Table(indexes = {@Index(columnList = "userId"), @Index(columnList = "idNo")})
 @AllArgsConstructor
 @NoArgsConstructor
 @Builder
@@ -50,4 +50,8 @@ public class IdentityAuth extends BaseEntity {
     @Enumerated(EnumType.STRING)
     private AuthStatus status;
 
+    private boolean autoValidated;
+
+    private String reason;
+
 }

+ 4 - 0
src/main/java/com/izouma/nineth/repo/IdentityAuthRepo.java

@@ -29,4 +29,8 @@ public interface IdentityAuthRepo extends JpaRepository<IdentityAuth, Long>, Jpa
     Optional<IdentityAuth> findByIdAndDelFalse(Long id);
 
     List<IdentityAuth> findAllByIdNoAndUserIdIsNotAndDelFalse(String idNo, Long userId);
+
+    List<IdentityAuth> findByStatusAndAutoValidated(AuthStatus status, boolean validated);
+
+    int countByIdNoAndStatus(String idNo, AuthStatus status);
 }

+ 71 - 3
src/main/java/com/izouma/nineth/service/IdentityAuthService.java

@@ -1,5 +1,7 @@
 package com.izouma.nineth.service;
 
+import com.alibaba.fastjson.JSONObject;
+import com.github.kevinsawicki.http.HttpRequest;
 import com.izouma.nineth.domain.IdentityAuth;
 import com.izouma.nineth.domain.User;
 import com.izouma.nineth.dto.PageQuery;
@@ -9,19 +11,26 @@ import com.izouma.nineth.repo.IdentityAuthRepo;
 import com.izouma.nineth.repo.UserRepo;
 import com.izouma.nineth.utils.JpaUtils;
 import lombok.AllArgsConstructor;
+import lombok.extern.slf4j.Slf4j;
 import org.springframework.data.domain.Page;
+import org.springframework.data.redis.core.BoundValueOperations;
+import org.springframework.data.redis.core.RedisTemplate;
+import org.springframework.scheduling.annotation.Scheduled;
 import org.springframework.stereotype.Service;
 
 import java.util.List;
+import java.util.concurrent.TimeUnit;
 import java.util.stream.Collectors;
 
 @Service
 @AllArgsConstructor
+@Slf4j
 public class IdentityAuthService {
 
-    private IdentityAuthRepo identityAuthRepo;
-    private UserRepo         userRepo;
-    private AdapayService    adapayService;
+    private IdentityAuthRepo              identityAuthRepo;
+    private UserRepo                      userRepo;
+    private AdapayService                 adapayService;
+    private RedisTemplate<String, Object> redisTemplate;
 
     public Page<IdentityAuth> all(PageQuery pageQuery) {
         return identityAuthRepo.findAll(JpaUtils.toSpecification(pageQuery, IdentityAuth.class), JpaUtils.toPageRequest(pageQuery));
@@ -68,4 +77,63 @@ public class IdentityAuthService {
         List<Long> userIds = auths.stream().map(IdentityAuth::getUserId).distinct().collect(Collectors.toList());
         return userRepo.findByIdInAndDelFalse(userIds);
     }
+
+    public void validate(String name, String phone, String idno) {
+        String body = HttpRequest.post("https://jubrige.market.alicloudapi.com/mobile/3-validate-transfer")
+                .header("Authorization", "APPCODE b48bc8f6759345a79ae20a951f03dabe")
+                .contentType(HttpRequest.CONTENT_TYPE_FORM)
+                .form("idCardNo", idno)
+                .form("mobile", phone)
+                .form("name", name)
+                .body();
+        JSONObject jsonObject = JSONObject.parseObject(body);
+        if (jsonObject.getInteger("code") != 200) {
+            String msg = jsonObject.getString("msg");
+            throw new BusinessException(msg);
+        } else {
+            JSONObject data = jsonObject.getJSONObject("data");
+            int result = data.getIntValue("result");
+            String desc = data.getString("desc");
+            if (result != 0) {
+                throw new BusinessException(desc);
+            } else {
+                log.info("{} {} {} 实名认证通过", name, phone, idno);
+            }
+        }
+    }
+
+    @Scheduled(fixedRate = 60000)
+    public void autoValidate() {
+        BoundValueOperations<String, Object> ops = redisTemplate.boundValueOps("autoValidate");
+        Boolean b = ops.setIfAbsent(1, 1, TimeUnit.HOURS);
+        if (Boolean.TRUE.equals(b)) {
+            try {
+                List<IdentityAuth> list = identityAuthRepo.findByStatusAndAutoValidated(AuthStatus.PENDING, false);
+                list.parallelStream().forEach(identityAuth -> {
+                    int count = identityAuthRepo.countByIdNoAndStatus(identityAuth.getIdNo(), AuthStatus.SUCCESS);
+                    boolean success = false;
+                    String reason = null;
+                    if (count >= 3) {
+                        success = false;
+                        reason = "同一身份证注册超过3个";
+                    } else {
+                        try {
+                            User user = userRepo.findById(identityAuth.getUserId())
+                                    .orElseThrow(new BusinessException("用户不存在"));
+                            validate(identityAuth.getRealName(), user.getPhone(), identityAuth.getIdNo());
+                            success = true;
+                        } catch (Exception e) {
+                            reason = e.getMessage();
+                        }
+                    }
+                    identityAuth.setAutoValidated(true);
+                    identityAuth.setReason(reason);
+                    identityAuth.setStatus(success ? AuthStatus.SUCCESS : AuthStatus.PENDING);
+                    identityAuthRepo.save(identityAuth);
+                });
+            } catch (Exception ignored) {
+            }
+            redisTemplate.delete("autoValidate");
+        }
+    }
 }

+ 2 - 2
src/test/java/com/izouma/nineth/service/AssetServiceTest.java

@@ -188,8 +188,8 @@ class AssetServiceTest extends ApplicationTests {
     }
 
     @Test
-    public void asdfasdfasdfasdf() {
-        List<kebi> list = EasyExcel.read("/Users/drew/Downloads/科比移民明细表(1).xlsx")
+    public void 科比移民() {
+        List<kebi> list = EasyExcel.read("/Users/drew/Desktop/副本科比移民明细表(1).xlsx")
                 .head(kebi.class).sheet()
                 .headRowNumber(2)
                 .doReadSync();

+ 18 - 0
src/test/java/com/izouma/nineth/service/IdentityAuthServiceTest.java

@@ -0,0 +1,18 @@
+package com.izouma.nineth.service;
+
+import com.izouma.nineth.ApplicationTests;
+import org.junit.Test;
+import org.springframework.beans.factory.annotation.Autowired;
+
+import static org.junit.Assert.*;
+
+public class IdentityAuthServiceTest extends ApplicationTests {
+    @Autowired
+    private IdentityAuthService identityAuthService;
+
+    @Test
+    public void validate() {
+        identityAuthService.validate("熊竹", "15077886171", "321002199408304611");
+        identityAuthService.validate("熊竹", "15077886171", "321002199408304614");
+    }
+}