浏览代码

增加api 计次

xiewj 2 年之前
父节点
当前提交
9dade669a3
共有 23 个文件被更改,包括 526 次插入179 次删除
  1. 20 11
      pom.xml
  2. 27 0
      src/main/java/com/fdkankan/openApi/component/LocalOverCache.java
  3. 8 4
      src/main/java/com/fdkankan/openApi/component/ValidateApi.java
  4. 78 25
      src/main/java/com/fdkankan/openApi/component/ValidateApiAOP.java
  5. 22 0
      src/main/java/com/fdkankan/openApi/constant/HttpStatus.java
  6. 12 0
      src/main/java/com/fdkankan/openApi/constant/RedisKey.java
  7. 19 3
      src/main/java/com/fdkankan/openApi/controller/TestController.java
  8. 0 44
      src/main/java/com/fdkankan/openApi/controller/www/VirtualSceneController.java
  9. 23 0
      src/main/java/com/fdkankan/openApi/entity/system/UserAuthInfo.java
  10. 1 1
      src/main/java/com/fdkankan/openApi/entity/www/SceneEditControls.java
  11. 1 1
      src/main/java/com/fdkankan/openApi/entity/www/SceneEditInfo.java
  12. 1 1
      src/main/java/com/fdkankan/openApi/entity/www/SceneEditInfoExt.java
  13. 1 1
      src/main/java/com/fdkankan/openApi/entity/www/ScenePlus.java
  14. 1 1
      src/main/java/com/fdkankan/openApi/entity/www/ScenePlusExt.java
  15. 117 0
      src/main/java/com/fdkankan/openApi/exception/ApiBusinessException.java
  16. 0 45
      src/main/java/com/fdkankan/openApi/exception/CustomExceptionHandler.java
  17. 41 0
      src/main/java/com/fdkankan/openApi/exception/OpenApiGlobalExceptionHandler.java
  18. 26 0
      src/main/java/com/fdkankan/openApi/mapper/system/IUserAuthMapper.java
  19. 28 0
      src/main/java/com/fdkankan/openApi/service/system/IUserAuthService.java
  20. 90 0
      src/main/java/com/fdkankan/openApi/service/system/impl/UserAuthServiceImpl.java
  21. 10 10
      src/main/java/com/fdkankan/openApi/service/system/impl/UserServiceImpl.java
  22. 0 0
      src/main/java/com/fdkankan/openApi/vo/text.txt
  23. 0 32
      src/main/java/com/fdkankan/openApi/vo/www/CreateFicTitiousSceneParamVO.java

+ 20 - 11
pom.xml

@@ -44,12 +44,11 @@
             <version>3.0.0-SNAPSHOT</version>
         </dependency>
 
-
-<!--        <dependency>-->
-<!--            <groupId>com.fdkankan</groupId>-->
-<!--            <artifactId>4dkankan-utils-db</artifactId>-->
-<!--            <version>3.0.0-SNAPSHOT</version>-->
-<!--        </dependency>-->
+        <dependency>
+            <groupId>com.fdkankan</groupId>
+            <artifactId>4dkankan-utils-rabbitmq</artifactId>
+            <version>3.0.0-SNAPSHOT</version>
+        </dependency>
         <dependency>
             <groupId>mysql</groupId>
             <artifactId>mysql-connector-java</artifactId>
@@ -67,6 +66,11 @@
             <version>3.6.1</version>
         </dependency>
         <dependency>
+            <groupId>com.yomahub</groupId>
+            <artifactId>tlog-web-spring-boot-starter</artifactId>
+            <version>1.5.0</version>
+        </dependency>
+        <dependency>
             <groupId>com.baomidou</groupId>
             <artifactId>mybatis-plus-boot-starter</artifactId>
             <version>3.5.2</version>
@@ -131,11 +135,11 @@
         </dependency>
 
         <!--        htt请求工具-->
-        <dependency>
-            <groupId>com.dtflys.forest</groupId>
-            <artifactId>forest-spring-boot-starter</artifactId>
-            <version>1.5.19</version>
-        </dependency>
+        <!--        <dependency>-->
+        <!--            <groupId>com.dtflys.forest</groupId>-->
+        <!--            <artifactId>forest-spring-boot-starter</artifactId>-->
+        <!--            <version>1.5.19</version>-->
+        <!--        </dependency>-->
 
         <dependency>
             <groupId>com.google.firebase</groupId>
