ソースを参照

!62 解决微信退款问题,增加微信小程序消息推送配置
Merge pull request !62 from 李阳/liyang

linlinjava 4 年 前
コミット
14bc821e5c

+ 4 - 2
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;
     }
 

+ 20 - 0
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;
     }

+ 4 - 0
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:

+ 84 - 0
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;
+    }
+
+}

+ 2 - 2
pom.xml

@@ -98,13 +98,13 @@
             <dependency>
                 <groupId>com.github.binarywang</groupId>
                 <artifactId>weixin-java-pay</artifactId>
-                <version>3.3.0</version>
+                <version>4.1.0</version>
             </dependency>
 
             <dependency>
                 <groupId>com.github.binarywang</groupId>
                 <artifactId>weixin-java-miniapp</artifactId>
-                <version>3.3.0</version>
+                <version>4.1.0</version>
             </dependency>
 
             <dependency>