Initial commit for tuoheng-common

This commit is contained in:
孙小云 2025-12-20 14:24:21 +08:00
commit e240f3660a
9 changed files with 460 additions and 0 deletions

45
.gitignore vendored Normal file
View File

@ -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

42
pom.xml Normal file
View File

@ -0,0 +1,42 @@
<?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.tuoheng.hxf</groupId>
<artifactId>parent</artifactId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<artifactId>tuoheng-common</artifactId>
<packaging>jar</packaging>
<name>Tuoheng Common</name>
<description>拓恒通用模块 - 包含通用工具类、响应对象、异常类等</description>
<dependencies>
<!-- Lombok -->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<!-- FastJSON2 -->
<dependency>
<groupId>com.alibaba.fastjson2</groupId>
<artifactId>fastjson2</artifactId>
<version>2.0.43</version>
</dependency>
<!-- Spring Boot Starter (for basic Spring support) -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
<optional>true</optional>
</dependency>
</dependencies>
</project>

View File

@ -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";
}

View File

@ -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;
}

View File

@ -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;
}
}

View File

@ -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<T> extends Result<List<T>> {
/**
* 总记录数
*/
private Long total;
/**
* 当前页码
*/
private Integer pageNum;
/**
* 每页大小
*/
private Integer pageSize;
/**
* 总页数
*/
private Integer pages;
public PageResult() {
super();
}
public PageResult(List<T> 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 <T> PageResult<T> success(List<T> data, Long total, Integer pageNum, Integer pageSize) {
return new PageResult<>(data, total, pageNum, pageSize);
}
}

View File

@ -0,0 +1,71 @@
package com.tuoheng.common.response;
import lombok.Data;
import java.io.Serializable;
/**
* 统一响应结果
*
* @author Tuoheng Team
*/
@Data
public class Result<T> 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 <T> Result<T> success() {
return new Result<>(200, "操作成功", null);
}
public static <T> Result<T> success(T data) {
return new Result<>(200, "操作成功", data);
}
public static <T> Result<T> success(String msg, T data) {
return new Result<>(200, msg, data);
}
public static <T> Result<T> error() {
return new Result<>(500, "操作失败", null);
}
public static <T> Result<T> error(String msg) {
return new Result<>(500, msg, null);
}
public static <T> Result<T> error(Integer code, String msg) {
return new Result<>(code, msg, null);
}
}

View File

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

View File

@ -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> T parseObject(String jsonStr, Class<T> 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);
}
}