xiongzhu 3 lat temu
rodzic
commit
9ebaa3f3a9

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

@@ -2,6 +2,8 @@ package com.izouma.nineth.repo;
 
 import com.izouma.nineth.domain.IdentityAuth;
 import com.izouma.nineth.enums.AuthStatus;
+import org.springframework.data.domain.Page;
+import org.springframework.data.domain.Pageable;
 import org.springframework.data.jpa.repository.JpaRepository;
 import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
 import org.springframework.data.jpa.repository.Modifying;
@@ -24,6 +26,8 @@ public interface IdentityAuthRepo extends JpaRepository<IdentityAuth, Long>, Jpa
 
     List<IdentityAuth> findByUserIdAndDelFalse(Long userId);
 
+    List<IdentityAuth> findByUserId(Long userId);
+
     Optional<IdentityAuth> findFirstByUserIdAndStatusAndDelFalseOrderByCreatedAtDesc(Long userId, AuthStatus status);
 
     Optional<IdentityAuth> findByIdAndDelFalse(Long id);
@@ -33,4 +37,10 @@ public interface IdentityAuthRepo extends JpaRepository<IdentityAuth, Long>, Jpa
     List<IdentityAuth> findByStatusAndAutoValidated(AuthStatus status, boolean validated);
 
     int countByIdNoAndStatus(String idNo, AuthStatus status);
+
+    @Query("select distinct i.userId from IdentityAuth i")
+    Page<Long> listUserId(Pageable pageable);
+
+    @Query("delete from IdentityAuth i where i.userId = ?1 and i.id <> ?2")
+    List<Long> deleteDuplicated(Long userId, Long id);
 }

+ 6 - 1
src/main/java/com/izouma/nineth/repo/UserRepo.java

@@ -1,6 +1,7 @@
 package com.izouma.nineth.repo;
 
 import com.izouma.nineth.domain.User;
+import com.izouma.nineth.enums.AuthStatus;
 import com.izouma.nineth.security.Authority;
 import org.springframework.cache.annotation.CachePut;
 import org.springframework.cache.annotation.Cacheable;
@@ -14,7 +15,6 @@ import javax.transaction.Transactional;
 import java.time.LocalDateTime;
 import java.util.List;
 import java.util.Optional;
-import java.util.Set;
 
 public interface UserRepo extends JpaRepository<User, Long>, JpaSpecificationExecutor<User> {
     @Transactional
@@ -190,4 +190,9 @@ public interface UserRepo extends JpaRepository<User, Long>, JpaSpecificationExe
     @Modifying
     @Query("update User set vipPoint = ?2 where id = ?1")
     void updateVipPoint(Long id, int num);
+
+    @Query("update User u set u.authStatus = ?2, u.authId = ?3 where u.id = ?1")
+    @Transactional
+    @Modifying
+    void setAuthStatus(Long id, AuthStatus status, Long authId);
 }

+ 81 - 11
src/main/java/com/izouma/nineth/service/IdentityAuthService.java

@@ -16,6 +16,7 @@ import lombok.AllArgsConstructor;
 import lombok.extern.slf4j.Slf4j;
 import org.springframework.core.env.Environment;
 import org.springframework.data.domain.Page;
+import org.springframework.data.domain.PageRequest;
 import org.springframework.data.redis.core.RedisTemplate;
 import org.springframework.scheduling.annotation.Scheduled;
 import org.springframework.stereotype.Service;
@@ -24,6 +25,7 @@ import java.time.LocalDate;
 import java.time.temporal.ChronoUnit;
 import java.util.Arrays;
 import java.util.List;
+import java.util.Optional;
 import java.util.concurrent.TimeUnit;
 import java.util.regex.Pattern;
 import java.util.stream.Collectors;
@@ -91,30 +93,98 @@ public class IdentityAuthService {
         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);
