refactor: 始终上传头像到服务器

- 前端始终上传头像到服务器,不再判断是否为临时路径
- 后端移除downloadAvatar方法
- 数据库avatar_url始终存储服务器路径
This commit is contained in:
H1d3rOne committed 2026-03-10 07:34:00 +08:00
1 parent 8249514262
commit f3b084a72f
2 files changed
+28 -71

No files matched your search

@@ -10,8 +10,6 @@ import xuan.service.UserInfoService;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
@@ -66,12 +64,7 @@ public class UserController {
openId = "guest_" + System.currentTimeMillis();
}
String localAvatarUrl = avatarUrl;
if (avatarUrl != null && !avatarUrl.isEmpty() && avatarUrl.startsWith("http")) {
localAvatarUrl = downloadAvatar(avatarUrl, openId);
}
UserInfo user = userInfoService.loginOrRegister(openId, username, localAvatarUrl);
UserInfo user = userInfoService.loginOrRegister(openId, username, avatarUrl);
Map<String, Object> data = new HashMap<>();
data.put("id", user.getId());
@@ -94,40 +87,6 @@ public class UserController {
return result;
}
private String downloadAvatar(String imageUrl, String userId) {
try {
String uploadDir = "uploads/avatars/";
File dir = new File(uploadDir);
if (!dir.exists()) {
dir.mkdirs();
}
String extension = ".jpg";
if (imageUrl.contains(".")) {
String tempExt = imageUrl.substring(imageUrl.lastIndexOf("."));
if (tempExt.contains("?")) {
tempExt = tempExt.substring(0, tempExt.indexOf("?"));
}
if (tempExt.length() <= 5 && tempExt.matches("\\.[a-zA-Z]+")) {
extension = tempExt;
}
}
String filename = userId + "_" + System.currentTimeMillis() + extension;
Path filePath = Paths.get(uploadDir + filename);
URL url = new URL(imageUrl);
try (InputStream in = url.openStream()) {
Files.copy(in, filePath);
}
return "/uploads/avatars/" + filename;
} catch (Exception e) {
System.err.println("下载头像失败: " + e.getMessage());
return imageUrl;
}
}
@GetMapping("/info")
public Map<String, Object> getUserInfo(@RequestParam String userId) {
Map<String, Object> result = new HashMap<>();