@@ -170,6 +174,11 @@
                 <type>pom</type>
                 <scope>import</scope>
             </dependency>
+            <!--Sentinel 服务限流,熔断-->
+            <dependency>
+                <groupId>com.alibaba.cloud</groupId>
+                <artifactId>spring-cloud-starter-alibaba-sentinel</artifactId>
+            </dependency>
 
             <dependency>
                 <groupId>org.springframework.cloud</groupId>

+ 27 - 0
src/main/java/com/fdkankan/openApi/component/LocalOverCache.java

@@ -0,0 +1,27 @@
+package com.fdkankan.openApi.component;
+
+import cn.hutool.cache.CacheUtil;
+import cn.hutool.cache.impl.TimedCache;
+import cn.hutool.core.date.DateUnit;
+import org.springframework.stereotype.Component;
+
+/**
+ * @author Xiewj
+ * @date 2023/2/17
+ */
+@Component
+public class LocalOverCache {
+
+    /**
+     * jvm内存缓存, 并发情况,可以减少redis连接
+     */
+    final TimedCache<String, Boolean> timedCache = CacheUtil.newTimedCache(DateUnit.DAY.getMillis() * 1);
+
+    public Boolean getTimedCache(String key) {
+        return timedCache.get(key);
+    }
+
+    public void setTimedCache(String key, Boolean flag) {
+        timedCache.put(key, flag);
+    }
+}

+ 8 - 4
src/main/java/com/fdkankan/openApi/component/ValidateApi.java

@@ -1,11 +1,15 @@
 package com.fdkankan.openApi.component;
 
-import java.lang.annotation.ElementType;
-import java.lang.annotation.Retention;
-import java.lang.annotation.RetentionPolicy;
-import java.lang.annotation.Target;
+import java.lang.annotation.*;
 
 @Target(ElementType.METHOD)
 @Retention(RetentionPolicy.RUNTIME)
+@Documented
 public @interface ValidateApi {
+
+
+    /**
+     * @return 接口根路径:接口名
+     */
+    String method() default "";
 }

+ 78 - 25
src/main/java/com/fdkankan/openApi/component/ValidateApiAOP.java

@@ -1,50 +1,103 @@
 package com.fdkankan.openApi.component;
 
+import cn.hutool.core.bean.BeanUtil;
+import cn.hutool.core.util.ObjectUtil;
 import cn.hutool.core.util.StrUtil;
 import com.fdkankan.common.constant.ErrorCode;
-import com.fdkankan.common.exception.BusinessException;
-import com.fdkankan.openApi.util.TokenUtil;
-import org.apache.commons.lang3.StringUtils;
-import org.aspectj.lang.ProceedingJoinPoint;
-import org.aspectj.lang.annotation.Around;
+import com.fdkankan.openApi.constant.HttpStatus;
+import com.fdkankan.openApi.constant.RedisKey;
+import com.fdkankan.openApi.exception.ApiBusinessException;
+import com.fdkankan.openApi.mq.dto.CallApiDTO;
+import com.fdkankan.openApi.mq.sender.Sender;
+import com.fdkankan.openApi.service.system.IUserAuthService;
+import com.fdkankan.redis.util.RedisUtil;
+import com.fdkankan.web.response.ResultData;
+import lombok.extern.slf4j.Slf4j;
+import org.aspectj.lang.JoinPoint;
+import org.aspectj.lang.annotation.AfterReturning;
 import org.aspectj.lang.annotation.Aspect;
-import org.aspectj.lang.annotation.Pointcut;
-import org.aspectj.lang.reflect.MethodSignature;
+import org.aspectj.lang.annotation.Before;
+import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.stereotype.Component;
 import org.springframework.web.context.request.RequestContextHolder;
 import org.springframework.web.context.request.ServletRequestAttributes;
 
 import javax.servlet.http.HttpServletRequest;
-import java.lang.reflect.Method;
-import java.util.Objects;
 
 @Component
 @Aspect
