licailing 4 år sedan
förälder
incheckning
3417e64a28

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

@@ -1,8 +1,11 @@
 package com.izouma.jiashanxia.service;
 
+import cn.binarywang.wx.miniapp.api.WxMaService;
+import cn.binarywang.wx.miniapp.bean.WxMaCodeLineColor;
 import cn.hutool.core.bean.BeanUtil;
 import cn.hutool.core.collection.CollUtil;
 import cn.hutool.core.util.ObjectUtil;
+import com.izouma.jiashanxia.config.Constants;
 import com.izouma.jiashanxia.domain.*;
 import com.izouma.jiashanxia.domain.Package;
 import com.izouma.jiashanxia.dto.PackageVO;
@@ -17,16 +20,23 @@ import com.izouma.jiashanxia.security.Authority;
 import com.izouma.jiashanxia.utils.JpaUtils;
 import lombok.AllArgsConstructor;
 import me.chanjar.weixin.common.error.WxErrorException;
+import net.coobird.thumbnailator.Thumbnails;
 import org.springframework.data.domain.Page;
 import org.springframework.stereotype.Service;
 
 import javax.imageio.ImageIO;
+import java.awt.*;
+import java.awt.geom.Ellipse2D;
 import java.awt.image.BufferedImage;
+import java.io.ByteArrayInputStream;
+import java.io.ByteArrayOutputStream;
 import java.io.IOException;
 import java.math.BigDecimal;
+import java.net.URL;
 import java.time.LocalDateTime;
 import java.time.format.DateTimeFormatter;
 import java.util.*;
+import java.util.List;
 
 
 @Service
@@ -42,6 +52,7 @@ public class PackageService {
     private final StockRepo          stockRepo;
     private final SysConfigService   sysConfigService;
     private final UserService        userService;
+    private final WxMaService        wxMaService;
 
     public Page<Package> all(PageQuery pageQuery) {
         return packageRepo.findAll(JpaUtils.toSpecification(pageQuery, Package.class), JpaUtils.toPageRequest(pageQuery));
@@ -156,10 +167,39 @@ public class PackageService {
     }
 
     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 Base64.getEncoder().encodeToString(userService.createShareImg(userId, false, map));
+        User user = userRepo.findById(userId).orElseThrow(new BusinessException("无用户"));
+        byte[] bytes = wxMaService.getQrcodeService()
+                .createWxaCodeUnlimitBytes("expert=true&invitor=" + userId + "&id=" + id, "pages/detail", 94 * 2, false, new WxMaCodeLineColor("0", "0", "0"), true);
+        ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(bytes);
+        BufferedImage codeImg = ImageIO.read(byteArrayInputStream);
+        BufferedImage result = new BufferedImage(100, 100, BufferedImage.TYPE_INT_RGB);
+        Graphics2D g = result.createGraphics();
+        g.drawImage(codeImg, 251 * 2, 543 * 2, 94 * 2, 94 * 2, null);
+
+        BufferedImage avatar = ImageIO
+                .read(new URL(Optional.ofNullable(user.getAvatar()).orElse(Constants.DEFAULT_AVATAR)));
+        int size = Math.min(avatar.getWidth(), avatar.getHeight());
+        BufferedImage subImg = avatar
+                .getSubimage((avatar.getWidth() - size) / 2, (avatar.getHeight() - size) / 2, size, size);
+        BufferedImage avatarImg = new BufferedImage(44 * 2, 44 * 2, BufferedImage.TYPE_INT_ARGB);
+        Graphics2D g2 = avatarImg.createGraphics();
+        g2.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
+        g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
+        g2.setRenderingHint(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY);
+        g2.setComposite(AlphaComposite.SrcOver);
+        g2.setBackground(Color.GREEN);
+        g2.setClip(new Ellipse2D.Float(0, 0, 44 * 2, 44 * 2));
+        g2.drawImage(subImg, 0, 0, 44 * 2, 44 * 2, null);
+
+        g.drawImage(avatarImg, 276 * 2, 568 * 2, 44 * 2, 44 * 2, null);
+
+        ByteArrayOutputStream out = new ByteArrayOutputStream(1024);
+        Thumbnails.of(result)
+                .scale(1)
+                .outputQuality(.9f)
+                .outputFormat("jpg")
+                .toOutputStream(out);
+//        return out.toByteArray();
+        return Base64.getEncoder().encodeToString(out.toByteArray());
     }
 }

+ 13 - 14
src/main/java/com/izouma/jiashanxia/service/UserService.java

@@ -602,20 +602,19 @@ 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/"))) {
-            ByteArrayInputStream in = new ByteArrayInputStream(this.createShareImg(userId, true, null));
-            String url = storageService.uploadFromInputStream(in, "share_img/v2/" + userId + ".jpg");
+            String url = this.createShareImg(userId);
             user.setShareImg(url);
             userRepo.save(user);
         }
         return user.getShareImg();
     }
 
-    public byte[] createShareImg(Long userId, boolean home, Map<String, Object> map) throws IOException, WxErrorException {
+    public String createShareImg(Long userId) throws IOException, WxErrorException {
         User user = userRepo.findById(userId).orElseThrow(new BusinessException("用户不存在"));
         BufferedImage shareImg = ImageIO.read(this.getClass().getResourceAsStream("/static/shareImg.png"));
-        if (!home) {
-            shareImg = ImageIO.read(new URL(String.valueOf(map.get("poster"))));
-        }
+//        if (!home) {
+//            shareImg = ImageIO.read(new URL(String.valueOf(map.get("poster"))));
+//        }
 
         BufferedImage result = new BufferedImage(shareImg.getWidth(), shareImg.getHeight(), BufferedImage.TYPE_INT_RGB);
         Graphics2D g = result.createGraphics();
@@ -627,11 +626,11 @@ public class UserService {
 
         byte[] bytes = wxMaService.getQrcodeService()
                 .createWxaCodeUnlimitBytes("expert=true&invitor=" + userId, "pages/home", 94 * 2, false, new WxMaCodeLineColor("0", "0", "0"), true);
-        if (!home) {
-            bytes = wxMaService.getQrcodeService()
-                    .createWxaCodeUnlimitBytes("expert=true&invitor=" + userId + "&id=" + map
-                            .get("id"), "pages/detail", 94 * 2, false, new WxMaCodeLineColor("0", "0", "0"), true);
-        }
+//        if (!home) {
+//            bytes = wxMaService.getQrcodeService()
+//                    .createWxaCodeUnlimitBytes("expert=true&invitor=" + userId + "&id=" + map
+//                            .get("id"), "pages/detail", 94 * 2, false, new WxMaCodeLineColor("0", "0", "0"), true);
+//        }
         ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(bytes);
         BufferedImage codeImg = ImageIO.read(byteArrayInputStream);
 
@@ -660,8 +659,8 @@ public class UserService {
                 .outputQuality(.9f)
                 .outputFormat("jpg")
                 .toOutputStream(out);
-        return out.toByteArray();
-//        ByteArrayInputStream in = new ByteArrayInputStream(out.toByteArray());
-//        return storageService.uploadFromInputStream(in, "share_img/v2/" + userId + ".jpg");
+//        return out.toByteArray();
+        ByteArrayInputStream in = new ByteArrayInputStream(out.toByteArray());
+        return storageService.uploadFromInputStream(in, "share_img/v2/" + userId + ".jpg");
     }
 }