fix: 修复微信头像上传到服务器功能
- auth-modal.vue: 登录前先上传微信临时头像到服务器 - uploadAvatar接口支持临时上传(temp参数) - 数据库avatar_url存储服务器路径而非临时路径
This commit is contained in:
1 parent
2667098632
commit
8249514262
2 files changed
+56
-13
No files matched your search
@@ -369,7 +369,8 @@ public class UserController {
|
||||
@PostMapping("/uploadAvatar")
|
||||
public Map<String, Object> uploadAvatar(
|
||||
@RequestParam("file") MultipartFile file,
|
||||
@RequestParam("userId") String userId) {
|
||||
@RequestParam(value = "userId", required = false) String userId,
|
||||
@RequestParam(value = "temp", required = false) String temp) {
|
||||
Map<String, Object> result = new HashMap<>();
|
||||
try {
|
||||
if (file.isEmpty()) {
|
||||
@@ -385,7 +386,7 @@ public class UserController {
|
||||
}
|
||||
|
||||
String originalFilename = file.getOriginalFilename();
|
||||
String extension = "";
|
||||
String extension = ".jpg";
|
||||
if (originalFilename != null && originalFilename.contains(".")) {
|
||||
extension = originalFilename.substring(originalFilename.lastIndexOf("."));
|
||||
}
|
||||
@@ -396,16 +397,19 @@ public class UserController {
|
||||
|
||||
String avatarUrl = "/uploads/avatars/" + filename;
|
||||
|
||||
UserInfo user = userInfoService.updateAvatarUrl(userId, avatarUrl);
|
||||
if (user != null) {
|
||||
Map<String, Object> data = new HashMap<>();
|
||||
data.put("avatarUrl", avatarUrl);
|
||||
result.put("success", true);
|
||||
result.put("data", data);
|
||||
} else {
|
||||
result.put("success", false);
|
||||
result.put("error", "用户不存在");
|
||||
if (userId != null && !userId.isEmpty() && !"true".equals(temp)) {
|
||||
UserInfo user = userInfoService.updateAvatarUrl(userId, avatarUrl);
|
||||
if (user == null) {
|
||||
result.put("success", false);
|
||||
result.put("error", "用户不存在");
|
||||
return result;
|
||||
}
|
||||
}
|
||||
|
||||
Map<String, Object> data = new HashMap<>();
|
||||
data.put("avatarUrl", avatarUrl);
|
||||
result.put("success", true);
|
||||
result.put("data", data);
|
||||
} catch (IOException e) {
|
||||
result.put("success", false);
|
||||
result.put("error", "文件上传失败: " + e.getMessage());
|
||||
|
||||
Reference in new issue
Block a user