|
@@ -4,10 +4,12 @@ import lombok.extern.slf4j.Slf4j;
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
import org.springframework.beans.factory.annotation.Value;
|
|
import org.springframework.beans.factory.annotation.Value;
|
|
|
import org.springframework.http.MediaType;
|
|
import org.springframework.http.MediaType;
|
|
|
|
|
+import org.springframework.http.ResponseEntity;
|
|
|
import org.springframework.web.bind.annotation.*;
|
|
import org.springframework.web.bind.annotation.*;
|
|
|
import top.husj.husj_wx.entity.model.WeChatMsg;
|
|
import top.husj.husj_wx.entity.model.WeChatMsg;
|
|
|
import top.husj.husj_wx.exception.AesException;
|
|
import top.husj.husj_wx.exception.AesException;
|
|
|
import top.husj.husj_wx.properties.WeChatProperties;
|
|
import top.husj.husj_wx.properties.WeChatProperties;
|
|
|
|
|
+import top.husj.husj_wx.service.WeChatService;
|
|
|
import top.husj.husj_wx.utils.SHA1;
|
|
import top.husj.husj_wx.utils.SHA1;
|
|
|
|
|
|
|
|
@RestController
|
|
@RestController
|
|
@@ -17,6 +19,9 @@ public class WeChatController {
|
|
|
@Autowired
|
|
@Autowired
|
|
|
private WeChatProperties weChatProperties;
|
|
private WeChatProperties weChatProperties;
|
|
|
|
|
|
|
|
|
|
+ @Autowired
|
|
|
|
|
+ private WeChatService weChatService;
|
|
|
|
|
+
|
|
|
|
|
|
|
|
// 1. 微信后台验证 URL 时使用 (GET)
|
|
// 1. 微信后台验证 URL 时使用 (GET)
|
|
|
@GetMapping("/callback")
|
|
@GetMapping("/callback")
|
|
@@ -47,6 +52,9 @@ public class WeChatController {
|
|
|
log.info("收到微信服务器消息,{}", msg);
|
|
log.info("收到微信服务器消息,{}", msg);
|
|
|
log.info("收到来自用户 {} 的消息:{}", msg.getFromUserName(), msg.getContent());
|
|
log.info("收到来自用户 {} 的消息:{}", msg.getFromUserName(), msg.getContent());
|
|
|
|
|
|
|
|
|
|
+ // 记录最后一个发消息的用户
|
|
|
|
|
+ weChatService.recordLastUser(msg.getFromUserName());
|
|
|
|
|
+
|
|
|
// 构造被动回复(同样是XML)
|
|
// 构造被动回复(同样是XML)
|
|
|
return String.format(
|
|
return String.format(
|
|
|
"<xml>" +
|
|
"<xml>" +
|
|
@@ -60,7 +68,24 @@ public class WeChatController {
|
|
|
);
|
|
);
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- public static void main(String[] args) throws AesException {
|
|
|
|
|
- System.out.println(SHA1.getSHA1("12345", "1770626722", "324960664"));
|
|
|
|
|
|
|
+ // 3. 发送消息给最后一个活跃用户 (GET)
|
|
|
|
|
+ @GetMapping("/send")
|
|
|
|
|
+ public ResponseEntity<String> sendMessageToLastUser(@RequestParam("msg") String msg) {
|
|
|
|
|
+ log.info("收到发送消息请求,消息内容: {}", msg);
|
|
|
|
|
+
|
|
|
|
|
+ String lastUserOpenId = weChatService.getLastUserOpenId();
|
|
|
|
|
+ if (lastUserOpenId == null || lastUserOpenId.isEmpty()) {
|
|
|
|
|
+ log.warn("暂无活跃用户,无法发送消息");
|
|
|
|
|
+ return ResponseEntity.badRequest().body("暂无活跃用户,请先通过公众号发送一条消息");
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ boolean success = weChatService.sendMessageToLastUser(msg);
|
|
|
|
|
+ if (success) {
|
|
|
|
|
+ log.info("成功发送消息给最后活跃用户: {}", lastUserOpenId);
|
|
|
|
|
+ return ResponseEntity.ok("消息发送成功");
|
|
|
|
|
+ } else {
|
|
|
|
|
+ log.error("发送消息失败");
|
|
|
|
|
+ return ResponseEntity.status(500).body("消息发送失败,请检查日志");
|
|
|
|
|
+ }
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|