Просмотр исходного кода

优化 证件识别内容缓存bug

y 2 лет назад
Родитель
Сommit
83c3cb01fa

+ 57 - 49
src/main/java/com/ydtech/modules/order/service/impl/IdentifyServiceImpl.java

@@ -47,31 +47,36 @@ public class IdentifyServiceImpl implements IdentifyService {
     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);
-        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(s1Key)), customerInfoVO);
-        } else {
-            //识别身份证正面
-            Idcard.getIdCardFront(s1, customerInfoVO);
-            AssertionUtils.isEmpty(customerInfoVO, "身份证识别失败");
-            //设置缓存
-            redisTemplate.opsForValue().set(s1Key, customerInfoVO, 1, TimeUnit.DAYS);
+        if (image1 != null){
+            String s1MD5 = DigestUtils.md5Hex(image1.getBytes());
+            String s1 = byteToBase64(image1);
+            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(s1Key)), customerInfoVO);
+            } else {
+                //识别身份证正面
+                Idcard.getIdCardFront(s1, customerInfoVO);
+                AssertionUtils.isEmpty(customerInfoVO, "身份证识别失败");
+                //设置缓存
+                redisTemplate.opsForValue().set(s1Key, customerInfoVO, 1, TimeUnit.DAYS);
+            }
         }
-        String s2MD5 = DigestUtils.md5Hex(image2.getBytes());
-        String s2 = byteToBase64(image2);
-        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(s2Key)), customerInfoVO);
-        } else {
-            //识别身份证背面
-            Idcard.getIdCardBack(s2, customerInfoVO);
-            AssertionUtils.isEmpty(customerInfoVO, "身份证识别失败");
-            //设置缓存
-            redisTemplate.opsForValue().set(s2Key, customerInfoVO, 1, TimeUnit.DAYS);
+
+        if (image2 != null) {
+            String s2MD5 = DigestUtils.md5Hex(image2.getBytes());
+            String s2 = byteToBase64(image2);
+            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(s2Key)), customerInfoVO);
+            } else {
+                //识别身份证背面
+                Idcard.getIdCardBack(s2, customerInfoVO);
+                AssertionUtils.isEmpty(customerInfoVO, "身份证识别失败");
+                //设置缓存
+                redisTemplate.opsForValue().set(s2Key, customerInfoVO, 1, TimeUnit.DAYS);
+            }
         }
 
         AssertionUtils.isEmpty(customerInfoVO, "身份证识别失败");
@@ -83,34 +88,37 @@ public class IdentifyServiceImpl implements IdentifyService {
     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);
-        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(s1Key)), cardUserInfo);
-        } else {
-            //行驶证正面识别
-            MultiObjectDetect.drivingPermitFront(s1, cardUserInfo);
-            AssertionUtils.isEmpty(cardUserInfo, "行驶证正面识别失败");
-            //设置缓存
-            redisTemplate.opsForValue().set(s1Key, cardUserInfo, 1, TimeUnit.DAYS);
+        if (image1 != null) {
+            String s1MD5 = DigestUtils.md5Hex(image1.getBytes());
+            String s1 = byteToBase64(image1);
+            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(s1Key)), cardUserInfo);
+            } else {
+                //行驶证正面识别
+                MultiObjectDetect.drivingPermitFront(s1, cardUserInfo);
+                AssertionUtils.isEmpty(cardUserInfo, "行驶证正面识别失败");
+                //设置缓存
+                redisTemplate.opsForValue().set(s1Key, cardUserInfo, 1, TimeUnit.DAYS);
+            }
         }
 
-        String s2MD5 = DigestUtils.md5Hex(image2.getBytes());
-        String s2 = byteToBase64(image2);
-        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(s2Key)), cardUserInfo);
-        } else {
-            //行驶证反面识别
-            MultiObjectDetect.drivingPermitBack(s2, cardUserInfo);
-            AssertionUtils.isEmpty(cardUserInfo, "行驶证正面识别失败");
-            //设置缓存
-            redisTemplate.opsForValue().set(s2Key, cardUserInfo, 1, TimeUnit.DAYS);
+        if (image2 != null) {
+            String s2MD5 = DigestUtils.md5Hex(image2.getBytes());
+            String s2 = byteToBase64(image2);
+            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(s2Key)), cardUserInfo);
+            } else {
+                //行驶证反面识别
+                MultiObjectDetect.drivingPermitBack(s2, cardUserInfo);
+                AssertionUtils.isEmpty(cardUserInfo, "行驶证正面识别失败");
+                //设置缓存
+                redisTemplate.opsForValue().set(s2Key, cardUserInfo, 1, TimeUnit.DAYS);
+            }
         }
-
         AssertionUtils.isEmpty(cardUserInfo, "行驶证正面识别失败");
         map.put("carInfo", cardUserInfo);
         return HttpResult.ok("success", map);