Prechádzať zdrojové kódy

新增发送客服消息

hsj 1 mesiac pred
rodič
commit
996d0c14f3

+ 2 - 0
src/main/java/top/husj/husj_wx/Application.java

@@ -3,9 +3,11 @@ package top.husj.husj_wx;
 import org.springframework.boot.SpringApplication;
 import org.springframework.boot.autoconfigure.SpringBootApplication;
 import org.springframework.context.annotation.Bean;
+import org.springframework.scheduling.annotation.EnableScheduling;
 import org.springframework.web.client.RestTemplate;
 
 @SpringBootApplication
+@EnableScheduling
 public class Application {
 
     public static void main(String[] args) {

+ 24 - 1
src/main/java/top/husj/husj_wx/config/AccessTokenConfig.java

@@ -7,6 +7,7 @@ import lombok.extern.slf4j.Slf4j;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.context.annotation.Bean;
 import org.springframework.context.annotation.Configuration;
+import org.springframework.scheduling.annotation.Scheduled;
 import org.springframework.web.client.RestTemplate;
 import top.husj.husj_wx.properties.WeChatProperties;
 import top.husj.husj_wx.utils.AccessTokenUtil;
@@ -21,7 +22,29 @@ public class AccessTokenConfig {
     private WeChatProperties weChatProperties;
 
     @PostConstruct
-    public void getAccessToken() throws MessagingException {
+    public void initAccessToken() throws MessagingException {
+        log.info("应用启动,正在获取初始access_token...");
+        refreshAccessToken();
+    }
+
+    /**
+     * 每小时自动刷新access_token
+     * fixedRate = 3600000 表示每3600000毫秒(1小时)执行一次
+     */
+    @Scheduled(fixedRate = 3600000)
+    public void scheduledRefreshAccessToken() {
+        log.info("定时任务触发,正在刷新access_token...");
+        try {
+            refreshAccessToken();
+        } catch (MessagingException e) {
+            log.error("定时刷新access_token失败: ", e);
+        }
+    }
+
+    /**
+     * 刷新access_token的核心逻辑
+     */
+    private void refreshAccessToken() throws MessagingException {
         String appId = weChatProperties.getAppid();
         String appSecret = weChatProperties.getAppsecret();
         try {

+ 27 - 2
src/main/java/top/husj/husj_wx/controller/WeChatController.java

@@ -4,10 +4,12 @@ import lombok.extern.slf4j.Slf4j;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.beans.factory.annotation.Value;
 import org.springframework.http.MediaType;
+import org.springframework.http.ResponseEntity;
 import org.springframework.web.bind.annotation.*;
 import top.husj.husj_wx.entity.model.WeChatMsg;
 import top.husj.husj_wx.exception.AesException;
 import top.husj.husj_wx.properties.WeChatProperties;
+import top.husj.husj_wx.service.WeChatService;
 import top.husj.husj_wx.utils.SHA1;
 
 @RestController
@@ -17,6 +19,9 @@ public class WeChatController {
     @Autowired
     private WeChatProperties weChatProperties;
 
+    @Autowired
+    private WeChatService weChatService;
+
 
     // 1. 微信后台验证 URL 时使用 (GET)
     @GetMapping("/callback")
@@ -47,6 +52,9 @@ public class WeChatController {
         log.info("收到微信服务器消息,{}", msg);
         log.info("收到来自用户 {} 的消息:{}", msg.getFromUserName(), msg.getContent());
 
+        // 记录最后一个发消息的用户
+        weChatService.recordLastUser(msg.getFromUserName());
+
         // 构造被动回复(同样是XML)
         return String.format(
                 "<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("消息发送失败,请检查日志");
+        }
     }
 }

+ 2 - 2
src/main/resources/application.yml

@@ -8,6 +8,6 @@ server:
 
 wechat:
   token: qwertyui
-  appid: wxb9510ec5ad7b4a5f
-  appsecret: 771278278f2cad8a32615c388a26e215
+  appid: wxebbc42cc41337ad6
+  appsecret: b0e5198cd442efe767f473f1445bd162
   encodingAESKey: TAuWfJHftr4CmGtLe8aHwkA9JVaEMCA3VoQi9VhLVpD