commit
46a2de73ff
|
|
@ -0,0 +1,39 @@
|
||||||
|
package com.kimgo.wepush.controller;
|
||||||
|
|
||||||
|
import com.kimgo.wepush.model.WeChatMsg;
|
||||||
|
import com.kimgo.wepush.response.ServerResponseEntity;
|
||||||
|
import com.kimgo.wepush.service.HandleWeChatMsgService;
|
||||||
|
import jakarta.validation.Valid;
|
||||||
|
import org.slf4j.Logger;
|
||||||
|
import org.slf4j.LoggerFactory;
|
||||||
|
import org.springframework.validation.annotation.Validated;
|
||||||
|
import org.springframework.web.bind.annotation.*;
|
||||||
|
|
||||||
|
@RestController
|
||||||
|
@Validated
|
||||||
|
@RequestMapping(value = "/api",method = RequestMethod.POST)
|
||||||
|
public class WeChatMsgForwarderController {
|
||||||
|
|
||||||
|
private final Logger logger = LoggerFactory.getLogger(WeChatMsgForwarderController.class);
|
||||||
|
|
||||||
|
private final HandleWeChatMsgService handleWeChatMsgService;
|
||||||
|
|
||||||
|
public WeChatMsgForwarderController(HandleWeChatMsgService handleWeChatMsgService) {
|
||||||
|
this.handleWeChatMsgService = handleWeChatMsgService;
|
||||||
|
}
|
||||||
|
|
||||||
|
@PostMapping("/wechat")
|
||||||
|
public ServerResponseEntity receiveMsg(@RequestHeader("accessToken") String accessToken,
|
||||||
|
@RequestBody @Valid WeChatMsg weChatMsg){
|
||||||
|
logger.info("CallInfoO: {}",weChatMsg.toString());
|
||||||
|
if(accessToken == null){
|
||||||
|
logger.warn("Access token is missing or empty");
|
||||||
|
return ServerResponseEntity.fail("accessToken cannot be empty.");
|
||||||
|
}
|
||||||
|
if (weChatMsg.isInvalid()) {
|
||||||
|
logger.debug("CallInfo has invalid fields.");
|
||||||
|
return ServerResponseEntity.fail("json body value error.");
|
||||||
|
}
|
||||||
|
return handleWeChatMsgService.handleMsg(accessToken,weChatMsg);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,22 @@
|
||||||
|
package com.kimgo.wepush.model;
|
||||||
|
|
||||||
|
import lombok.Data;
|
||||||
|
|
||||||
|
@Data
|
||||||
|
public class WeChatMsg {
|
||||||
|
private String packageName;
|
||||||
|
private String appName;
|
||||||
|
private String title;
|
||||||
|
private String sender;
|
||||||
|
private String message;
|
||||||
|
private String currentTime;
|
||||||
|
public boolean isInvalid() {
|
||||||
|
return !isStringValid(packageName) || !isStringValid(appName)
|
||||||
|
|| !isStringValid(title) || !isStringValid(sender)
|
||||||
|
|| !isStringValid(message) || !isStringValid(currentTime);
|
||||||
|
}
|
||||||
|
|
||||||
|
private boolean isStringValid(String value) {
|
||||||
|
return value != null && !value.isEmpty() && !"null".equals(value);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -20,8 +20,6 @@ import java.io.IOException;
|
||||||
@Service
|
@Service
|
||||||
public class CallService {
|
public class CallService {
|
||||||
private final Logger logger = LoggerFactory.getLogger(CallService.class);
|
private final Logger logger = LoggerFactory.getLogger(CallService.class);
|
||||||
|
|
||||||
private ServerResponseEntity serverResponseEntity;
|
|
||||||
@Autowired
|
@Autowired
|
||||||
private QyWeChatURLService qyWeChatURLService;
|
private QyWeChatURLService qyWeChatURLService;
|
||||||
@Autowired
|
@Autowired
|
||||||
|
|
@ -62,7 +60,7 @@ public class CallService {
|
||||||
// 将TextCard对象设置到TextCardMessage中
|
// 将TextCard对象设置到TextCardMessage中
|
||||||
textCardMessage.setTextcard(textCard);
|
textCardMessage.setTextcard(textCard);
|
||||||
|
|
||||||
logger.info("TextCardMessage: {}", textCardMessage.toString());
|
logger.info("TextCardMessage: {}", textCardMessage);
|
||||||
|
|
||||||
return textCardMessage;
|
return textCardMessage;
|
||||||
}
|
}
|
||||||
|
|
@ -74,7 +72,7 @@ public class CallService {
|
||||||
TextCardMessage textCardMessage = setTextCardMessage(phoneNumber,callTime);
|
TextCardMessage textCardMessage = setTextCardMessage(phoneNumber,callTime);
|
||||||
// 使用Jackson进行序列化
|
// 使用Jackson进行序列化
|
||||||
ObjectMapper objectMapper = new ObjectMapper();
|
ObjectMapper objectMapper = new ObjectMapper();
|
||||||
String jsonBody = null;
|
String jsonBody;
|
||||||
try {
|
try {
|
||||||
jsonBody = objectMapper.writeValueAsString(textCardMessage);
|
jsonBody = objectMapper.writeValueAsString(textCardMessage);
|
||||||
logger.info("jsonBody: {}",jsonBody);
|
logger.info("jsonBody: {}",jsonBody);
|
||||||
|
|
|
||||||
|
|
@ -0,0 +1,107 @@
|
||||||
|
package com.kimgo.wepush.service;
|
||||||
|
|
||||||
|
|
||||||
|
import com.fasterxml.jackson.core.JsonProcessingException;
|
||||||
|
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||||
|
import com.kimgo.wepush.model.TextMessage;
|
||||||
|
import com.kimgo.wepush.model.WeChatMsg;
|
||||||
|
import com.kimgo.wepush.response.QyWeChatSendMessageApiResponse;
|
||||||
|
import com.kimgo.wepush.response.ServerResponseEntity;
|
||||||
|
import okhttp3.*;
|
||||||
|
import org.slf4j.Logger;
|
||||||
|
import org.slf4j.LoggerFactory;
|
||||||
|
import org.springframework.stereotype.Service;
|
||||||
|
|
||||||
|
import java.io.IOException;
|
||||||
|
|
||||||
|
@Service
|
||||||
|
public class HandleWeChatMsgService {
|
||||||
|
private final TokenService tokenService;
|
||||||
|
private final ApiSettingService apiSettingService;
|
||||||
|
private final QyWeChatURLService qyWeChatURLService;
|
||||||
|
private final Logger logger = LoggerFactory.getLogger(HandleWeChatMsgService.class);
|
||||||
|
|
||||||
|
public HandleWeChatMsgService(TokenService tokenService, ApiSettingService apiSettingService, QyWeChatURLService qyWeChatURLService) {
|
||||||
|
this.tokenService = tokenService;
|
||||||
|
this.apiSettingService = apiSettingService;
|
||||||
|
this.qyWeChatURLService = qyWeChatURLService;
|
||||||
|
}
|
||||||
|
|
||||||
|
public ServerResponseEntity handleMsg(String accessToken, WeChatMsg weChatMsg){
|
||||||
|
String correctAccessToken = tokenService.getApiAccessToken();
|
||||||
|
logger.info("accessToken: {} correctAccessToken: {}",accessToken,correctAccessToken);
|
||||||
|
if (!correctAccessToken.equals(accessToken)){
|
||||||
|
return ServerResponseEntity.fail("Invalid accessToken");
|
||||||
|
}
|
||||||
|
String qyAccessToken = tokenService.getAccessToken();
|
||||||
|
QyWeChatSendMessageApiResponse qyWeChatSendMessageApiResponse = requestWithOkhttp(qyAccessToken,weChatMsg);
|
||||||
|
if (qyWeChatSendMessageApiResponse != null){
|
||||||
|
return ServerResponseEntity.success(qyWeChatSendMessageApiResponse.getMsgid());
|
||||||
|
}
|
||||||
|
return ServerResponseEntity.fail("request qyWeChat error,please check server error log.");
|
||||||
|
}
|
||||||
|
|
||||||
|
public TextMessage setTextMessage(WeChatMsg weChatMsg){
|
||||||
|
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 接受时间: " +weChatMsg.getCurrentTime() +
|
||||||
|
"\nsender: " + weChatMsg.getSender() +
|
||||||
|
"\ntitle: "+ weChatMsg.getTitle() +
|
||||||
|
"\n内容: \n"+ weChatMsg.getMessage());
|
||||||
|
textMessage.setText(text);
|
||||||
|
logger.info("TextMessage: {}", textMessage);
|
||||||
|
return textMessage;
|
||||||
|
}
|
||||||
|
|
||||||
|
private<T> Request bulidRequest(String accessToken, T object){
|
||||||
|
String url = qyWeChatURLService.getSendTextCardMessageUrl() + "?access_token=" + accessToken;
|
||||||
|
// 使用Jackson进行序列化
|
||||||
|
ObjectMapper objectMapper = new ObjectMapper();
|
||||||
|
String jsonBody;
|
||||||
|
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;
|
||||||
|
}
|
||||||
|
|
||||||
|
public QyWeChatSendMessageApiResponse requestWithOkhttp(String accessToken, WeChatMsg weChatMsg){
|
||||||
|
OkHttpClient client = new OkHttpClient();
|
||||||
|
TextMessage textMessage = setTextMessage(weChatMsg);
|
||||||
|
Request request = bulidRequest(accessToken,textMessage);
|
||||||
|
try (Response response = client.newCall(request).execute()) {
|
||||||
|
String responseBody = response.body().string();
|
||||||
|
logger.info("Response: {}", responseBody);
|
||||||
|
|
||||||
|
ObjectMapper objectMapper1 = new ObjectMapper();
|
||||||
|
QyWeChatSendMessageApiResponse apiResponse = objectMapper1.readValue(responseBody, QyWeChatSendMessageApiResponse.class);
|
||||||
|
|
||||||
|
if (apiResponse.getErrcode() == 0) {
|
||||||
|
return apiResponse;
|
||||||
|
} else if (apiResponse.getErrcode() == 42001 || apiResponse.getErrcode() == 40014) {
|
||||||
|
logger.info("Access token expired. Refreshing token...");
|
||||||
|
tokenService.setAccessToken(); // 一个方法来刷新accessToken
|
||||||
|
String newAccessToken = tokenService.getAccessToken();
|
||||||
|
return requestWithOkhttp(newAccessToken, weChatMsg);
|
||||||
|
} else {
|
||||||
|
// 处理其他错误情况
|
||||||
|
logger.error("Error: {}", apiResponse.getErrmsg());
|
||||||
|
return apiResponse;
|
||||||
|
}
|
||||||
|
} catch (IOException e) {
|
||||||
|
logger.error("OkHttp request error", e);
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,55 @@
|
||||||
|
package com.kimgo.wepush.model;
|
||||||
|
|
||||||
|
import org.junit.jupiter.api.Test;
|
||||||
|
import static org.junit.jupiter.api.Assertions.*;
|
||||||
|
|
||||||
|
class WeChatMsgTest {
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void isInvalid_AllFieldsValid() {
|
||||||
|
WeChatMsg weChatMsg = new WeChatMsg();
|
||||||
|
weChatMsg.setPackageName("com.example.package");
|
||||||
|
weChatMsg.setAppName("Example App");
|
||||||
|
weChatMsg.setTitle("Example Title");
|
||||||
|
weChatMsg.setSender("Example Sender");
|
||||||
|
weChatMsg.setMessage("Example Message");
|
||||||
|
|
||||||
|
assertFalse(weChatMsg.isInvalid(), "WeChatMsg should be valid when all fields are valid.");
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void isInvalid_OneFieldIsNull() {
|
||||||
|
WeChatMsg weChatMsg = new WeChatMsg();
|
||||||
|
weChatMsg.setPackageName(null); // 设置一个字段为 null
|
||||||
|
weChatMsg.setAppName("Example App");
|
||||||
|
weChatMsg.setTitle("Example Title");
|
||||||
|
weChatMsg.setSender("Example Sender");
|
||||||
|
weChatMsg.setMessage("Example Message");
|
||||||
|
|
||||||
|
assertTrue(weChatMsg.isInvalid(), "WeChatMsg should be invalid if any field is null.");
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void isInvalid_OneFieldIsEmpty() {
|
||||||
|
WeChatMsg weChatMsg = new WeChatMsg();
|
||||||
|
weChatMsg.setPackageName(""); // 设置一个字段为空字符串
|
||||||
|
weChatMsg.setAppName("Example App");
|
||||||
|
weChatMsg.setTitle("Example Title");
|
||||||
|
weChatMsg.setSender("Example Sender");
|
||||||
|
weChatMsg.setMessage("Example Message");
|
||||||
|
|
||||||
|
assertTrue(weChatMsg.isInvalid(), "WeChatMsg should be invalid if any field is an empty string.");
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void isInvalid_OneFieldIsStringNull() {
|
||||||
|
WeChatMsg weChatMsg = new WeChatMsg();
|
||||||
|
weChatMsg.setPackageName("null"); // 设置一个字段为字面字符串 "null"
|
||||||
|
weChatMsg.setAppName("Example App");
|
||||||
|
weChatMsg.setTitle("Example Title");
|
||||||
|
weChatMsg.setSender("Example Sender");
|
||||||
|
weChatMsg.setMessage("Example Message");
|
||||||
|
|
||||||
|
assertTrue(weChatMsg.isInvalid(), "WeChatMsg should be invalid if any field is the literal string \"null\".");
|
||||||
|
}
|
||||||
|
}
|
||||||
Loading…
Reference in New Issue