add
This commit is contained in:
@@ -1,11 +1,18 @@
|
||||
package com.car.config;
|
||||
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
|
||||
import org.springframework.web.servlet.config.annotation.CorsRegistry;
|
||||
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
|
||||
|
||||
@Configuration
|
||||
public class WebConfig implements WebMvcConfigurer {
|
||||
private final String uploadDir;
|
||||
|
||||
public WebConfig(org.springframework.core.env.Environment environment) {
|
||||
this.uploadDir = environment.getProperty("file.upload-dir", "");
|
||||
}
|
||||
|
||||
@Override
|
||||
public void addCorsMappings(CorsRegistry registry) {
|
||||
registry.addMapping("/**")
|
||||
@@ -14,4 +21,12 @@ public class WebConfig implements WebMvcConfigurer {
|
||||
.allowedHeaders("*")
|
||||
.allowCredentials(true);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void addResourceHandlers(ResourceHandlerRegistry registry) {
|
||||
if (!uploadDir.isBlank()) {
|
||||
String location = "file:" + (uploadDir.endsWith("/") ? uploadDir : uploadDir + "/");
|
||||
registry.addResourceHandler("/uploads/**").addResourceLocations(location);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,38 @@
|
||||
package com.car.controller;
|
||||
|
||||
import com.car.common.ApiResponse;
|
||||
import org.springframework.beans.factory.annotation.Value;
|
||||
import org.springframework.util.StringUtils;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
import org.springframework.web.multipart.MultipartFile;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.util.UUID;
|
||||
|
||||
@RestController
|
||||
@RequestMapping("/api")
|
||||
public class UploadController {
|
||||
|
||||
@Value("${file.upload-dir}")
|
||||
private String uploadDir;
|
||||
|
||||
@PostMapping("/upload")
|
||||
public ApiResponse<String> upload(@RequestParam("file") MultipartFile file) throws IOException {
|
||||
if (file.isEmpty()) {
|
||||
return new ApiResponse<>(400, "文件不能为空", null);
|
||||
}
|
||||
File dir = new File(uploadDir);
|
||||
if (!dir.exists() && !dir.mkdirs()) {
|
||||
return new ApiResponse<>(500, "上传目录创建失败", null);
|
||||
}
|
||||
String ext = StringUtils.getFilenameExtension(file.getOriginalFilename());
|
||||
String filename = UUID.randomUUID().toString().replace("-", "");
|
||||
if (ext != null && !ext.isBlank()) {
|
||||
filename += "." + ext;
|
||||
}
|
||||
File dest = new File(dir, filename);
|
||||
file.transferTo(dest);
|
||||
return ApiResponse.ok("/uploads/" + filename);
|
||||
}
|
||||
}
|
||||
@@ -74,6 +74,12 @@ public class UserService {
|
||||
if (db == null) {
|
||||
throw new ApiException(ErrorCode.NOT_FOUND, "用户不存在");
|
||||
}
|
||||
if ("PENDING".equals(db.getRealNameStatus())) {
|
||||
throw new ApiException(ErrorCode.CONFLICT, "实名认证正在审核中");
|
||||
}
|
||||
if ("APPROVED".equals(db.getRealNameStatus())) {
|
||||
throw new ApiException(ErrorCode.CONFLICT, "实名认证已通过");
|
||||
}
|
||||
db.setRealName(request.getRealName());
|
||||
db.setIdNumber(request.getIdNumber());
|
||||
db.setIdFront(request.getIdFront());
|
||||
|
||||
@@ -3,9 +3,9 @@ server:
|
||||
|
||||
spring:
|
||||
datasource:
|
||||
url: jdbc:mysql://localhost:3306/car_rental?useSSL=false&characterEncoding=utf8&serverTimezone=Asia/Shanghai
|
||||
url: jdbc:mysql://localhost:3307/car_rental?useSSL=false&characterEncoding=utf8&serverTimezone=Asia/Shanghai
|
||||
username: root
|
||||
password: root
|
||||
password: qq5211314
|
||||
jackson:
|
||||
date-format: yyyy-MM-dd HH:mm:ss
|
||||
time-zone: Asia/Shanghai
|
||||
@@ -24,3 +24,6 @@ sa-token:
|
||||
is-share: true
|
||||
token-prefix: Bearer
|
||||
is-log: false
|
||||
|
||||
file:
|
||||
upload-dir: ${user.dir}/uploads
|
||||
|
||||
Reference in New Issue
Block a user