licailing 4 лет назад
Родитель
Сommit
a486e20a69

+ 2 - 2
src/main/java/com/izouma/jiashanxia/service/PackageService.java

@@ -155,11 +155,11 @@ public class PackageService {
         return vo;
     }
 
-    public BufferedImage shareImg(Long userId, Long id) throws IOException, WxErrorException {
+    public String shareImg(Long userId, Long id) throws IOException, WxErrorException {
         Package aPackage = packageRepo.findById(id).orElseThrow(new BusinessException("无产品"));
         Map<String, Object> map = new HashMap<>();
         map.put("poster", aPackage.getPoster());
         map.put("id", aPackage.getId());
-        return ImageIO.read(userService.createShareImg(userId, false, map));
+        return Base64.getEncoder().encodeToString(userService.createShareImg(userId, false, map));
     }
 }

+ 4 - 3
src/main/java/com/izouma/jiashanxia/service/UserService.java

@@ -602,14 +602,15 @@ public class UserService {
     public String shareImg(Long userId) throws IOException, WxErrorException {
         User user = userRepo.findById(userId).orElseThrow(new BusinessException("用户不存在"));
         if (!(StringUtils.isNotEmpty(user.getShareImg()) && user.getShareImg().contains("/v2/"))) {
-            String url =storageService.uploadFromInputStream(createShareImg(userId, true, null), "share_img/v2/" + userId + ".jpg");
+            ByteArrayInputStream in = new ByteArrayInputStream(this.createShareImg(userId, true, null));
+            String url = storageService.uploadFromInputStream(in, "share_img/v2/" + userId + ".jpg");
             user.setShareImg(url);
             userRepo.save(user);
         }
         return user.getShareImg();
     }
 
-    public ByteArrayInputStream createShareImg(Long userId, boolean home, Map<String, Object> map) throws IOException, WxErrorException {
+    public byte[] createShareImg(Long userId, boolean home, Map<String, Object> map) throws IOException, WxErrorException {
         User user = userRepo.findById(userId).orElseThrow(new BusinessException("用户不存在"));
         BufferedImage shareImg = ImageIO.read(this.getClass().getResourceAsStream("/static/shareImg.png"));
         if (!home) {
@@ -659,8 +660,8 @@ public class UserService {
                 .outputQuality(.9f)
                 .outputFormat("jpg")
                 .toOutputStream(out);
+        return out.toByteArray();
 //        ByteArrayInputStream in = new ByteArrayInputStream(out.toByteArray());
-        return new ByteArrayInputStream(out.toByteArray());
 //        return storageService.uploadFromInputStream(in, "share_img/v2/" + userId + ".jpg");
     }
 }

+ 1 - 3
src/main/java/com/izouma/jiashanxia/web/PackageController.java

@@ -6,7 +6,6 @@ import com.izouma.jiashanxia.domain.Category;
 import com.izouma.jiashanxia.domain.Package;
 import com.izouma.jiashanxia.dto.PackageDTO;
 import com.izouma.jiashanxia.dto.PackageVO;
-import com.izouma.jiashanxia.enums.PackageType;
 import com.izouma.jiashanxia.repo.AttractionsRepo;
 import com.izouma.jiashanxia.repo.CategoryRepo;
 import com.izouma.jiashanxia.service.PackageService;
@@ -24,7 +23,6 @@ import org.springframework.data.domain.Page;
 import org.springframework.web.bind.annotation.*;
 
 import javax.servlet.http.HttpServletResponse;
-import java.awt.image.BufferedImage;
 import java.io.IOException;
 import java.math.BigDecimal;
 import java.util.List;
@@ -140,7 +138,7 @@ public class PackageController extends BaseController {
     }
 
     @GetMapping("/shareImg")
-    public BufferedImage shareImg(@RequestParam Long id) throws IOException, WxErrorException {
+    public String shareImg(@RequestParam Long id) throws IOException, WxErrorException {
         return packageService.shareImg(SecurityUtils.getAuthenticatedUser().getId(), id);
     }
 }