Explorar o código

上传头像base64

shiqian %!s(int64=4) %!d(string=hai) anos
pai
achega
5771502989

+ 8 - 0
boman-api/boman-api-system/src/main/java/com/boman/system/api/RemoteFileService.java

@@ -1,8 +1,10 @@
 package com.boman.system.api;
 package com.boman.system.api;
 
 
+import com.boman.domain.dto.AjaxResult;
 import org.springframework.cloud.openfeign.FeignClient;
 import org.springframework.cloud.openfeign.FeignClient;
 import org.springframework.http.MediaType;
 import org.springframework.http.MediaType;
 import org.springframework.web.bind.annotation.PostMapping;
 import org.springframework.web.bind.annotation.PostMapping;
+import org.springframework.web.bind.annotation.RequestBody;
 import org.springframework.web.bind.annotation.RequestPart;
 import org.springframework.web.bind.annotation.RequestPart;
 import org.springframework.web.multipart.MultipartFile;
 import org.springframework.web.multipart.MultipartFile;
 import com.boman.domain.constant.ServiceNameConstants;
 import com.boman.domain.constant.ServiceNameConstants;
@@ -26,4 +28,10 @@ public interface RemoteFileService
      */
      */
     @PostMapping(value = "/upload", consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
     @PostMapping(value = "/upload", consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
     public R<SysFile> upload(@RequestPart(value = "file") MultipartFile file);
     public R<SysFile> upload(@RequestPart(value = "file") MultipartFile file);
+
+    /**
+     * 通用上传请求
+     */
+    @PostMapping("/upload/base64")
+    AjaxResult uploadFileBase64(@RequestBody String base64);
 }
 }

+ 11 - 0
boman-api/boman-api-system/src/main/java/com/boman/system/api/factory/RemoteFileFallbackFactory.java

@@ -1,5 +1,6 @@
 package com.boman.system.api.factory;
 package com.boman.system.api.factory;
 
 
+import com.boman.domain.dto.AjaxResult;
 import org.slf4j.Logger;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 import org.slf4j.LoggerFactory;
 import org.springframework.stereotype.Component;
 import org.springframework.stereotype.Component;
@@ -30,6 +31,16 @@ public class RemoteFileFallbackFactory implements FallbackFactory<RemoteFileServ
             {
             {
                 return R.fail("上传文件失败:" + throwable.getMessage());
                 return R.fail("上传文件失败:" + throwable.getMessage());
             }
             }
+
+            /**
+             * 通用上传请求
+             *
+             * @param base64
+             */
+            @Override
+            public AjaxResult uploadFileBase64(String base64) {
+                return AjaxResult.error("上传文件失败:" + throwable.getMessage());
+            }
         };
         };
     }
     }
 }
 }

+ 25 - 0
boman-modules/boman-system/src/main/java/com/boman/system/controller/SysProfileController.java

@@ -135,4 +135,29 @@ public class SysProfileController extends BaseController
         }
         }
         return AjaxResult.error("上传图片异常,请联系管理员");
         return AjaxResult.error("上传图片异常,请联系管理员");
     }
     }
+
+    /**
+     * 头像上传
+     */
+    @Log(title = "用户头像", businessType = BusinessType.UPDATE)
+    @PostMapping("/avatarBase64")
+    public AjaxResult avatarBase64(@RequestBody String base64) {
+        LoginUser loginUser = tokenService.getLoginUser(ServletUtils.getRequest());
+        AjaxResult ajaxResult = remoteFileService.uploadFileBase64(base64);
+        if (AjaxResult.checkFail(ajaxResult)) {
+            return AjaxResult.error("文件服务异常,请联系管理员");
+        }
+
+        String url = ((String) ajaxResult.get("url"));
+        if (userService.updateUserAvatar(loginUser.getUsername(), url)) {
+            AjaxResult ajax = AjaxResult.success();
+            ajax.put("imgUrl", url);
+            // 更新缓存用户头像
+            loginUser.getSysUser().setAvatar(url);
+            tokenService.setLoginUser(loginUser);
+            return ajax;
+        }
+
+        return AjaxResult.error("上传图片异常,请联系管理员");
+    }
 }
 }