|
|
@@ -2,9 +2,11 @@ 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.ServiceUtils;
|
|
|
import com.fs.wxcid.dto.common.ApiResponseCommon;
|
|
|
import com.fs.wxcid.dto.login.RequestBaseVo;
|
|
|
+import com.fs.wxcid.dto.message.MsgItem;
|
|
|
import com.fs.wxcid.dto.message.SendMessageResult;
|
|
|
import com.fs.wxcid.dto.message.SendTextMessageRequest;
|
|
|
import com.fs.wxcid.service.MessageService;
|
|
|
@@ -22,8 +24,12 @@ public class MessageServiceImpl implements MessageService {
|
|
|
private ServiceUtils serviceUtils;
|
|
|
|
|
|
@Override
|
|
|
- public ApiResponseCommon<List<SendMessageResult>> sendTextMessage(Long accountId) {
|
|
|
+ public ApiResponseCommon<List<SendMessageResult>> sendTextMessage(Long accountId, String txt) {
|
|
|
SendTextMessageRequest request = new SendTextMessageRequest();
|
|
|
+ List<MsgItem> list = new ArrayList<>();
|
|
|
+ MsgItem msgItem = new MsgItem();
|
|
|
+ msgItem.setTextContent(txt);
|
|
|
+ request.setMsgItem(list);
|
|
|
ApiResponseCommon<List<SendMessageResult>> response = serviceUtils.sendPost(BASE_URL + "SendTextMessage", RequestBaseVo.builder().accountId(accountId).data(request).build(), new TypeReference<ApiResponseCommon<List<SendMessageResult>>>() {});
|
|
|
// 第二层:检查每条消息是否真正发送成功
|
|
|
List<SendMessageResult> results = response.getData();
|
|
|
@@ -39,4 +45,32 @@ public class MessageServiceImpl implements MessageService {
|
|
|
}
|
|
|
return response;
|
|
|
}
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public ApiResponseCommon<List<SendMessageResult>> sendImageMessage(Long accountId, String imgUrl) {
|
|
|
+ SendTextMessageRequest request = new SendTextMessageRequest();
|
|
|
+ List<MsgItem> list = new ArrayList<>();
|
|
|
+ MsgItem msgItem = new MsgItem();
|
|
|
+ try {
|
|
|
+ msgItem.setImageContent(ImageToBase64Util.convertImageUrlToBase64(imgUrl));
|
|
|
+ }catch (Exception e){
|
|
|
+ log.error("发送消息时,图片转换base64错误", e);
|
|
|
+ throw new CustomException("图片消息发送失败");
|
|
|
+ }
|
|
|
+ request.setMsgItem(list);
|
|
|
+ ApiResponseCommon<List<SendMessageResult>> response = serviceUtils.sendPost(BASE_URL + "SendImageNewMessage", RequestBaseVo.builder().accountId(accountId).data(request).build(), new TypeReference<ApiResponseCommon<List<SendMessageResult>>>() {});
|
|
|
+ // 第二层:检查每条消息是否真正发送成功
|
|
|
+ List<SendMessageResult> results = response.getData();
|
|
|
+ List<String> failedRecipients = new ArrayList<>();
|
|
|
+ for (SendMessageResult result : results) {
|
|
|
+ if (!result.isSendSuccess()) {
|
|
|
+ failedRecipients.add(result.getToUserName());
|
|
|
+ }
|
|
|
+ }
|
|
|
+ if (!failedRecipients.isEmpty()) {
|
|
|
+ String errorMsg = "部分消息发送失败,失败接收人: " + String.join(", ", failedRecipients);
|
|
|
+ throw new CustomException("发送文本消息部分失败: " + errorMsg);
|
|
|
+ }
|
|
|
+ return response;
|
|
|
+ }
|
|
|
}
|