Commit 0e98bcdb authored by 陈炎's avatar 陈炎

🎨 红旗数据api-优化3

parent 496c245c
package com.afanticar.afantiopenapi.config;
import com.afanticar.afantiopenapi.constant.ExceptionEnum;
import com.afanticar.afantiopenapi.controller.BaseController;
import com.afanticar.afantiopenapi.model.BaseResponse;
import org.apache.commons.lang3.exception.ExceptionUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;
import javax.servlet.http.HttpServletResponse;
import javax.validation.ConstraintViolationException;
import java.util.StringJoiner;
@RestControllerAdvice
public class GlobalExceptionHandler {
protected final Logger LOGGER = LoggerFactory.getLogger(this.getClass());
@ExceptionHandler(ConstraintViolationException.class)
public BaseResponse processMethodArgumentNotValidException(HttpServletResponse response, ConstraintViolationException e) {
StringJoiner message = new StringJoiner(";");
e.getConstraintViolations().forEach(constraintViolation -> message.add(constraintViolation.getMessage()));
LOGGER.error("参数校验异常 => ", e);
return handleError(ExceptionEnum.PARAM_INVALID.getCode(), String.join(":", ExceptionEnum.PARAM_INVALID.getMessage(), message.toString()), null);
}
@ExceptionHandler(Exception.class)
public BaseResponse processException(Exception e) {
LOGGER.error("未知异常 => :{}", ExceptionUtils.getStackTrace(e));
return handleError(ExceptionEnum.ERROR, e);
}
private BaseResponse<Object> handleError(int code, String message, Exception ex) {
return BaseController.error(code + "", message);
}
private BaseResponse<Object> handleError(ExceptionEnum exceptionEnum, Exception ex) {
// 状态码
Integer code = exceptionEnum.getCode();
// 状态码描述
String message = exceptionEnum.getMessage();
return handleError(code, message, ex);
}
}
...@@ -17,10 +17,7 @@ public class MybatisPlusConfig { ...@@ -17,10 +17,7 @@ public class MybatisPlusConfig {
@Bean @Bean
public MybatisPlusInterceptor mybatisPlusInterceptor() { public MybatisPlusInterceptor mybatisPlusInterceptor() {
MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor(); MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
PaginationInnerInterceptor paginationInnerInterceptor = new PaginationInnerInterceptor(DbType.MYSQL); interceptor.addInnerInterceptor(new PaginationInnerInterceptor(DbType.MYSQL));
paginationInnerInterceptor.setOverflow(true);
paginationInnerInterceptor.setMaxLimit(500L);
interceptor.addInnerInterceptor(paginationInnerInterceptor);
return interceptor; return interceptor;
} }
......
package com.afanticar.afantiopenapi.config; package com.afanticar.afantiopenapi.constant;
/** /**
* @author chin * @author chin
......
package com.afanticar.afantiopenapi.constant;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;
/**
* @author Wsr
* @ClassName ExceptionEnum
* @desc 异常枚举
* @date 2023/01/06 18:59
**/
@AllArgsConstructor
@NoArgsConstructor
public enum ExceptionEnum {
/**
*
*/
USER_ERROR(400, "用户错误"),
ERROR(500, "发生未知异常,请联系管理员"),
SUCCESS(200, "成功"),
PARAM_INVALID(90001, "请求参数不合法"),
FLOW_LIMIT_ALL(10010, "访问过于频繁"),
// ==================== token ====================
/**
* token 无效 401
*/
TOKEN_INVALID(10002, "token 无效"),
/**
* token 过期
*/
TOKEN_EXPIRED(10003, "token过期"),
/**
* token 不存在
*/
TOKEN_NOT_EXIST(10004, "请求头未传递token"),
// ==================== 限流 ====================
/**
* api限流
*/
API_LIMIT_EXCEEDED(20001, "api限流"),
/**
* api-app限流
*/
API_APP_LIMIT_EXCEEDED(20002, "特殊app限流"),
/**
* app限流
*/
APP_LIMIT_EXCEEDED(20003, "app限流"),
// ==================== 授权 ====================
/**
* app未授权
*/
APP_UNAUTHORIZED(30001, "app未授权"),
/**
* app不存在
*/
APP_NOT_EXIST(30002, "app不存在"),
// ==================== 超时 ====================
/**
* 接口超时
*/
API_TIMEOUT(40001, "接口超时"),
/**
* sql超时
*/
SQL_TIMEOUT(40002, "sql超时"),
// ==================== app ====================
/**
* app未注册
*/
APP_UNREGISTERED(50001, "app未注册"),
/**
* app密钥错误 业务异常
*/
APP_ERROR_SECRET(50002, "app密钥错误"),
// ==================== data ====================
/**
* 字段重复
*/
DUPLICATE_FIELD_ERROR(60001, "字段重复");
@Getter
private Integer code;
@Getter
private String message;
public static ExceptionEnum findExceptionEnumByCode(Integer code) {
for (ExceptionEnum exceptionEnum : values()) {
if (exceptionEnum.code.equals(code)) {
return exceptionEnum;
}
}
return null;
}
}
...@@ -15,10 +15,12 @@ import io.swagger.annotations.ApiOperation; ...@@ -15,10 +15,12 @@ import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value; import org.springframework.beans.factory.annotation.Value;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*; import org.springframework.web.bind.annotation.*;
import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid; import javax.validation.Valid;
import javax.validation.constraints.Max;
/** /**
* @author chin * @author chin
...@@ -29,6 +31,7 @@ import javax.validation.Valid; ...@@ -29,6 +31,7 @@ import javax.validation.Valid;
@RestController @RestController
@RequestMapping("/public") @RequestMapping("/public")
@Slf4j @Slf4j
@Validated
public class CommonDataController extends BaseController { public class CommonDataController extends BaseController {
@Autowired @Autowired
...@@ -54,7 +57,7 @@ public class CommonDataController extends BaseController { ...@@ -54,7 +57,7 @@ public class CommonDataController extends BaseController {
@ApiImplicitParam(name = "page_size", value = "页数", paramType = "path", dataType = "Integer"), @ApiImplicitParam(name = "page_size", value = "页数", paramType = "path", dataType = "Integer"),
}) })
@GetMapping("/activity") @GetMapping("/activity")
public BaseResponse<BasePageVO<ActivityInfoVO>> getActionData(HttpServletRequest request, @RequestParam(name = "page_size", defaultValue = "100", required = false) Integer pageSize, public BaseResponse<BasePageVO<ActivityInfoVO>> getActionData(HttpServletRequest request, @Max(value = 500,message = "单页数据page_size过大") @RequestParam(name = "page_size", defaultValue = "100", required = false) Integer pageSize,
@RequestParam(name = "current_page", defaultValue = "1", required = false) Integer pageNum) { @RequestParam(name = "current_page", defaultValue = "1", required = false) Integer pageNum) {
BaseResponse check = baseService.baseLimit(request, dateType, limit, limitPeriod); BaseResponse check = baseService.baseLimit(request, dateType, limit, limitPeriod);
if (check != null) { if (check != null) {
......
...@@ -14,12 +14,14 @@ import lombok.extern.slf4j.Slf4j; ...@@ -14,12 +14,14 @@ import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value; import org.springframework.beans.factory.annotation.Value;
import org.springframework.format.annotation.DateTimeFormat; import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController; import org.springframework.web.bind.annotation.RestController;
import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletRequest;
import javax.validation.constraints.Max;
import java.util.Date; import java.util.Date;
/** /**
...@@ -31,6 +33,7 @@ import java.util.Date; ...@@ -31,6 +33,7 @@ import java.util.Date;
@RestController @RestController
@RequestMapping("/faw") @RequestMapping("/faw")
@Slf4j @Slf4j
@Validated
public class FawOpenController extends BaseController { public class FawOpenController extends BaseController {
@Autowired @Autowired
...@@ -59,7 +62,7 @@ public class FawOpenController extends BaseController { ...@@ -59,7 +62,7 @@ public class FawOpenController extends BaseController {
@ApiImplicitParam(name = "page_size", value = "页数", paramType = "path", dataType = "Integer"), @ApiImplicitParam(name = "page_size", value = "页数", paramType = "path", dataType = "Integer"),
}) })
@GetMapping("/aweme") @GetMapping("/aweme")
public BaseResponse<BasePageVO<HongqiAwemeIncentiveVO>> getAwemeData(HttpServletRequest request, @RequestParam(name = "page_size", defaultValue = "100", required = false) Integer pageSize, public BaseResponse<BasePageVO<HongqiAwemeIncentiveVO>> getAwemeData(HttpServletRequest request, @Max(value = 500,message = "单页数据page_size过大") @RequestParam(name = "page_size", defaultValue = "100", required = false) Integer pageSize,
@RequestParam(name = "current_page", defaultValue = "1", required = false) Integer pageNum, @RequestParam(name = "current_page", defaultValue = "1", required = false) Integer pageNum,
@RequestParam(name = "publish_date_start") @DateTimeFormat(pattern = "yyyy-MM-dd") Date publishDateStart, @RequestParam(name = "publish_date_start") @DateTimeFormat(pattern = "yyyy-MM-dd") Date publishDateStart,
@RequestParam(name = "publish_date_end") @DateTimeFormat(pattern = "yyyy-MM-dd") Date publishDateEnd, @RequestParam(name = "publish_date_end") @DateTimeFormat(pattern = "yyyy-MM-dd") Date publishDateEnd,
...@@ -80,7 +83,7 @@ public class FawOpenController extends BaseController { ...@@ -80,7 +83,7 @@ public class FawOpenController extends BaseController {
@ApiImplicitParam(name = "page_size", value = "页数", paramType = "path", dataType = "Integer"), @ApiImplicitParam(name = "page_size", value = "页数", paramType = "path", dataType = "Integer"),
}) })
@GetMapping("/live") @GetMapping("/live")
public BaseResponse<BasePageVO<HongqiLiveIncentiveVO>> getLiveData(HttpServletRequest request, @RequestParam(name = "page_size", defaultValue = "100", required = false) Integer pageSize, public BaseResponse<BasePageVO<HongqiLiveIncentiveVO>> getLiveData(HttpServletRequest request, @Max(value = 500,message = "单页数据page_size过大") @RequestParam(name = "page_size", defaultValue = "100", required = false) Integer pageSize,
@RequestParam(name = "current_page", defaultValue = "1", required = false) Integer pageNum, @RequestParam(name = "current_page", defaultValue = "1", required = false) Integer pageNum,
@RequestParam(name = "publish_date_start") @DateTimeFormat(pattern = "yyyy-MM-dd") Date publishDateStart, @RequestParam(name = "publish_date_start") @DateTimeFormat(pattern = "yyyy-MM-dd") Date publishDateStart,
@RequestParam(name = "publish_date_end") @DateTimeFormat(pattern = "yyyy-MM-dd") Date publishDateEnd, @RequestParam(name = "publish_date_end") @DateTimeFormat(pattern = "yyyy-MM-dd") Date publishDateEnd,
......
...@@ -33,7 +33,7 @@ public class BaseService { ...@@ -33,7 +33,7 @@ public class BaseService {
RRateLimiter rateLimiter = redissonClient.getRateLimiter(fawKey); RRateLimiter rateLimiter = redissonClient.getRateLimiter(fawKey);
rateLimiter.trySetRate(RateType.OVERALL, limit, limitPeriod, RateIntervalUnit.SECONDS); rateLimiter.trySetRate(RateType.OVERALL, limit, limitPeriod, RateIntervalUnit.SECONDS);
if (!rateLimiter.tryAcquire(1, waiting, TimeUnit.MILLISECONDS)) { if (!rateLimiter.tryAcquire(1, waiting, TimeUnit.MILLISECONDS)) {
return BaseController.error("403", "您的请求太过频繁,请稍后再试!"); return BaseController.error("10010", "您的请求太过频繁,请稍后再试!");
} }
return null; return null;
} }
...@@ -47,7 +47,7 @@ public class BaseService { ...@@ -47,7 +47,7 @@ public class BaseService {
RRateLimiter rateLimiter = redissonClient.getRateLimiter(fawKey); RRateLimiter rateLimiter = redissonClient.getRateLimiter(fawKey);
rateLimiter.trySetRate(RateType.OVERALL, limit,limitPeriod, RateIntervalUnit.SECONDS); rateLimiter.trySetRate(RateType.OVERALL, limit,limitPeriod, RateIntervalUnit.SECONDS);
if (!rateLimiter.tryAcquire(1, waiting, TimeUnit.MILLISECONDS)) { if (!rateLimiter.tryAcquire(1, waiting, TimeUnit.MILLISECONDS)) {
return BaseController.error("403", "您的请求太过频繁,请稍后再试!"); return BaseController.error("10010", "您的请求太过频繁,请稍后再试!");
} }
return null; return null;
} }
......
package com.afanticar.afantiopenapi.service; package com.afanticar.afantiopenapi.service;
import com.afanticar.afantiopenapi.config.Constant; import com.afanticar.afantiopenapi.constant.Constant;
import com.afanticar.afantiopenapi.feign.AfantiCasFeign; import com.afanticar.afantiopenapi.feign.AfantiCasFeign;
import com.afanticar.afantiopenapi.mapper.ActivityInfoMapper; import com.afanticar.afantiopenapi.mapper.ActivityInfoMapper;
import com.afanticar.afantiopenapi.mapper.struct.ActivityInfoStructMapper; import com.afanticar.afantiopenapi.mapper.struct.ActivityInfoStructMapper;
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment