提交 eb5f8f89 authored 作者: 吴德鹏's avatar 吴德鹏

评论优化

上级 cee3cccb
...@@ -32,6 +32,12 @@ ...@@ -32,6 +32,12 @@
<dependencies> <dependencies>
<dependency>
<groupId>net.coobird</groupId>
<artifactId>thumbnailator</artifactId>
<version>0.4.8</version>
</dependency>
<dependency> <dependency>
<groupId>org.springframework.boot</groupId> <groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId> <artifactId>spring-boot-starter-data-redis</artifactId>
......
...@@ -10,6 +10,7 @@ import com.example.afrishop_v3.repository.*; ...@@ -10,6 +10,7 @@ import com.example.afrishop_v3.repository.*;
import com.example.afrishop_v3.security.services.AuthenticationUser; import com.example.afrishop_v3.security.services.AuthenticationUser;
import com.example.afrishop_v3.util.IdUtil; import com.example.afrishop_v3.util.IdUtil;
import com.example.afrishop_v3.util.OssUtil; import com.example.afrishop_v3.util.OssUtil;
import com.example.afrishop_v3.util.PicUtils;
import org.apache.commons.lang3.StringUtils; import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
...@@ -21,6 +22,7 @@ import org.springframework.transaction.annotation.Transactional; ...@@ -21,6 +22,7 @@ import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*; import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile; import org.springframework.web.multipart.MultipartFile;
import java.io.ByteArrayInputStream;
import java.io.IOException; import java.io.IOException;
import java.math.BigDecimal; import java.math.BigDecimal;
import java.math.RoundingMode; import java.math.RoundingMode;
...@@ -558,7 +560,7 @@ public class OrderController extends Controller { ...@@ -558,7 +560,7 @@ public class OrderController extends Controller {
@PostMapping("/add/comment") @PostMapping("/add/comment")
public Result addComment(@RequestBody TbCfItemComment comment, @RequestParam(value = "files", required = false) MultipartFile[] files) throws Exception { public Result addComment(TbCfItemComment comment, @RequestParam(value = "files", required = false) MultipartFile[] files) throws Exception {
TbCfUserInfo user = this.user.user(); TbCfUserInfo user = this.user.user();
...@@ -576,7 +578,10 @@ public class OrderController extends Controller { ...@@ -576,7 +578,10 @@ public class OrderController extends Controller {
List<String> urls = new ArrayList<>(); List<String> urls = new ArrayList<>();
if (files != null) { if (files != null) {
for (MultipartFile file : files) { for (MultipartFile file : files) {
String url = OssUtil.upload(file, "comment"); byte[] bytes = PicUtils.compressPicForScale(file.getBytes(), 120);
// ByteArrayInputStream inputStream = new ByteArrayInputStream(bytes);
String urlName = UUID.randomUUID() + ".jpg";
String url = OssUtil.upload(bytes, urlName, "comment");
urls.add(url); urls.add(url);
} }
String strUrl = urls.stream().collect(Collectors.joining(";")); String strUrl = urls.stream().collect(Collectors.joining(";"));
...@@ -590,7 +595,7 @@ public class OrderController extends Controller { ...@@ -590,7 +595,7 @@ public class OrderController extends Controller {
// comment.setLikeNum(0L); // comment.setLikeNum(0L);
comment.setCreateTime(new Date()); comment.setCreateTime(new Date());
comment.setUpdateTime(new Date()); comment.setUpdateTime(new Date());
comment.setReal(1); // comment.setReal(true);
// comment.setType(0); // comment.setType(0);
// if (!StringUtils.isBlank(comment.getUrls())) { // if (!StringUtils.isBlank(comment.getUrls())) {
// comment.setType(1); // comment.setType(1);
......
...@@ -89,8 +89,9 @@ public class TbCfItemComment { ...@@ -89,8 +89,9 @@ public class TbCfItemComment {
*/ */
private Date createTime; private Date createTime;
// @Transient @JsonIgnore
private Integer real; @Transient
private Boolean real;
/** /**
* 更新时间 * 更新时间
*/ */
...@@ -116,6 +117,16 @@ public class TbCfItemComment { ...@@ -116,6 +117,16 @@ public class TbCfItemComment {
@Transient @Transient
private double totalScore; private double totalScore;
private String attributs;
public String getAttributs() {
return attributs;
}
public void setAttributs(String attributs) {
this.attributs = attributs;
}
public double getTotalScore() { public double getTotalScore() {
return totalScore; return totalScore;
} }
...@@ -387,5 +398,31 @@ public class TbCfItemComment { ...@@ -387,5 +398,31 @@ public class TbCfItemComment {
return updateTime; return updateTime;
} }
public TbCfUserInfo getUser() {
return user;
}
public Boolean getReal() {
return real;
}
public void setReal(Boolean real) {
this.real = real;
}
public String getAvatar() {
return avatar;
}
public void setAvatar(String avatar) {
this.avatar = avatar;
}
public int getLikeCount() {
return likeCount;
}
public void setLikeCount(int likeCount) {
this.likeCount = likeCount;
}
} }
...@@ -71,7 +71,7 @@ public class WebSecurityConfig extends WebSecurityConfigurerAdapter { ...@@ -71,7 +71,7 @@ public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
"/goodsType/**", "/home/**", "/spider/**", "/store/**", "/shopify/**", "/community/**", "/version/**", "/goodsType/**", "/home/**", "/spider/**", "/store/**", "/shopify/**", "/community/**", "/version/**",
"/flutterwave/notify/**", "/dpo/notify/**", "/advertisement/**", "/website/**", "/paypal/**", "/discover/bonus/**", "/flutterwave/notify/**", "/dpo/notify/**", "/advertisement/**", "/website/**", "/paypal/**", "/discover/bonus/**",
"/problem/**", "/cube/**", "/activity/**", "/attributes/**", "/stripe/**", "/coupon/**", "/logistics/freeShippingThreshold", "/problem/**", "/cube/**", "/activity/**", "/attributes/**", "/stripe/**", "/coupon/**", "/logistics/freeShippingThreshold",
"/visit/**", "/search/**", "/country/**", "/term/**").permitAll() "/visit/**", "/search/**", "/country/**", "/term/**","/order/**").permitAll()
.antMatchers("/api/test/**").permitAll() .antMatchers("/api/test/**").permitAll()
.anyRequest().authenticated(); .anyRequest().authenticated();
......
package com.example.afrishop_v3.util;
import com.example.afrishop_v3.controllers.CartController;
import net.coobird.thumbnailator.Thumbnails;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
/**
* @Auther: wudepeng
* @Date: 2021/01/25
* @Description:
*/
public class PicUtils {
//以下是常量,按照阿里代码开发规范,不允许代码中出现魔法值
private static Logger logger = LoggerFactory.getLogger(PicUtils.class);
private static final Integer ZERO = 0;
private static final Integer ONE_ZERO_TWO_FOUR = 1024;
private static final Integer NINE_ZERO_ZERO = 900;
private static final Integer THREE_TWO_SEVEN_FIVE = 3275;
private static final Integer TWO_ZERO_FOUR_SEVEN = 2047;
private static final Double ZERO_EIGHT_FIVE = 0.85;
private static final Double ZERO_SIX = 0.6;
private static final Double ZERO_FOUR_FOUR = 0.44;
private static final Double ZERO_FOUR = 0.4;
/**
* 根据指定大小压缩图片
*
* @param imageBytes 源图片字节数组
* @param desFileSize 指定图片大小,单位kb
* @return 压缩质量后的图片字节数组
*/
public static byte[] compressPicForScale(byte[] imageBytes, long desFileSize) {
if (imageBytes == null || imageBytes.length <= ZERO || imageBytes.length < desFileSize * ONE_ZERO_TWO_FOUR) {
return imageBytes;
}
long srcSize = imageBytes.length;
double accuracy = getAccuracy(srcSize / ONE_ZERO_TWO_FOUR);
try {
while (imageBytes.length > desFileSize * ONE_ZERO_TWO_FOUR) {
ByteArrayInputStream inputStream = new ByteArrayInputStream(imageBytes);
ByteArrayOutputStream outputStream = new ByteArrayOutputStream(imageBytes.length);
Thumbnails.of(inputStream)
.scale(accuracy)
.outputQuality(accuracy)
.toOutputStream(outputStream);
imageBytes = outputStream.toByteArray();
}
logger.info("图片原大小={}kb | 压缩后大小={}kb",
srcSize / ONE_ZERO_TWO_FOUR, imageBytes.length / ONE_ZERO_TWO_FOUR);
} catch (Exception e) {
logger.error("【图片压缩】msg=图片压缩失败!", e);
}
return imageBytes;
}
/**
* 自动调节精度(经验数值)
*
* @param size 源图片大小
* @return 图片压缩质量比
*/
private static double getAccuracy(long size) {
double accuracy;
if (size < NINE_ZERO_ZERO) {
accuracy = ZERO_EIGHT_FIVE;
} else if (size < TWO_ZERO_FOUR_SEVEN) {
accuracy = ZERO_SIX;
} else if (size < THREE_TWO_SEVEN_FIVE) {
accuracy = ZERO_FOUR_FOUR;
} else {
accuracy = ZERO_FOUR;
}
return accuracy;
}
}
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论