Ver Fonte

个微SOP发消息完善

吴树波 há 6 dias atrás
pai
commit
8e1aeadef4

+ 82 - 0
fs-service/src/main/java/com/fs/wxcid/FileToBase64Util.java

@@ -0,0 +1,82 @@
+package com.fs.wxcid;
+
+import javax.imageio.stream.FileImageInputStream;
+import java.io.ByteArrayOutputStream;
+import java.io.FileInputStream;
+import java.io.InputStream;
+import java.net.HttpURLConnection;
+import java.net.URL;
+import java.util.Arrays;
+import java.util.Base64;
+
+/**
+ * 网络图片转Base64工具类
+ */
+public class FileToBase64Util {
+
+    /**
+     * 网络文件 URL 转 byte[]
+     *
+     * @param fileUrl 文件的网络URL
+     * @return 文件字节数组
+     */
+    public static byte[] downloadToBytes(String fileUrl) throws Exception {
+        if (fileUrl == null || fileUrl.trim().isEmpty()) {
+            throw new IllegalArgumentException("文件URL不能为空");
+        }
+        HttpURLConnection connection = null;
+        InputStream inputStream = null;
+        ByteArrayOutputStream outputStream = null;
+        try {
+            URL url = new URL(fileUrl);
+            connection = (HttpURLConnection) url.openConnection();
+            connection.setRequestMethod("GET");
+            connection.setConnectTimeout(5000);
+            connection.setReadTimeout(10000);
+            connection.setInstanceFollowRedirects(true);
+
+            int responseCode = connection.getResponseCode();
+            if (responseCode != HttpURLConnection.HTTP_OK) {
+                throw new Exception("文件URL访问失败,响应码:" + responseCode);
+            }
+
+            inputStream = connection.getInputStream();
+            outputStream = new ByteArrayOutputStream();
+            byte[] buffer = new byte[4096];
+            int len;
+            while ((len = inputStream.read(buffer)) != -1) {
+                outputStream.write(buffer, 0, len);
+            }
+            return outputStream.toByteArray();
+        } finally {
+            if (outputStream != null) outputStream.close();
+            if (inputStream != null) inputStream.close();
+            if (connection != null) connection.disconnect();
+        }
+    }
+
+    /**
+     * 网络文件 URL 转 Base64 编码字符串
+     *
+     * @param imageUrl 文件的网络URL
+     * @return Base64编码字符串(不带data:image/xxx;base64,前缀)
+     */
+    public static String convertImageUrlToBase64(String imageUrl) throws Exception {
+        byte[] bytes = downloadToBytes(imageUrl);
+        return Base64.getEncoder().encodeToString(bytes);
+    }
+
+    public static void main(String[] args) throws Exception {
+        FileInputStream inputStream = new FileInputStream("F:\\google下载\\a71990bf-4d22-4e9e-9d58-05bf99b6784b.mp3");
+        ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
+        byte[] buffer = new byte[4096];
+        int len;
+        while ((len = inputStream.read(buffer)) != -1) {
+            outputStream.write(buffer, 0, len);
+        }
+        byte[] bytes = outputStream.toByteArray();
+        System.out.printf(Base64.getEncoder().encodeToString(bytes));
+//        String s = convertImageUrlToBase64("https://cdn.his.cdwjyyh.com/fs/20250627/ec72b8ea378340b2b804f38983f1e5e8.wav");
+//        System.out.println(s);
+    }
+}

+ 0 - 85
fs-service/src/main/java/com/fs/wxcid/ImageToBase64Util.java

