|
@@ -2,6 +2,7 @@ package com.izouma.nineth.service;
|
|
|
|
|
|
|
|
import com.alibaba.fastjson.JSONObject;
|
|
import com.alibaba.fastjson.JSONObject;
|
|
|
import com.github.kevinsawicki.http.HttpRequest;
|
|
import com.github.kevinsawicki.http.HttpRequest;
|
|
|
|
|
+import com.izouma.nineth.annotations.RedisLock;
|
|
|
import com.izouma.nineth.domain.IdentityAuth;
|
|
import com.izouma.nineth.domain.IdentityAuth;
|
|
|
import com.izouma.nineth.domain.User;
|
|
import com.izouma.nineth.domain.User;
|
|
|
import com.izouma.nineth.dto.PageQuery;
|
|
import com.izouma.nineth.dto.PageQuery;
|
|
@@ -12,12 +13,13 @@ import com.izouma.nineth.repo.UserRepo;
|
|
|
import com.izouma.nineth.utils.JpaUtils;
|
|
import com.izouma.nineth.utils.JpaUtils;
|
|
|
import lombok.AllArgsConstructor;
|
|
import lombok.AllArgsConstructor;
|
|
|
import lombok.extern.slf4j.Slf4j;
|
|
import lombok.extern.slf4j.Slf4j;
|
|
|
|
|
+import org.springframework.core.env.Environment;
|
|
|
import org.springframework.data.domain.Page;
|
|
import org.springframework.data.domain.Page;
|
|
|
-import org.springframework.data.redis.core.BoundValueOperations;
|
|
|
|
|
import org.springframework.data.redis.core.RedisTemplate;
|
|
import org.springframework.data.redis.core.RedisTemplate;
|
|
|
import org.springframework.scheduling.annotation.Scheduled;
|
|
import org.springframework.scheduling.annotation.Scheduled;
|
|
|
import org.springframework.stereotype.Service;
|
|
import org.springframework.stereotype.Service;
|
|
|
|
|
|
|
|
|
|
+import java.util.Arrays;
|
|
|
import java.util.List;
|
|
import java.util.List;
|
|
|
import java.util.concurrent.TimeUnit;
|
|
import java.util.concurrent.TimeUnit;
|
|
|
import java.util.stream.Collectors;
|
|
import java.util.stream.Collectors;
|
|
@@ -31,6 +33,7 @@ public class IdentityAuthService {
|
|
|
private UserRepo userRepo;
|
|
private UserRepo userRepo;
|
|
|
private AdapayService adapayService;
|
|
private AdapayService adapayService;
|
|
|
private RedisTemplate<String, Object> redisTemplate;
|
|
private RedisTemplate<String, Object> redisTemplate;
|
|
|
|
|
+ private Environment env;
|
|
|
|
|
|
|
|
public Page<IdentityAuth> all(PageQuery pageQuery) {
|
|
public Page<IdentityAuth> all(PageQuery pageQuery) {
|
|
|
return identityAuthRepo.findAll(JpaUtils.toSpecification(pageQuery, IdentityAuth.class), JpaUtils.toPageRequest(pageQuery));
|
|
return identityAuthRepo.findAll(JpaUtils.toSpecification(pageQuery, IdentityAuth.class), JpaUtils.toPageRequest(pageQuery));
|
|
@@ -54,7 +57,7 @@ public class IdentityAuthService {
|
|
|
userRepo.save(user);
|
|
userRepo.save(user);
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- public void audit(Long id, AuthStatus status) {
|
|
|
|
|
|
|
+ public void audit(Long id, AuthStatus status, String reason) {
|
|
|
IdentityAuth auth = identityAuthRepo.findByIdAndDelFalse(id).orElseThrow(new BusinessException("申请不存在"));
|
|
IdentityAuth auth = identityAuthRepo.findByIdAndDelFalse(id).orElseThrow(new BusinessException("申请不存在"));
|
|
|
if (auth.getStatus() != AuthStatus.PENDING) {
|
|
if (auth.getStatus() != AuthStatus.PENDING) {
|
|
|
throw new BusinessException("已经审核过");
|
|
throw new BusinessException("已经审核过");
|
|
@@ -64,6 +67,8 @@ public class IdentityAuthService {
|
|
|
user.setAuthId(auth.getId());
|
|
user.setAuthId(auth.getId());
|
|
|
}
|
|
}
|
|
|
auth.setStatus(status);
|
|
auth.setStatus(status);
|
|
|
|
|
+ auth.setReason(reason);
|
|
|
|
|
+ auth.setAutoValidated(true);
|
|
|
identityAuthRepo.save(auth);
|
|
identityAuthRepo.save(auth);
|
|
|
user.setAuthStatus(status);
|
|
user.setAuthStatus(status);
|
|
|
userRepo.save(user);
|
|
userRepo.save(user);
|
|
@@ -103,37 +108,34 @@ public class IdentityAuthService {
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
@Scheduled(fixedRate = 60000)
|
|
@Scheduled(fixedRate = 60000)
|
|
|
|
|
+ @RedisLock(value = "autoValidate", expire = 30, unit = TimeUnit.MINUTES)
|
|
|
public void autoValidate() {
|
|
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();
|
|
|
|
|
- }
|
|
|
|
|
|
|
+ log.info("autoValidate");
|
|
|
|
|
+ if (Arrays.asList(env.getActiveProfiles()).contains("dev")) {
|
|
|
|
|
+ return;
|
|
|
|
|
+ }
|
|
|
|
|
+ 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");
|
|
|
|
|
|
|
+ }
|
|
|
|
|
+ audit(identityAuth.getId(), success ? AuthStatus.SUCCESS : AuthStatus.PENDING, reason);
|
|
|
|
|
+ });
|
|
|
|
|
+ } catch (Exception ignored) {
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|