init: 2026Technology-Competition initial commit

This commit is contained in:
xunhe
2026-08-27 17:35:01 +08:00
commit e2a4d40c9e
311 changed files with 37037 additions and 0 deletions
@@ -0,0 +1,22 @@
package com.ims.common.constant;
import lombok.Getter;
@Getter
public enum ResultCode {
SUCCESS(200, "success"),
BAD_REQUEST(400, "bad request"),
UNAUTHORIZED(401, "unauthorized"),
FORBIDDEN(403, "forbidden"),
NOT_FOUND(404, "not found"),
INTERNAL_ERROR(500, "internal server error"),
BUSINESS_ERROR(1001, "business error");
private final int code;
private final String message;
ResultCode(int code, String message) {
this.code = code;
this.message = message;
}
}
@@ -0,0 +1,37 @@
package com.ims.common.dto;
import com.ims.common.constant.ResultCode;
import lombok.Data;
import java.time.LocalDateTime;
@Data
public class ApiResponse<T> {
private int code;
private String message;
private T data;
private String timestamp;
public ApiResponse() {
this.timestamp = LocalDateTime.now().toString();
}
public static <T> ApiResponse<T> success(T data) {
ApiResponse<T> response = new ApiResponse<>();
response.setCode(ResultCode.SUCCESS.getCode());
response.setMessage(ResultCode.SUCCESS.getMessage());
response.setData(data);
return response;
}
public static <T> ApiResponse<T> error(int code, String message) {
ApiResponse<T> response = new ApiResponse<>();
response.setCode(code);
response.setMessage(message);
return response;
}
public static <T> ApiResponse<T> error(ResultCode resultCode) {
return error(resultCode.getCode(), resultCode.getMessage());
}
}
@@ -0,0 +1,26 @@
package com.ims.common.dto;
import lombok.Data;
import java.util.Collections;
import java.util.List;
@Data
public class PageResult<T> {
private List<T> items;
private long total;
private int page;
private int pageSize;
private int totalPages;
public PageResult() {
this.items = Collections.emptyList();
}
public PageResult(List<T> items, long total, int page, int pageSize) {
this.items = items;
this.total = total;
this.page = page;
this.pageSize = pageSize;
this.totalPages = pageSize > 0 ? (int) Math.ceil((double) total / pageSize) : 0;
}
}
@@ -0,0 +1,19 @@
package com.ims.common.exception;
import com.ims.common.constant.ResultCode;
import lombok.Getter;
@Getter
public class BusinessException extends RuntimeException {
private final int code;
public BusinessException(String message) {
super(message);
this.code = ResultCode.BUSINESS_ERROR.getCode();
}
public BusinessException(int code, String message) {
super(message);
this.code = code;
}
}
@@ -0,0 +1,58 @@
package com.ims.common.util;
import io.jsonwebtoken.*;
import javax.crypto.SecretKey;
import javax.crypto.spec.SecretKeySpec;
import java.nio.charset.StandardCharsets;
import java.util.Date;
public class JwtUtil {
private final SecretKey key;
private final long accessTokenExpiration;
private final long refreshTokenExpiration;
public JwtUtil(String secret, long accessTokenExpiration, long refreshTokenExpiration) {
this.key = new SecretKeySpec(secret.getBytes(StandardCharsets.UTF_8), "HmacSHA256");
this.accessTokenExpiration = accessTokenExpiration;
this.refreshTokenExpiration = refreshTokenExpiration;
}
public String generateAccessToken(Long userId, String username) {
return Jwts.builder()
.subject(username)
.claim("userId", userId)
.issuedAt(new Date())
.expiration(new Date(System.currentTimeMillis() + accessTokenExpiration))
.signWith(key, Jwts.SIG.HS256)
.compact();
}
public String generateRefreshToken(Long userId, String username) {
return Jwts.builder()
.subject(username)
.claim("userId", userId)
.issuedAt(new Date())
.expiration(new Date(System.currentTimeMillis() + refreshTokenExpiration))
.signWith(key, Jwts.SIG.HS256)
.compact();
}
public Claims parseToken(String token) {
return Jwts.parser()
.verifyWith(key)
.build()
.parseSignedClaims(token)
.getPayload();
}
public boolean validateToken(String token) {
try {
parseToken(token);
return true;
} catch (JwtException | IllegalArgumentException e) {
return false;
}
}
}