diff --git a/opsli-api/src/main/java/org/opsli/api/web/system/user/UserApi.java b/opsli-api/src/main/java/org/opsli/api/web/system/user/UserApi.java index a15b2c5..174ee50 100644 --- a/opsli-api/src/main/java/org/opsli/api/web/system/user/UserApi.java +++ b/opsli-api/src/main/java/org/opsli/api/web/system/user/UserApi.java @@ -128,11 +128,11 @@ public interface UserApi { /** * 上传头像 - * @param request 文件流 request + * @param userAvatarModel 图片地址 * @return ResultVo */ @PostMapping("/updateAvatar") - ResultVo updateAvatar(MultipartHttpServletRequest request); + ResultVo updateAvatar(@RequestBody UserAvatarModel userAvatarModel); /** diff --git a/opsli-api/src/main/java/org/opsli/api/wrapper/system/user/UserAvatarModel.java b/opsli-api/src/main/java/org/opsli/api/wrapper/system/user/UserAvatarModel.java new file mode 100644 index 0000000..5d12f93 --- /dev/null +++ b/opsli-api/src/main/java/org/opsli/api/wrapper/system/user/UserAvatarModel.java @@ -0,0 +1,38 @@ +/** + * Copyright 2020 OPSLI 快速开发平台 https://www.opsli.com + *

+ * Licensed under the Apache License, Version 2.0 (the "License"); you may not + * use this file except in compliance with the License. You may obtain a copy of + * the License at + *

+ * http://www.apache.org/licenses/LICENSE-2.0 + *

+ * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the + * License for the specific language governing permissions and limitations under + * the License. + */ +package org.opsli.api.wrapper.system.user; + +import io.swagger.annotations.ApiModelProperty; +import lombok.Data; +import lombok.EqualsAndHashCode; +import org.opsli.common.annotation.validator.Validator; +import org.opsli.common.enums.ValidatorType; + +/** + * 用户信息表 + * + * @author Parker + * @date 2020-09-16 17:33 + */ +@Data +@EqualsAndHashCode(callSuper = false) +public class UserAvatarModel { + + @ApiModelProperty(value = "图片地址") + @Validator({ValidatorType.IS_NOT_NULL, ValidatorType.IS_URL}) + private String imgUrl; + +} diff --git a/opsli-modulars/opsli-modulars-system/src/main/java/org/opsli/modulars/system/user/web/UserRestController.java b/opsli-modulars/opsli-modulars-system/src/main/java/org/opsli/modulars/system/user/web/UserRestController.java index c549597..f5157c5 100644 --- a/opsli-modulars/opsli-modulars-system/src/main/java/org/opsli/modulars/system/user/web/UserRestController.java +++ b/opsli-modulars/opsli-modulars-system/src/main/java/org/opsli/modulars/system/user/web/UserRestController.java @@ -63,6 +63,7 @@ import org.opsli.plugins.oss.service.BaseOssStorageService; import org.opsli.plugins.oss.service.OssStorageService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.core.io.Resource; +import org.springframework.web.bind.annotation.RequestBody; import org.springframework.web.multipart.MultipartFile; import org.springframework.web.multipart.MultipartHttpServletRequest; @@ -201,44 +202,20 @@ public class UserRestController extends BaseRestController updateAvatar(MultipartHttpServletRequest request) { - Iterator itr = request.getFileNames(); - String uploadedFile = itr.next(); - List files = request.getFiles(uploadedFile); - if (CollectionUtils.isEmpty(files)) { - // 请选择文件 - return ResultVo.error(SystemMsg.EXCEPTION_USER_FILE_NULL.getCode(), - SystemMsg.EXCEPTION_USER_FILE_NULL.getMessage()); - } - - try { - MultipartFile multipartFile = files.get(0); - Resource resource = multipartFile.getResource(); - String filename = resource.getFilename(); - - // 调用OSS 服务保存头像 - OssStorageService ossStorageService = OssStorageFactory.INSTANCE.getHandle(); - BaseOssStorageService.FileAttr fileAttr = ossStorageService.upload( - multipartFile.getInputStream(), FileUtil.extName(filename)); - - UserModel user = UserUtil.getUserBySource(); - // 更新头像至数据库 - UserModel userModel = new UserModel(); - userModel.setId(user.getId()); - userModel.setAvatar(fileAttr.getFileStoragePath()); - IService.updateAvatar(userModel); - // 刷新用户信息 - UserUtil.refreshUser(user); - }catch (IOException e){ - log.error(e.getMessage(), e); - return ResultVo.error("更新头像失败,请稍后再试"); - } - + public ResultVo updateAvatar(UserAvatarModel userAvatarModel) { + UserModel user = UserUtil.getUserBySource(); + // 更新头像至数据库 + UserModel userModel = new UserModel(); + userModel.setId(user.getId()); + userModel.setAvatar(userAvatarModel.getImgUrl()); + IService.updateAvatar(userModel); + // 刷新用户信息 + UserUtil.refreshUser(user); return ResultVo.success(); } diff --git a/opsli-modulars/opsli-modulars-system/src/main/java/org/opsli/modulars/tools/oss/web/OssRestController.java b/opsli-modulars/opsli-modulars-system/src/main/java/org/opsli/modulars/tools/oss/web/OssRestController.java index 410e8a9..5ad50c9 100644 --- a/opsli-modulars/opsli-modulars-system/src/main/java/org/opsli/modulars/tools/oss/web/OssRestController.java +++ b/opsli-modulars/opsli-modulars-system/src/main/java/org/opsli/modulars/tools/oss/web/OssRestController.java @@ -1,8 +1,27 @@ package org.opsli.modulars.tools.oss.web; +import cn.hutool.core.io.FileUtil; +import com.alibaba.excel.util.CollectionUtils; +import com.baomidou.mybatisplus.extension.service.IService; import io.swagger.annotations.Api; +import io.swagger.annotations.ApiOperation; import lombok.extern.slf4j.Slf4j; +import org.opsli.api.base.result.ResultVo; +import org.opsli.api.wrapper.system.user.UserModel; import org.opsli.common.annotation.ApiRestController; +import org.opsli.core.utils.UserUtil; +import org.opsli.modulars.system.SystemMsg; +import org.opsli.plugins.oss.OssStorageFactory; +import org.opsli.plugins.oss.service.BaseOssStorageService; +import org.opsli.plugins.oss.service.OssStorageService; +import org.springframework.core.io.Resource; +import org.springframework.web.bind.annotation.PostMapping; +import org.springframework.web.multipart.MultipartFile; +import org.springframework.web.multipart.MultipartHttpServletRequest; + +import java.io.IOException; +import java.util.Iterator; +import java.util.List; /** @@ -16,6 +35,39 @@ import org.opsli.common.annotation.ApiRestController; @ApiRestController("/{ver}/tools/oss") public class OssRestController { + /** + * 文件上传 + * @param request 文件流 request + * @return ResultVo + */ + @ApiOperation(value = "文件上传", notes = "文件上传") + @PostMapping("/upload") + public ResultVo upload(MultipartHttpServletRequest request) { + Iterator itr = request.getFileNames(); + String uploadedFile = itr.next(); + List files = request.getFiles(uploadedFile); + if (CollectionUtils.isEmpty(files)) { + // 请选择文件 + return ResultVo.error(SystemMsg.EXCEPTION_USER_FILE_NULL.getCode(), + SystemMsg.EXCEPTION_USER_FILE_NULL.getMessage()); + } + + try { + MultipartFile multipartFile = files.get(0); + Resource resource = multipartFile.getResource(); + String filename = resource.getFilename(); + + // 调用OSS 服务保存文件 + OssStorageService ossStorageService = OssStorageFactory.INSTANCE.getHandle(); + BaseOssStorageService.FileAttr fileAttr = ossStorageService.upload( + multipartFile.getInputStream(), FileUtil.extName(filename)); + + return ResultVo.success(fileAttr); + }catch (IOException e){ + log.error(e.getMessage(), e); + } + return ResultVo.error(); + } }