更新 SMSService.java
parent
1baf697227
commit
28b8588073
|
|
@ -2,6 +2,7 @@ package com.kimgo.wepush.service;
|
||||||
|
|
||||||
import com.fasterxml.jackson.core.JsonProcessingException;
|
import com.fasterxml.jackson.core.JsonProcessingException;
|
||||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||||
|
import com.kimgo.wepush.model.TextMessage;
|
||||||
import com.kimgo.wepush.response.QyWeChatSendMessageApiResponse;
|
import com.kimgo.wepush.response.QyWeChatSendMessageApiResponse;
|
||||||
import com.kimgo.wepush.model.SMSInfo;
|
import com.kimgo.wepush.model.SMSInfo;
|
||||||
import com.kimgo.wepush.model.TextCardMessage;
|
import com.kimgo.wepush.model.TextCardMessage;
|
||||||
|
|
@ -41,22 +42,8 @@ public class SMSService {
|
||||||
|
|
||||||
public QyWeChatSendMessageApiResponse requestWithOkhttp(String accessToken,SMSInfo smsInfo){
|
public QyWeChatSendMessageApiResponse requestWithOkhttp(String accessToken,SMSInfo smsInfo){
|
||||||
OkHttpClient client = new OkHttpClient();
|
OkHttpClient client = new OkHttpClient();
|
||||||
String url = qyWeChatURLService.getSendTextCardMessageUrl() + "?access_token=" + accessToken;
|
|
||||||
|
|
||||||
TextCardMessage textCardMessage = setTextCardMessage(smsInfo);
|
Request request = handleMessage(accessToken,smsInfo);
|
||||||
// 使用Jackson进行序列化
|
|
||||||
ObjectMapper objectMapper = new ObjectMapper();
|
|
||||||
String jsonBody = null;
|
|
||||||
try {
|
|
||||||
jsonBody = objectMapper.writeValueAsString(textCardMessage);
|
|
||||||
logger.info("jsonBody: {}",jsonBody);
|
|
||||||
} catch (JsonProcessingException e) {
|
|
||||||
logger.error("JSON processing error", e);
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
// 构建请求体
|
|
||||||
RequestBody body = RequestBody.create(MediaType.parse("application/json; charset=utf-8"), jsonBody);
|
|
||||||
Request request = new Request.Builder().url(url).post(body).build();
|
|
||||||
try (Response response = client.newCall(request).execute()) {
|
try (Response response = client.newCall(request).execute()) {
|
||||||
String responseBody = response.body().string();
|
String responseBody = response.body().string();
|
||||||
logger.info("Response: {}", responseBody);
|
logger.info("Response: {}", responseBody);
|
||||||
|
|
@ -102,4 +89,79 @@ public class SMSService {
|
||||||
|
|
||||||
return textCardMessage;
|
return textCardMessage;
|
||||||
}
|
}
|
||||||
|
public TextMessage setTextMessage(SMSInfo smsInfo){
|
||||||
|
TextMessage textMessage = new TextMessage();
|
||||||
|
textMessage.setTouser(apiSettingService.getApiSetting().getTouser());
|
||||||
|
textMessage.setMsgtype("text");
|
||||||
|
textMessage.setAgentid(apiSettingService.getApiSetting().getAgentid());
|
||||||
|
textMessage.setEnable_duplicate_check(apiSettingService.getApiSetting().getEnableDuplicateCheck());
|
||||||
|
textMessage.setDuplicate_check_interval(apiSettingService.getApiSetting().getDuplicateCheckInterval());
|
||||||
|
TextMessage.Text text = new TextMessage.Text();
|
||||||
|
text.setContent("你有一条新短信\n短信号码: "+smsInfo.getSmsNumber() + "\n接受时间: " + smsInfo.getSmsAcceptanceTime() + "\n内容: \n"+ smsInfo.getSmsContent());
|
||||||
|
textMessage.setText(text);
|
||||||
|
logger.info("TextMessage: {}", textMessage.toString());
|
||||||
|
return textMessage;
|
||||||
|
}
|
||||||
|
private<T> Request bulidRequest(String accessToken,T object){
|
||||||
|
String url = qyWeChatURLService.getSendTextCardMessageUrl() + "?access_token=" + accessToken;
|
||||||
|
// 使用Jackson进行序列化
|
||||||
|
ObjectMapper objectMapper = new ObjectMapper();
|
||||||
|
String jsonBody = null;
|
||||||
|
try {
|
||||||
|
jsonBody = objectMapper.writeValueAsString(object);
|
||||||
|
logger.info("jsonBody: {}",jsonBody);
|
||||||
|
} catch (JsonProcessingException e) {
|
||||||
|
logger.error("JSON processing error", e);
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
// 构建请求体
|
||||||
|
RequestBody body = RequestBody.create(MediaType.parse("application/json; charset=utf-8"), jsonBody);
|
||||||
|
Request request = new Request.Builder().url(url).post(body).build();
|
||||||
|
return request;
|
||||||
|
}
|
||||||
|
private Request handleMessage(String accessToken, SMSInfo smsInfo) {
|
||||||
|
TextCardMessage textCardMessage = setTextCardMessage(smsInfo);
|
||||||
|
int textCardMessageSize = checkMessageSize(textCardMessage.getTextcard().getDescription());
|
||||||
|
|
||||||
|
if (textCardMessageSize <= 512) {
|
||||||
|
return bulidRequest(accessToken, textCardMessage);
|
||||||
|
} else {
|
||||||
|
TextMessage textMessage = setTextMessage(smsInfo);
|
||||||
|
int textMessageSize = checkMessageSize(textMessage.getText().getContent());
|
||||||
|
|
||||||
|
if (textMessageSize <= 2048) {
|
||||||
|
return bulidRequest(accessToken, textMessage);
|
||||||
|
} else {
|
||||||
|
TextCardMessage wrMsg = setWarningTextCardMessage();
|
||||||
|
return bulidRequest(accessToken, wrMsg);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public TextCardMessage setWarningTextCardMessage() {
|
||||||
|
TextCardMessage textCardMessage = new TextCardMessage();
|
||||||
|
|
||||||
|
textCardMessage.setTouser(apiSettingService.getApiSetting().getTouser());
|
||||||
|
textCardMessage.setMsgtype(apiSettingService.getApiSetting().getMsgtype());
|
||||||
|
textCardMessage.setAgentid(apiSettingService.getApiSetting().getAgentid());
|
||||||
|
textCardMessage.setEnable_duplicate_check(apiSettingService.getApiSetting().getEnableDuplicateCheck());
|
||||||
|
textCardMessage.setDuplicate_check_interval(apiSettingService.getApiSetting().getDuplicateCheckInterval());
|
||||||
|
|
||||||
|
TextCardMessage.TextCard textCard = new TextCardMessage.TextCard();
|
||||||
|
textCard.setTitle("警告消息");
|
||||||
|
textCard.setDescription("<div class='highlight'>警告: 短信消息过长请查看手机。</div>");
|
||||||
|
textCard.setUrl("https://kimgo.cn");
|
||||||
|
// 将TextCard对象设置到TextCardMessage中
|
||||||
|
textCardMessage.setTextcard(textCard);
|
||||||
|
|
||||||
|
logger.info("TextCardMessage: {}", textCardMessage.toString());
|
||||||
|
|
||||||
|
return textCardMessage;
|
||||||
|
}
|
||||||
|
public int checkMessageSize(String message){
|
||||||
|
byte[] messageBytes = message.getBytes(java.nio.charset.StandardCharsets.UTF_8);
|
||||||
|
int size = messageBytes.length;
|
||||||
|
logger.info("Message size in bytes: " + size);
|
||||||
|
return size;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
Loading…
Reference in New Issue