commit e240f3660a922a307461fc4a44fb5f10fbd2d303 Author: 孙小云 Date: Sat Dec 20 14:24:21 2025 +0800 Initial commit for tuoheng-common diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..9cb91b4 --- /dev/null +++ b/.gitignore @@ -0,0 +1,45 @@ +target/ +!.mvn/wrapper/maven-wrapper.jar +!**/src/main/**/target/ +!**/src/test/**/target/ + +### STS ### +.apt_generated +.classpath +.factorypath +.project +.settings +.springBeans +.sts4-cache + +### IntelliJ IDEA ### +.idea +*.iws +*.iml +*.ipr + +### NetBeans ### +/nbproject/private/ +/nbbuild/ +/dist/ +/nbdist/ +/.nb-gradle/ +build/ +!**/src/main/**/build/ +!**/src/test/**/build/ + +### VS Code ### +.vscode/ + +### Mac OS ### +.DS_Store + +### Logs ### +logs/ +*.log + +### Temp Files ### +*.tmp +*.bak +*.swp +*~.nib \ No newline at end of file diff --git a/pom.xml b/pom.xml new file mode 100644 index 0000000..a67161d --- /dev/null +++ b/pom.xml @@ -0,0 +1,42 @@ + + + 4.0.0 + + + com.tuoheng.hxf + parent + 0.0.1-SNAPSHOT + + + tuoheng-common + jar + + Tuoheng Common + 拓恒通用模块 - 包含通用工具类、响应对象、异常类等 + + + + + org.projectlombok + lombok + true + + + + + com.alibaba.fastjson2 + fastjson2 + 2.0.43 + + + + + org.springframework.boot + spring-boot-starter + true + + + diff --git a/src/main/java/com/tuoheng/common/constant/CommonConstants.java b/src/main/java/com/tuoheng/common/constant/CommonConstants.java new file mode 100644 index 0000000..27cee73 --- /dev/null +++ b/src/main/java/com/tuoheng/common/constant/CommonConstants.java @@ -0,0 +1,64 @@ +package com.tuoheng.common.constant; + +/** + * 通用常量 + * + * @author Tuoheng Team + */ +public class CommonConstants { + + /** + * 成功标记 + */ + public static final Integer SUCCESS = 200; + + /** + * 失败标记 + */ + public static final Integer FAIL = 500; + + /** + * 删除标记 - 已删除 + */ + public static final Integer DELETED = 1; + + /** + * 删除标记 - 未删除 + */ + public static final Integer NOT_DELETED = 0; + + /** + * 启用状态 - 启用 + */ + public static final Integer ENABLED = 1; + + /** + * 启用状态 - 禁用 + */ + public static final Integer DISABLED = 0; + + /** + * 是 + */ + public static final Integer YES = 1; + + /** + * 否 + */ + public static final Integer NO = 0; + + /** + * UTF-8编码 + */ + public static final String UTF8 = "UTF-8"; + + /** + * Token前缀 + */ + public static final String TOKEN_PREFIX = "Bearer "; + + /** + * Token请求头 + */ + public static final String TOKEN_HEADER = "Authorization"; +} diff --git a/src/main/java/com/tuoheng/common/enums/ResultCode.java b/src/main/java/com/tuoheng/common/enums/ResultCode.java new file mode 100644 index 0000000..2dbbdec --- /dev/null +++ b/src/main/java/com/tuoheng/common/enums/ResultCode.java @@ -0,0 +1,47 @@ +package com.tuoheng.common.enums; + +import lombok.AllArgsConstructor; +import lombok.Getter; + +/** + * 结果码枚举 + * + * @author Tuoheng Team + */ +@Getter +@AllArgsConstructor +public enum ResultCode { + + SUCCESS(200, "操作成功"), + FAIL(500, "操作失败"), + + // 参数错误 1xxx + PARAM_ERROR(1001, "参数错误"), + PARAM_IS_NULL(1002, "参数为空"), + PARAM_TYPE_ERROR(1003, "参数类型错误"), + + // 用户错误 2xxx + USER_NOT_EXIST(2001, "用户不存在"), + USER_PASSWORD_ERROR(2002, "用户密码错误"), + USER_ACCOUNT_LOCKED(2003, "用户账号被锁定"), + USER_NOT_LOGIN(2004, "用户未登录"), + USER_NO_PERMISSION(2005, "用户无权限"), + TOKEN_INVALID(2006, "Token无效"), + TOKEN_EXPIRED(2007, "Token已过期"), + + // 业务错误 3xxx + AIRPORT_NOT_EXIST(3001, "机场不存在"), + DRONE_NOT_EXIST(3002, "无人机不存在"), + TASK_NOT_EXIST(3003, "任务不存在"), + AIRLINE_NOT_EXIST(3004, "航线不存在"), + + // 系统错误 9xxx + SYSTEM_ERROR(9001, "系统错误"), + DATABASE_ERROR(9002, "数据库错误"), + REDIS_ERROR(9003, "Redis错误"), + MQTT_ERROR(9004, "MQTT错误"), + KAFKA_ERROR(9005, "Kafka错误"); + + private final Integer code; + private final String message; +} diff --git a/src/main/java/com/tuoheng/common/exception/BusinessException.java b/src/main/java/com/tuoheng/common/exception/BusinessException.java new file mode 100644 index 0000000..77e2e89 --- /dev/null +++ b/src/main/java/com/tuoheng/common/exception/BusinessException.java @@ -0,0 +1,44 @@ +package com.tuoheng.common.exception; + +import lombok.Data; +import lombok.EqualsAndHashCode; + +/** + * 业务异常 + * + * @author Tuoheng Team + */ +@Data +@EqualsAndHashCode(callSuper = true) +public class BusinessException extends RuntimeException { + + private static final long serialVersionUID = 1L; + + /** + * 错误码 + */ + private Integer code; + + /** + * 错误消息 + */ + private String message; + + public BusinessException(String message) { + super(message); + this.code = 500; + this.message = message; + } + + public BusinessException(Integer code, String message) { + super(message); + this.code = code; + this.message = message; + } + + public BusinessException(Integer code, String message, Throwable cause) { + super(message, cause); + this.code = code; + this.message = message; + } +} diff --git a/src/main/java/com/tuoheng/common/response/PageResult.java b/src/main/java/com/tuoheng/common/response/PageResult.java new file mode 100644 index 0000000..48843e1 --- /dev/null +++ b/src/main/java/com/tuoheng/common/response/PageResult.java @@ -0,0 +1,52 @@ +package com.tuoheng.common.response; + +import lombok.Data; +import lombok.EqualsAndHashCode; + +import java.util.List; + +/** + * 分页响应结果 + * + * @author Tuoheng Team + */ +@Data +@EqualsAndHashCode(callSuper = true) +public class PageResult extends Result> { + + /** + * 总记录数 + */ + private Long total; + + /** + * 当前页码 + */ + private Integer pageNum; + + /** + * 每页大小 + */ + private Integer pageSize; + + /** + * 总页数 + */ + private Integer pages; + + public PageResult() { + super(); + } + + public PageResult(List data, Long total, Integer pageNum, Integer pageSize) { + super(200, "查询成功", data); + this.total = total; + this.pageNum = pageNum; + this.pageSize = pageSize; + this.pages = (int) Math.ceil((double) total / pageSize); + } + + public static PageResult success(List data, Long total, Integer pageNum, Integer pageSize) { + return new PageResult<>(data, total, pageNum, pageSize); + } +} diff --git a/src/main/java/com/tuoheng/common/response/Result.java b/src/main/java/com/tuoheng/common/response/Result.java new file mode 100644 index 0000000..cc7304d --- /dev/null +++ b/src/main/java/com/tuoheng/common/response/Result.java @@ -0,0 +1,71 @@ +package com.tuoheng.common.response; + +import lombok.Data; + +import java.io.Serializable; + +/** + * 统一响应结果 + * + * @author Tuoheng Team + */ +@Data +public class Result implements Serializable { + + private static final long serialVersionUID = 1L; + + /** + * 响应码 + */ + private Integer code; + + /** + * 响应消息 + */ + private String msg; + + /** + * 响应数据 + */ + private T data; + + /** + * 时间戳 + */ + private Long timestamp; + + public Result() { + this.timestamp = System.currentTimeMillis(); + } + + public Result(Integer code, String msg, T data) { + this.code = code; + this.msg = msg; + this.data = data; + this.timestamp = System.currentTimeMillis(); + } + + public static Result success() { + return new Result<>(200, "操作成功", null); + } + + public static Result success(T data) { + return new Result<>(200, "操作成功", data); + } + + public static Result success(String msg, T data) { + return new Result<>(200, msg, data); + } + + public static Result error() { + return new Result<>(500, "操作失败", null); + } + + public static Result error(String msg) { + return new Result<>(500, msg, null); + } + + public static Result error(Integer code, String msg) { + return new Result<>(code, msg, null); + } +} diff --git a/src/main/java/com/tuoheng/common/util/DateUtil.java b/src/main/java/com/tuoheng/common/util/DateUtil.java new file mode 100644 index 0000000..02f422b --- /dev/null +++ b/src/main/java/com/tuoheng/common/util/DateUtil.java @@ -0,0 +1,53 @@ +package com.tuoheng.common.util; + +import java.time.LocalDateTime; +import java.time.format.DateTimeFormatter; + +/** + * 日期工具类 + * + * @author Tuoheng Team + */ +public class DateUtil { + + public static final String PATTERN_DATETIME = "yyyy-MM-dd HH:mm:ss"; + public static final String PATTERN_DATE = "yyyy-MM-dd"; + public static final String PATTERN_TIME = "HH:mm:ss"; + + /** + * 格式化日期时间 + */ + public static String format(LocalDateTime dateTime) { + if (dateTime == null) { + return null; + } + return dateTime.format(DateTimeFormatter.ofPattern(PATTERN_DATETIME)); + } + + /** + * 格式化日期时间(自定义格式) + */ + public static String format(LocalDateTime dateTime, String pattern) { + if (dateTime == null) { + return null; + } + return dateTime.format(DateTimeFormatter.ofPattern(pattern)); + } + + /** + * 解析日期时间字符串 + */ + public static LocalDateTime parse(String dateTimeStr) { + if (dateTimeStr == null || dateTimeStr.isEmpty()) { + return null; + } + return LocalDateTime.parse(dateTimeStr, DateTimeFormatter.ofPattern(PATTERN_DATETIME)); + } + + /** + * 获取当前日期时间 + */ + public static LocalDateTime now() { + return LocalDateTime.now(); + } +} diff --git a/src/main/java/com/tuoheng/common/util/JsonUtil.java b/src/main/java/com/tuoheng/common/util/JsonUtil.java new file mode 100644 index 0000000..bfc2493 --- /dev/null +++ b/src/main/java/com/tuoheng/common/util/JsonUtil.java @@ -0,0 +1,42 @@ +package com.tuoheng.common.util; + +import com.alibaba.fastjson2.JSON; +import com.alibaba.fastjson2.JSONObject; + +/** + * JSON工具类 + * + * @author Tuoheng Team + */ +public class JsonUtil { + + /** + * 对象转JSON字符串 + */ + public static String toJsonString(Object obj) { + if (obj == null) { + return null; + } + return JSON.toJSONString(obj); + } + + /** + * JSON字符串转对象 + */ + public static T parseObject(String jsonStr, Class clazz) { + if (jsonStr == null || jsonStr.isEmpty()) { + return null; + } + return JSON.parseObject(jsonStr, clazz); + } + + /** + * JSON字符串转JSONObject + */ + public static JSONObject parseObject(String jsonStr) { + if (jsonStr == null || jsonStr.isEmpty()) { + return null; + } + return JSON.parseObject(jsonStr); + } +}