+@Slf4j
 public class ValidateApiAOP {
-    @Pointcut("@annotation(com.fdkankan.openApi.component.ValidateApi)")
-    public void pointCut(){}
-    @Around("pointCut()")
-    public Object handle(ProceedingJoinPoint proceedingJoinPoint) throws Throwable{
+
+    @Autowired
+    private RedisUtil redisUtil;
+    @Autowired
+    private Sender sender;
+    @Autowired
+    private IUserAuthService userAuthService;
+    @Autowired
+    private LocalOverCache localOverCache;
+
+    @Before("@annotation(validateApi)")
+    public void Before(JoinPoint joinPoint, ValidateApi validateApi) {
+        String method = validateApi.method();
+        log.info("前置拦截鉴权,{}", method);
         //获取请求对象
         ServletRequestAttributes servletRequestAttributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
         HttpServletRequest request = servletRequestAttributes.getRequest();
-        //处理
         //请求头
-        String authorization =  request.getHeader("Authorization");
+        String authorization = request.getHeader("Authorization");
+        if (ObjectUtil.isNull(authorization)) {
+            throw new ApiBusinessException(ErrorCode.AUTH_FAIL);
 
-        if(StrUtil.isEmpty(authorization)){
-            throw new BusinessException(ErrorCode.AUTH_FAIL);
         }
-
-        if (!TokenUtil.validateToken(authorization)){
-            throw new BusinessException(ErrorCode.AUTH_FAIL);
+        if (!authorization.equalsIgnoreCase("admin")) {
+            if (StrUtil.isEmpty(authorization)) {
+                throw new ApiBusinessException(ErrorCode.AUTH_FAIL);
+            }
+            //数据库鉴权
+            if (!userAuthService.findExistByAppKey(authorization)) {
+                throw new ApiBusinessException(ErrorCode.AUTH_FAIL);
+            }
+            //内存标记,减少redis访问
+            Boolean over = localOverCache.getTimedCache(authorization);
+            if (ObjectUtil.isNotNull(over) && over) {
+                throw new ApiBusinessException(HttpStatus.COUNT_OVER, "");
+            }
         }
+        //预减次数
+        long decrStock = redisUtil.decr(String.format(RedisKey.API_METHOD_COUNT, authorization, method), 1);
+        log.info("前置减次数后,{}", decrStock);
+        if (decrStock < 1) {
+            localOverCache.setTimedCache(authorization, true);
+        }
+    }
 
-        //注解处理
-        Class<?> clazz = proceedingJoinPoint.getTarget().getClass();
+    /**
+     * 处理请求后执行
+     *
+     * @param joinPoint
+     * @param validateApi
+     * @param jsonResult
+     * @return
+     */
+    @AfterReturning(pointcut = "@annotation(validateApi)", returning = "jsonResult")
+    public void doAfterReturning(JoinPoint joinPoint, ValidateApi validateApi, Object jsonResult) throws NoSuchMethodException {
+        String method = validateApi.method();
+        log.info("后置拦截截鉴权计数,{},返回内容{}", method, jsonResult);
+        //获取请求对象
+        ServletRequestAttributes servletRequestAttributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
+        HttpServletRequest request = servletRequestAttributes.getRequest();
+        String authorization = request.getHeader("Authorization");
         //方法签名
-        MethodSignature methodSignature = (MethodSignature) proceedingJoinPoint.getSignature();
-        Method method = clazz.getDeclaredMethod(methodSignature.getName(), methodSignature.getParameterTypes());
-        return proceedingJoinPoint.proceed();
+        if (BeanUtil.toBean(jsonResult, ResultData.class).getCode() == 0) {
+            //预减次数
+            //进入队列,同步数据库
+            log.info("进入队列,同步数据库");
+            sender.callApi(new CallApiDTO(authorization));
+        } else {
+            log.info("业务非正常返回,进行次数回滚+1:{}");
+            long incrStock = redisUtil.incr(String.format(RedisKey.API_METHOD_COUNT, authorization, method), 1);
+        }
     }
 }

+ 22 - 0
src/main/java/com/fdkankan/openApi/constant/HttpStatus.java

@@ -7,4 +7,26 @@ package com.fdkankan.openApi.constant;
  */
 public enum HttpStatus {
 
+    COUNT_OVER(10001, "次数已使用完")
+   ;
+
+    private Integer code;
+    private String message;
+
+    private HttpStatus(Integer code, String message) {
+        this.code = code;
+        this.message = message;
+    }
+
+    public Integer code() {
+        return this.code;
+    }
+
+    public String message() {
+        return this.message;
+    }
+
+    public String formatMessage(Object... args) {
+        return String.format(this.message, args);
+    }
 }

+ 12 - 0
src/main/java/com/fdkankan/openApi/constant/RedisKey.java

@@ -0,0 +1,12 @@
+package com.fdkankan.openApi.constant;
+
+/**
+ * @author Xiewj
+ * @date 2023/2/17
+ */
+public class RedisKey {
+
+    public static String USER_APP_KEY_INFO = "api:user:%s";
+    //API:用户key:接口名
+    public static String API_METHOD_COUNT = "api:count:%s";
+}

+ 19 - 3
src/main/java/com/fdkankan/openApi/controller/TestController.java

@@ -2,14 +2,18 @@ package com.fdkankan.openApi.controller;
 
 import cn.dev33.satoken.annotation.SaCheckLogin;
 import cn.dev33.satoken.annotation.SaIgnore;
+import com.fdkankan.openApi.component.LocalOverCache;
+import com.fdkankan.openApi.component.ValidateApi;
 import com.fdkankan.openApi.entity.laser.SceneEntity;
 import com.fdkankan.openApi.entity.system.User;
 import com.fdkankan.openApi.service.laser.SceneService;
+import com.fdkankan.openApi.service.system.IUserAuthService;
 import com.fdkankan.openApi.service.system.IUserService;
 import com.fdkankan.web.controller.BaseController;
 import com.fdkankan.web.response.ResultData;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.web.bind.annotation.GetMapping;
+import org.springframework.web.bind.annotation.PathVariable;
 import org.springframework.web.bind.annotation.RequestMapping;
 import org.springframework.web.bind.annotation.RestController;
 
@@ -23,17 +27,29 @@ import org.springframework.web.bind.annotation.RestController;
 public class TestController extends BaseController {
     @Autowired
     private SceneService sceneService;
-    
+
     @Autowired
     private IUserService userService;
-    
+    @Autowired
+    private IUserAuthService userAuthService;
+
+    @Autowired
+    private LocalOverCache localOverCache;
 
     @GetMapping("/test")
     @SaIgnore
-    public ResultData test(){
+    @ValidateApi(method = "test:test")
+    public ResultData test() {
         SceneEntity bySceneCode = sceneService.findBySceneCode("SS-t-yotkjVf55A");
         User byUserName = userService.findByUserName("18666186515");
+        return ResultData.ok("ok");
+    }
 
+    @GetMapping("/updateCount/{appKey}/{count}")
+    @SaIgnore
+    public ResultData updateCount(@PathVariable String appKey, @PathVariable Integer count) {
+        localOverCache.setTimedCache(appKey, false);
+        userAuthService.updateCallCounts(appKey, count);
         return ResultData.ok("ok");
     }
 }

+ 0 - 44
src/main/java/com/fdkankan/openApi/controller/www/VirtualSceneController.java

@@ -1,44 +0,0 @@
-package com.fdkankan.openApi.controller.www;
-
-
-import cn.dev33.satoken.annotation.SaIgnore;
-import com.fdkankan.openApi.service.www.IScenePlusService;
-import com.fdkankan.openApi.vo.www.CreateFicTitiousSceneParamVO;
-import com.fdkankan.web.response.ResultData;
-import org.springframework.beans.factory.annotation.Autowired;
-import org.springframework.validation.annotation.Validated;
-import org.springframework.web.bind.annotation.PostMapping;
-import org.springframework.web.bind.annotation.RequestBody;
-import org.springframework.web.bind.annotation.RequestMapping;
-
-import org.springframework.web.bind.annotation.RestController;
-
-/**
- *
- * 场景主表 前端控制器
- *
- * @author 
- * @since 2023-02-15
- */
-@RestController
-@RequestMapping("/scene")
-public class VirtualSceneController {
-
-    @Autowired
-    private IScenePlusService scenePlusService;
-
-    /**
-     * 创建虚拟场景
-     * @param param
-     * @return
-     * @throws Exception
-     */
-    @SaIgnore
-    @PostMapping("/createVirtualScene")
-    public ResultData createVirtualScene(@RequestBody @Validated CreateFicTitiousSceneParamVO param) throws Exception {
-        return scenePlusService.createVirtualScene(param);
-    }
-
-
-}
-

+ 23 - 0
src/main/java/com/fdkankan/openApi/entity/system/UserAuthInfo.java

@@ -0,0 +1,23 @@
+package com.fdkankan.openApi.entity.system;
+
+import com.baomidou.mybatisplus.annotation.TableField;
+import com.baomidou.mybatisplus.annotation.TableName;
+import lombok.Data;
+
+/**
+ * @author Xiewj
+ * @date 2022/12/19
+ */
+@Data
+@TableName("t_users_auth_info")
+public class UserAuthInfo extends SystemBaseEntity {
+
+    @TableField("user_id")
+    private Integer userId;
+    @TableField("app_key")
+    private String appKey;
+
+    @TableField("call_counts")
+    private Integer callCounts;
+
+}

+ 1 - 1
src/main/java/com/fdkankan/openApi/entity/www/SceneEditControls.java

@@ -107,7 +107,7 @@ public class SceneEditControls implements Serializable {
      * A-有效,I-无效
      */
     @TableField("rec_status")
-    @TableLogic(value = "A", delval = "I")
+    @TableLogic
     private String recStatus;
 
 

+ 1 - 1
src/main/java/com/fdkankan/openApi/entity/www/SceneEditInfo.java

@@ -197,7 +197,7 @@ public class SceneEditInfo implements Serializable {
      * A-有效,I-无效
      */
     @TableField("rec_status")
-    @TableLogic(value = "A", delval = "I")
+    @TableLogic
     private String recStatus;
 
 

+ 1 - 1
src/main/java/com/fdkankan/openApi/entity/www/SceneEditInfoExt.java

@@ -125,7 +125,7 @@ public class SceneEditInfoExt implements Serializable {
      * A-有效,I-无效
      */
     @TableField("rec_status")
-    @TableLogic(value = "A", delval = "I")
+    @TableLogic
     private String recStatus;
 
 

+ 1 - 1
src/main/java/com/fdkankan/openApi/entity/www/ScenePlus.java

@@ -116,7 +116,7 @@ public class ScenePlus implements Serializable {
      * A-有效,I-无效
      */
     @TableField("rec_status")
-    @TableLogic(value = "A", delval = "I")
+    @TableLogic
     private String recStatus;
 
 

+ 1 - 1
src/main/java/com/fdkankan/openApi/entity/www/ScenePlusExt.java

@@ -170,7 +170,7 @@ public class ScenePlusExt implements Serializable {
      * A-有效,I-无效
      */
     @TableField("rec_status")
-    @TableLogic(value = "A", delval = "I")
+    @TableLogic
     private String recStatus;
 
     @TableField("is_obj")

+ 117 - 0
src/main/java/com/fdkankan/openApi/exception/ApiBusinessException.java

@@ -0,0 +1,117 @@
+package com.fdkankan.openApi.exception;
+
+import com.fdkankan.common.constant.CameraConstant;
+import com.fdkankan.common.constant.ErrorCode;
+import com.fdkankan.common.constant.ServerCode;
+import com.fdkankan.openApi.constant.HttpStatus;
+
+/**
+ * @author Xiewj
+ * @date 2023/2/16
+ */
+public class ApiBusinessException extends RuntimeException {
+    private int code;
+    private String message;
+
+    private String authorization;
+
+    public ApiBusinessException(ErrorCode errorCode, String authorization) {
+        this.code = errorCode.code();
+        this.message = errorCode.message();
+        this.authorization = authorization;
+    }
+
+    public ApiBusinessException(HttpStatus httpStatus, String authorization) {
+        this.code = httpStatus.code();
+        this.message = httpStatus.message();
+        this.authorization = authorization;
+    }
+
+    public ApiBusinessException(ServerCode serverCode) {
+        this.code = serverCode.code();
+        this.message = serverCode.message();
+    }
+
+    public ApiBusinessException(CameraConstant errorCode) {
+        this.code = errorCode.code();
+        this.message = errorCode.message();
+    }
+
+    public ApiBusinessException(int code, String msg) {
+        this.code = code;
+        this.message = msg;
+    }
+
+    public ApiBusinessException(ServerCode serverCode, Object... args) {
+        this.code = serverCode.code();
+        this.message = serverCode.formatMessage(args);
+    }
+
+    public ApiBusinessException(ErrorCode errorCode, Object... args) {
+        this.code = errorCode.code();
+        this.message = errorCode.formatMessage(args);
+    }
+
+    public int getCode() {
+        return this.code;
+    }
+
+    @Override
+    public String getMessage() {
+        return this.message;
+    }
+
+    public void setCode(int code) {
+        this.code = code;
+    }
+
+    public void setMessage(String message) {
+        this.message = message;
+    }
+
+    @Override
+    public boolean equals(Object o) {
+        if (o == this) {
+            return true;
+        } else if (!(o instanceof ApiBusinessException)) {
+            return false;
+        } else {
+            ApiBusinessException other = (ApiBusinessException) o;
+            if (!other.canEqual(this)) {
+                return false;
+            } else if (this.getCode() != other.getCode()) {
+                return false;
+            } else {
+                Object this$message = this.getMessage();
+                Object other$message = other.getMessage();
+                if (this$message == null) {
+                    if (other$message != null) {
+                        return false;
+                    }
+                } else if (!this$message.equals(other$message)) {
+                    return false;
+                }
+
+                return true;
+            }
+        }
+    }
+
+    protected boolean canEqual(Object other) {
+        return other instanceof ApiBusinessException;
+    }
+
+    @Override
+    public int hashCode() {
+        int result = 1;
+        result = result * 59 + this.getCode();
+        Object $message = this.getMessage();
+        result = result * 59 + ($message == null ? 43 : $message.hashCode());
+        return result;
+    }
+
+    @Override
+    public String toString() {
+        return "ApiBusinessException(code=" + this.getCode() + ", message=" + this.getMessage() + ")";
+    }
+}

+ 0 - 45
src/main/java/com/fdkankan/openApi/exception/CustomExceptionHandler.java

@@ -1,45 +0,0 @@
-package com.fdkankan.openApi.exception;
-
-import cn.dev33.satoken.exception.SaTokenException;
-import cn.hutool.core.util.NumberUtil;
-import com.fdkankan.common.constant.ErrorCode;
-import com.fdkankan.common.exception.BusinessException;
-import com.fdkankan.web.response.ResultData;
-import lombok.extern.slf4j.Slf4j;
-import org.springframework.web.bind.annotation.ExceptionHandler;
-import org.springframework.web.bind.annotation.ResponseBody;
-import org.springframework.web.bind.annotation.RestControllerAdvice;
-
-/**
- * 全局异常处理器
- */
-@RestControllerAdvice
-@Slf4j
-public class CustomExceptionHandler {
-
-//    /**
-//     * 处理未知异常
-//     */
-//    @ResponseBody
-//    @ExceptionHandler(value = Exception.class)
-//    public ResultData exceptionHandler(Exception e) throws Exception {
-//        log.error("服务错误:", e);
-//        return ResultData.error( 500, "系统错误");
-//    }
-    @ExceptionHandler(SaTokenException.class)
-    public ResultData handlerSaTokenException(SaTokenException e) {
-        if( NumberUtil.equals(e.getCode() , 11012)|| NumberUtil.equals(e.getCode() , 11011)) {
-            return ResultData.error(ErrorCode.USER_NOT_LOGIN);
-        }
-        return ResultData.error(e.getCode(),e.getMessage());
-    }
-//    /**
-//     * 处理业务异常
-//     */
-//    @ResponseBody
-//    @ExceptionHandler(value = BusinessException.class)
-//    public ResultData businessExceptionHandler(BusinessException e) {
-//        log.warn("业务异常code:{},message:{}", e.getCode(), e.getMessage());
-//        return ResultData.error(e.getCode(), e.getMessage());
-//    }
-}

+ 41 - 0
src/main/java/com/fdkankan/openApi/exception/OpenApiGlobalExceptionHandler.java

@@ -0,0 +1,41 @@
+package com.fdkankan.openApi.exception;
+
+import cn.dev33.satoken.exception.SaTokenException;
+import cn.hutool.core.util.NumberUtil;
+import com.fdkankan.common.constant.ErrorCode;
+import com.fdkankan.web.exception.GlobalExceptionHandler;
+import com.fdkankan.web.response.ResultData;
+import lombok.extern.slf4j.Slf4j;
+import org.springframework.http.HttpStatus;
+import org.springframework.web.bind.annotation.ExceptionHandler;
+import org.springframework.web.bind.annotation.ResponseBody;
+import org.springframework.web.bind.annotation.ResponseStatus;
+import org.springframework.web.bind.annotation.RestControllerAdvice;
+
+/**
+ * 全局异常处理器
+ */
+@Slf4j
+@RestControllerAdvice
+public class OpenApiGlobalExceptionHandler extends GlobalExceptionHandler {
+
+
+    @ExceptionHandler(SaTokenException.class)
+    public ResultData handlerSaTokenException(SaTokenException e) {
+        if (NumberUtil.equals(e.getCode(), 11012) || NumberUtil.equals(e.getCode(), 11011)) {
+            return ResultData.error(ErrorCode.USER_NOT_LOGIN);
+        }
+        return ResultData.error(e.getCode(), e.getMessage());
+    }
+
+    /**
+     * 处理业务异常
+     */
+    @ResponseBody
+    @ExceptionHandler(value = ApiBusinessException.class)
+    @ResponseStatus(HttpStatus.BAD_REQUEST) // 这里非常关键,这是修改的状态吗
+    public ResultData ApiBusinessExceptionHandler(ApiBusinessException e) {
+        log.error("业务异常code:{},message:{}", e.getCode(), e.getMessage());
+        return ResultData.error(e.getCode(), e.getMessage());
+    }
+}

+ 26 - 0
src/main/java/com/fdkankan/openApi/mapper/system/IUserAuthMapper.java

@@ -0,0 +1,26 @@
+package com.fdkankan.openApi.mapper.system;
+
+import com.baomidou.mybatisplus.core.mapper.BaseMapper;
+import com.fdkankan.openApi.entity.system.UserAuthInfo;
+import org.apache.ibatis.annotations.Mapper;
+import org.apache.ibatis.annotations.Param;
+import org.springframework.stereotype.Component;
+
+/**
+ * <p>
+ * Mapper 接口
+ * </p>
+ *
+ * @author
+ * @since 2023年2月10日14:38:33
+ */
+@Mapper
+@Component("IUserAuthMapper")
+public interface IUserAuthMapper extends BaseMapper<UserAuthInfo> {
+    int updateCallCounts(@Param("appKey") String appKey);
+
+    int updateCallCountsByParam(@Param("appKey") String appKey, @Param("count") Integer count);
+
+    int updateCallCountsByParamAndType(@Param("appKey") String appKey, @Param("count") Integer count, @Param("type") Integer type);
+
+}

+ 28 - 0
src/main/java/com/fdkankan/openApi/service/system/IUserAuthService.java

@@ -0,0 +1,28 @@
+package com.fdkankan.openApi.service.system;
+
+import com.baomidou.mybatisplus.extension.service.IService;
+import com.fdkankan.openApi.entity.system.UserAuthInfo;
+
+/**
+ * <p>
+ * 服务类
+ * </p>
+ *
+ * @author
+ * @since 2022年12月19日09:44:17
+ */
+public interface IUserAuthService extends IService<UserAuthInfo> {
+
+    UserAuthInfo findByUserId(Integer userId);
+
+    UserAuthInfo findByAppKey(String appKey);
+
+    boolean findExistByAppKey(String appKey);
+
+    int updateCallCounts(String appKey);
+
+    boolean updateCallCounts(String appKey, Integer count);
+
+    public boolean updateCallCountsByType(String appKey, Integer count, Integer type);
+
+}

+ 90 - 0
src/main/java/com/fdkankan/openApi/service/system/impl/UserAuthServiceImpl.java

@@ -0,0 +1,90 @@
+package com.fdkankan.openApi.service.system.impl;
+
+import cn.hutool.core.util.ObjectUtil;
+import com.baomidou.dynamic.datasource.annotation.DS;
+import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
+import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
+import com.fdkankan.openApi.constant.RedisKey;
+import com.fdkankan.openApi.entity.system.UserAuthInfo;
+import com.fdkankan.openApi.mapper.system.IUserAuthMapper;
+import com.fdkankan.openApi.service.system.IUserAuthService;
+import com.fdkankan.redis.util.RedisUtil;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.stereotype.Service;
+import org.springframework.transaction.annotation.Isolation;
+import org.springframework.transaction.annotation.Propagation;
+import org.springframework.transaction.annotation.Transactional;
+
+@DS("system")
+@Service
+public class UserAuthServiceImpl extends ServiceImpl<IUserAuthMapper, UserAuthInfo> implements IUserAuthService {
+
+    @Autowired
+    RedisUtil redisUtil;
+
+
+    @Override
+    public UserAuthInfo findByUserId(Integer userId) {
+        LambdaQueryWrapper<UserAuthInfo> wrapper = new LambdaQueryWrapper<>();
+        wrapper.eq(UserAuthInfo::getUserId, userId);
+        return getOne((wrapper));
+    }
+
+    @Override
+    public UserAuthInfo findByAppKey(String appKey) {
+        LambdaQueryWrapper<UserAuthInfo> wrapper = new LambdaQueryWrapper<>();
+        wrapper.eq(UserAuthInfo::getAppKey, appKey);
+        return getOne((wrapper));
+    }
+
+    @Override
+    public boolean findExistByAppKey(String appKey) {
+        String key = String.format(RedisKey.USER_APP_KEY_INFO, appKey);
+        if (redisUtil.hasKey(key)) {
+            return true;
+        } else {
+            UserAuthInfo userAuthInfo = findByAppKey(appKey);
+            if (ObjectUtil.isNotNull(userAuthInfo)) {
+                redisUtil.set(key, userAuthInfo.getAppKey(), 3600);
+                return true;
+            } else {
+                return false;
+            }
+        }
+    }
+
+    @Override
+    @Transactional(isolation = Isolation.READ_COMMITTED, propagation = Propagation.REQUIRED)
+    public int updateCallCounts(String appKey) {
+        return getBaseMapper().updateCallCounts(appKey);
+    }
+
+    @Override
+    public boolean updateCallCounts(String appKey, Integer count) {
+        int i = getBaseMapper().updateCallCountsByParam(appKey, count);
+        if (i > 0) {
+            redisUtil.set(String.format(RedisKey.API_METHOD_COUNT, appKey), String.valueOf(count));
+            return true;
+        }
+        return false;
+    }
+
+    //1:累加,2:累减
+    @Override
+    public boolean updateCallCountsByType(String appKey, Integer count, Integer type) {
+        int i = getBaseMapper().updateCallCountsByParamAndType(appKey, count, type);
+        if (i > 0) {
+            switch (type) {
+                case 1:
+                    redisUtil.incr(String.format(RedisKey.API_METHOD_COUNT, appKey), count);
+                    break;
+                case 2:
+                    redisUtil.decr(String.format(RedisKey.API_METHOD_COUNT, appKey), count);
+                    break;
+
+            }
+            return true;
+        }
+        return false;
+    }
+}

+ 10 - 10
src/main/java/com/fdkankan/openApi/service/system/impl/UserServiceImpl.java

@@ -17,12 +17,12 @@ public class UserServiceImpl extends ServiceImpl<IUserMapper, User> implements I
     @Override
     public User findByUserName(String userName) {
         LambdaQueryWrapper<User> wrapper = new LambdaQueryWrapper<>();
-        wrapper.in(User::getUserName,userName);
+        wrapper.eq(User::getUserName, userName);
         User one = this.getOne(wrapper);
-        if (ObjectUtil.isNotNull(one)){
+        if (ObjectUtil.isNotNull(one)) {
             return one;
-        }else {
-            User u=new User();
+        } else {
+            User u = new User();
             u.setUserName(userName);
             boolean save = save(u);
             return u;
@@ -30,18 +30,18 @@ public class UserServiceImpl extends ServiceImpl<IUserMapper, User> implements I
     }
 
     @Override
-    public User findByUserName(String userName, Integer fdkkId,String head) {
+    public User findByUserName(String userName, Integer fdkkId, String head) {
         LambdaQueryWrapper<User> wrapper = new LambdaQueryWrapper<>();
-        wrapper.in(User::getUserName,userName);
+        wrapper.eq(User::getUserName, userName);
         User one = this.getOne(wrapper);
-        if (ObjectUtil.isNotNull(one)){
-            if (ObjectUtil.isNull(one.getHead())){
+        if (ObjectUtil.isNotNull(one)) {
+            if (ObjectUtil.isNull(one.getHead())) {
                 one.setHead(head);
                 updateById(one);
             }
             return one;
-        }else {
-            User u=new User();
+        } else {
+            User u = new User();
             u.setUserName(userName);
             u.setId(fdkkId);
             boolean save = save(u);

+ 0 - 0
src/main/java/com/fdkankan/openApi/vo/text.txt


+ 0 - 32
src/main/java/com/fdkankan/openApi/vo/www/CreateFicTitiousSceneParamVO.java

@@ -1,32 +0,0 @@
-package com.fdkankan.openApi.vo.www;
-
-import lombok.Data;
-
-import javax.validation.constraints.NotBlank;
-import javax.validation.constraints.NotNull;
-
-@Data
-public class CreateFicTitiousSceneParamVO {
-
-    @NotBlank(message = "num不能为空")
-    private String num;
-
-    @NotBlank(message = "objName不能为空")
-    private String objName;
-
-    @NotBlank(message = "mtlName不能为空")
-    private String mtlName;
-
-    @NotNull(message = "userId不能为空")
-    private Long userId;
-
-    @NotBlank(message = "sceneResolution不能为空")
-    private String sceneResolution;
-
-    @NotBlank(message = "sceneFrom不能为空")
-    private String sceneFrom;
-
-    @NotBlank(message = "sceneKind不能为空")
-    private String sceneKind;
-
-}