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,67 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>com.timeline</groupId>
<artifactId>timeline</artifactId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<artifactId>timeline-component-common</artifactId>
<properties>
<maven.compiler.source>21</maven.compiler.source>
<maven.compiler.target>21</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-aop</artifactId>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
</dependency>
<dependency>
<groupId>org.apache.tomcat.embed</groupId>
<artifactId>tomcat-embed-core</artifactId>
</dependency>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.5.17</version>
<scope>compile</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<executions>
<execution>
<id>repackage</id>
<phase>none</phase>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>3.4.0</version>
<configuration>
<addClasspath>false</addClasspath>
<mainClass />
</configuration>
</plugin>
</plugins>
</build>
</project>

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();
}
}

View File

@@ -0,0 +1,33 @@
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.timeline.story.dao.CommonRelationMapper">
<insert id="insertImageStoryItemRelation">
INSERT INTO common_relation (rela_id, sub_rela_id, rela_type, user_id)
VALUES (#{imageInstanceId}, #{storyItemId}, 1, #{userId})
</insert>
<select id="getImagesByStoryItemId" resultType="string">
SELECT rela_id
FROM common_relation
WHERE sub_rela_id = #{storyItemId} AND rela_type = 1 AND is_delete = 0
</select>
<select id="getStoryItemsByImageInstanceId" resultType="string">
SELECT sub_rela_id
FROM common_relation
WHERE rela_id = #{imageInstanceId} AND rela_type = 1 AND is_delete = 0
</select>
<update id="deleteImageStoryItemRelation">
UPDATE common_relation
SET is_delete = 1
WHERE rela_id = #{imageInstanceId} AND sub_rela_id = #{storyItemId}
</update>
<insert id="insertRelation">
INSERT INTO common_relation (rela_id, sub_rela_id, rela_type, user_id)
VALUES (#{relaId}, #{subRelaId}, #{relationType}, #{userId})
</insert>
</mapper>