孙永军 il y a 2 ans
Parent
commit
9f32e83da8

+ 24 - 0
src/main/java/com/sxtvs/open/api/clue/dto/WxReply.java

@@ -0,0 +1,24 @@
+package com.sxtvs.open.api.clue.dto;
+
+import com.fasterxml.jackson.annotation.JsonProperty;
+import lombok.Data;
+import lombok.NoArgsConstructor;
+
+@NoArgsConstructor
+@Data
+public class WxReply {
+
+    @JsonProperty("touser")
+    private String touser;
+    @JsonProperty("msgtype")
+    private String msgtype;
+    @JsonProperty("text")
+    private TextDTO text;
+
+    @NoArgsConstructor
+    @Data
+    public static class TextDTO {
+        @JsonProperty("content")
+        private String content;
+    }
+}

+ 17 - 0
src/main/java/com/sxtvs/open/api/clue/job/WxTokenRefresh.java

@@ -0,0 +1,17 @@
+package com.sxtvs.open.api.clue.job;
+
+import com.sxtvs.open.api.clue.service.impl.WxTokenCache;
+import jakarta.annotation.Resource;
+import org.springframework.scheduling.annotation.Scheduled;
+import org.springframework.stereotype.Component;
+
+import java.util.concurrent.TimeUnit;
+
+@Component
+public class WxTokenRefresh {
+
+    @Scheduled(fixedDelay = 100, timeUnit = TimeUnit.MINUTES)
+    public void refresh() {
+        WxTokenCache.getInstance().setAccessToken();
+    }
+}

+ 28 - 0
src/main/java/com/sxtvs/open/api/clue/service/impl/ClueServiceImpl.java

@@ -1,13 +1,19 @@
 package com.sxtvs.open.api.clue.service.impl;
 
 import cn.hutool.core.bean.BeanUtil;
+import cn.hutool.http.HttpRequest;
+import cn.hutool.http.HttpResponse;
+import cn.hutool.http.HttpUtil;
 import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
 import com.baomidou.mybatisplus.core.metadata.IPage;
 import com.baomidou.mybatisplus.core.toolkit.Wrappers;
 import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
 import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
+import com.fasterxml.jackson.core.JsonProcessingException;
+import com.fasterxml.jackson.databind.ObjectMapper;
 import com.sxtvs.open.api.clue.dto.ClueDTO;
 import com.sxtvs.open.api.clue.dto.VerifyDTO;
+import com.sxtvs.open.api.clue.dto.WxReply;
 import com.sxtvs.open.api.clue.entity.*;
 import com.sxtvs.open.api.clue.mapper.ClueMapper;
 import com.sxtvs.open.api.clue.service.IClueService;
@@ -45,6 +51,9 @@ public class ClueServiceImpl extends ServiceImpl<ClueMapper, Clue> implements IC
     @Resource
     private ClueInfoServiceImpl clueInfoService;
 
+    @Resource
+    private ObjectMapper objectMapper;
+
     public IPage<Clue> cluePage(ClueDTO clueDTO){
 
         LambdaQueryWrapper<Clue> wrapper = Wrappers.lambdaQuery();
@@ -98,6 +107,25 @@ public class ClueServiceImpl extends ServiceImpl<ClueMapper, Clue> implements IC
         clueReply.setId(null);
         clueReply.setReplyTime(LocalDateTime.now());
         clueReply.setModifyTime(LocalDateTime.now());
+
+        //公众号
+        if (clue.getSource() == 2){
+            WxReply wxReply = new WxReply();
+            wxReply.setMsgtype("text");
+            WxReply.TextDTO textDTO = new WxReply.TextDTO();
+            textDTO.setContent(clueReply.getContent());
+            wxReply.setText(textDTO);
+            wxReply.setTouser(clue.getAuthor());
+            try {
+                HttpResponse resp = HttpRequest.post("https://api.weixin.qq.com/cgi-bin/message/custom/send?access_token=" + WxTokenCache.getInstance().getAccessToken())
+                        .body(objectMapper.writeValueAsString(wxReply)).contentType("application/json;charset=UTF-8").execute();
+                System.out.println(resp.body());
+            } catch (JsonProcessingException e) {
+                e.printStackTrace();
+            }
+
+        }
+
         clueReplyService.save(clueReply);
     }
 

