This commit is contained in:
jiangh277
2025-08-04 16:51:13 +08:00
parent f8fb9b561c
commit eba0eb085e
41 changed files with 451 additions and 73 deletions

View File

@@ -1,4 +1,4 @@
package com.timeline.aop;
package com.timeline.common.aop;
import jakarta.servlet.http.HttpServletRequest;
import lombok.extern.slf4j.Slf4j;

View File

@@ -0,0 +1,26 @@
package com.timeline.common.config;
import com.github.pagehelper.PageInterceptor;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import java.util.Properties;
@Configuration
public class PageHelperConfig {
@Bean(name = "pageHelper")
public PageInterceptor pageInterceptor() {
PageInterceptor interceptor = new PageInterceptor();
Properties properties = new Properties();
// 配置数据库方言
properties.setProperty("helperDialect", "mysql");
// 分页合理化参数默认值为false
properties.setProperty("reasonable", "true");
// 支持通过Mapper接口参数来传递分页参数
properties.setProperty("supportMethodsArguments", "true");
// always总是返回PageInfo类型
properties.setProperty("returnPageInfo", "check");
interceptor.setProperties(properties);
return interceptor;
}
}

View File

@@ -1,4 +1,4 @@
package com.timeline.constants;
package com.timeline.common.constants;
public class CommonConstants {
private static final int RELATION_USER_AND_STORY = 1;
@@ -9,4 +9,8 @@ public class CommonConstants {
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;
public static final int DELETED = 1;
public static final int NOT_DELETED = 0;
}

View File

@@ -1,4 +1,4 @@
package com.timeline.dto;
package com.timeline.common.dto;
import lombok.Data;

View File

@@ -0,0 +1,23 @@
package com.timeline.common.dto;
import com.github.pagehelper.PageInfo;
import lombok.AllArgsConstructor;
import lombok.Data;
import java.util.List;
@Data
@AllArgsConstructor
public class PageResult<T> {
private long total;
private int pageNum;
private int pageSize;
private int pages;
private List<T> list;
public static <T> PageResult<T> of(List<T> list) {
PageInfo<T> pageInfo = new PageInfo<>(list);
return new PageResult<T>(pageInfo.getTotal(), pageInfo.getPageNum(),
pageInfo.getPageSize(), pageInfo.getPages(), pageInfo.getList());
}
}

View File

@@ -1,4 +1,4 @@
package com.timeline.enums;
package com.timeline.common.enums;
public enum RelationTypeEnum {

View File

@@ -1,6 +1,6 @@
package com.timeline.exception;
package com.timeline.common.exception;
import com.timeline.response.ResponseEnum;
import com.timeline.common.response.ResponseEnum;
public class CustomException extends RuntimeException {
private final int code;

View File

@@ -1,8 +1,8 @@
package com.timeline.handler;
package com.timeline.common.handler;
import com.timeline.exception.CustomException;
import com.timeline.response.ResponseEntity;
import com.timeline.response.ResponseEnum;
import com.timeline.common.exception.CustomException;
import com.timeline.common.response.ResponseEntity;
import com.timeline.common.response.ResponseEnum;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

View File

@@ -1,4 +1,4 @@
package com.timeline.response;
package com.timeline.common.response;
import lombok.AllArgsConstructor;
import lombok.Data;

View File

@@ -1,4 +1,4 @@
package com.timeline.response;
package com.timeline.common.response;
import lombok.AllArgsConstructor;
import lombok.Getter;
@@ -25,7 +25,10 @@ public enum ResponseEnum {
NOT_IMPLEMENTED(501, "功能未实现"),
BAD_GATEWAY(502, "网关错误"),
SERVICE_UNAVAILABLE(503, "服务不可用"),
GATEWAY_TIMEOUT(504, "网关超时");
GATEWAY_TIMEOUT(504, "网关超时"),
// 操作错误
SEARCH_ERROR(4001, "查询数据库错误");
private final int code;
private final String message;

View File

@@ -0,0 +1,20 @@
package com.timeline.common.utils;
import org.springframework.web.multipart.MultipartFile;
import java.io.IOException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
public class CommonUtils {
public static String calculateFileHash(MultipartFile file) throws IOException, NoSuchAlgorithmException {
MessageDigest digest = MessageDigest.getInstance("SHA-256");
byte[] fileBytes = file.getBytes();
byte[] hashBytes = digest.digest(fileBytes);
StringBuilder sb = new StringBuilder();
for (byte b : hashBytes) {
sb.append(String.format("%02x", b));
}
return sb.toString();
}
}

View File

@@ -1,4 +1,4 @@
package com.timeline.utils;
package com.timeline.common.utils;
import java.util.UUID;

View File

@@ -0,0 +1,49 @@
package com.timeline.common.utils;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.timeline.common.exception.CustomException;
import com.timeline.common.response.ResponseEnum;
import lombok.extern.slf4j.Slf4j;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
@Slf4j
public class PageUtils {
public PageUtils() {
}
public static Map pageQuery(Integer pageNum, Integer pageSize, Class clas, String methodName, Object params, String listKey) {
Map pageResult = new HashMap();
if (pageNum == null) {
pageNum = 0;
}
if (pageSize == null) {
pageSize = 0;
}
try {
if (pageNum >= 1 && pageSize >= 1) {
PageHelper.startPage(pageNum, pageSize);
List list = (List)ReflectUtils.call(clas, methodName, params);
PageInfo pageInfo = new PageInfo(list);
pageResult.put("pageNumber", pageInfo.getPageNum());
pageResult.put("pageSize", pageInfo.getPageSize());
pageResult.put("total", pageInfo.getTotal());
pageResult.put("pages", pageInfo.getPages());
pageResult.put(listKey, pageInfo.getList());
return pageResult;
} else {
List list = (List)ReflectUtils.call(clas, methodName, params);
pageResult.put(listKey, list);
return pageResult;
}
} catch (Exception e) {
log.error("查询失败", e);
throw new CustomException(ResponseEnum.SEARCH_ERROR);
}
}
}

View File

@@ -0,0 +1,58 @@
package com.timeline.common.utils;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import org.springframework.util.StringUtils;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.Map;
public class ReflectUtils {
public ReflectUtils() {
}
public static Object persistenceData(String entityPath, String persisPath, String methodName, Map params) throws Exception {
Object result = null;
try {
if (!StringUtils.isEmpty(entityPath) && !StringUtils.isEmpty(persisPath)) {
Class entityClass = Class.forName(entityPath);
Class persisClass = Class.forName(persisPath);
Object daoBean = SpringContextUtils.getBean(persisClass);
Object o = JSONObject.toJavaObject(JSON.parseObject(JSONObject.toJSONString(params)), entityClass);
Method method = persisClass.getMethod(methodName, entityClass);
result = method.invoke(daoBean, o);
return result;
} else {
return null;
}
} catch (Exception e) {
throw e;
}
}
public static Object call(Class clas, String methodName, Object params) throws Exception {
Object bean = SpringContextUtils.getBean(clas);
Method method = clas.getMethod(methodName, Map.class);
Object result = method.invoke(bean, params);
return result;
}
public static Object call(String className, String methodName, String parameterType, JSONObject parameter) throws NoSuchMethodException, InvocationTargetException, IllegalAccessException, ClassNotFoundException {
Class clas = Class.forName(className);
Class parameterTypeClass = Class.forName(parameterType);
Object daoBean = SpringContextUtils.getBean(clas);
Object o = JSONObject.toJavaObject(parameter, parameterTypeClass);
Method method = clas.getMethod(methodName, parameterTypeClass);
return method.invoke(daoBean, o);
}
public static Object call(String className, String methodName, String parameterType, Object parameter) throws NoSuchMethodException, InvocationTargetException, IllegalAccessException, ClassNotFoundException {
Class clas = Class.forName(className);
Class parameterTypeClass = Class.forName(parameterType);
Object daoBean = SpringContextUtils.getBean(clas);
Method method = clas.getMethod(methodName, parameterTypeClass);
return method.invoke(daoBean, parameter);
}
}

View File

@@ -0,0 +1,30 @@
package com.timeline.common.utils;
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;
@Component
public class SpringContextUtils implements ApplicationContextAware {
private static ApplicationContext applicationContext;
public SpringContextUtils() {
}
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
SpringContextUtils.applicationContext = applicationContext;
}
public static ApplicationContext getApplicationContext() {
return applicationContext;
}
public static <T> T getBean(String name) throws BeansException {
return (T)applicationContext.getBean(name);
}
public static <T> T getBean(Class<?> clz) throws BeansException {
return (T)applicationContext.getBean(clz);
}
}

View File

@@ -0,0 +1,9 @@
package com.timeline.common.vo;
import lombok.Data;
@Data
public class CommonVo {
private Integer pageSize;
private Integer current;
}