Browse Source

异常设置。

zhengqiang 4 năm trước cách đây
mục cha
commit
f03a38762b

+ 7 - 0
common/src/main/java/com/jpsoft/bus/modules/common/exception/CustomException.java

@@ -0,0 +1,7 @@
+package com.jpsoft.bus.modules.common.exception;
+
+public class CustomException extends Exception {
+    public CustomException(String message){
+        super(message);
+    }
+}

+ 21 - 4
web/src/main/java/com/jpsoft/bus/modules/mobile/controller/MerchantApiController.java

@@ -16,6 +16,7 @@ import com.jpsoft.bus.modules.bus.entity.*;
 import com.jpsoft.bus.modules.bus.service.*;
 import com.jpsoft.bus.modules.common.dto.MessageResult;
 import com.jpsoft.bus.modules.common.dto.Sort;
+import com.jpsoft.bus.modules.common.exception.CustomException;
 import com.jpsoft.bus.modules.common.utils.DES3;
 import com.jpsoft.bus.modules.common.utils.JwtUtil;
 import com.jpsoft.bus.modules.common.utils.OSSUtil;
@@ -43,6 +44,7 @@ import org.springframework.beans.BeanUtils;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.beans.factory.annotation.Value;
 import org.springframework.data.redis.core.RedisTemplate;
+import org.springframework.data.redis.core.ValueOperations;
 import org.springframework.web.bind.annotation.*;
 
 import java.math.BigDecimal;
@@ -110,6 +112,8 @@ public class MerchantApiController {
     @Autowired
     private AccountInfoService accountInfoService;
 
+    @Autowired
+    private ValueOperations<String,Object> valueOperations;
 
     @PostMapping("getVerifyCode")
     @ApiOperation(value = "商户获取短信验证码(公开接口)")
@@ -1184,22 +1188,29 @@ public class MerchantApiController {
         MessageResult<DriverRecordInfo> messageResult = new MessageResult<>();
 
         try {
+            boolean absent = valueOperations.setIfAbsent("driverAttendance_" + subject, true, 3, TimeUnit.SECONDS);
+
+            if (!absent){
+                throw new CustomException("3秒内重复打卡!");
+            }
+
             AccountInfo accountInfo = accountInfoService.get(subject);
 
             if (accountInfo == null) {
-                throw new Exception("当前用户不存在!");
+
+                throw new CustomException("当前用户不存在!");
             }
 
             DriverInfo driverInfo = driverInfoService.findByPhone(accountInfo.getPhone());
 
             if (driverInfo == null){
-                throw new Exception("当前用户不是司机!");
+                throw new CustomException("当前用户不是司机!");
             }
 
             VehicleInfo vehicleInfo = vehicleInfoService.findByCarNum(carNum);
 
             if (vehicleInfo==null){
-                throw new Exception("车牌号" + carNum + "不存在!");
+                throw new CustomException("车牌号" + carNum + "不存在!");
             }
 
             DriverRecordInfo driverRecordInfo = new DriverRecordInfo();
@@ -1224,7 +1235,13 @@ public class MerchantApiController {
             messageResult.setResult(true);
             messageResult.setCode(200);
         } catch (Exception ex) {
-            log.error(ex.getMessage(),ex);
+            if(ex instanceof CustomException) {
+                log.error(ex.getMessage());
+            }
+            else{
+                log.error(ex.getMessage(),ex);
+            }
+
             messageResult.setResult(false);
             messageResult.setMessage(ex.getMessage());
         }