+ 40 - 0
src/main/java/com/sxtvs/open/api/clue/service/impl/WxTokenCache.java

@@ -0,0 +1,40 @@
+package com.sxtvs.open.api.clue.service.impl;
+
+import cn.hutool.http.HttpUtil;
+import com.fasterxml.jackson.core.JsonProcessingException;
+import com.fasterxml.jackson.databind.ObjectMapper;
+
+import static com.sxtvs.open.core.conf.Constant.weixinGZHAppid;
+import static com.sxtvs.open.core.conf.Constant.weixinGZHSecret;
+
+public class WxTokenCache {
+    private String accessToken;
+
+    private static WxTokenCache wxTokenCache;
+
+    private WxTokenCache(){
+
+    }
+
+    public static WxTokenCache getInstance(){
+        if(wxTokenCache == null){
+            wxTokenCache = new WxTokenCache();
+            wxTokenCache.setAccessToken();
+        }
+        return wxTokenCache;
+    }
+
+    public void setAccessToken(){
+        String response = HttpUtil.get("https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=" + weixinGZHAppid + "&secret=" + weixinGZHSecret);
+        ObjectMapper objectMapper = new ObjectMapper();
+        try {
+            this.accessToken = objectMapper.readTree(response).get("access_token").asText();
+        } catch (JsonProcessingException e) {
+            e.printStackTrace();
+        }
+    }
+
+    public String getAccessToken() {
+        return accessToken;
+    }
+}

+ 2 - 3
src/main/java/com/sxtvs/open/api/odata/service/WxRestService.java

@@ -25,8 +25,7 @@ import org.springframework.web.client.RestTemplate;
 
 import java.util.Date;
 
-import static com.sxtvs.open.core.conf.Constant.weixinZhiHuiRongMeiAppId;
-import static com.sxtvs.open.core.conf.Constant.weixinZhiHuiRongMeiSecret;
+import static com.sxtvs.open.core.conf.Constant.*;
 
 @Service
 @Slf4j
@@ -259,7 +258,7 @@ public class WxRestService {
     }
 
     public static void main(String[] args) throws JsonProcessingException {
-        String response = HttpUtil.get("https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=" + weixinZhiHuiRongMeiAppId + "&secret=" + weixinZhiHuiRongMeiSecret);
+        String response = HttpUtil.get("https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=" + weixinGZHAppid + "&secret=" + weixinGZHSecret);
         ObjectMapper objectMapper = new ObjectMapper();
         String access_token = objectMapper.readTree(response).get("access_token").asText();
         System.out.println(access_token);

+ 3 - 0
src/main/java/com/sxtvs/open/core/conf/Constant.java

@@ -36,6 +36,9 @@ public class Constant {
     public static String weixinZhiHuiRongMeiAppId = "wx7040933fd0e4b0e8";
     public static String weixinZhiHuiRongMeiSecret = "934c2b93115ae6ec8472cf153e584124";
 
+    public static String weixinGZHAppid = "wx5e1699e1707030ac";
+    public static String weixinGZHSecret = "65c4f421f2bba4cd5d7f1219123e0fd9";
+
     public static String bilibiliClientId = "e79e6fdef9d446d7";
     public static String bilibiliSecret = "70d40a332aa54132a72239efba022450";
 

Fichier diff supprimé car celui-ci est trop grand
+ 65 - 21
src/test/data-service.http


Certains fichiers n'ont pas été affichés car il y a eu trop de fichiers modifiés dans ce diff