50 lines
2.1 KiB
Java
50 lines
2.1 KiB
Java
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;
|
||
}
|
||
|
||
/**
|
||
* 接收微信消息。
|
||
*
|
||
* @param accessToken 访问令牌,用于验证请求的合法性。该参数通过请求头传递。
|
||
* @param weChatMsg 微信消息体,包含消息的各种内容。该参数通过请求体传递,并需通过验证。
|
||
* @return 返回处理结果,如果处理成功,返回成功状态和处理信息;如果失败,返回失败状态和错误信息。
|
||
*/
|
||
@PostMapping("/wechat")
|
||
public ServerResponseEntity receiveMsg(@RequestHeader("accessToken") String accessToken,
|
||
@RequestBody @Valid WeChatMsg weChatMsg){
|
||
logger.info("weChatMsg: {}",weChatMsg.toString());
|
||
// 检查accessToken是否为空,为空则返回错误信息
|
||
if(accessToken == null){
|
||
logger.warn("Access token is missing or empty");
|
||
return ServerResponseEntity.fail("accessToken cannot be empty.");
|
||
}
|
||
// 检查weChatMsg消息体是否有效,无效则返回错误信息
|
||
if (weChatMsg.isInvalid()) {
|
||
logger.debug("CallInfo has invalid fields.");
|
||
return ServerResponseEntity.fail("json body value error.");
|
||
}
|
||
// 调用服务处理微信消息
|
||
return handleWeChatMsgService.handleMsg(accessToken,weChatMsg);
|
||
}
|
||
}
|