diff --git a/jeecg-boot-module-system/src/main/java/org/jeecg/modules/api/service/impl/YaoDuShopServiceImpl.java b/jeecg-boot-module-system/src/main/java/org/jeecg/modules/api/service/impl/YaoDuShopServiceImpl.java index 2848017..1ef779e 100644 --- a/jeecg-boot-module-system/src/main/java/org/jeecg/modules/api/service/impl/YaoDuShopServiceImpl.java +++ b/jeecg-boot-module-system/src/main/java/org/jeecg/modules/api/service/impl/YaoDuShopServiceImpl.java @@ -1,11 +1,15 @@ package org.jeecg.modules.api.service.impl; import com.alibaba.fastjson.JSON; +import com.xkcoding.http.util.StringUtil; import lombok.extern.log4j.Log4j2; +import org.jeecg.common.util.RedisUtil; import org.jeecg.common.util.oss.OssBootUtil; import org.jeecg.config.shiro.ShiroRealm; import org.jeecg.modules.api.service.YaoDuShopService; import org.jeecg.modules.api.utils.WxHttpUtils; +import org.jeecg.modules.cityShop.entity.CityShop; +import org.jeecg.modules.cityShop.service.ICityShopService; import org.jeecg.modules.hanHaiMember.entity.HanHaiMember; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.http.HttpEntity; @@ -14,6 +18,7 @@ import org.springframework.http.ResponseEntity; import org.springframework.stereotype.Service; import org.springframework.util.LinkedMultiValueMap; import org.springframework.util.MultiValueMap; +import org.springframework.util.StringUtils; import org.springframework.web.client.RestTemplate; import javax.annotation.Resource; @@ -21,11 +26,16 @@ import javax.imageio.ImageIO; import java.awt.*; import java.awt.image.BufferedImage; import java.io.ByteArrayInputStream; +import java.io.ByteArrayOutputStream; import java.io.File; +import java.io.InputStream; import java.net.URL; import java.nio.file.Files; import java.util.HashMap; import java.util.Map; +import java.util.concurrent.CompletableFuture; +import java.util.concurrent.ExecutorService; +import java.util.concurrent.Executors; @Log4j2 @Service @@ -38,13 +48,72 @@ public class YaoDuShopServiceImpl implements YaoDuShopService { @Autowired private WxHttpUtils wxHttpUtils; + @Autowired + private ICityShopService cityShopService; + + @Autowired + private RedisUtil redisUtil; + + private final ExecutorService asyncExecutor = Executors.newFixedThreadPool(5); @Override public byte[] shopQrCode(String token, String id) throws Exception { -// HanHaiMember member = shiroRealm.checkUserTokenIsEffectHanHaiOpenId(token); - return generateWxQrCode("1", "pages_order/gourmet/gourmetDetail?id=" + id, "release"); - } + CityShop byId = cityShopService.getById(id); + + if (byId == null){ + return null; + } + + if (StringUtil.isNotEmpty(byId.getQrCodeBg())){ + String finalPath = String.format("gourmet/gourmetDetail/final/%s_%s.jpg", + id, byId.getQrCodeBg().hashCode()); + try { + InputStream ossFile = OssBootUtil.getOssFile(finalPath, null); + if (ossFile != null) { + try { + // 使用ByteArrayOutputStream读取InputStream中的所有字节 + ByteArrayOutputStream buffer = new java.io.ByteArrayOutputStream(); + int nRead; + byte[] data = new byte[1024]; + while ((nRead = ossFile.read(data, 0, data.length)) != -1) { + buffer.write(data, 0, nRead); + } + buffer.flush(); + byte[] ossImageBytes = buffer.toByteArray(); + + log.info("从OSS返回店铺图片,店铺ID: {}", id); + return ossImageBytes; + } finally { + ossFile.close(); + } + } + } catch (Exception e) { + log.debug("OSS中未找到最终图片,需要重新生成,店铺ID: {}", id); + } + } + + + try { + byte[] qrCodeBytes = generateWxQrCode("1", "pages_order/gourmet/gourmetDetail?id=" + id, "release"); + + if (StringUtil.isNotEmpty(byId.getQrCodeBg())){ + return qrCodeBytes; + } + + // 生成最终合成图片,传递已获取的配置参数避免重复调用 + byte[] finalImage = this.generateAndCombineImagesFromUrl2(qrCodeBytes, byId.getQrCodeBg(), 0, 0); + + // 异步上传到OSS(移除Redis缓存) + uploadToOssAsync(finalImage, String.format("gourmet/gourmetDetail/final/%s_%s.jpg", + id, byId.getQrCodeBg().hashCode())); + + return finalImage; + } catch (Exception e) { + return null; + } + } +// // public byte[] getInviteCode(HanHaiMember user){ // // // 获取环境配置 @@ -143,21 +212,21 @@ public class YaoDuShopServiceImpl implements YaoDuShopService { return entity.getBody(); } -// /** -// * 异步上传到OSS -// */ -// private void uploadToOssAsync(byte[] imageBytes, String ossPath) { -// // 使用线程池异步执行上传操作 -// CompletableFuture.runAsync(() -> { -// try { -// // 上传到OSS -// OssBootUtil.upload(new ByteArrayInputStream(imageBytes), ossPath); -// log.info("异步上传OSS完成,路径: {}", ossPath); -// } catch (Exception e) { -// log.error("异步上传OSS失败,路径: {}", ossPath, e); -// } -// }, asyncExecutor); -// } + /** + * 异步上传到OSS + */ + private void uploadToOssAsync(byte[] imageBytes, String ossPath) { + // 使用线程池异步执行上传操作 + CompletableFuture.runAsync(() -> { + try { + // 上传到OSS + OssBootUtil.upload(new ByteArrayInputStream(imageBytes), ossPath); + log.info("异步上传OSS完成,路径: {}", ossPath); + } catch (Exception e) { + log.error("异步上传OSS失败,路径: {}", ossPath, e); + } + }, asyncExecutor); + } public byte[] generateAndCombineImagesFromUrl2(byte[] qrCodeImageByte, String backgroundUrl, int qr_code_x, int qr_code_y) {