给商品相关接口增加图片缩略图字段的返回

This commit is contained in:
季圣华
2023-07-05 23:59:55 +08:00
parent 62c011e954
commit d67fa0ccf6
5 changed files with 85 additions and 4 deletions

View File

@@ -254,6 +254,7 @@ public class DepotItemController {
BigDecimal allWeight = diEx.getBasicNumber()==null||diEx.getWeight()==null?BigDecimal.ZERO:diEx.getBasicNumber().multiply(diEx.getWeight());
item.put("weight", allWeight);
item.put("remark", diEx.getRemark());
item.put("imgName", diEx.getImgName());
item.put("linkId", diEx.getLinkId());
item.put("depotId", diEx.getDepotId() == null ? "" : diEx.getDepotId());
item.put("depotName", diEx.getDepotId() == null ? "" : diEx.getDepotName());

View File

@@ -6,16 +6,13 @@ import com.jsh.erp.service.systemConfig.SystemConfigService;
import com.jsh.erp.service.user.UserService;
import com.jsh.erp.service.userBusiness.UserBusinessService;
import com.jsh.erp.utils.BaseResponseInfo;
import com.jsh.erp.utils.FileUtils;
import com.jsh.erp.utils.StringUtil;
import com.jsh.erp.utils.Tools;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.util.AntPathMatcher;
import org.springframework.util.FileCopyUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
@@ -25,8 +22,10 @@ import org.springframework.web.multipart.MultipartHttpServletRequest;
import org.springframework.web.servlet.HandlerMapping;
import javax.annotation.Resource;
import javax.imageio.ImageIO;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.awt.image.BufferedImage;
import java.io.*;
import java.net.HttpURLConnection;
import java.net.URL;
@@ -227,6 +226,51 @@ public class SystemConfigController {
}
}
/**
* 预览缩略图&下载文件
* @param request
* @param response
*/
@GetMapping(value = "/static/mini/**")
@ApiOperation(value = "预览缩略图&下载文件")
public void viewMini(HttpServletRequest request, HttpServletResponse response) {
// ISO-8859-1 ==> UTF-8 进行编码转换
String imgPath = extractPathFromPattern(request);
if(StringUtil.isEmpty(imgPath) || imgPath=="null"){
return;
}
OutputStream outputStream = null;
try {
imgPath = imgPath.replace("..", "");
if (imgPath.endsWith(",")) {
imgPath = imgPath.substring(0, imgPath.length() - 1);
}
String fileUrl = "";
if(fileUploadType == 1) {
fileUrl = systemConfigService.getFileUrlLocal(imgPath);
} else if(fileUploadType == 2) {
fileUrl = systemConfigService.getFileUrlAliOss(imgPath);
}
int index = fileUrl.lastIndexOf(".");
String ext = fileUrl.substring(index + 1);
BufferedImage image = systemConfigService.getImageMini(fileUrl, 40);
outputStream = response.getOutputStream();
ImageIO.write(image, ext, outputStream);
response.flushBuffer();
} catch (Exception e) {
response.setStatus(404);
e.printStackTrace();
} finally {
if (outputStream != null) {
try {
outputStream.close();
} catch (IOException e) {
logger.error(e.getMessage(), e);
}
}
}
}
/**
* 把指定URL后的字符串全部截断当成参数
* 这么做是为了防止URL中包含中文或者特殊字符/等)时,匹配不了的问题

View File

@@ -50,6 +50,8 @@ public class DepotItemVo4WithInfoEx extends DepotItem{
private BigDecimal weight;
private String imgName;
public Long getMId() {
return MId;
}
@@ -233,4 +235,12 @@ public class DepotItemVo4WithInfoEx extends DepotItem{
public void setWeight(BigDecimal weight) {
this.weight = weight;
}
public String getImgName() {
return imgName;
}
public void setImgName(String imgName) {
this.imgName = imgName;
}
}

View File

@@ -31,7 +31,10 @@ import org.springframework.web.context.request.ServletRequestAttributes;
import org.springframework.web.multipart.MultipartFile;
import javax.annotation.Resource;
import javax.imageio.ImageIO;
import javax.servlet.http.HttpServletRequest;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.*;
import java.util.Date;
import java.util.List;
@@ -287,6 +290,28 @@ public class SystemConfigService {
return linkUrl + filePath + "/" + imgPath;
}
public BufferedImage getImageMini(String fileUrl, int w) throws Exception {
BufferedImage img = ImageIO.read(new File(fileUrl));
//获取图片的长和宽
int width = img.getWidth();
int height = img.getHeight();
int tempw = 0;
int temph = 0;
if(width>height){
tempw = w;
temph = height* w/width;
}else{
tempw = w*width/height;
temph = w;
}
Image _img = img.getScaledInstance(tempw, temph, Image.SCALE_DEFAULT);
BufferedImage image = new BufferedImage(tempw, temph, BufferedImage.TYPE_INT_RGB);
Graphics2D graphics = image.createGraphics();
graphics.drawImage(_img, 0, 0, null);
graphics.dispose();
return image;
}
/**
* 获取仓库开关
* @return