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 f84fba468b6836b8a1fc27a9ea9b59fd07f5f2ce..c97a92e535c14cbc06cb1d16e82a28baf58794a1 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 f3b71322d98286b946ebf2b4eb57da3a268dae74..a5f3dace52d39aed66df327b62858191408dd1e7 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 d665219c0e583af218fef66d18df0581fbae3827..7300e9b12e1f4826dab46f9c767796c2df52140e 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/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 0000000000000000000000000000000000000000..12462ff82a6743913175f3527e010fc93168c1b6 --- /dev/null +++ b/litemall-wx-api/src/main/java/org/linlinjava/litemall/wx/web/WxMsgController.java @@ -0,0 +1,84 @@ +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; +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; + +/** + * @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 { + wxMaService.getMsgService().sendKefuMsg(WxMaKefuMessage.newTextBuilder().content(wxMaMessage.getContent()).toUser(wxMaMessage.getFromUser()).build()); + } catch (WxErrorException e) { + logger.error("消息自动回复失败"); + } + } + 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; + } + +} diff --git a/pom.xml b/pom.xml index 9ad8a46a58b7c96c2bc7785753fc391bd662be9e..a6d1e9a0563e2838fd8b654f9775cea6e799f200 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