This commit is contained in:
王子琦
2026-01-13 15:19:49 +08:00
parent f58e05d962
commit 6af59d985f
16 changed files with 339 additions and 33 deletions

View File

@@ -37,6 +37,7 @@
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.33</version>
<scope>runtime</scope>
</dependency>
<dependency>

View File

@@ -51,6 +51,29 @@ public class ConfessionController {
return ApiResponse.ok(confessionRepository.save(confession));
}
@GetMapping("/order/{orderId}")
public ApiResponse<Confession> getByOrder(@PathVariable Long orderId) {
Order order = orderRepository.findById(orderId)
.orElseThrow(() -> new ApiException(404, "订单不存在"));
if (!order.getUserId().equals(AuthContext.get().getId())) {
throw new ApiException(403, "无权限");
}
return ApiResponse.ok(confessionRepository.findByOrderId(orderId).orElse(null));
}
@PutMapping("/{id}")
public ApiResponse<Confession> update(@PathVariable Long id, @RequestBody CreateConfessionRequest request) {
Confession confession = confessionRepository.findById(id)
.orElseThrow(() -> new ApiException(404, "告白不存在"));
if (!confession.getUserId().equals(AuthContext.get().getId())) {
throw new ApiException(403, "无权限");
}
confession.setTitle(request.getTitle());
confession.setMessage(request.getMessage());
confession.setImageUrl(request.getImageUrl());
return ApiResponse.ok(confessionRepository.save(confession));
}
@PublicEndpoint
@GetMapping("/{code}")
public ApiResponse<Confession> get(@PathVariable String code) {

View File

@@ -6,4 +6,5 @@ import java.util.Optional;
public interface ConfessionRepository extends JpaRepository<Confession, Long> {
Optional<Confession> findByCode(String code);
Optional<Confession> findByOrderId(Long orderId);
}

View File

@@ -5,17 +5,21 @@ import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
@Configuration
public class WebConfig implements WebMvcConfigurer {
private final AuthInterceptor authInterceptor;
private final String allowedOrigins;
private final String uploadDir;
public WebConfig(AuthInterceptor authInterceptor,
@Value("${app.cors.allowed-origins}") String allowedOrigins) {
@Value("${app.cors.allowed-origins}") String allowedOrigins,
@Value("${app.upload.dir}") String uploadDir) {
this.authInterceptor = authInterceptor;
this.allowedOrigins = allowedOrigins;
this.uploadDir = uploadDir;
}
@Override
@@ -30,4 +34,21 @@ public class WebConfig implements WebMvcConfigurer {
.allowedMethods("GET", "POST", "PUT", "DELETE", "OPTIONS")
.allowCredentials(true);
}
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
registry.addResourceHandler("/uploads/**")
.addResourceLocations("file:" + toFileLocation(uploadDir));
}
private String toFileLocation(String path) {
String normalized = path.replace("\\", "/");
if (normalized.matches("^[A-Za-z]:/.*")) {
return "/" + normalized + "/";
}
if (!normalized.startsWith("/")) {
return "/" + normalized + "/";
}
return normalized.endsWith("/") ? normalized : normalized + "/";
}
}

View File

@@ -3,9 +3,9 @@ server:
spring:
datasource:
url: jdbc:mysql://localhost:3306/flower_shop?useUnicode=true&characterEncoding=utf8&useSSL=false&serverTimezone=Asia/Shanghai
url: jdbc:mysql://localhost:3307/flower_shop?useUnicode=true&characterEncoding=utf8&useSSL=false&serverTimezone=Asia/Shanghai
username: root
password: root
password: qq5211314
jpa:
hibernate:
ddl-auto: update
@@ -16,9 +16,18 @@ spring:
jackson:
date-format: yyyy-MM-dd HH:mm:ss
time-zone: Asia/Shanghai
servlet:
multipart:
max-file-size: 10MB
max-request-size: 10MB
app:
cors:
allowed-origins: http://localhost:8081
qr:
base-url: http://localhost:8081/#/gift
upload:
dir: D:/bs/flower/files
ai:
token: b676775d829147dea4955ce809cc1beb.GZIuVsLf19YT5B12
model: glm-4.7