This commit is contained in:
jiangh277
2025-07-22 23:00:39 +08:00
commit f8fb9b561c
59 changed files with 2456 additions and 0 deletions

View File

@@ -0,0 +1,48 @@
package com.timeline.aop;
import jakarta.servlet.http.HttpServletRequest;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
@Slf4j
@Aspect
@Component
public class RequestLogAspect {
@Pointcut("execution(* com..controller.*.*(..))")
public void log() {
}
@Around("log()")
public Object doAround(ProceedingJoinPoint joinPoint) throws Throwable {
long startTime = System.currentTimeMillis();
try {
// 获取请求信息
ServletRequestAttributes requestAttributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
HttpServletRequest request = requestAttributes.getRequest();
log.info("【请求开始】");
log.info("接受请求, URL : {}", request.getRequestURL());
log.info("HTTP Method : {}", request.getMethod());
log.info("IP Address : {}", request.getRemoteAddr());
log.info("Class Method: {}.{}", joinPoint.getSignature().getDeclaringTypeName(), joinPoint.getSignature().getName());
// 执行原方法
Object result = joinPoint.proceed();
// 输出返回值
log.info("响应结果 : {}", result);
log.info("结束请求, 请求耗时 : {} ms", System.currentTimeMillis() - startTime);
return result;
} catch (Exception e) {
log.error("请求发生异常", e);
throw e;
}
}
}

View File

@@ -0,0 +1,12 @@
package com.timeline.constants;
public class CommonConstants {
private static final int RELATION_USER_AND_STORY = 1;
public static final int RELATION_USER_AND_STORY_ITEM = 2;
public static final int RELATION_USER_AND_IMAGE = 3;
public static final int RELATION_STORY_AND_IMAGE = 4;
public static final int RELATION_STORY_ITEM_AND_IMAGE = 5;
public static final int RELATION_STORY_ITEM_AND_COVER = 6;
public static final int STORY_ITEM_IS_ROOT = 1;
public static final int STORY_ITEM_IS_NOT_ROOT = 0;
}

View File

@@ -0,0 +1,17 @@
package com.timeline.dto;
import lombok.Data;
import java.time.LocalDateTime;
@Data
public class CommonRelationDTO {
private Integer id;
private String relaId;
private String subRelaId;
private Integer relationType;
private String userId;
private LocalDateTime createTime;
private LocalDateTime updateTime;
private Integer isDeleted;
}

View File

@@ -0,0 +1,5 @@
package com.timeline.enums;
public enum RelationTypeEnum {
}

View File

@@ -0,0 +1,32 @@
package com.timeline.exception;
import com.timeline.response.ResponseEnum;
public class CustomException extends RuntimeException {
private final int code;
private final String message;
public CustomException(ResponseEnum responseEnum) {
this.code = responseEnum.getCode();
this.message = responseEnum.getMessage();
}
public CustomException(ResponseEnum responseEnum, String detailMessage) {
this.code = responseEnum.getCode();
this.message = responseEnum.getMessage() + ": " + detailMessage;
}
public CustomException(int code, String message) {
this.code = code;
this.message = message;
}
public int getCode() {
return code;
}
@Override
public String getMessage() {
return message;
}
}

View File

@@ -0,0 +1,22 @@
package com.timeline.handler;
import com.timeline.exception.CustomException;
import com.timeline.response.ResponseEntity;
import com.timeline.response.ResponseEnum;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;
@RestControllerAdvice
public class GlobalExceptionHandler {
@ExceptionHandler(CustomException.class)
public ResponseEntity<String> handleCustomException(CustomException ex) {
return ResponseEntity.error(ex.getCode(), ex.getMessage());
}
@ExceptionHandler(Exception.class)
public ResponseEntity<String> handleGenericException(Exception ex) {
return ResponseEntity.error(ResponseEnum.INTERNAL_SERVER_ERROR.getCode(),
ResponseEnum.INTERNAL_SERVER_ERROR.getMessage() + ": " + ex.getMessage());
}
}

View File

@@ -0,0 +1,28 @@
package com.timeline.response;
import lombok.AllArgsConstructor;
import lombok.Data;
@Data
@AllArgsConstructor
public class ResponseEntity<T> {
private int code;
private String message;
private T data;
public static <T> ResponseEntity<T> success(T data) {
return new ResponseEntity<>(ResponseEnum.SUCCESS.getCode(), ResponseEnum.SUCCESS.getMessage(), data);
}
public static <T> ResponseEntity<T> error(int code, String message) {
return new ResponseEntity<>(code, message, null);
}
public static <T> ResponseEntity<T> error(ResponseEnum responseEnum) {
return new ResponseEntity<>(responseEnum.getCode(), responseEnum.getMessage(), null);
}
public static <T> ResponseEntity<T> error(ResponseEnum responseEnum, String detailMessage) {
return new ResponseEntity<>(responseEnum.getCode(), responseEnum.getMessage() + ": " + detailMessage, null);
}
}

View File

@@ -0,0 +1,32 @@
package com.timeline.response;
import lombok.AllArgsConstructor;
import lombok.Getter;
@Getter
@AllArgsConstructor
public enum ResponseEnum {
// 成功
SUCCESS(200, "请求成功"),
// 客户端错误
BAD_REQUEST(400, "客户端错误"),
UNAUTHORIZED(401, "未授权访问"),
FORBIDDEN(403, "禁止访问"),
NOT_FOUND(404, "资源不存在"),
METHOD_NOT_ALLOWED(405, "方法不允许"),
REQUEST_TIMEOUT(408, "请求超时"),
CONFLICT(409, "操作冲突"),
UNSUPPORTED_MEDIA_TYPE(415, "不支持的媒体类型"),
TOO_MANY_REQUESTS(429, "请求过多,请稍后再试"),
// 服务端错误
INTERNAL_SERVER_ERROR(500, "服务器内部错误"),
NOT_IMPLEMENTED(501, "功能未实现"),
BAD_GATEWAY(502, "网关错误"),
SERVICE_UNAVAILABLE(503, "服务不可用"),
GATEWAY_TIMEOUT(504, "网关超时");
private final int code;
private final String message;
}

View File

@@ -0,0 +1,18 @@
package com.timeline.utils;
import java.util.UUID;
public class IdUtils {
public static String randomUuid() {
return UUID.randomUUID().toString().replace("-", "");
}
public static String randomUuid(boolean dash) {
return dash ? UUID.randomUUID().toString() : UUID.randomUUID().toString().replace("-", "");
}
/**
* 获取无分隔符的 UUID大写
*/
public static String randomUuidUpper() {
return randomUuid().toUpperCase();
}
}