lyhzzz 3 лет назад
Родитель
Сommit
ea782a2faf

+ 37 - 0
src/main/java/com/fdkankan/fusion/common/ResultCode.java

@@ -0,0 +1,37 @@
+package com.fdkankan.fusion.common;
+
+public enum ResultCode {
+    SUCCESS(0,"操作成功"),
+    ERROR(-1,"操作失败"),
+
+    NOT_LOGIN(2001,"请重新登录!"),
+    USER_NOT_EXIST(2002,"用户不存在!"),
+    FD_USER_NOT_EXIST(2003,"四维用户不存在!"),
+    PHONE_EXIST(2004,"手机号码已注册!"),
+    OTHER_LOGIN(2005,"登录信息失效请重新登录!"),
+    PASSWORD_ERROR(2006,"账号或密码错误!"),
+
+    NOT_RECORD(4001,"没有记录"),
+    PARAM_MISS(40002,"参数缺失!"),
+    NOT_PERM(5014,"您没有该场景的编辑权限!"),
+
+    OLD_PASSWORD_ERROR(50001,"原密码错误!"),
+    OLD_NEW_PASSWORD_EQ(50002,"原密码与新密码一致无需更改!"),
+
+    NOT_DELETE_PARENT_DEPT(6001,"集团总部不可删除"),
+    NOT_DELETE_DEPT_USER(6002,"请先删除专柜下用户"),
+    NOT_DELETE_DEPT_SHOP(6003,"请先删除专柜下店铺"),
+
+    UPLOAD_ERROR(7001,"文件上传失败"),
+    UPLOAD_FILE_NO_EXIST(7002,"上传文件不存在"),
+    UPLOAD_FILE_TO_LONG(7003,"文件上传过大");
+
+    public int code;
+    public String msg;
+
+    ResultCode(int code , String msg){
+        this.code = code;
+        this.msg = msg;
+    }
+
+}

+ 53 - 0
src/main/java/com/fdkankan/fusion/config/OriginFilter.java

@@ -0,0 +1,53 @@
+package com.fdkankan.fusion.config;
+
+import org.springframework.boot.web.servlet.ServletComponentScan;
+import org.springframework.stereotype.Component;
+
+import javax.servlet.*;
+import javax.servlet.annotation.WebFilter;
+import javax.servlet.http.HttpServletRequest;
+import javax.servlet.http.HttpServletResponse;
+import java.io.IOException;
+
+
+@Component("originFilter")
+@ServletComponentScan
+@WebFilter(urlPatterns = "/*", filterName = "shiroLoginFilter")
+public class OriginFilter implements Filter {
+
+	private FilterConfig config = null;
+
+	@Override
+	public void init(FilterConfig config) throws ServletException {
+		this.config = config;
+	}
+
+	@Override
+	public void destroy() {
+		this.config = null;
+	}
+
+	@Override
+	public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
+		HttpServletResponse response = (HttpServletResponse) servletResponse;
+		HttpServletRequest request = (HttpServletRequest) servletRequest;
+		//指定允许其他域名访问
+		response.setHeader("Access-Control-Allow-Origin", "*");
+		// 允许请求的方法
+		response.setHeader("Access-Control-Allow-Methods", "POST,GET,OPTIONS,DELETE,PUT");
+		// 多少秒内,不需要再发送预检验请求,可以缓存该结果
+		response.setHeader("Access-Control-Max-Age", "3600");
+		// 表明它允许跨域请求包含xxx头
+		response.setHeader("Access-Control-Allow-Headers",
+				"Origin, No-Cache, X-Requested-With, If-Modified-Since, Pragma, Last-Modified, Cache-Control, Expires,userAgent,User-Agent,version, " +
+				" Content-Type, content-type,X-E4M-With,Authorization,token,authorization");
+		response.setHeader("Access-Control-Allow-Credentials", "true");
+		if (request.getMethod().equals("OPTIONS")) {
+			response.setStatus(200);
+			response.getWriter().write("OPTIONS returns OK");
+			return;
+		}
+		filterChain.doFilter(servletRequest, response);
+	}
+}
+

+ 37 - 0
src/main/java/com/fdkankan/fusion/config/WebAppConfig.java

@@ -0,0 +1,37 @@
+package com.fdkankan.fusion.config;
+
+import com.fdkankan.fusion.interceptor.TokenInterceptor;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.context.annotation.Configuration;
+import org.springframework.web.servlet.config.annotation.CorsRegistry;
+import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
+import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
+import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
+
+
+@Configuration
+public class WebAppConfig implements WebMvcConfigurer {
+
+	@Autowired
+	TokenInterceptor tokenInterceptor;
+
+	@Override
+	public void addCorsMappings(CorsRegistry registry) {
+		registry.addMapping("/**").allowCredentials(true).allowedHeaders("*").allowedOrigins("*").allowedMethods("*");
+
+	}
+
+	@Override
+	public void addInterceptors(InterceptorRegistry registry) {
+		registry.addInterceptor(tokenInterceptor).addPathPatterns("/**")
+				.excludePathPatterns("/**/login/**","/test/**");
+		WebMvcConfigurer.super.addInterceptors(registry);
+	}
+
+	@Override
+	public void addResourceHandlers(ResourceHandlerRegistry registry) {
+		WebMvcConfigurer.super.addResourceHandlers(registry);
+	}
+
+}
+

+ 4 - 3
src/main/java/com/fdkankan/fusion/controller/CaseVideoController.java

