From 374f673641a324f66d977a7525f67fa1a775f532 Mon Sep 17 00:00:00 2001 From: liyang Date: Wed, 10 Nov 2021 17:27:24 +0800 Subject: [PATCH 1/3] =?UTF-8?q?1.=E5=BE=AE=E4=BF=A1=E9=80=80=E6=AC=BE?= =?UTF-8?q?=E5=8D=8F=E8=AE=AE=E9=97=AE=E9=A2=98=E8=A7=A3=E5=86=B3=202.?= =?UTF-8?q?=E6=96=B0=E5=A2=9E=E5=BE=AE=E4=BF=A1=E5=B0=8F=E7=A8=8B=E5=BA=8F?= =?UTF-8?q?=E6=B6=88=E6=81=AF=E6=8E=A8=E9=80=81=E9=85=8D=E7=BD=AE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../litemall/core/config/WxConfig.java | 6 ++++-- .../litemall/core/config/WxProperties.java | 20 +++++++++++++++++++ .../src/main/resources/application-core.yml | 4 ++++ pom.xml | 4 ++-- 4 files changed, 30 insertions(+), 4 deletions(-) diff --git a/litemall-core/src/main/java/org/linlinjava/litemall/core/config/WxConfig.java b/litemall-core/src/main/java/org/linlinjava/litemall/core/config/WxConfig.java index f84fba46..c97a92e5 100644 --- a/litemall-core/src/main/java/org/linlinjava/litemall/core/config/WxConfig.java +++ b/litemall-core/src/main/java/org/linlinjava/litemall/core/config/WxConfig.java @@ -3,7 +3,7 @@ package org.linlinjava.litemall.core.config; import cn.binarywang.wx.miniapp.api.WxMaService; import cn.binarywang.wx.miniapp.api.impl.WxMaServiceImpl; import cn.binarywang.wx.miniapp.config.WxMaConfig; -import cn.binarywang.wx.miniapp.config.WxMaInMemoryConfig; +import cn.binarywang.wx.miniapp.config.impl.WxMaDefaultConfigImpl; import com.github.binarywang.wxpay.config.WxPayConfig; import com.github.binarywang.wxpay.service.WxPayService; import com.github.binarywang.wxpay.service.impl.WxPayServiceImpl; @@ -18,9 +18,11 @@ public class WxConfig { @Bean public WxMaConfig wxMaConfig() { - WxMaInMemoryConfig config = new WxMaInMemoryConfig(); + WxMaDefaultConfigImpl config = new WxMaDefaultConfigImpl(); config.setAppid(properties.getAppId()); config.setSecret(properties.getAppSecret()); + config.setToken(properties.getToken()); + config.setAesKey(properties.getAesKey()); return config; } diff --git a/litemall-core/src/main/java/org/linlinjava/litemall/core/config/WxProperties.java b/litemall-core/src/main/java/org/linlinjava/litemall/core/config/WxProperties.java index f3b71322..a5f3dace 100644 --- a/litemall-core/src/main/java/org/linlinjava/litemall/core/config/WxProperties.java +++ b/litemall-core/src/main/java/org/linlinjava/litemall/core/config/WxProperties.java @@ -19,6 +19,26 @@ public class WxProperties { private String keyPath; + private String token; + + private String aesKey; + + public String getToken() { + return token; + } + + public void setToken(String token) { + this.token = token; + } + + public String getAesKey() { + return aesKey; + } + + public void setAesKey(String aesKey) { + this.aesKey = aesKey; + } + public String getNotifyUrl() { return notifyUrl; } diff --git a/litemall-core/src/main/resources/application-core.yml b/litemall-core/src/main/resources/application-core.yml index d665219c..7300e9b1 100644 --- a/litemall-core/src/main/resources/application-core.yml +++ b/litemall-core/src/main/resources/application-core.yml @@ -9,6 +9,10 @@ litemall: # 商户证书文件路径 # 请参考“商户证书”一节 https://pay.weixin.qq.com/wiki/doc/api/wxa/wxa_api.php?chapter=4_3 key-path: xxxxx + # 消息推送配置token + token: + # 消息推送配置AesKey + aes-key: #通知相关配置 notify: diff --git a/pom.xml b/pom.xml index 9ad8a46a..a6d1e9a0 100644 --- a/pom.xml +++ b/pom.xml @@ -98,13 +98,13 @@ com.github.binarywang weixin-java-pay - 3.3.0 + 4.1.0 com.github.binarywang weixin-java-miniapp - 3.3.0 + 4.1.0 -- Gitee From a2838dd8a23fe5367a8f7ba9d5213a827645574b Mon Sep 17 00:00:00 2001 From: liyang Date: Wed, 10 Nov 2021 17:34:02 +0800 Subject: [PATCH 2/3] =?UTF-8?q?1.=E6=B6=88=E6=81=AF=E6=8E=A8=E9=80=81?= =?UTF-8?q?=E9=85=8D=E7=BD=AE=EF=BC=8C=E6=99=BA=E8=83=BD=E5=9B=9E=E5=A4=8D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../litemall/wx/web/WxMsgController.java | 92 +++++++++++++++++++ 1 file changed, 92 insertions(+) create mode 100644 litemall-wx-api/src/main/java/org/linlinjava/litemall/wx/web/WxMsgController.java diff --git a/litemall-wx-api/src/main/java/org/linlinjava/litemall/wx/web/WxMsgController.java b/litemall-wx-api/src/main/java/org/linlinjava/litemall/wx/web/WxMsgController.java new file mode 100644 index 00000000..0c615673 --- /dev/null +++ b/litemall-wx-api/src/main/java/org/linlinjava/litemall/wx/web/WxMsgController.java @@ -0,0 +1,92 @@ +package org.linlinjava.litemall.wx.web; + +import cn.binarywang.wx.miniapp.api.WxMaService; +import cn.binarywang.wx.miniapp.bean.WxMaMessage; +import cn.binarywang.wx.miniapp.message.WxMaXmlOutMessage; +import me.chanjar.weixin.common.error.WxErrorException; +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.web.bind.annotation.*; + +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; +import java.io.IOException; +import java.util.HashMap; +import java.util.Map; + +/** + * @author liyang + * @Description 微信消息推送配置 + * @date 2021-11-10 17:32:25 + */ +@RestController +@RequestMapping(value = "/wx/msg") +public class WxMsgController { + + private final Log logger = LogFactory.getLog(WxMsgController.class); + + @Autowired + private WxMaService wxMaService; + + /** + * token校验 + * + * @param signature + * @param timestamp + * @param nonce + * @param echostr + * @return + */ + @GetMapping(value = "/config", produces = "text/plain;charset=utf-8") + public String config(@RequestParam(required = false) String signature, + @RequestParam(required = false) String timestamp, + @RequestParam(required = false) String nonce, + @RequestParam(required = false) String echostr) { + return !wxMaService.checkSignature(timestamp, nonce, signature) ? "fail" : echostr; + } + + /** + * 消息回复 + * + * @param request + * @param response + * @return + */ + @PostMapping(value = "/config", produces = "application/xml;charset=utf-8") + public String config(HttpServletRequest request, HttpServletResponse response) { + WxMaMessage wxMaMessage = null; + try { + wxMaMessage = WxMaMessage.fromXml(request.getInputStream()); + } catch (IOException e) { + e.printStackTrace(); + } + if (wxMaMessage != null) { + String msgType = wxMaMessage.getMsgType(); + if ("text".equals(msgType)) { + try { + Map msg = new HashMap<>(); + Map body = new HashMap<>(); + msg.put("access_token", wxMaService.getAccessToken()); + msg.put("touser", wxMaMessage.getFromUser()); + msg.put("msgtype", wxMaMessage.getMsgType()); + body.put("content", wxMaMessage.getContent()); + msg.put("text", body); + wxMaService.post("https://api.weixin.qq.com/cgi-bin/message/custom/send", msg); + } catch (WxErrorException e) { + e.printStackTrace(); + } + } + WxMaXmlOutMessage wxMaXmlOutMessage = new WxMaXmlOutMessage(); + wxMaXmlOutMessage.setMsgType("transfer_customer_service"); + wxMaXmlOutMessage.setToUserName(wxMaMessage.getFromUser()); + wxMaXmlOutMessage.setFromUserName(wxMaMessage.getToUser()); + wxMaXmlOutMessage.setCreateTime(wxMaMessage.getCreateTime().longValue()); + final String xml = wxMaXmlOutMessage.toXml(); + logger.info(xml); + return xml; + } + return null; + } + +} -- Gitee From f25c6a339a8fa1d8d08a8268859b5304dd8879e2 Mon Sep 17 00:00:00 2001 From: liyang Date: Thu, 11 Nov 2021 15:54:42 +0800 Subject: [PATCH 3/3] =?UTF-8?q?1.=E6=B6=88=E6=81=AF=E6=8E=A8=E9=80=81?= =?UTF-8?q?=E9=85=8D=E7=BD=AE=EF=BC=8C=E6=99=BA=E8=83=BD=E5=9B=9E=E5=A4=8D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../litemall/wx/web/WxMsgController.java | 14 +++----------- 1 file changed, 3 insertions(+), 11 deletions(-) diff --git a/litemall-wx-api/src/main/java/org/linlinjava/litemall/wx/web/WxMsgController.java b/litemall-wx-api/src/main/java/org/linlinjava/litemall/wx/web/WxMsgController.java index 0c615673..12462ff8 100644 --- a/litemall-wx-api/src/main/java/org/linlinjava/litemall/wx/web/WxMsgController.java +++ b/litemall-wx-api/src/main/java/org/linlinjava/litemall/wx/web/WxMsgController.java @@ -1,6 +1,7 @@ package org.linlinjava.litemall.wx.web; import cn.binarywang.wx.miniapp.api.WxMaService; +import cn.binarywang.wx.miniapp.bean.WxMaKefuMessage; import cn.binarywang.wx.miniapp.bean.WxMaMessage; import cn.binarywang.wx.miniapp.message.WxMaXmlOutMessage; import me.chanjar.weixin.common.error.WxErrorException; @@ -12,8 +13,6 @@ import org.springframework.web.bind.annotation.*; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import java.io.IOException; -import java.util.HashMap; -import java.util.Map; /** * @author liyang @@ -65,16 +64,9 @@ public class WxMsgController { String msgType = wxMaMessage.getMsgType(); if ("text".equals(msgType)) { try { - Map msg = new HashMap<>(); - Map body = new HashMap<>(); - msg.put("access_token", wxMaService.getAccessToken()); - msg.put("touser", wxMaMessage.getFromUser()); - msg.put("msgtype", wxMaMessage.getMsgType()); - body.put("content", wxMaMessage.getContent()); - msg.put("text", body); - wxMaService.post("https://api.weixin.qq.com/cgi-bin/message/custom/send", msg); + wxMaService.getMsgService().sendKefuMsg(WxMaKefuMessage.newTextBuilder().content(wxMaMessage.getContent()).toUser(wxMaMessage.getFromUser()).build()); } catch (WxErrorException e) { - e.printStackTrace(); + logger.error("消息自动回复失败"); } } WxMaXmlOutMessage wxMaXmlOutMessage = new WxMaXmlOutMessage(); -- Gitee