|
|
@@ -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());
|
|
|
}
|
|
|
}
|