feat: 项目结构重构
This commit is contained in:
@ -1,45 +1,32 @@
|
||||
<?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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
|
||||
<parent>
|
||||
<groupId>day.gitlab</groupId>
|
||||
<artifactId>dolphin-commons</artifactId>
|
||||
<version>0.0.1-SNAPSHOT</version>
|
||||
</parent>
|
||||
|
||||
<artifactId>dolphin-common-core</artifactId>
|
||||
<packaging>jar</packaging>
|
||||
<artifactId>dolphin-common-core</artifactId>
|
||||
<packaging>jar</packaging>
|
||||
|
||||
<dependencies>
|
||||
<!-- Spring Boot -->
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-web</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-validation</artifactId>
|
||||
</dependency>
|
||||
<!-- jjwt -->
|
||||
<dependency>
|
||||
<groupId>io.jsonwebtoken</groupId>
|
||||
<artifactId>jjwt-api</artifactId>
|
||||
</dependency>
|
||||
<!-- Lombok -->
|
||||
<dependency>
|
||||
<groupId>org.projectlombok</groupId>
|
||||
<artifactId>lombok</artifactId>
|
||||
<optional>true</optional>
|
||||
</dependency>
|
||||
<groupId>org.projectlombok</groupId>
|
||||
<artifactId>lombok</artifactId>
|
||||
<optional>true</optional>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<build>
|
||||
<plugins>
|
||||
<build>
|
||||
<plugins>
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-compiler-plugin</artifactId>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
</build>
|
||||
</project>
|
||||
|
||||
@ -1,4 +0,0 @@
|
||||
package day.gitlab.dolphin.common.core.constants;
|
||||
|
||||
public class Constants {
|
||||
}
|
||||
@ -1,25 +0,0 @@
|
||||
package day.gitlab.dolphin.common.core.entity;
|
||||
|
||||
import jakarta.validation.constraints.Min;
|
||||
import jakarta.validation.constraints.NotNull;
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Builder;
|
||||
import lombok.Data;
|
||||
import lombok.NoArgsConstructor;
|
||||
|
||||
@Data
|
||||
@Builder
|
||||
@NoArgsConstructor
|
||||
@AllArgsConstructor
|
||||
public class PageRequest {
|
||||
|
||||
/** 当前分页,默认1 */
|
||||
@Min(1)
|
||||
@NotNull
|
||||
private Long pageNumber = 1L;
|
||||
|
||||
/** 分页条数,默认10 */
|
||||
@Min(1)
|
||||
@NotNull
|
||||
private Long pageSize = 10L;
|
||||
}
|
||||
@ -1,27 +0,0 @@
|
||||
package day.gitlab.dolphin.common.core.entity;
|
||||
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Builder;
|
||||
import lombok.Data;
|
||||
import lombok.NoArgsConstructor;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
@Data
|
||||
@Builder
|
||||
@NoArgsConstructor
|
||||
@AllArgsConstructor
|
||||
public class PageResponse<T> {
|
||||
|
||||
/** 总记录数 */
|
||||
private Long total = 0L;
|
||||
|
||||
/** 当前分页,默认1 */
|
||||
private Long pageNumber = 1L;
|
||||
|
||||
/** 分页条数,默认10 */
|
||||
private Long pageSize = 10L;
|
||||
|
||||
/** 记录 */
|
||||
private List<T> records;
|
||||
}
|
||||
@ -1,15 +0,0 @@
|
||||
package day.gitlab.dolphin.common.core.entity;
|
||||
|
||||
import lombok.*;
|
||||
|
||||
@Data
|
||||
@NoArgsConstructor
|
||||
@AllArgsConstructor
|
||||
@EqualsAndHashCode(callSuper = true)
|
||||
public class QueryPageRequest<T> extends PageRequest {
|
||||
|
||||
/**
|
||||
* 查询参数
|
||||
*/
|
||||
private T query;
|
||||
}
|
||||
@ -1,63 +0,0 @@
|
||||
package day.gitlab.dolphin.common.core.entity;
|
||||
|
||||
import day.gitlab.dolphin.common.core.exception.BusinessException;
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Data;
|
||||
import lombok.NoArgsConstructor;
|
||||
|
||||
@Data
|
||||
@NoArgsConstructor
|
||||
@AllArgsConstructor
|
||||
public class Result<T> {
|
||||
|
||||
public static final String SUCCESS = "00000000";
|
||||
public static final String FAILURE = "99999999";
|
||||
|
||||
public static final String SUCCESS_MESSAGE = "success";
|
||||
public static final String FAILURE_MESSAGE = "failure";
|
||||
|
||||
private String code;
|
||||
private String message;
|
||||
private T data;
|
||||
private long timestamp;
|
||||
|
||||
// ========== 通用响应方法 ==========
|
||||
|
||||
public static <T> Result<T> result(String code, String message, T data) {
|
||||
return new Result<>(code, message, data, System.currentTimeMillis());
|
||||
}
|
||||
|
||||
// ========== 成功响应方法 ==========
|
||||
|
||||
public static <T> Result<T> success() {
|
||||
return result(SUCCESS, SUCCESS_MESSAGE, null);
|
||||
}
|
||||
|
||||
public static <T> Result<T> success(T data) {
|
||||
return result(SUCCESS, SUCCESS_MESSAGE, data);
|
||||
}
|
||||
|
||||
public static <T> Result<T> success(String message, T data) {
|
||||
return result(SUCCESS, message, data);
|
||||
}
|
||||
|
||||
// ========== 失败响应方法 ==========
|
||||
|
||||
public static <T> Result<T> failure() {
|
||||
return result(FAILURE, FAILURE_MESSAGE, null);
|
||||
}
|
||||
|
||||
public static <T> Result<T> failure(String message) {
|
||||
return result(FAILURE, message, null);
|
||||
}
|
||||
|
||||
public static <T> Result<T> failure(String code, String message) {
|
||||
return result(code, message, null);
|
||||
}
|
||||
|
||||
// ========== 异常响应方法 ==========
|
||||
|
||||
public static <T> Result<T> failure(BusinessException e) {
|
||||
return result(e.getCode(), e.getMessage(), null);
|
||||
}
|
||||
}
|
||||
@ -1,14 +0,0 @@
|
||||
package day.gitlab.dolphin.common.core.exception;
|
||||
|
||||
import lombok.Getter;
|
||||
|
||||
@Getter
|
||||
public class BusinessException extends RuntimeException {
|
||||
|
||||
private final String code;
|
||||
|
||||
public BusinessException(String code, String message) {
|
||||
super(message);
|
||||
this.code = code;
|
||||
}
|
||||
}
|
||||
@ -1,19 +0,0 @@
|
||||
package day.gitlab.dolphin.common.core.exception;
|
||||
|
||||
import day.gitlab.dolphin.common.core.entity.Result;
|
||||
import org.springframework.web.bind.annotation.ExceptionHandler;
|
||||
import org.springframework.web.bind.annotation.RestControllerAdvice;
|
||||
|
||||
@RestControllerAdvice
|
||||
public class GlobalExceptionHandler {
|
||||
|
||||
@ExceptionHandler(value = BusinessException.class)
|
||||
public <T> Result<T> handleBusinessException(BusinessException e) {
|
||||
return Result.failure(e);
|
||||
}
|
||||
|
||||
@ExceptionHandler(value = Exception.class)
|
||||
public <T> Result<T> handleException(Exception e) {
|
||||
return Result.failure(e.getMessage());
|
||||
}
|
||||
}
|
||||
@ -1,29 +0,0 @@
|
||||
package day.gitlab.dolphin.common.core.i18n;
|
||||
|
||||
import org.springframework.context.MessageSource;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.context.support.ResourceBundleMessageSource;
|
||||
import org.springframework.web.servlet.LocaleResolver;
|
||||
import org.springframework.web.servlet.i18n.AcceptHeaderLocaleResolver;
|
||||
|
||||
import java.util.Locale;
|
||||
|
||||
@Configuration
|
||||
public class MessagesConfiguration {
|
||||
|
||||
@Bean
|
||||
public MessageSource messageSource() {
|
||||
ResourceBundleMessageSource messageSource = new ResourceBundleMessageSource();
|
||||
messageSource.setBasename("i18n.messages");
|
||||
messageSource.setDefaultEncoding("UTF-8");
|
||||
return messageSource;
|
||||
}
|
||||
|
||||
@Bean
|
||||
public LocaleResolver localeResolver() {
|
||||
AcceptHeaderLocaleResolver localeResolver = new AcceptHeaderLocaleResolver();
|
||||
localeResolver.setDefaultLocale(Locale.ENGLISH);
|
||||
return localeResolver;
|
||||
}
|
||||
}
|
||||
@ -1,51 +0,0 @@
|
||||
package day.gitlab.dolphin.common.core.i18n;
|
||||
|
||||
import day.gitlab.dolphin.common.core.entity.Result;
|
||||
import day.gitlab.dolphin.common.core.exception.BusinessException;
|
||||
import lombok.Getter;
|
||||
import org.springframework.context.MessageSource;
|
||||
import org.springframework.context.i18n.LocaleContextHolder;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import java.util.Locale;
|
||||
|
||||
@Getter
|
||||
@Component
|
||||
public class MessagesHelper {
|
||||
|
||||
private final MessageSource messageSource;
|
||||
|
||||
public MessagesHelper(MessageSource messageSource) {
|
||||
this.messageSource = messageSource;
|
||||
}
|
||||
|
||||
// ========== 国际化消息 ==========
|
||||
|
||||
public Locale getCurrentLocale() {
|
||||
return LocaleContextHolder.getLocale();
|
||||
}
|
||||
|
||||
public String getMessage(String code, Object... args) {
|
||||
return messageSource.getMessage(code, args, getCurrentLocale());
|
||||
}
|
||||
|
||||
public String getMessage(String code, String defaultMessage, Object... args) {
|
||||
return messageSource.getMessage(code, args, defaultMessage, getCurrentLocale());
|
||||
}
|
||||
|
||||
// ========== 成功响应方法 ==========
|
||||
|
||||
public <T> Result<T> success() {
|
||||
return Result.success();
|
||||
}
|
||||
|
||||
public <T> Result<T> success(T data) {
|
||||
return Result.success(data);
|
||||
}
|
||||
|
||||
// ========== 业务异常快捷方法 ==========
|
||||
|
||||
public BusinessException newBusinessException(String code, Object... args) {
|
||||
return new BusinessException(code, getMessage(code, args));
|
||||
}
|
||||
}
|
||||
@ -14,7 +14,7 @@ import java.security.SecureRandom;
|
||||
* String pw_hash = BCrypt.hashpw(plain_password, BCrypt.gensalt());
|
||||
* }
|
||||
* <p>
|
||||
* 使用checkpw方法检查被加密的字符串是否与原始字符串匹配:
|
||||
* 使用checkpw方法检查被加密的字符串是否与原始字符串匹配:
|
||||
* <p>
|
||||
* {@code
|
||||
* BCrypt.checkpw(candidate_password, stored_hash);
|
||||
|
||||
@ -1,50 +0,0 @@
|
||||
package day.gitlab.dolphin.common.core.util;
|
||||
|
||||
import io.jsonwebtoken.Claims;
|
||||
import io.jsonwebtoken.ClaimsBuilder;
|
||||
import io.jsonwebtoken.Jwts;
|
||||
import io.jsonwebtoken.security.Keys;
|
||||
|
||||
import javax.crypto.SecretKey;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.security.MessageDigest;
|
||||
import java.security.NoSuchAlgorithmException;
|
||||
import java.util.Date;
|
||||
import java.util.function.Function;
|
||||
|
||||
public final class JWT {
|
||||
|
||||
private JWT() {}
|
||||
|
||||
public static String generate(String secret, long expiration, Function<ClaimsBuilder, Claims> func) {
|
||||
return JWT.generate(secret, expiration, func.apply(Jwts.claims()));
|
||||
}
|
||||
|
||||
public static String generate(String secret, long expiration, Claims claims) {
|
||||
return Jwts.builder()
|
||||
.id(UUIDv7.randomUUID().toString())
|
||||
.claims(claims)
|
||||
.issuedAt(new Date())
|
||||
.expiration(new Date(System.currentTimeMillis() + expiration))
|
||||
.signWith(getSecretKey(secret))
|
||||
.compact();
|
||||
}
|
||||
|
||||
public static Claims parse(String secret, String token) {
|
||||
return Jwts.parser()
|
||||
.verifyWith(getSecretKey(secret))
|
||||
.build()
|
||||
.parseSignedClaims(token)
|
||||
.getPayload();
|
||||
}
|
||||
|
||||
private static SecretKey getSecretKey(String secret) {
|
||||
try {
|
||||
MessageDigest messageDigest = MessageDigest.getInstance("SHA-256");
|
||||
byte[] digest = messageDigest.digest(secret.getBytes(StandardCharsets.UTF_8));
|
||||
return Keys.hmacShaKeyFor(digest);
|
||||
} catch (NoSuchAlgorithmException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,20 @@
|
||||
package day.gitlab.dolphin.common.core.util;
|
||||
|
||||
public class Strings {
|
||||
|
||||
public static boolean isEmpty(String str) {
|
||||
return str == null || str.isEmpty();
|
||||
}
|
||||
|
||||
public static boolean isNotEmpty(String str) {
|
||||
return !isEmpty(str);
|
||||
}
|
||||
|
||||
public static boolean isBlank(String str) {
|
||||
return str == null || str.trim().isEmpty();
|
||||
}
|
||||
|
||||
public static boolean isNotBlank(String str) {
|
||||
return !isBlank(str);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user