@@ -1,85 +0,0 @@
-package com.fs.wxcid;
-
-import java.io.ByteArrayOutputStream;
-import java.io.InputStream;
-import java.net.HttpURLConnection;
-import java.net.URL;
-import java.util.Base64;
-
-/**
- * 网络图片转Base64工具类
- */
-public class ImageToBase64Util {
-
-    /**
-     * 网络图片转Base64编码字符串
-     * @param imageUrl 图片的网络URL(如https://xxx.com/xxx.png)
-     * @return Base64编码字符串(不带data:image/xxx;base64,前缀)
-     * @throws Exception 异常(网络错误、图片读取失败等)
-     */
-    public static String convertImageUrlToBase64(String imageUrl) throws Exception {
-        // 1. 校验URL参数
-        if (imageUrl == null || imageUrl.trim().isEmpty()) {
-            throw new IllegalArgumentException("图片URL不能为空");
-        }
-
-        HttpURLConnection connection = null;
-        InputStream inputStream = null;
-        ByteArrayOutputStream outputStream = null;
-
-        try {
-            // 2. 打开URL连接
-            URL url = new URL(imageUrl);
-            connection = (HttpURLConnection) url.openConnection();
-            // 设置连接参数(防超时、防重定向问题)
-            connection.setRequestMethod("GET");
-            connection.setConnectTimeout(5000); // 连接超时5秒
-            connection.setReadTimeout(5000);    // 读取超时5秒
-            connection.setInstanceFollowRedirects(true); // 允许重定向
-
-            // 3. 校验响应码(200表示成功)
-            int responseCode = connection.getResponseCode();
-            if (responseCode != HttpURLConnection.HTTP_OK) {
-                throw new Exception("图片URL访问失败,响应码:" + responseCode);
-            }
-
-            // 4. 读取图片流到字节数组
-            inputStream = connection.getInputStream();
-            outputStream = new ByteArrayOutputStream();
-            byte[] buffer = new byte[1024]; // 缓冲区,每次读1KB
-            int len;
-            while ((len = inputStream.read(buffer)) != -1) {
-                outputStream.write(buffer, 0, len);
-            }
-
-            // 5. 将字节数组编码为Base64
-            byte[] imageBytes = outputStream.toByteArray();
-            return Base64.getEncoder().encodeToString(imageBytes);
-
-        } finally {
-            // 6. 关闭所有资源(避免内存泄漏)
-            if (outputStream != null) {
-                outputStream.close();
-            }
-            if (inputStream != null) {
-                inputStream.close();
-            }
-            if (connection != null) {
-                connection.disconnect();
-            }
-        }
-    }
-
-    // 测试示例
-    public static void main(String[] args) {
-        try {
-            // 替换为你的网络图片URL
-            String imageUrl = "https://czwh.obs.cn-southwest-2.myhuaweicloud.com/fs/20260225/1771999986358.png";
-            String base64Str = convertImageUrlToBase64(imageUrl);
-            System.out.println(base64Str);
-        } catch (Exception e) {
-            e.printStackTrace();
-            System.out.println("转换失败:" + e.getMessage());
-        }
-    }
-}

+ 74 - 0
fs-service/src/main/java/com/fs/wxcid/dto/message/CdnUploadVideoResult.java

@@ -0,0 +1,74 @@
+package com.fs.wxcid.dto.message;
+
+import com.alibaba.fastjson.annotation.JSONField;
+import lombok.Data;
+
+/**
+ * /message/CdnUploadVideo 接口返回的 Data 结构
+ */
+@Data
+public class CdnUploadVideoResult {
+
+    @JSONField(name = "FileKey")
+    private String fileKey;
+
+    @JSONField(name = "Ver")
+    private Integer ver;
+
+    @JSONField(name = "ThumbDataSize")
+    private Integer thumbDataSize;
+
+    @JSONField(name = "ThumbURL")
+    private String thumbURL;
+
+    @JSONField(name = "FileAesKey")
+    private String fileAesKey;
+
+    @JSONField(name = "Mp4identify")
+    private String mp4identify;
+
+    @JSONField(name = "EnableQuic")
+    private Integer enableQuic;
+
+    @JSONField(name = "IsRetry")
+    private Integer isRetry;
+
+    @JSONField(name = "IsOverLoad")
+    private Integer isOverLoad;
+
+    @JSONField(name = "RecvLen")
+    private Integer recvLen;
+
+    @JSONField(name = "IsGetCDN")
+    private Integer isGetCDN;
+
+    @JSONField(name = "RetrySec")
+    private Integer retrySec;
+
+    @JSONField(name = "XClientIP")
+    private String xClientIP;
+
+    @JSONField(name = "FileURL")
+    private String fileURL;
+
+    @JSONField(name = "VideoDataMD5")
+    private String videoDataMD5;
+
+    @JSONField(name = "RetCode")
+    private Integer retCode;
+
+    @JSONField(name = "FileID")
+    private String fileID;
+
+    @JSONField(name = "ThumbHeight")
+    private Integer thumbHeight;
+
+    @JSONField(name = "ThumbWidth")
+    private Integer thumbWidth;
+
+    @JSONField(name = "Seq")
+    private Integer seq;
+
+    @JSONField(name = "VideoDataSize")
+    private Integer videoDataSize;
+}

+ 27 - 0
fs-service/src/main/java/com/fs/wxcid/dto/message/SendVideoMessageParam.java

@@ -0,0 +1,27 @@
+package com.fs.wxcid.dto.message;
+
+import com.fs.wxcid.dto.common.BaseAccountRequest;
+import lombok.Data;
+import lombok.EqualsAndHashCode;
+
+/**
+ * 发送视频消息业务请求参数
+ */
+@Data
+@EqualsAndHashCode(callSuper = true)
+public class SendVideoMessageParam extends BaseAccountRequest {
+    /**
+     * 视频封面文件 URL
+     */
+    private String thumbUrl;
+
+    /**
+     * 视频文件 URL
+     */
+    private String videoUrl;
+
+    /**
+     * 接收方 wxid
+     */
+    private String toUser;
+}

