diff --git a/mall-shop/src/main/java/com/suisung/mall/shop/config/OssUtils.java b/mall-shop/src/main/java/com/suisung/mall/shop/config/OssUtils.java index d98ba66c..1ab54ef6 100644 --- a/mall-shop/src/main/java/com/suisung/mall/shop/config/OssUtils.java +++ b/mall-shop/src/main/java/com/suisung/mall/shop/config/OssUtils.java @@ -30,6 +30,78 @@ public class OssUtils { @Autowired private OssService ossService; + /** + * 网络图片转输入流(改进版) + * + * @param fileUrl 网络文件URL + * @return InputStream 输入流,如果失败返回null + */ + public static InputStream urlToInputSteam(String fileUrl) { + if (fileUrl == null || fileUrl.trim().isEmpty()) { + System.err.println("文件URL为空"); + return null; + } + + try { + URL url = new URL(fileUrl); + HttpURLConnection connection = (HttpURLConnection) url.openConnection(); + connection.setRequestMethod("GET"); + connection.setConnectTimeout(10 * 1000); + connection.setReadTimeout(10 * 1000); + connection.setRequestProperty("User-Agent", "Mozilla/5.0"); + + int responseCode = connection.getResponseCode(); + if (responseCode != HttpURLConnection.HTTP_OK) { + System.err.println("HTTP响应码错误: " + responseCode); + return null; + } + + // 直接返回连接的输入流,由调用者负责关闭 + return connection.getInputStream(); + + } catch (IOException e) { + System.err.println("网络图片转输入流失败: " + e.getMessage()); + e.printStackTrace(); + return null; + } + } + + + /** + * 缓冲图像转输入流 + * + * @param bufferedImage + * @Return Map + */ + public static Map toStreamMap(BufferedImage bufferedImage) { + InputStream stream = null; + ByteArrayOutputStream arrayOutputStream = null; + try { + arrayOutputStream = new ByteArrayOutputStream(); + ImageIO.write(bufferedImage, "png", arrayOutputStream); + + int file_size = arrayOutputStream.size(); + byte[] result = arrayOutputStream.toByteArray(); + + stream = new ByteArrayInputStream(result); + Map resultMap = new HashMap<>(); + resultMap.put("stream", stream); + resultMap.put("file_size", Convert.toLong(file_size)); + return resultMap; + } catch (IOException e) { + e.printStackTrace(); + } finally { + // 释放连接 + try { + arrayOutputStream.close(); + stream.close(); + } catch (IOException e) { + e.printStackTrace(); + } + } + return null; + } + /** * oos直传,本地不存储图片 * @@ -79,83 +151,4 @@ public class OssUtils { return ossFilePath; } - /** - * 网络图片转输入流 - * - * @param fileUrl - * @return - */ - public static InputStream urlToInputSteam(String fileUrl) { - HttpURLConnection connection = null; - InputStream is = null; - InputStream stream = null; - try { - // 打开服务器图片路径 - URL url = new URL(fileUrl); - //创建连接 - connection = (HttpURLConnection) url.openConnection(); - connection.setRequestMethod("GET"); // 提交模式 - connection.setConnectTimeout(10 * 1000); //连接超时5秒 - connection.setReadTimeout(10 * 1000); //读取超时10秒 - - is = connection.getInputStream(); - int file_size = connection.getContentLength(); //文件大小 - byte[] result = new byte[file_size]; - int readCount = 0; - while (readCount < file_size) { - readCount += is.read(result, readCount, file_size - readCount); - } - stream = new ByteArrayInputStream(result); - return stream; - } catch (IOException e) { - e.printStackTrace(); - } finally { - try { - connection.disconnect(); - is.close(); - stream.close(); - } catch (IOException e) { - e.printStackTrace(); - } - } - - return null; - - } - - /** - * 缓冲图像转输入流 - * - * @param bufferedImage - * @Return Map - */ - public static Map toStreamMap(BufferedImage bufferedImage) { - InputStream stream = null; - ByteArrayOutputStream arrayOutputStream = null; - try { - arrayOutputStream = new ByteArrayOutputStream(); - ImageIO.write(bufferedImage, "png", arrayOutputStream); - - int file_size = arrayOutputStream.size(); - byte[] result = arrayOutputStream.toByteArray(); - - stream = new ByteArrayInputStream(result); - Map resultMap = new HashMap<>(); - resultMap.put("stream", stream); - resultMap.put("file_size", Convert.toLong(file_size)); - return resultMap; - } catch (IOException e) { - e.printStackTrace(); - } finally { - // 释放连接 - try { - arrayOutputStream.close(); - stream.close(); - } catch (IOException e) { - e.printStackTrace(); - } - } - return null; - } - } diff --git a/mall-shop/src/main/java/com/suisung/mall/shop/distribution/service/impl/ShopDistributionUserServiceImpl.java b/mall-shop/src/main/java/com/suisung/mall/shop/distribution/service/impl/ShopDistributionUserServiceImpl.java index f161381e..bfbcc743 100644 --- a/mall-shop/src/main/java/com/suisung/mall/shop/distribution/service/impl/ShopDistributionUserServiceImpl.java +++ b/mall-shop/src/main/java/com/suisung/mall/shop/distribution/service/impl/ShopDistributionUserServiceImpl.java @@ -669,30 +669,67 @@ public class ShopDistributionUserServiceImpl extends BaseServiceImpl impl = new PosterDefaultImpl<>(); - BufferedImage bufferedImage = impl.annotationDrawPoster(poster).draw(null); - if (!ConfigConstant.FILE_STORAGE_DISK) { - filename = filename.replace("-qrcode", ""); - - String postUrl = null; - Integer uploadType = accountBaseConfigService.getConfig("upload", 1); - - if (uploadType.equals(1)) { - postUrl = ConfigConstant.URL_BASE + "/admin/oss/upload/" + dir + "/" + filename; // 文件本地路径 - } else if (uploadType.equals(2)) { - // oss 服务 - try { - Map streamMap = OssUtils.toStreamMap(bufferedImage); - InputStream stream = (InputStream) streamMap.get("stream"); - Long file_size = (Long) streamMap.get("file_size"); - String floder = null; - if (poster_type == 1) { - floder = ALIYUN_OSS_DIR_PREFIX.concat("/") + dir + "poster/1/"; - } else if (poster_type == 2) { - floder = ALIYUN_OSS_DIR_PREFIX.concat("/") + dir + "poster/2/"; - } - postUrl = ossService.uploadObject2OSS(null, floder + filename, stream, filename, file_size); - } catch (Exception e) { - e.printStackTrace(); - logger.error("文件上传失败!", e); - return null; - } - } else if (uploadType.equals(4)) { - try { - ImageIO.write(bufferedImage, "png", poster_file); - } catch (IOException e) { - throw new ApiException(I18nUtil._("生成海报失败!")); - } - postUrl = ossService.uploadObject4OSS(poster_file, TENGXUN_DEFAULT_DIR.concat("/").concat(dir).concat("/").concat(filename)); + // 绘制海报图 + try { + InputStream backgroundStream = OssUtils.urlToInputSteam(plantform_poster_bg); + if (backgroundStream == null) { + logger.error("获取海报背景图失败,用户ID: {}, 背景图URL: {}", user.getId(), plantform_poster_bg); + throw new ApiException(I18nUtil._("获取背景图失败")); } - data.put("poster_url", String.format("%s?rand=%s", postUrl, RandomUtil.randomString(13))); + BufferedImage background = ImageIO.read(backgroundStream); + BufferedImage qrcodeImage = null; - } else { - ImageIO.write(bufferedImage, "png", poster_file); + if (!ConfigConstant.FILE_STORAGE_DISK) { + if (qrcode_stream != null) { + qrcodeImage = ImageIO.read(qrcode_stream); + } + } else { + if (qrcode_file != null && qrcode_file.exists()) { + qrcodeImage = ImageIO.read(qrcode_file); + } + } + + BufferedImage productImage = null; + if (main_file != null && main_file.exists()) { + productImage = ImageIO.read(main_file); + } + + if (background == null) { + logger.error("读取背景图失败,用户ID: {}", user.getId()); + throw new ApiException(I18nUtil._("读取背景图失败")); + } + + PromotePoster poster = new PromotePoster(); + poster.setBackgroundImage(background); + poster.setProductName(poster_name); + + if (qrcodeImage != null) { + poster.setQrcodeImage(qrcodeImage); + } + + if (main_price != null) { + poster.setProductPrice(main_price); + } + + if (StrUtil.isNotBlank(main_price)) { + // 产品海报 + if (productImage != null) { + poster.setProductImage(productImage); + } + } else { + // 推广海报 + if (productImage != null) { + poster.setPlantformImage(productImage); + } + } + + PosterDefaultImpl impl = new PosterDefaultImpl<>(); + BufferedImage bufferedImage = impl.annotationDrawPoster(poster).draw(null); + + if (bufferedImage == null) { + logger.error("绘制海报失败,用户ID: {}", user.getId()); + throw new ApiException(I18nUtil._("绘制海报失败")); + } + + if (!ConfigConstant.FILE_STORAGE_DISK) { + filename = filename.replace("-qrcode", ""); + + String postUrl = null; + Integer uploadType = accountBaseConfigService.getConfig("upload", 1); + + if (uploadType.equals(1)) { + postUrl = ConfigConstant.URL_BASE + "/admin/oss/upload/" + dir + "/" + filename; // 文件本地路径 + } else if (uploadType.equals(2)) { + // oss 服务 + try { + Map streamMap = OssUtils.toStreamMap(bufferedImage); + if (streamMap != null) { + InputStream stream = (InputStream) streamMap.get("stream"); + Long file_size = (Long) streamMap.get("file_size"); + String floder = null; + if (poster_type == 1) { + floder = ALIYUN_OSS_DIR_PREFIX.concat("/") + dir + "poster/1/"; + } else if (poster_type == 2) { + floder = ALIYUN_OSS_DIR_PREFIX.concat("/") + dir + "poster/2/"; + } + postUrl = ossService.uploadObject2OSS(null, floder + filename, stream, filename, file_size); + } + } catch (Exception e) { + logger.error("OSS上传海报失败,用户ID: {}: {}", user.getId(), e.getMessage(), e); + throw new ApiException(I18nUtil._("海报上传失败")); + } + } else if (uploadType.equals(4)) { + try { + ImageIO.write(bufferedImage, "png", poster_file); + } catch (IOException e) { + logger.error("生成海报文件失败,用户ID: {}: {}", user.getId(), e.getMessage(), e); + throw new ApiException(I18nUtil._("生成海报失败!")); + } + try { + postUrl = ossService.uploadObject4OSS(poster_file, TENGXUN_DEFAULT_DIR.concat("/").concat(dir).concat("/").concat(filename)); + } catch (Exception e) { + logger.error("腾讯云上传海报失败,用户ID: {}: {}", user.getId(), e.getMessage(), e); + throw new ApiException(I18nUtil._("海报上传失败")); + } + } + + if (postUrl != null) { + data.put("poster_url", String.format("%s?rand=%s", postUrl, RandomUtil.randomString(13))); + } + + } else { + ImageIO.write(bufferedImage, "png", poster_file); + } + } catch (Exception e) { + logger.error("生成海报失败,用户ID: {}: {}", user.getId(), e.getMessage(), e); + throw new ApiException(I18nUtil._("生成海报失败!")); + } + + Float plantform_fx_cps_rate_1 = accountBaseConfigService.getConfig("plantform_fx_cps_rate_1", 0f); + Float plantform_fx_cps_rate_2 = accountBaseConfigService.getConfig("plantform_fx_cps_rate_2", 0f); + Float plantform_fx_cps_rate_3 = accountBaseConfigService.getConfig("plantform_fx_cps_rate_3", 0f); + + data.put("plantform_fx_cps_rate_1", plantform_fx_cps_rate_1); + data.put("plantform_fx_cps_rate_2", plantform_fx_cps_rate_2); + data.put("plantform_fx_cps_rate_3", plantform_fx_cps_rate_3); + + try { + redisService.set(cacheKey, data); + } catch (Exception e) { + logger.warn("海报数据缓存失败,用户ID: {}: {}", user.getId(), e.getMessage()); + } + + return CommonResult.success(data); + + } finally { + // 确保资源被正确关闭 + if (qrcode_stream != null) { + try { + qrcode_stream.close(); + } catch (IOException e) { + logger.warn("关闭二维码输入流失败,用户ID: {}: {}", user.getId(), e.getMessage()); + } } - } catch (Exception e) { - throw new ApiException(I18nUtil._("生成海报失败!")); } - - Float plantform_fx_cps_rate_1 = accountBaseConfigService.getConfig("plantform_fx_cps_rate_1", 0f); - Float plantform_fx_cps_rate_2 = accountBaseConfigService.getConfig("plantform_fx_cps_rate_2", 0f); - Float plantform_fx_cps_rate_3 = accountBaseConfigService.getConfig("plantform_fx_cps_rate_3", 0f); - - data.put("plantform_fx_cps_rate_1", plantform_fx_cps_rate_1); - data.put("plantform_fx_cps_rate_2", plantform_fx_cps_rate_2); - data.put("plantform_fx_cps_rate_3", plantform_fx_cps_rate_3); - - redisService.set(cacheKey, data); - - return CommonResult.success(data); } /** diff --git a/mall-shop/src/main/java/com/suisung/mall/shop/lakala/service/impl/LakalaApiServiceImpl.java b/mall-shop/src/main/java/com/suisung/mall/shop/lakala/service/impl/LakalaApiServiceImpl.java index 9aa51315..ad8a5e7c 100644 --- a/mall-shop/src/main/java/com/suisung/mall/shop/lakala/service/impl/LakalaApiServiceImpl.java +++ b/mall-shop/src/main/java/com/suisung/mall/shop/lakala/service/impl/LakalaApiServiceImpl.java @@ -550,7 +550,6 @@ public class LakalaApiServiceImpl implements LakalaApiService { payload.put("category", CommonConstant.PUSH_MSG_CATE_EC); pushMessageService.noticeMerchantSignEcContract(shopMchEntry.getCreated_by(), payload); - return Pair.of(true, "商家入网申请电子合同成功"); } diff --git a/mall-shop/src/main/java/com/suisung/mall/shop/store/service/impl/ShopMchEntryServiceImpl.java b/mall-shop/src/main/java/com/suisung/mall/shop/store/service/impl/ShopMchEntryServiceImpl.java index 1839138f..5e5a1bee 100644 --- a/mall-shop/src/main/java/com/suisung/mall/shop/store/service/impl/ShopMchEntryServiceImpl.java +++ b/mall-shop/src/main/java/com/suisung/mall/shop/store/service/impl/ShopMchEntryServiceImpl.java @@ -695,7 +695,6 @@ public class ShopMchEntryServiceImpl extends BaseServiceImpl