Pārlūkot izejas kodu

重构 修复bug

wuweihao 4 gadi atpakaļ
vecāks
revīzija
4a7757f920

+ 4 - 3
720yun_fd_consumer/src/main/java/com/gis/listener/Fd720Listener.java

@@ -38,15 +38,16 @@ public class Fd720Listener {
         } catch (InterruptedException e) {
             e.printStackTrace();
         }
-        log.warn("run Fd720Listener: " + param);
+        log.warn("run Fd720Listener id: " + param);
         cmdPano(param);
-        log.warn("end Fd720Listener: "+ param);
+        log.warn("end Fd720Listener id: "+ param);
     }
 
 
     public void cmdPano(Long id) {
         log.info("start task cmdPano");
-        FodderEntity entity = fodderMapper.selectByPrimaryKey(id);
+//        FodderEntity entity = fodderMapper.selectByPrimaryKey(id);
+        FodderEntity entity = fodderMapper.findById(id);
         if (entity == null) {
             log.error("场景不存在: " + id);
             // 直接结束,抛异常的话,会造成死循环,产生大量日志,而且队列也跑不下去

+ 3 - 1
720yun_fd_consumer/src/main/java/com/gis/mapper/FodderMapper.java

@@ -3,6 +3,7 @@ package com.gis.mapper;
 
 import com.gis.entity.FodderEntity;
 import org.apache.ibatis.annotations.Mapper;
+import org.apache.ibatis.annotations.Select;
 import org.springframework.stereotype.Component;
 
 
@@ -10,5 +11,6 @@ import org.springframework.stereotype.Component;
 @Mapper
 public interface FodderMapper extends IBaseMapper<FodderEntity, Long> {
 
-
+    @Select("select * from tb_fodder where is_delete = 0 and id = #{id}")
+    FodderEntity findById(Long id);
 }

+ 2 - 1
720yun_fd_manage/gis_application/src/main/resources/data/someData.json

@@ -15,7 +15,7 @@
       ]
     }
   ],
-  "firstScene": {},
+  "firstScene": "",
   "userId": "",
   "isLogo": 1,
   "appIcon": "",
@@ -36,5 +36,6 @@
   "logoChange": false,
   "share": "",
   "id": "",
+  "remindTime": 1,
   "status": 0
 }

+ 3 - 0
720yun_fd_manage/gis_common/src/main/java/com/gis/common/constant/MsgCode.java

