Skip to content

Commit 72e5837

Browse files
committed
增加全网发布测试用例,完善readme
1 parent 33e3f21 commit 72e5837

File tree

3 files changed

+36
-4
lines changed

3 files changed

+36
-4
lines changed

.gitignore

+2
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
#mac
2+
.DS_Store
13
# https://github.com/github/gitignore/blob/master/Java.gitignore
24

35
*.class

README.MD

+3-2
Original file line numberDiff line numberDiff line change
@@ -9,5 +9,6 @@
99
1. 配置:修改`/src/main/resources/application.yml` ,根据自己需要填写相关配置(需要注意的是:yml文件内的属性冒号后面的文字之前需要加空格,可参考已有配置,否则属性会设置不成功);
1010
1. 运行Java程序:`com.github.binarywang.demo.wechat.WxOpenDemoApplication`
1111
1. 配置微信第三方平台中的授权事件接收URL:http://my-domain/notify/receive_ticket
12-
1. 配置微信第三方平台中的公众号消息与事件接收URL http://my-domain/api/notify/$APPID$/callback
13-
12+
1. 配置微信第三方平台中的公众号消息与事件接收URL http://my-domain/notify/$APPID$/callback
13+
1. 首次启动后需要 等待收到 微信推送的 component_verify_ticket 后才可以使用接口 (在第三方平台创建审核通过后,微信服务器每隔10分钟会向第三方的消息接收地址推送一次component_verify_ticket,用于获取第三方平台接口调用凭据)
14+
1. 浏览器访问:http://my-domain/api/auth/goto_auth_url_show 点击 go 跳转到微信授权页面 扫码授权

src/main/java/com/github/binarywang/demo/wechat/controller/WechatNotifyController.java

+31-2
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,16 @@
22

33
import com.github.binarywang.demo.wechat.service.WxOpenServiceDemo;
44
import me.chanjar.weixin.common.exception.WxErrorException;
5+
import me.chanjar.weixin.mp.bean.kefu.WxMpKefuMessage;
56
import me.chanjar.weixin.mp.bean.message.WxMpXmlMessage;
7+
import me.chanjar.weixin.mp.bean.message.WxMpXmlOutMessage;
68
import me.chanjar.weixin.open.bean.message.WxOpenXmlMessage;
79
import org.apache.commons.lang3.StringUtils;
810
import org.slf4j.Logger;
911
import org.slf4j.LoggerFactory;
1012
import org.springframework.beans.factory.annotation.Autowired;
1113
import org.springframework.web.bind.annotation.*;
14+
1215
/**
1316
* @author <a href="https://github.com/007gzs">007</a>
1417
*/
@@ -18,6 +21,7 @@ public class WechatNotifyController {
1821
private final Logger logger = LoggerFactory.getLogger(this.getClass());
1922
@Autowired
2023
protected WxOpenServiceDemo wxOpenService;
24+
2125
@RequestMapping("receive_ticket")
2226
public Object receiveTicket(@RequestBody(required = false) String requestBody, @RequestParam("timestamp") String timestamp,
2327
@RequestParam("nonce") String nonce, @RequestParam("signature") String signature,
@@ -46,8 +50,9 @@ public Object receiveTicket(@RequestBody(required = false) String requestBody, @
4650

4751
return out;
4852
}
53+
4954
@RequestMapping("{appId}/callback")
50-
public Object callback(@RequestBody(required = false)String requestBody,
55+
public Object callback(@RequestBody(required = false) String requestBody,
5156
@PathVariable("appId") String appId,
5257
@RequestParam("signature") String signature,
5358
@RequestParam("timestamp") String timestamp,
@@ -67,7 +72,31 @@ public Object callback(@RequestBody(required = false)String requestBody,
6772
// aes加密的消息
6873
WxMpXmlMessage inMessage = WxOpenXmlMessage.fromEncryptedMpXml(requestBody, wxOpenService.getWxOpenConfigStorage(), timestamp, nonce, msgSignature);
6974
this.logger.debug("\n消息解密后内容为:\n{} ", inMessage.toString());
70-
//wxOpenService.getWxOpenComponentService().getWxMpServiceByAppid(appId);
75+
// 全网发布测试用例
76+
if (StringUtils.equalsAnyIgnoreCase(appId, "wxd101a85aa106f53e", "wx570bc396a51b8ff8")) {
77+
try {
78+
if (StringUtils.equals(inMessage.getMsgType(), "text")) {
79+
if (StringUtils.equals(inMessage.getContent(), "TESTCOMPONENT_MSG_TYPE_TEXT")) {
80+
out = WxOpenXmlMessage.wxMpOutXmlMessageToEncryptedXml(
81+
WxMpXmlOutMessage.TEXT().content("TESTCOMPONENT_MSG_TYPE_TEXT_callback")
82+
.fromUser(inMessage.getToUser())
83+
.toUser(inMessage.getFromUser())
84+
.build(),
85+
wxOpenService.getWxOpenConfigStorage()
86+
);
87+
} else if (StringUtils.startsWith(inMessage.getContent(), "QUERY_AUTH_CODE:")) {
88+
String msg = inMessage.getContent().replace("QUERY_AUTH_CODE:", "") + "_from_api";
89+
WxMpKefuMessage kefuMessage = WxMpKefuMessage.TEXT().content(msg).toUser(inMessage.getFromUser()).build();
90+
wxOpenService.getWxOpenComponentService().getWxMpServiceByAppid(appId).getKefuService().sendKefuMessage(kefuMessage);
91+
}
92+
} else if (StringUtils.equals(inMessage.getMsgType(), "event")) {
93+
WxMpKefuMessage kefuMessage = WxMpKefuMessage.TEXT().content(inMessage.getEvent() + "from_callback").toUser(inMessage.getFromUser()).build();
94+
wxOpenService.getWxOpenComponentService().getWxMpServiceByAppid(appId).getKefuService().sendKefuMessage(kefuMessage);
95+
}
96+
} catch (WxErrorException e) {
97+
logger.error("callback", e);
98+
}
99+
}
71100
return out;
72101
}
73102
}

0 commit comments

Comments
 (0)