@@ -32,8 +32,9 @@ public class CaseVideoController {
     ICaseVideoService caseVideoService;
     @Autowired
     UploadToOssUtil uploadToOssUtil;
-    @Value("${oss.prefix.ali}")
-    private String ossVideoPath;
+
+    @Value("${upload.query-path}")
+    private String queryPath;
 
     @PostMapping("/list")
     public ResultData list(@RequestBody CaseVideoParam param){
@@ -50,7 +51,7 @@ public class CaseVideoController {
         CaseVideo caseVideo = caseVideoService.getById(param.getVideoId());
         caseVideoService.removeById(param.getVideoId());
         try {
-            String replace = caseVideo.getVideoPath().replace(ossVideoPath, "");
+            String replace = caseVideo.getVideoPath().replace(queryPath, "");
             uploadToOssUtil.delete(replace);
         }catch (Exception e){
             e.printStackTrace();

+ 60 - 0
src/main/java/com/fdkankan/fusion/controller/UploadController.java

@@ -0,0 +1,60 @@
+package com.fdkankan.fusion.controller;
+
+import com.fdkankan.common.constant.ErrorCode;
+import com.fdkankan.common.exception.BusinessException;
+import com.fdkankan.common.response.ResultData;
+import com.fdkankan.fusion.common.ResultCode;
+import com.fdkankan.fyun.oss.UploadToOssUtil;
+import org.apache.commons.lang3.StringUtils;
+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 javax.annotation.Resource;
+import java.io.File;
+import java.util.UUID;
+
+@RestController
+@RequestMapping("/upload")
+public class UploadController {
+
+    @Resource
+    private UploadToOssUtil uploadToOssUtil;
+
+    @Value("${upload.file-path}")
+    private String filePath;
+    @Value("${upload.query-path}")
+    private String queryPath;
+
+
+    @PostMapping("/file")
+    public ResultData file(@RequestParam(required = false) MultipartFile file) throws Exception {
+        if(file.isEmpty()){
+            throw new BusinessException(ResultCode.UPLOAD_FILE_NO_EXIST.code,ResultCode.UPLOAD_FILE_NO_EXIST.msg);
+        }
+        if(file.getSize()>10 * 1024 * 1024 * 100){
+            System.out.println(file.getSize());
+            throw new BusinessException(ResultCode.UPLOAD_FILE_TO_LONG.code,ResultCode.UPLOAD_FILE_TO_LONG.msg);
+        }
+        //获取文件名
+        String fileName = file.getOriginalFilename();
+        if(StringUtils.isEmpty(fileName)){
+            throw new BusinessException(ResultCode.UPLOAD_FILE_NO_EXIST.code,ResultCode.UPLOAD_FILE_NO_EXIST.msg);
+        }
+        //获取文件后缀名
+        String suffixName = fileName.substring(fileName.lastIndexOf("."));
+        //重新生成文件名
+        fileName = UUID.randomUUID().toString().replace("-","");
+        File localFile = File.createTempFile(fileName,suffixName);
+        file.transferTo(localFile);
+        String path = localFile.getPath();
+        uploadToOssUtil.upload(path,filePath + fileName + suffixName);
+        if(!uploadToOssUtil.existKey(filePath + fileName + suffixName)){
+            throw new BusinessException(ResultCode.UPLOAD_ERROR.code,ResultCode.UPLOAD_ERROR.msg);
+        }
+        return ResultData.ok( queryPath + filePath + fileName + suffixName);
+    }
+}

+ 58 - 0
src/main/java/com/fdkankan/fusion/interceptor/TokenInterceptor.java

@@ -0,0 +1,58 @@
+package com.fdkankan.fusion.interceptor;
+
+
+import com.alibaba.fastjson.JSONObject;
+import com.fdkankan.common.constant.ErrorCode;
+import com.fdkankan.common.response.ResultData;
+import com.fdkankan.redis.constant.RedisKey;
+import com.fdkankan.redis.util.RedisUtil;
+import lombok.extern.slf4j.Slf4j;
+import org.apache.commons.lang3.StringUtils;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.stereotype.Component;
+import org.springframework.web.servlet.HandlerInterceptor;
+
+import javax.servlet.http.HttpServletRequest;
+import javax.servlet.http.HttpServletResponse;
+import java.io.IOException;
+
+@Component
+@Slf4j
+public class TokenInterceptor implements HandlerInterceptor {
+
+	@Autowired
+	private RedisUtil redisUtil;
+
+	@Override
+	public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
+		response.setContentType("text/html;charset=UTF-8");
+		String token = request.getHeader("token");
+		if(StringUtils.isEmpty(token)){
+			this.needLogin(request,response);
+			return false;
+		}
+		try {
+			String redisKey = String.format(RedisKey.TOKEN_V3,token);
+			if(redisUtil.hasKey(redisKey)){
+				redisUtil.expire(redisKey,2 * 60 * 60);
+				return true;
+			}
+		}catch (Exception e){
+			e.printStackTrace();
+		}
+		this.needLogin(request,response);
+		return false;
+	}
+
+	private void needLogin(HttpServletRequest request, HttpServletResponse response) {
+		try {
+			String result = JSONObject.toJSONString(ResultData.error(ErrorCode.USER_NOT_LOGIN));
+			response.getWriter().append(result);
+		} catch (IOException e) {
+			log.info("LoginInterceptor|needLogin|IOException|" + e);
+			e.printStackTrace();
+		}
+	}
+
+}
+

+ 2 - 1
src/main/resources/application.yaml

@@ -74,7 +74,8 @@ forest:
 
 upload:
   type: oss
-
+  file-path : fusion/file/
+  query-path: https://4dkk.4dage.com/
 oss:
   #point: http://oss-cn-shenzhen-internal.aliyuncs.com
   point: http://oss-cn-shenzhen.aliyuncs.com