init2
This commit is contained in:
@@ -0,0 +1,48 @@
|
||||
package com.timeline.common.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;
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
@@ -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;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,16 @@
|
||||
package com.timeline.common.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;
|
||||
|
||||
|
||||
public static final int DELETED = 1;
|
||||
public static final int NOT_DELETED = 0;
|
||||
}
|
||||
@@ -0,0 +1,17 @@
|
||||
package com.timeline.common.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;
|
||||
}
|
||||
@@ -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());
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,5 @@
|
||||
package com.timeline.common.enums;
|
||||
|
||||
public enum RelationTypeEnum {
|
||||
|
||||
}
|
||||
@@ -0,0 +1,32 @@
|
||||
package com.timeline.common.exception;
|
||||
|
||||
import com.timeline.common.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;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,22 @@
|
||||
package com.timeline.common.handler;
|
||||
|
||||
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;
|
||||
|
||||
@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());
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,28 @@
|
||||
package com.timeline.common.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);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,35 @@
|
||||
package com.timeline.common.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, "网关超时"),
|
||||
|
||||
// 操作错误
|
||||
SEARCH_ERROR(4001, "查询数据库错误");
|
||||
|
||||
private final int code;
|
||||
private final String message;
|
||||
}
|
||||
@@ -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();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,18 @@
|
||||
package com.timeline.common.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();
|
||||
}
|
||||
}
|
||||
@@ -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);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -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);
|
||||
}
|
||||
}
|
||||
@@ -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);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,9 @@
|
||||
package com.timeline.common.vo;
|
||||
|
||||
import lombok.Data;
|
||||
|
||||
@Data
|
||||
public class CommonVo {
|
||||
private Integer pageSize;
|
||||
private Integer current;
|
||||
}
|
||||
Reference in New Issue
Block a user