@@ -19,6 +19,9 @@ public class MsgCode {
     /** 不是该用户作品 */
     public final static Integer e3005 = 3005;
 
+    /** 作品已被删除,无法编辑 */
+    public final static Integer e3006 = 3006;
+
     public final static Integer e5003 = 5003;
     public final static String m5003 = "不支持此图片";
 

+ 3 - 0
720yun_fd_manage/gis_mapper/src/main/java/com/gis/mapper/provider/FodderProvider.java

@@ -26,6 +26,9 @@ public class FodderProvider {
         String type = param.getType();
         if(!StringUtils.isAllBlank(type)){
             sql.append(" and ( type = '").append(type).append("' )");
+            if ("pano".equals(type)) {
+                sql.append(" and status >= ").append(2);
+            }
         }
 
         sql.append(" order by create_time desc");

+ 4 - 2
720yun_fd_manage/gis_service/src/main/java/com/gis/service/FodderService.java

@@ -18,9 +18,9 @@ public interface FodderService extends IBaseService<FodderEntity, Long> {
 
 
 
-    Result upload(MultipartFile file, String type) ;
-
+    Result upload2(MultipartFile file, String type) ;
 
+    Result upload(MultipartFile file, String type) ;
 
     Result search(FodderPageDto param);
 
@@ -29,4 +29,6 @@ public interface FodderService extends IBaseService<FodderEntity, Long> {
 
 
     Result checkStatus(String ids);
+
+
 }

+ 3 - 0
720yun_fd_manage/gis_service/src/main/java/com/gis/service/WorkService.java

@@ -7,6 +7,7 @@ import com.gis.domain.dto.SomeDataDto;
 import com.gis.domain.dto.WorkDto;
 import com.gis.domain.entity.WorkEntity;
 import com.gis.domain.vo.WorkInfoVo;
+import org.springframework.web.multipart.MultipartFile;
 
 
 /**
@@ -30,4 +31,6 @@ public interface WorkService extends IBaseService<WorkEntity, Long> {
     Result edit(SomeDataDto param);
 
     Result select4dkk(PageDto param, Long workId);
+
+    Result editUpload(MultipartFile file);
 }

+ 138 - 1
720yun_fd_manage/gis_service/src/main/java/com/gis/service/impl/FodderServiceImpl.java

@@ -67,7 +67,141 @@ public class FodderServiceImpl extends IBaseServiceImpl<FodderEntity, Long> impl
 
 
     @Override
+    public Result upload2(MultipartFile file, String type)  {
+        log.info("now time: " + new Date());
+
+        long start = System.currentTimeMillis();
+        // 检查非法文件上传
+        boolean checkFile = FileUtils.checkFile(file);
+        if (!checkFile) {
+            return Result.failure("上传文件格式有误, 请重新上传");
+        }
+
+        String time = DateUtil.format(new Date(), "yyyyMMdd_HHmmssSSS");
+        String fileName = file.getOriginalFilename();
+        // 文件名校验长度
+        String prefix = StringUtils.substringBeforeLast(fileName, ".");
+        if (prefix.length() > 50) {
+            return Result.failure(3003, "文件名称不允许超过50个字节");
+        }
+        String suffix = StringUtils.substringAfterLast(fileName, ".");
+        String newName = time  + "."  +suffix;
+        String dirType = "fodder/";
+        long size = file.getSize();
+        size = size/1024;
+        log.info("fileSize: " + size);
+
+        String ossUrl = null;
+        String iconPath = "0";
+        String dpi = "0";
+        String ossPath = configConstant.ossBasePath+dirType+newName;
+        String savePath = configConstant.serverBasePath;
+        String ossPreviewIcon = null;
+
+        FodderEntity entity = new FodderEntity();
+        try {
+            if (type.equals("pano")) {
+
+                if ((size/1024) >= 120) {
+                    return Result.failure(MsgCode.e3004, "全景图文件不能超过120MB");
+                }
+
+                String sceneCode = RandomUtils.getSceneCode("fd720_");
+                newName = sceneCode+"." + suffix;
+                savePath =savePath + sceneCode + "/" + newName;
+
+
+                log.info("准备写入全景图: " +  new Date());
+                Thread.sleep(10000);
+
+                FileUtil.writeFromStream(file.getInputStream(), savePath);
+
+                log.info("pano serverPath: " + savePath);
+                log.info("全景图片写入完成");
+
+                log.info("睡眠10s: " +  new Date());
+                Thread.sleep(10000);
+
+                log.info("准备执行压缩图片: " +  new Date());
+                // 压缩图片并上传oss
+                iconPath = fileUtils.compressImgAndUploadOss2(
+                        savePath, configConstant.ossBasePath + sceneCode , configConstant.ossDomain, 600, 300, "/thumb_"+sceneCode+".jpg");
+
+
+                log.info("压缩图片2: " +  new Date());
+                // 全景图的预览图
+                ossPreviewIcon = fileUtils.compressImgAndUploadOss2(
+                        savePath, configConstant.ossBasePath + sceneCode , configConstant.ossDomain, 1920, 960, "/preview.jpg");
+
+
+                ossUrl = configConstant.ossDomain+configConstant.ossBasePath + sceneCode;
+
+
+
+                entity.setSceneCode(sceneCode);
+                entity.setFilePath(savePath);
+                entity.setStatus(1);
+
+            } else {
+                ossUrl = configConstant.ossDomain+ossPath;
+
+                if (type.equals("image")) {
+                    // 图片大于1MB生成缩略图, 小于1MB使用原图作为缩略图
+                    if (size > 1024) {
+                        savePath = savePath + dirType + newName;
+                        FileUtil.writeFromStream(file.getInputStream(), savePath);
+                        iconPath = fileUtils.scaleImgAndUploadOss(savePath, configConstant.ossBasePath + dirType , configConstant.ossDomain);
+                    } else {
+                        iconPath = ossUrl;
+                    }
+
+                    dpi = ImageUtil.dpi(file);
+
+                }
+                // 普通素材直接上传oss, 服务器不保留文件
+                aliyunOssUtil.upload(file.getBytes(), ossPath);
+
+
+            }
+            entity.setType(type);
+            // 用前缀做名称
+            entity.setName(prefix);
+            entity.setFileName(newName);
+            log.info("ossUrl: " + ossUrl);
+            log.info("iconPath:" + iconPath);
+
+            entity.setOssPath(ossUrl);
+            entity.setIcon(iconPath);
+            entity.setUserId("15015980188");
+            entity.setFileSize(size+"");
+            entity.setDpi(dpi);
+            entity.setPreviewIcon(ossPreviewIcon);
+
+//            save(entity);
+
+//            if (type.equals("pano")) {
+//                //发消息到mq
+//                rabbitTemplate.convertAndSend(RabbitConfig.PANO_EXCHANGE, RabbitConfig.PANO_QUEUE_ROUTING, entity.getId());
+//                log.info("发送消息到队列完成: " + entity.getId());
+//            }
+            long end = System.currentTimeMillis();
+            log.info("上传完成,耗时: {} s" , (end-start)/1000);
+        } catch (Exception e) {
+            e.printStackTrace();
+            log.error(e.getMessage());
+            throw new BaseRuntimeException(MsgCode.e5003, "不支持此图片");
+        }
+
+
+        return Result.success(entity);
+    }
+
+
+
+    @Override
     public Result upload(MultipartFile file, String type)  {
+
+        long start = System.currentTimeMillis();
         // 检查非法文件上传
         boolean checkFile = FileUtils.checkFile(file);
         if (!checkFile) {
@@ -173,13 +307,15 @@ public class FodderServiceImpl extends IBaseServiceImpl<FodderEntity, Long> impl
             rabbitTemplate.convertAndSend(RabbitConfig.PANO_EXCHANGE, RabbitConfig.PANO_QUEUE_ROUTING, entity.getId());
             log.info("发送消息到队列完成: " + entity.getId());
         }
-
+            long end = System.currentTimeMillis();
+            log.info("上传完成,耗时: {} s" , (end-start)/1000);
         } catch (Exception e) {
             e.printStackTrace();
             log.error(e.getMessage());
             throw new BaseRuntimeException(MsgCode.e5003, "不支持此图片");
         }
 
+
         return Result.success(entity);
     }
 
@@ -240,4 +376,5 @@ public class FodderServiceImpl extends IBaseServiceImpl<FodderEntity, Long> impl
 
 
 
+
 }

+ 36 - 13
720yun_fd_manage/gis_service/src/main/java/com/gis/service/impl/WorkServiceImpl.java

@@ -32,6 +32,7 @@ import org.apache.http.util.EntityUtils;
 import org.springframework.beans.BeanUtils;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.stereotype.Service;
+import org.springframework.web.multipart.MultipartFile;
 
 import javax.transaction.Transactional;
 import java.io.IOException;
@@ -76,6 +77,10 @@ public class WorkServiceImpl extends IBaseServiceImpl<WorkEntity, Long> implemen
     @Autowired
     AliyunOssUtil aliyunOssUtil;
 
+    @Autowired
+    FileUtils fileUtils;
+
+
 
     @Override
     public IBaseMapper<WorkEntity, Long> getBaseMapper() {
@@ -120,9 +125,22 @@ public class WorkServiceImpl extends IBaseServiceImpl<WorkEntity, Long> implemen
         this.save(entity);
 
         // 创建someData
+        // 如基someData作出修改,记得把代码里的someData 跟服务器里的someData都修改
+        String baseSomeDataPath = configConstant.serverBasePath + "baseData/someData.json";
+        log.info("服务器base someData.json path: {}", baseSomeDataPath);
+        // someData.json 内容
+        String content = null;
+        if (FileUtil.isFile(baseSomeDataPath)) {
+            log.info("使用服务器someData.json");
+            content = FileUtil.readUtf8String(baseSomeDataPath);
+        } else {
+            // 获取文件内容
+            log.info("创建someData.json");
+            content  = FileUtils.getResourceContent("data/someData.json");
+            FileUtil.writeUtf8String(content, baseSomeDataPath);
+        }
+
 
-        // 获取文件内容
-        String content = FileUtils.getResourceContent("data/someData.json");
 
         Long id = entity.getId();
 
@@ -136,10 +154,7 @@ public class WorkServiceImpl extends IBaseServiceImpl<WorkEntity, Long> implemen
         jsonObject.put("share", shareUrl);
         jsonObject.put("qrCode", qrCode);
 
-
-        String baseSomeDataPath = id + "/someData.json";
-
-        String ossKeyPath = configConstant.ossBasePath + baseSomeDataPath;
+        String ossKeyPath = configConstant.ossBasePath + id + "/someData.json";
 
         try {
             aliyunOssUtil.upload(jsonObject.toJSONString().getBytes(), ossKeyPath);
@@ -247,18 +262,13 @@ public class WorkServiceImpl extends IBaseServiceImpl<WorkEntity, Long> implemen
         Long id = param.getId();
         WorkEntity entity = this.findById(id);
         if (entity == null) {
-            log.error("对象不存在: " + id);
-            return false;
+            log.error("作品已被删除,无法编辑, id: " + id);
+            throw new BaseRuntimeException(MsgCode.e3006, "作品已被删除,无法编辑, id: " + id);
         }
 
         String someData = param.getSomeData();
         JSONObject sd = JSONObject.parseObject(someData);
         entity.setName(sd.getString("name"));
-//        String description = sd.getString("description");
-//        if (description != null && description.length() >= 254 ) {
-//            throw new BaseRuntimeException(-1, "简介超出长度");
-//        }
-//        entity.setDescription(description);
         entity.setIcon(sd.getString("icon"));
         entity.setShare(sd.getString("share"));
         entity.setQrCode(sd.getString("qrCode"));
@@ -408,4 +418,17 @@ public class WorkServiceImpl extends IBaseServiceImpl<WorkEntity, Long> implemen
 
     }
 
+
+    @Override
+    public Result editUpload(MultipartFile file) {
+
+        // 检查非法文件上传
+        boolean checkFile = FileUtils.checkFile(file);
+        if (!checkFile) {
+            return Result.failure("上传文件格式有误, 请重新上传");
+        }
+        return Result.success(fileUtils.renameUploadOss(file, configConstant.serverBasePath, configConstant.ossBasePath, configConstant.ossDomain));
+    }
+
+
 }

+ 2 - 0
720yun_fd_manage/gis_web/src/main/java/com/gis/web/controller/FodderController.java

@@ -126,4 +126,6 @@ public class FodderController extends BaseController {
 
 
 
+
+
 }

+ 6 - 0
720yun_fd_manage/gis_web/src/main/java/com/gis/web/controller/TestController.java

@@ -59,6 +59,12 @@ public class TestController extends BaseController {
     @Autowired
     private RedisTemplate<String, String> redisTemplate;
 
+
+
+
+
+
+
     /**
      * 直接文件流响应前端,测试成功
      * 浏览器:http://127.0.0.1:8112/test/testFrontStream

+ 41 - 12
720yun_fd_manage/gis_web/src/main/java/com/gis/web/controller/WebController.java

@@ -1,11 +1,14 @@
 package com.gis.web.controller;
 
 
+import cn.hutool.core.io.FileUtil;
+import com.gis.common.constant.ConfigConstant;
 import com.gis.common.constant.MsgCode;
 import com.gis.common.util.Result;
 import com.gis.domain.entity.SceneEntity;
 import com.gis.domain.entity.WorkEntity;
 import com.gis.service.CatalogService;
+import com.gis.service.FodderService;
 import com.gis.service.SceneService;
 import com.gis.service.WorkService;
 import io.swagger.annotations.Api;
@@ -14,7 +17,9 @@ import lombok.extern.log4j.Log4j2;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.data.redis.core.RedisTemplate;
 import org.springframework.web.bind.annotation.*;
+import org.springframework.web.multipart.MultipartFile;
 
+import java.io.IOException;
 
 
 /**
@@ -38,6 +43,12 @@ public class WebController extends BaseController {
     @Autowired
     SceneService sceneService;
 
+    @Autowired
+    FodderService fodderService;
+
+    @Autowired
+    ConfigConstant configConstant;
+
 
 
 
@@ -84,9 +95,11 @@ public class WebController extends BaseController {
 
 
 
+
+
     @ApiOperation(value = "检查token是否有效" , position = 3)
-    @GetMapping("checkToken/{workId}")
-    public Result checkToken(@PathVariable Long workId) {
+    @GetMapping("checkToken")
+    public Result checkToken() {
         String token = getToken();
         if (token == null) {
             log.error("token is null");
@@ -99,19 +112,35 @@ public class WebController extends BaseController {
             log.error("redis token is null");
             return Result.failure(5001, "Token无效");
         }
+        return Result.success();
 
-        String userId = getUserNameForToken();
-        WorkEntity entity = workService.findById(workId);
-        if (entity == null) {
-            return Result.failure("对象不存在: " + workId);
-        }
+    }
 
-        if (!userId.equals(entity.getUserId())) {
-            return Result.failure(MsgCode.e3005, "不是该用户作品");
-        }
 
-        return Result.success();
+//    @ApiOperation(value = "上传素材", notes = "type:类型, 全景图:pano, 图片:image, 音频:audio, 视频:video")
+//    @PostMapping("upload2/{type}")
+//    public Result upload2(MultipartFile file, @PathVariable String type) {
+//
+//        if (file == null) {
+//            log.error("文件不能为空");
+//            return Result.failure("文件不能为空");
+//        }
+//        return fodderService.upload2(file, type);
+//    }
+//
+//    @ApiOperation(value = "普通上传")
+//    @PostMapping("upload3")
+//    public Result upload3(MultipartFile file) throws IOException {
+//
+//        if (file == null) {
+//            log.error("文件不能为空");
+//            return Result.failure("文件不能为空");
+//        }
+//        String savePath = configConstant.serverBasePath+ "test/" + file.getOriginalFilename();
+//        FileUtil.writeFromStream(file.getInputStream(), savePath);
+//
+//        return Result.success(savePath);
+//    }
 
-    }
 
 }

+ 43 - 0
720yun_fd_manage/gis_web/src/main/java/com/gis/web/controller/WorkController.java

@@ -1,6 +1,7 @@
 package com.gis.web.controller;
 
 
+import com.gis.common.constant.MsgCode;
 import com.gis.common.util.Result;
 import com.gis.domain.dto.*;
 import com.gis.domain.entity.FodderEntity;
@@ -14,6 +15,7 @@ import io.swagger.annotations.ApiOperation;
 import lombok.extern.log4j.Log4j2;
 import org.springframework.beans.BeanUtils;
 import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.data.redis.core.RedisTemplate;
 import org.springframework.web.bind.annotation.*;
 import org.springframework.web.multipart.MultipartFile;
 
@@ -39,6 +41,9 @@ public class WorkController extends BaseController {
     @Autowired
     FodderService fodderService;
 
+    @Autowired
+    private RedisTemplate<String, String> redisTemplate;
+
 
 
     @ApiOperation(value = "列表", position = 1)
@@ -88,6 +93,44 @@ public class WorkController extends BaseController {
     }
 
 
+    @ApiOperation(value = "上传-图标", position = 2)
+    @PostMapping(value = "upload")
+    public Result upload(@RequestParam("file") MultipartFile file) {
+        log.info("上传图片");
+        return workService.editUpload(file);
+    }
+
+
+    @ApiOperation(value = "检查作品token是否有效" , position = 3)
+    @GetMapping("checkToken/{workId}")
+    public Result checkToken(@PathVariable Long workId) {
+        String token = getToken();
+        if (token == null) {
+            log.error("token is null");
+            return Result.failure(5001, "Token为空");
+        }
+
+        String redisKey = "token#"+ token;
+        String redisToken = redisTemplate.opsForValue().get(redisKey);
+        if (redisToken == null) {
+            log.error("redis token is null");
+            return Result.failure(5001, "Token无效");
+        }
+
+        String userId = getUserNameForToken();
+        WorkEntity entity = workService.findById(workId);
+        if (entity == null) {
+            return Result.failure("对象不存在: " + workId);
+        }
+
+        if (!userId.equals(entity.getUserId())) {
+            return Result.failure(MsgCode.e3005, "不是该用户作品");
+        }
+
+        return Result.success();
+
+    }
+