Pārlūkot izejas kodu

微信公众号模版消息测试

skyline 2 gadi atpakaļ
vecāks
revīzija
48164de4b7

+ 39 - 0
miniapp/src/main/java/com/kym/miniapp/config/WxMpConfig.java

@@ -0,0 +1,39 @@
+package com.kym.miniapp.config;
+
+import lombok.Data;
+import me.chanjar.weixin.mp.api.WxMpService;
+import me.chanjar.weixin.mp.api.impl.WxMpServiceImpl;
+import me.chanjar.weixin.mp.config.impl.WxMpDefaultConfigImpl;
+import org.springframework.boot.context.properties.ConfigurationProperties;
+import org.springframework.context.annotation.Bean;
+import org.springframework.context.annotation.Configuration;
+
+/**
+ * @author skyline
+ * @description 微信
+ * @date 2023-07-22 23:09
+ */
+@Data
+@Configuration
+@ConfigurationProperties(prefix = "wechat.mp")
+public class WxMpConfig {
+    private String appid;
+    private String secret;
+    private String token;
+    private String aeskey;
+
+
+    @Bean
+    public WxMpService wxMpService() {
+        WxMpService service = new WxMpServiceImpl();
+        WxMpDefaultConfigImpl configStorage = new WxMpDefaultConfigImpl();
+
+        configStorage.setAppId(appid);
+        configStorage.setSecret(secret);
+        configStorage.setToken(token);
+        configStorage.setAesKey("aeskey");
+        service.addConfigStorage("MP", configStorage);
+        return service;
+    }
+
+}

+ 57 - 43
miniapp/src/main/java/com/kym/miniapp/controller/WeixinController.java

@@ -1,65 +1,27 @@
 package com.kym.miniapp.controller;
 
 
-import cn.hutool.core.io.IoUtil;
 import com.kym.common.annotation.SysLog;
-import com.kym.common.utils.CommUtil;
 import com.kym.common.utils.wx.WxPbUtil;
-import jakarta.servlet.http.HttpServletRequest;
+import jakarta.annotation.Resource;
 import jakarta.servlet.http.HttpServletResponse;
+import me.chanjar.weixin.common.error.WxErrorException;
+import me.chanjar.weixin.mp.api.WxMpService;
+import me.chanjar.weixin.mp.bean.template.WxMpTemplateData;
+import me.chanjar.weixin.mp.bean.template.WxMpTemplateMessage;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
-import org.springframework.http.MediaType;
 import org.springframework.web.bind.annotation.GetMapping;
-import org.springframework.web.bind.annotation.PostMapping;
 import org.springframework.web.bind.annotation.RequestMapping;
 import org.springframework.web.bind.annotation.RestController;
 
-import java.io.IOException;
 import java.io.PrintWriter;
-import java.util.Map;
 
 
 @RestController
 @RequestMapping("wx")
 public class WeixinController {
-    private Logger logger = LoggerFactory.getLogger(WeixinController.class);
-
-
     /**
-     * 微信服务器token验证
-     */
-    @GetMapping(value = "check")
-    @SysLog("微信服务器token验证")
-    public void checkSign(HttpServletResponse response, String signature, String timestamp, String nonce, String echostr) {
-        String responseVal = echostr;
-        try {
-
-            boolean resp = WxPbUtil.checkSign(signature, timestamp, nonce);
-            if (!resp) {
-                responseVal = "failure";
-                logger.error("wxpb check sign ERR!!!!");
-            }
-        } catch (Exception e) {
-            responseVal = "failure";
-        } finally {
-            try {
-                PrintWriter writer = response.getWriter();
-                writer.print(responseVal);
-                writer.flush();
-                writer.close();
-            } catch (Exception e) {
-                logger.error(e.getMessage(), e);
-            }
-        }
-    }
-
-
-/*
-
-
-    */
-/**
      * 消息通知(订阅、用户消息)
      *
      * @param request
@@ -92,5 +54,57 @@ public class WeixinController {
     }
 */
 
+    @Resource
+    WxMpService wxMpService;
+    private Logger logger = LoggerFactory.getLogger(WeixinController.class);
+
+
+    /*
+
+
+     */
+
+    /**
+     * 微信服务器token验证
+     */
+    @GetMapping(value = "check")
+    @SysLog("微信服务器token验证")
+    public void checkSign(HttpServletResponse response, String signature, String timestamp, String nonce, String echostr) {
+        String responseVal = echostr;
+        try {
+
+            boolean resp = WxPbUtil.checkSign(signature, timestamp, nonce);
+            if (!resp) {
+                responseVal = "failure";
+                logger.error("wxpb check sign ERR!!!!");
+            }
+        } catch (Exception e) {
+            responseVal = "failure";
+        } finally {
+            try {
+                PrintWriter writer = response.getWriter();
+                writer.print(responseVal);
+                writer.flush();
+                writer.close();
+            } catch (Exception e) {
+                logger.error(e.getMessage(), e);
+            }
+        }
+    }
+
+    @GetMapping(value = "/test")
+    public void test() throws WxErrorException {
+        WxMpTemplateMessage templateMessage = WxMpTemplateMessage.builder()
+                .toUser("oK-oR5gAn9hrYNim4x64l4kzmMXs")
+                .templateId("SjszYYiHcN-GaEeZTKJsCcB6ACxYJWfTddkSywf1q1g")
+                .url("")
+                .build();
+        templateMessage.addData(new WxMpTemplateData("character_string2.DATA", "000001"));
+        templateMessage.addData(new WxMpTemplateData("thing9.DATA}", "测试站点"));
+        templateMessage.addData(new WxMpTemplateData("character_string3.DATA", "编号001"));
+        templateMessage.addData(new WxMpTemplateData("time4.DATA", "2022-12-01 19:30:00"));
+        wxMpService.getTemplateMsgService().sendTemplateMsg(templateMessage);
+
+    }
 
 }

+ 6 - 0
miniapp/src/main/resources/application-dev.yml

@@ -48,6 +48,12 @@ wechat:
     userTitle: https://api.mch.weixin.qq.com/v3/new-tax-control-fapiao/user-title?scene=WITHOUT_WECHATPAY&fapiao_apply_id=%s
     cardTemplate: https://api.mch.weixin.qq.com/v3/new-tax-control-fapiao/card-template
 
+  mp:
+    appid: wx93b0ef1be901bd19
+    secret: eea715b3058717f44e17c3e4e5cf1d2f
+    token: kym
+    aeskey: U1ZC5gRrY4DDLZeyKxwpvU5Q7lQvvnQdOV0aX0UPpn6
+
 spring:
   datasource:
     druid: #以下是全局默认值,可以全局更改

+ 6 - 0
service/pom.xml

@@ -37,6 +37,12 @@
             <version>0.2.11</version>
         </dependency>
 
+        <dependency>
+            <groupId>com.github.binarywang</groupId>
+            <artifactId>weixin-java-mp</artifactId>
+            <version>4.5.0</version>
+        </dependency>
+
     </dependencies>
 
 </project>