Преглед на файлове

优化 证件识别内容缓存key

y преди 2 години
родител
ревизия
7c8e043e99

+ 1 - 1
src/main/java/com/ydtech/SpringInsApplication.java

@@ -38,4 +38,4 @@ public class SpringInsApplication {
         SpringApplication.run(SpringInsApplication.class, args);
     }
 
-}
+}

+ 4 - 3
src/main/java/com/ydtech/modules/order/controller/IdentifyController.java

@@ -11,6 +11,7 @@ import org.springframework.web.bind.annotation.RequestMapping;
 import org.springframework.web.bind.annotation.RestController;
 import org.springframework.web.multipart.MultipartFile;
 
+import java.io.IOException;
 import java.util.Map;
 
 @RestController
@@ -27,13 +28,13 @@ public class IdentifyController {
 
     @PostMapping("/idCard")
     @ApiOperation(value = "身份证正反面识别")
-    public HttpResult<Map<String, Object>> idCard(MultipartFile image1, MultipartFile image2) {
+    public HttpResult<Map<String, Object>> idCard(MultipartFile image1, MultipartFile image2) throws IOException {
         return identifyService.idCard(image1, image2);
     }
 
     @PostMapping("/drivingPermit")
-    @ApiOperation(value = "行驶证正反面识别")  
-    public HttpResult<Map<String, Object>> drivingPermit(MultipartFile image1, MultipartFile image2) {
+    @ApiOperation(value = "行驶证正反面识别")
+    public HttpResult<Map<String, Object>> drivingPermit(MultipartFile image1, MultipartFile image2) throws IOException {
         return identifyService.drivingPermit(image1, image2);
     }
 

+ 3 - 2
src/main/java/com/ydtech/modules/order/service/IdentifyService.java

@@ -4,6 +4,7 @@ import com.ydtech.core.page.HttpResult;
 import com.ydtech.modules.order.entity.BusinessLicense;
 import org.springframework.web.multipart.MultipartFile;
 
+import java.io.IOException;
 import java.util.Map;
 
 public interface IdentifyService {
@@ -15,7 +16,7 @@ public interface IdentifyService {
      * @param image2 反面
      * @return
      */
-    HttpResult<Map<String, Object>> idCard(MultipartFile image1, MultipartFile image2);
+    HttpResult<Map<String, Object>> idCard(MultipartFile image1, MultipartFile image2) throws IOException;
 
 
     /**
@@ -25,7 +26,7 @@ public interface IdentifyService {
      * @param image2 反面
      * @return
      */
-    HttpResult<Map<String, Object>> drivingPermit(MultipartFile image1, MultipartFile image2);
+    HttpResult<Map<String, Object>> drivingPermit(MultipartFile image1, MultipartFile image2) throws IOException;
 
     /**
      * 营业执照识别

+ 25 - 20
src/main/java/com/ydtech/modules/order/service/impl/IdentifyServiceImpl.java

@@ -13,6 +13,7 @@ import com.ydtech.utils.StringUtils;
 import com.ydtech.utils.baidu.BusinessLicenseRecognition;
 import com.ydtech.utils.baidu.Idcard;
 import com.ydtech.utils.baidu.MultiObjectDetect;
+import org.apache.commons.codec.digest.DigestUtils;
 import org.springframework.beans.BeanUtils;
 import org.springframework.data.redis.core.RedisTemplate;
 import org.springframework.stereotype.Service;
@@ -43,33 +44,34 @@ public class IdentifyServiceImpl implements IdentifyService {
     private RedisTemplate<String, Object> redisTemplate;
 
     @Override
-    public HttpResult<Map<String, Object>> idCard(MultipartFile image1, MultipartFile image2) {
+    public HttpResult<Map<String, Object>> idCard(MultipartFile image1, MultipartFile image2) throws IOException {
         Map<String, Object> map = new HashMap<>();
         CustomerInfoVO customerInfoVO = new CustomerInfoVO();
-
+        String s1MD5 = DigestUtils.md5Hex(image1.getBytes());
         String s1 = byteToBase64(image1);
-
-        if (StringUtils.isNotBlank(s1) && !ObjectUtil.isAllFieldNull(redisTemplate.opsForValue().get(IMAGE_IDENTIFY_KEY + s1))) {
+        String s1Key = IMAGE_IDENTIFY_KEY + "idCard:" + "front:" + s1MD5;
+        if (StringUtils.isNotBlank(s1) && !ObjectUtil.isAllFieldNull(redisTemplate.opsForValue().get(s1Key))) {
             //走缓存
-            BeanUtils.copyProperties(Objects.requireNonNull(redisTemplate.opsForValue().get(IMAGE_IDENTIFY_KEY + s1)), customerInfoVO);
+            BeanUtils.copyProperties(Objects.requireNonNull(redisTemplate.opsForValue().get(s1Key)), customerInfoVO);
         } else {
             //识别身份证正面
             Idcard.getIdCardFront(s1, customerInfoVO);
             AssertionUtils.isEmpty(customerInfoVO, "身份证识别失败");
             //设置缓存
-            redisTemplate.opsForValue().set(IMAGE_IDENTIFY_KEY + s1, customerInfoVO, 1, TimeUnit.DAYS);
+            redisTemplate.opsForValue().set(s1Key, customerInfoVO, 1, TimeUnit.DAYS);
         }
-
+        String s2MD5 = DigestUtils.md5Hex(image2.getBytes());
         String s2 = byteToBase64(image2);
-        if (StringUtils.isNotBlank(s2) && !ObjectUtil.isAllFieldNull(redisTemplate.opsForValue().get(IMAGE_IDENTIFY_KEY + s2))) {
+        String s2Key = IMAGE_IDENTIFY_KEY + "idCard:" + "back:" + s2MD5;
+        if (StringUtils.isNotBlank(s2) && !ObjectUtil.isAllFieldNull(redisTemplate.opsForValue().get(s2Key))) {
             //走缓存
-            BeanUtils.copyProperties(Objects.requireNonNull(redisTemplate.opsForValue().get(IMAGE_IDENTIFY_KEY + s2)),customerInfoVO);
-        }else {
+            BeanUtils.copyProperties(Objects.requireNonNull(redisTemplate.opsForValue().get(s2Key)), customerInfoVO);
+        } else {
             //识别身份证背面
             Idcard.getIdCardBack(s2, customerInfoVO);
             AssertionUtils.isEmpty(customerInfoVO, "身份证识别失败");
             //设置缓存
-            redisTemplate.opsForValue().set(IMAGE_IDENTIFY_KEY + s2, customerInfoVO, 1, TimeUnit.DAYS);
+            redisTemplate.opsForValue().set(s2Key, customerInfoVO, 1, TimeUnit.DAYS);
         }
 
         AssertionUtils.isEmpty(customerInfoVO, "身份证识别失败");
@@ -78,32 +80,35 @@ public class IdentifyServiceImpl implements IdentifyService {
     }
 
     @Override
-    public HttpResult<Map<String, Object>> drivingPermit(MultipartFile image1, MultipartFile image2) {
+    public HttpResult<Map<String, Object>> drivingPermit(MultipartFile image1, MultipartFile image2) throws IOException {
         Map<String, Object> map = new HashMap<>();
         CardUserInfo cardUserInfo = new CardUserInfo();
-
+        String s1MD5 = DigestUtils.md5Hex(image1.getBytes());
         String s1 = byteToBase64(image1);
-        if (StringUtils.isNotBlank(s1) && !ObjectUtil.isAllFieldNull(redisTemplate.opsForValue().get(IMAGE_IDENTIFY_KEY + s1))) {
+        String s1Key = IMAGE_IDENTIFY_KEY + "drivingPermit:" + "front:" + s1MD5;
+        if (StringUtils.isNotBlank(s1) && !ObjectUtil.isAllFieldNull(redisTemplate.opsForValue().get(s1Key))) {
             //走缓存
-            BeanUtils.copyProperties(Objects.requireNonNull(redisTemplate.opsForValue().get(IMAGE_IDENTIFY_KEY + s1)),cardUserInfo);
+            BeanUtils.copyProperties(Objects.requireNonNull(redisTemplate.opsForValue().get(s1Key)), cardUserInfo);
         } else {
             //行驶证正面识别
             MultiObjectDetect.drivingPermitFront(s1, cardUserInfo);
             AssertionUtils.isEmpty(cardUserInfo, "行驶证正面识别失败");
             //设置缓存
-            redisTemplate.opsForValue().set(IMAGE_IDENTIFY_KEY + s1, cardUserInfo, 1, TimeUnit.DAYS);
+            redisTemplate.opsForValue().set(s1Key, cardUserInfo, 1, TimeUnit.DAYS);
         }
 
+        String s2MD5 = DigestUtils.md5Hex(image2.getBytes());
         String s2 = byteToBase64(image2);
-        if (StringUtils.isNotBlank(s2) && !ObjectUtil.isAllFieldNull(redisTemplate.opsForValue().get(IMAGE_IDENTIFY_KEY + s2))) {
+        String s2Key = IMAGE_IDENTIFY_KEY + "drivingPermit:" + "back:" + s2MD5;
+        if (StringUtils.isNotBlank(s2) && !ObjectUtil.isAllFieldNull(redisTemplate.opsForValue().get(s2Key))) {
             //走缓存
-            BeanUtils.copyProperties(Objects.requireNonNull(redisTemplate.opsForValue().get(IMAGE_IDENTIFY_KEY + s2)),cardUserInfo);
-        }else {
+            BeanUtils.copyProperties(Objects.requireNonNull(redisTemplate.opsForValue().get(s2Key)), cardUserInfo);
+        } else {
             //行驶证反面识别
             MultiObjectDetect.drivingPermitBack(s2, cardUserInfo);
             AssertionUtils.isEmpty(cardUserInfo, "行驶证正面识别失败");
             //设置缓存
-            redisTemplate.opsForValue().set(IMAGE_IDENTIFY_KEY + s2, cardUserInfo, 1, TimeUnit.DAYS);
+            redisTemplate.opsForValue().set(s2Key, cardUserInfo, 1, TimeUnit.DAYS);
         }
 
         AssertionUtils.isEmpty(cardUserInfo, "行驶证正面识别失败");