Explorar o código

增加上传照片接口

houweiyu %!s(int64=4) %!d(string=hai) anos
pai
achega
282f7b6702

+ 2 - 4
dinner-application/src/main/java/com/fdage/DinnerReservationApplication.java

@@ -9,8 +9,6 @@ import org.springframework.boot.SpringApplication;
 import org.springframework.boot.autoconfigure.SpringBootApplication;
 import org.springframework.boot.builder.SpringApplicationBuilder;
 import org.springframework.boot.web.servlet.support.SpringBootServletInitializer;
-import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
-import org.springframework.cloud.openfeign.EnableFeignClients;
 import org.springframework.context.annotation.Bean;
 import org.springframework.http.converter.json.Jackson2ObjectMapperBuilder;
 
@@ -25,8 +23,8 @@ import java.time.format.DateTimeFormatter;
 @SpringBootApplication(scanBasePackages = {"fdage.back.sdk.core" ,
         "fdage.back.sdk.base.swagger" , "com.fdage"})
 @MapperScan(basePackages = {"com.fdage.base.dao"})
-@EnableFeignClients
-@EnableDiscoveryClient
+/*@EnableFeignClients
+@EnableDiscoveryClient*/
 public class DinnerReservationApplication  extends SpringBootServletInitializer {
 
 

+ 2 - 2
dinner-application/src/main/resources/application-dev.properties

@@ -28,9 +28,9 @@ oss.key=LTAIUrvuHqj8pvry
 oss.secrey=JLOVl0k8Ke0aaM8nLMMiUAZ3EiiqI4
 oss.bucket=4d-tjw
 
-share.logo.oss.path=domain/4dhouse/
+image.oss.path=domain/4dhouse/
 oss.query.url=https://houseoss.4dkankan.com/
-image.local.path=C:\\Users\\4dage\\Desktop\\logo-file\\
+image.local.path=C:\\Users\\4dage\\Desktop\\logo-file
 
 
 #小程序ID

+ 2 - 3
dinner-application/src/main/resources/application-prod.properties

@@ -24,10 +24,9 @@ oss.key=LTAIUrvuHqj8pvry
 oss.secrey=JLOVl0k8Ke0aaM8nLMMiUAZ3EiiqI4
 oss.bucket=4d-tjw
 
-share.logo.oss.path=domain/4dhouse/
+image.oss.path=domain/4dhouse/
 oss.query.url=https://houseoss.4dkankan.com/
-image.local.path=C:\\Users\\4dage\\Desktop\\logo-file\\
-
+image.local.path=/image
 
 #小程序ID
 wx.appId=wx0509bd21546d1597

+ 2 - 3
dinner-application/src/main/resources/application-test.properties

@@ -28,10 +28,9 @@ oss.key=LTAIUrvuHqj8pvry
 oss.secrey=JLOVl0k8Ke0aaM8nLMMiUAZ3EiiqI4
 oss.bucket=4d-tjw
 
-share.logo.oss.path=domain/4dhouse/
+image.oss.path=domain/4dhouse/
 oss.query.url=https://houseoss.4dkankan.com/
-image.local.path=C:\\Users\\4dage\\Desktop\\logo-file\\
-
+image.local.path=/image
 
 #小程序ID
 wx.appId=wx88d97f41fa6f8804

+ 2 - 3
dinner-application/src/main/resources/application-uat.properties

@@ -24,10 +24,9 @@ oss.key=LTAIUrvuHqj8pvry
 oss.secrey=JLOVl0k8Ke0aaM8nLMMiUAZ3EiiqI4
 oss.bucket=4d-tjw
 
-share.logo.oss.path=domain/4dhouse/
+image.oss.path=domain/4dhouse/
 oss.query.url=https://houseoss.4dkankan.com/
-image.local.path=C:\\Users\\4dage\\Desktop\\logo-file\\
-
+image.local.path=/image
 
 #小程序ID
 wx.appId=wx0509bd21546d1597

+ 6 - 6
dinner-core/pom.xml

@@ -42,13 +42,13 @@
             </dependency>
 
         <!-- Rpc feign依赖 -->
-        <dependency>
+       <!-- <dependency>
             <groupId>org.springframework.cloud</groupId>
             <artifactId>spring-cloud-starter-openfeign</artifactId>
-        </dependency>
+        </dependency>-->
 
         <!-- nacos 注册中心依赖 -->
-        <dependency>
+      <!--  <dependency>
             <groupId>org.springframework.cloud</groupId>
             <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
             <exclusions>
@@ -65,9 +65,9 @@
                     <artifactId>spring-boot-starter-tomcat</artifactId>
                 </exclusion>
             </exclusions>
-        </dependency>
+        </dependency>-->
         <!-- nacos 配置中心依赖 -->
-        <dependency>
+      <!--  <dependency>
             <groupId>com.alibaba.boot</groupId>
             <artifactId>nacos-config-spring-boot-starter</artifactId>
             <version>${nacos-config-spring-boot.version}</version>
@@ -77,7 +77,7 @@
                     <artifactId>guava</artifactId>
                 </exclusion>
             </exclusions>
-        </dependency>
+        </dependency>-->
     </dependencies>
 
 

+ 100 - 0
dinner-core/src/main/java/com/fdage/controller/app/AppCommonController.java

@@ -0,0 +1,100 @@
+package com.fdage.controller.app;
+
+import fdage.back.sdk.base.entity.Result;
+import fdage.back.sdk.core.alibabaUtils.AlibabaOssHelper;
+import io.swagger.annotations.Api;
+import io.swagger.annotations.ApiOperation;
+import lombok.extern.log4j.Log4j2;
+import org.apache.commons.lang3.StringUtils;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.beans.factory.annotation.Value;
+import org.springframework.web.bind.annotation.PostMapping;
+import org.springframework.web.bind.annotation.RequestMapping;
+import org.springframework.web.bind.annotation.RequestParam;
+import org.springframework.web.bind.annotation.RestController;
+import org.springframework.web.multipart.MultipartFile;
+
+import java.io.File;
+import java.io.IOException;
+import java.util.HashMap;
+import java.util.Map;
+import java.util.regex.Matcher;
+import java.util.regex.Pattern;
+
+/**
+ * 2 * @Author: Abner
+ * 3 * @Date: 2021/3/2 12:15
+ * 4
+ */
+@Api(tags = "小程序公共接口")
+@RestController
+@RequestMapping("app/common")
+@Log4j2
+public class AppCommonController {
+
+
+    @Value("${image.local.path}")
+    private String imageLocalPath;
+
+    @Value("${image.oss.path}")
+    private String ossPath;
+
+    @Value("${oss.query.url}")
+    private String ossQueryUrl;
+
+    @Autowired
+    private AlibabaOssHelper alibabaOssHelper;
+
+    public static String IMAGE_PATH = ".+(.JPEG|.jpeg|.JPG|.jpg|.GIF|.gif|.BMP|.bmp|.PNG|.png)";
+
+    @ApiOperation(value = "上传照片")
+    @PostMapping("/uploadImage")
+    public Result uploadImage(@RequestParam("file") MultipartFile file) {
+        if (null == file) {
+            return Result.failure("文件缺失");
+        }
+        Pattern pattern = Pattern.compile(IMAGE_PATH);
+        Matcher matcher = pattern.matcher(file.getOriginalFilename());
+        //TODO:这里限制照片大小,如果很大,则缩略
+        if(!matcher.find()){
+            return Result.failure("只能上传照片格式的文件");
+        }
+        int index = file.getOriginalFilename().indexOf(".");
+        String newFileName = System.currentTimeMillis() + "";
+        if(index != -1){
+            newFileName = newFileName + file.getOriginalFilename().substring(index);
+        }
+        if(StringUtils.isBlank(newFileName)){
+            return Result.failure("获取文件后缀名失败");
+        }
+        String fullLocalDirect = imageLocalPath;
+        File direct = new File(fullLocalDirect);
+        if(!direct.exists()){
+            direct.mkdir();
+        }
+        String fullLocalImagePath = imageLocalPath.concat(File.separator).concat(newFileName);
+        log.info("照片文件下载到本地路径为:{}", fullLocalImagePath);
+        File totalFile = new File(fullLocalImagePath);
+        if(totalFile.exists()){
+            totalFile.delete();
+        }
+        try {
+            file.transferTo(totalFile);
+        } catch (IOException e) {
+            e.printStackTrace();
+            log.info("照片文件已经下载到本地出现异常:{}", e);
+            return Result.failure("下载文件出现异常");
+        }
+        if(!totalFile.exists()){
+            log.info("缓存照片到本地失败:{}" , fullLocalImagePath);
+            return Result.failure("上传照片失败");
+        }
+        String totalOssQueryPath = "";
+        String resultOssPath = ossPath + newFileName;
+        alibabaOssHelper.doUploadThenDelete(fullLocalImagePath , resultOssPath);
+        totalOssQueryPath = ossQueryUrl + resultOssPath;
+        Map<String , Object> result = new HashMap<>();
+        result.put("link" , totalOssQueryPath);
+        return Result.success(result);
+    }
+}