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

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

@@ -20,6 +20,8 @@ import me.chanjar.weixin.common.error.WxErrorException;
 import org.springframework.data.domain.Page;
 import org.springframework.stereotype.Service;
 
+import javax.imageio.ImageIO;
+import java.awt.image.BufferedImage;
 import java.io.IOException;
 import java.math.BigDecimal;
 import java.time.LocalDateTime;
@@ -153,11 +155,11 @@ public class PackageService {
         return vo;
     }
 
-    public String shareImg(Long userId, Long id) throws IOException, WxErrorException {
+    public BufferedImage 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 userService.createShareImg(userId, false, map);
+        return ImageIO.read(userService.createShareImg(userId, false, map));
     }
 }

+ 5 - 8
src/main/java/com/izouma/jiashanxia/service/UserService.java

@@ -602,14 +602,14 @@ 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 = createShareImg(userId, true, null);
+            String url =storageService.uploadFromInputStream(createShareImg(userId, true, null), "share_img/v2/" + userId + ".jpg");
             user.setShareImg(url);
             userRepo.save(user);
         }
         return user.getShareImg();
     }
 
-    public String createShareImg(Long userId, boolean home, Map<String, Object> map) throws IOException, WxErrorException {
+    public ByteArrayInputStream 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,11 +659,8 @@ public class UserService {
                 .outputQuality(.9f)
                 .outputFormat("jpg")
                 .toOutputStream(out);
-        ByteArrayInputStream in = new ByteArrayInputStream(out.toByteArray());
-        if (!home) {
-            return new BufferedReader(new InputStreamReader(in))
-                    .lines().parallel().collect(Collectors.joining(System.lineSeparator()));
-        }
-        return storageService.uploadFromInputStream(in, "share_img/v2/" + userId + ".jpg");
+//        ByteArrayInputStream in = new ByteArrayInputStream(out.toByteArray());
+        return new ByteArrayInputStream(out.toByteArray());
+//        return storageService.uploadFromInputStream(in, "share_img/v2/" + userId + ".jpg");
     }
 }

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

@@ -24,6 +24,7 @@ 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;
@@ -139,7 +140,7 @@ public class PackageController extends BaseController {
     }
 
     @GetMapping("/shareImg")
-    public String shareImg(@RequestParam Long id) throws IOException, WxErrorException {
+    public BufferedImage shareImg(@RequestParam Long id) throws IOException, WxErrorException {
         return packageService.shareImg(SecurityUtils.getAuthenticatedUser().getId(), id);
     }
 }

+ 1 - 1
src/test/java/com/izouma/jiashanxia/service/PackageServiceTest.java

@@ -28,6 +28,6 @@ public class PackageServiceTest extends ApplicationTests {
 
     @Test
     public void shareImg()throws IOException, WxErrorException {
-        packageService.shareImg(908L, 1594L);
+        System.out.println(packageService.shareImg(908L, 1594L));
     }
 }

+ 1 - 1
src/test/java/com/izouma/jiashanxia/service/UserServiceTest.java

@@ -76,7 +76,7 @@ public class UserServiceTest {
 
     @Test
     public void shareImg() throws IOException, WxErrorException {
-        System.out.println(userService.createShareImg(908L, false, null));
+        System.out.println(userService.shareImg(908L));
 
     }