|
@@ -0,0 +1,86 @@
|
|
|
+package com.qunzhixinxi.hnqz.admin.listener;
|
|
|
+
|
|
|
+import cn.hutool.core.util.StrUtil;
|
|
|
+import com.qunzhixinxi.hnqz.admin.config.UpmsConfig;
|
|
|
+import com.qunzhixinxi.hnqz.admin.entity.WmTask;
|
|
|
+import com.qunzhixinxi.hnqz.admin.event.TaskInfoImgEvent;
|
|
|
+import com.qunzhixinxi.hnqz.admin.service.SysFileService;
|
|
|
+import com.qunzhixinxi.hnqz.admin.service.WmTaskService;
|
|
|
+import com.qunzhixinxi.hnqz.admin.util.Html2ImageByJsWrapper;
|
|
|
+import com.qunzhixinxi.hnqz.common.core.util.R;
|
|
|
+import lombok.AllArgsConstructor;
|
|
|
+import lombok.extern.slf4j.Slf4j;
|
|
|
+import org.springframework.context.annotation.Configuration;
|
|
|
+import org.springframework.context.event.EventListener;
|
|
|
+import org.springframework.core.annotation.Order;
|
|
|
+import org.springframework.scheduling.annotation.Async;
|
|
|
+
|
|
|
+import javax.imageio.ImageIO;
|
|
|
+import javax.imageio.stream.ImageOutputStream;
|
|
|
+import java.awt.image.BufferedImage;
|
|
|
+import java.io.ByteArrayInputStream;
|
|
|
+import java.io.ByteArrayOutputStream;
|
|
|
+import java.io.InputStream;
|
|
|
+
|
|
|
+/**
|
|
|
+ * 任务h5页面截图事件监听器
|
|
|
+ *
|
|
|
+ * @author lixuesong
|
|
|
+ * @date 2024/04/16
|
|
|
+ */
|
|
|
+@Slf4j
|
|
|
+@AllArgsConstructor
|
|
|
+@Configuration
|
|
|
+public class TaskInfoImgEventListener {
|
|
|
+
|
|
|
+ private final WmTaskService wmTaskService;
|
|
|
+
|
|
|
+ private final SysFileService sysFileService;
|
|
|
+
|
|
|
+ private final UpmsConfig upmsConfig;
|
|
|
+
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 推送事件
|
|
|
+ *
|
|
|
+ * @param event 事件
|
|
|
+ */
|
|
|
+ @Async
|
|
|
+ @Order
|
|
|
+ @EventListener(TaskInfoImgEvent.class)
|
|
|
+ public void pushEvent(TaskInfoImgEvent event) {
|
|
|
+ log.info("任务h5页面截图事件参数:taskId={}", event.getTaskId());
|
|
|
+
|
|
|
+ WmTask wmTask = wmTaskService.getById(event.getTaskId());
|
|
|
+ if (null == wmTask) {
|
|
|
+ log.warn("任务不存在:taskId={}", wmTask.getId());
|
|
|
+ return;
|
|
|
+ }
|
|
|
+
|
|
|
+ try {
|
|
|
+ BufferedImage bufferedImage = Html2ImageByJsWrapper.renderHtml2Image(upmsConfig.getApiUrl()
|
|
|
+ + "/h5/#/pages/task/task-detail/index?id=" + wmTask.getId(), upmsConfig.getServerUrl());
|
|
|
+
|
|
|
+ if (bufferedImage == null || bufferedImage.getWidth() == 0 || bufferedImage.getHeight() == 0) {
|
|
|
+ log.error("图片截取失败: 截图服务处理失败");
|
|
|
+ return;
|
|
|
+ }
|
|
|
+
|
|
|
+ ByteArrayOutputStream bs = new ByteArrayOutputStream();
|
|
|
+ ImageOutputStream imOut = ImageIO.createImageOutputStream(bs);
|
|
|
+ ImageIO.write(bufferedImage, "png", imOut);
|
|
|
+
|
|
|
+ InputStream inputStream = new ByteArrayInputStream(bs.toByteArray());
|
|
|
+
|
|
|
+ String path = sysFileService.uploadImgByByte(inputStream);
|
|
|
+ if (StrUtil.isNotBlank(path)) {
|
|
|
+ WmTask updateTask = new WmTask();
|
|
|
+ updateTask.setId(wmTask.getId());
|
|
|
+ updateTask.setTaskInfoImg(path);
|
|
|
+ wmTaskService.updateById(updateTask);
|
|
|
+ }
|
|
|
+ } catch (Exception e) {
|
|
|
+ log.error("图片截取失败", e);
|
|
|
+ }
|
|
|
+ }
|
|
|
+}
|