+ 20 - 0
fs-service/src/main/java/com/fs/wxcid/dto/message/SendVideoMessageRequest.java

@@ -0,0 +1,20 @@
+package com.fs.wxcid.dto.message;
+
+import com.fasterxml.jackson.annotation.JsonProperty;
+import lombok.Data;
+
+/**
+ * /message/CdnUploadVideo 请求体
+ */
+@Data
+public class SendVideoMessageRequest {
+
+    @JsonProperty("ThumbData")
+    private byte[] thumbData;
+
+    @JsonProperty("ToUserName")
+    private String toUserName;
+
+    @JsonProperty("VideoData")
+    private byte[] videoData;
+}

+ 1 - 0
fs-service/src/main/java/com/fs/wxcid/service/MessageService.java

@@ -8,6 +8,7 @@ import java.util.List;
 public interface MessageService {
     List<SendMessageResult> sendTextMessage(SendTextMessageParam param);
     List<SendImageMessageResult> sendImageMessage(SendImageMessageParam param);
+    CdnUploadVideoResult sendVideoMessage(SendVideoMessageParam param);
     RevokeMsgResult revokeMessage(RevokeMsgRequest request);
 
 }

+ 28 - 2
fs-service/src/main/java/com/fs/wxcid/service/impl/MessageServiceImpl.java

@@ -2,18 +2,21 @@ package com.fs.wxcid.service.impl;
 
 import com.alibaba.fastjson.TypeReference;
 import com.fs.common.exception.CustomException;
-import com.fs.wxcid.ImageToBase64Util;
+import com.fs.wxcid.FileToBase64Util;
 import com.fs.wxcid.ServiceUtils;
 import com.fs.wxcid.dto.common.ApiResponseCommon;
 import com.fs.wxcid.dto.login.RequestBaseVo;
+import com.fs.wxcid.dto.message.CdnUploadVideoResult;
 import com.fs.wxcid.dto.message.MsgItem;
 import com.fs.wxcid.dto.message.RevokeMsgRequest;
 import com.fs.wxcid.dto.message.SendImageMessageParam;
 import com.fs.wxcid.dto.message.SendTextMessageParam;
+import com.fs.wxcid.dto.message.SendVideoMessageParam;
 import com.fs.wxcid.dto.message.RevokeMsgResult;
 import com.fs.wxcid.dto.message.SendImageMessageResult;
 import com.fs.wxcid.dto.message.SendMessageResult;
 import com.fs.wxcid.dto.message.SendTextMessageRequest;
+import com.fs.wxcid.dto.message.SendVideoMessageRequest;
 import com.fs.wxcid.service.MessageService;
 import lombok.extern.slf4j.Slf4j;
 import org.springframework.beans.factory.annotation.Autowired;
@@ -59,7 +62,7 @@ public class MessageServiceImpl implements MessageService {
         MsgItem msgItem = new MsgItem();
         try {
             msgItem.setMsgType(0);
-            msgItem.setImageContent(ImageToBase64Util.convertImageUrlToBase64(param.getImgUrl()));
+            msgItem.setImageContent(FileToBase64Util.convertImageUrlToBase64(param.getImgUrl()));
             msgItem.setToUserName(param.getToUser());
         }catch (Exception e){
             log.error("发送消息时,图片转换base64错误", e);
@@ -93,4 +96,27 @@ public class MessageServiceImpl implements MessageService {
         }
         return response.getData();
     }
+
+    @Override
+    public CdnUploadVideoResult sendVideoMessage(SendVideoMessageParam param) {
+        SendVideoMessageRequest request = new SendVideoMessageRequest();
+        request.setToUserName(param.getToUser());
+        try {
+            request.setThumbData(FileToBase64Util.downloadToBytes(param.getThumbUrl()));
+            request.setVideoData(FileToBase64Util.downloadToBytes(param.getVideoUrl()));
+        } catch (Exception e) {
+            log.error("发送视频消息时,文件下载失败", e);
+            throw new CustomException("视频消息发送失败");
+        }
+        ApiResponseCommon<CdnUploadVideoResult> response = serviceUtils.sendPost(
+                BASE_URL + "CdnUploadVideo",
+                RequestBaseVo.builder().accountId(param.getAccountId()).data(request).build(),
+                new TypeReference<ApiResponseCommon<CdnUploadVideoResult>>() {}
+        );
+        CdnUploadVideoResult result = response.getData();
+        if (result != null && result.getRetCode() != null && result.getRetCode() != 0) {
+            throw new CustomException("视频上传失败,RetCode=" + result.getRetCode());
+        }
+        return result;
+    }
 }