Browse Source

修改bug

suntianwu 3 năm trước cách đây
mục cha
commit
4293985bdc

+ 24 - 4
src/main/java/com/care/bms/controller/OrganizationController.java

@@ -14,14 +14,12 @@ import com.care.common.util.WebPageUtils;
 import com.care.common.vo.PageReqVO;
 import com.care.common.vo.UserLogindConvertVO;
 import com.care.common.vo.org.OrganizationVO;
-import io.swagger.annotations.Api;
-import io.swagger.annotations.ApiImplicitParam;
-import io.swagger.annotations.ApiImplicitParams;
-import io.swagger.annotations.ApiOperation;
+import io.swagger.annotations.*;
 import lombok.extern.slf4j.Slf4j;
 import org.apache.commons.codec.digest.DigestUtils;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.web.bind.annotation.*;
+import org.springframework.web.multipart.MultipartFile;
 
 import javax.annotation.Resource;
 import javax.servlet.http.HttpServletRequest;
@@ -105,6 +103,28 @@ public class OrganizationController {
     }
 
 
+    @ApiOperation(value = "修改机构 -logo图片上传 -- 四期新增")
+    @ApiImplicitParams({
+            @ApiImplicitParam(name = "token", value = "放在请求头中的令牌",
+                    dataType = "String", paramType = "header",
+                    required = true)
+    })
+    @CrossOrigin
+    @PostMapping(value = "/img/upload", headers="content-type=multipart/form-data",produces = "application/json;charset=UTF-8")
+    public Result<String> imgUpload( @ApiParam(value="图片",required=true) MultipartFile file,
+                                     @RequestHeader(name = "token") String token) {
+        try {
+            String url = organizationService.upload(file);
+            return  Result.success("上传成功!",url);
+        } catch (BDException e) {
+            log.error("logo图片上传出现异常",e);
+            return Result.error(e.getMessage());
+        } catch (Exception e) {
+            log.error("机构管理: logo图片上传出现异常",e);
+            return Result.error("logo图片上传失败!");
+        }
+    }
+
     @PostMapping("/updateOrganization")
     @ApiOperation(value = "修改机构 --四期新增")
     public Result<Object> updateOrganization(@RequestHeader("token") String token,

+ 35 - 18
src/main/java/com/care/bms/service/OrganizationService.java

@@ -3,6 +3,7 @@ package com.care.bms.service;
 
 import cn.hutool.core.bean.BeanUtil;
 import cn.hutool.core.collection.CollUtil;
+
 import cn.hutool.core.util.StrUtil;
 import cn.hutool.json.JSONArray;
 import cn.hutool.json.JSONObject;
@@ -38,7 +39,9 @@ import org.springframework.util.StringUtils;
 import org.springframework.web.multipart.MultipartFile;
 
 import javax.annotation.Resource;
+import java.io.File;
 import java.io.FileOutputStream;
+import java.io.IOException;
 import java.util.*;
 
 
@@ -181,6 +184,38 @@ public class OrganizationService {
         }
 
     }
+    /**
+     * 上传文件
+     * @param file
+     * @return  访问URL
+     */
+    public String upload(MultipartFile file) throws Exception {
+         String logo = null;
+         try {
+            if (file != null) {
+                //获取文件名
+                String fileName = file.getOriginalFilename();
+                if (StringUtils.isEmpty(fileName) || file.getSize() == 0) {
+                    throw new BDException("logo文件不能为空!");
+                }
+                //验证文件名是否合格
+                if (!ImgUtil.isImg(fileName)) {
+                    throw new BDException("logo文件必须是图片格式!");
+                }
+                String saveFileName = UUID.randomUUID().toString() + "_" + fileName;
+                String picFullFileName = savePath + "/logo/" + saveFileName;
+                FileOutputStream fos = new FileOutputStream(picFullFileName);
+                fos.write(file.getBytes());
+                logo = fileUrl + "logo/" + saveFileName;
+            } else {
+                throw new BDException("上传失败");
+            }
+        } catch (Exception e) {
+            throw new BDException("上传失败",e);
+        }
+
+        return logo;
+    }
 
     /**
      * 修改机构
@@ -189,24 +224,6 @@ public class OrganizationService {
     @Transactional(rollbackFor = Exception.class)
     public void updateOrganization(OrganizationVO vo) throws Exception {
 
-        MultipartFile logoImg = vo.getLogoImg();
-        if (logoImg != null) {
-            //获取文件名
-            String fileName = logoImg.getOriginalFilename();
-            if (StringUtils.isEmpty(fileName) || logoImg.getSize() == 0) {
-                throw new BDException("logo文件不能为空!");
-            }
-            //验证文件名是否合格
-            if (!ImgUtil.isImg(fileName)) {
-                throw new BDException("logo文件必须是图片格式!");
-            }
-            String saveFileName = UUID.randomUUID().toString() + "_" + fileName;
-            String picFullFileName = savePath + "/logo/" + saveFileName;
-            FileOutputStream fos = new FileOutputStream(picFullFileName);
-            fos.write(logoImg.getBytes());
-            vo.setLogo(fileUrl + "logo/" + saveFileName);
-        }
-
         CareOrganization careOrganization = new CareOrganization();
         BeanUtil.copyProperties(vo, careOrganization);
         if(!this.careOrganizationService.updateById(careOrganization)){