+//            }
+//        }
+//    }
     public void validate(String name, String phone, String idno) {
-        String body = HttpRequest.post("https://jubrige.market.alicloudapi.com/mobile/3-validate-transfer")
+        String body = HttpRequest.post("https://zid.market.alicloudapi.com/idcheck/Post")
                 .header("Authorization", "APPCODE b48bc8f6759345a79ae20a951f03dabe")
                 .contentType(HttpRequest.CONTENT_TYPE_FORM)
-                .form("idCardNo", idno)
-                .form("mobile", phone)
-                .form("name", name)
+                .form("cardNo", idno)
+                .form("realName", name)
                 .body();
         JSONObject jsonObject = JSONObject.parseObject(body);
-        if (jsonObject.getInteger("code") != 200) {
-            String msg = jsonObject.getString("msg");
+        if (jsonObject.getInteger("error_code") != 0) {
+            String msg = jsonObject.getString("reason");
             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);
+            JSONObject data = jsonObject.getJSONObject("result");
+            boolean isOK = Optional.ofNullable(data.getBoolean("isok")).orElse(Boolean.FALSE);
+            if (!isOK) {
+                throw new BusinessException("不匹配");
             } else {
                 log.info("{} {} {} 实名认证通过", name, phone, idno);
             }
         }
     }
 
+    public void removeDuplicated() {
+        boolean hasMore = true;
+        int pageNum = 1;
+        while (hasMore) {
+            Page<Long> page = identityAuthRepo.listUserId(PageRequest.of(pageNum, 100));
+            List<Long> userIds = page.getContent();
+            userIds.parallelStream().forEach(userId -> {
+                userRepo.findById(userId).ifPresent(user -> {
+                    List<IdentityAuth> list = identityAuthRepo.findByUserId(userId);
+                    if (list.size() > 1) {
+                        IdentityAuth auth = list.stream()
+                                .filter(i -> i.getStatus() == AuthStatus.SUCCESS)
+                                .findAny().orElse(null);
+                        if (auth != null) {
+                            userRepo.setAuthStatus(user.getId(), auth.getStatus(), auth.getId());
+                            identityAuthRepo.deleteDuplicated(user.getId(), auth.getId());
+                            return;
+                        }
+
+                        auth = list.stream()
+                                .filter(i -> i.getStatus() == AuthStatus.PENDING)
+                                .findAny().orElse(null);
+                        if (auth != null) {
+                            userRepo.setAuthStatus(user.getId(), auth.getStatus(), auth.getId());
+                            identityAuthRepo.deleteDuplicated(user.getId(), auth.getId());
+                            return;
+                        }
+
+                        auth = list.stream()
+                                .filter(i -> i.getStatus() == AuthStatus.FAIL)
+                                .findAny().orElse(null);
+                        if (auth != null) {
+                            userRepo.setAuthStatus(user.getId(), auth.getStatus(), auth.getId());
+                            identityAuthRepo.deleteDuplicated(user.getId(), auth.getId());
+                            return;
+                        }
+
+                    } else if (list.size() == 1) {
+                        userRepo.setAuthStatus(user.getId(), list.get(0).getStatus(), list.get(0).getId());
+                    }
+                });
+            });
+            hasMore = page.hasNext();
+            pageNum++;
+        }
+    }
+
     @Scheduled(fixedRate = 60000)
     @RedisLock(value = "autoValidate", expire = 30, unit = TimeUnit.MINUTES)
     public void autoValidate() {

+ 5 - 0
src/main/java/com/izouma/nineth/web/IdentityAuthController.java

@@ -75,5 +75,10 @@ public class IdentityAuthController extends BaseController {
     public List<User> repeat(@RequestParam String idNo, @RequestParam Long userId) {
         return identityAuthService.repeat(idNo, userId);
     }
+
+    @GetMapping("/removeDuplicated")
+    public void removeDuplicated() {
+        identityAuthService.removeDuplicated();
+    }
 }
 

+ 3 - 4
src/test/java/com/izouma/nineth/CommonTest.java

@@ -455,12 +455,11 @@ public class CommonTest {
 
     @Test
     public void aasdf() {
-        String body = HttpRequest.post("https://jubrige.market.alicloudapi.com/mobile/3-validate-transfer")
+        String body = HttpRequest.post("https://zid.market.alicloudapi.com/idcheck/Post")
                 .header("Authorization", "APPCODE b48bc8f6759345a79ae20a951f03dabe")
                 .contentType(HttpRequest.CONTENT_TYPE_FORM)
-                .form("idCardNo", "430223198503027240")
-                .form("mobile", "13342533959")
-                .form("name", "陈赛")
+                .form("cardNo", "1")
+                .form("realName", "2")
                 .body();
         System.out.println(JSON.toJSONString(JSON.parseObject(body), SerializerFeature.PrettyFormat));
     }