Skip to content

Commit 53ce610

Browse files
committed
优化部分代码
1 parent 26ba117 commit 53ce610

File tree

4 files changed

+45
-18
lines changed

4 files changed

+45
-18
lines changed

pom.xml

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,4 +75,13 @@
7575
</dependency>
7676
</dependencies>
7777

78+
<build>
79+
<plugins>
80+
<plugin>
81+
<groupId>org.springframework.boot</groupId>
82+
<artifactId>spring-boot-maven-plugin</artifactId>
83+
</plugin>
84+
</plugins>
85+
</build>
86+
7887
</project>

src/main/java/com/github/binarywang/demo/wx/mp/controller/WxPortalController.java

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,20 @@
11
package com.github.binarywang.demo.wx.mp.controller;
22

3+
import org.apache.commons.lang3.StringUtils;
4+
import org.slf4j.Logger;
5+
import org.slf4j.LoggerFactory;
6+
import org.springframework.web.bind.annotation.GetMapping;
7+
import org.springframework.web.bind.annotation.PathVariable;
8+
import org.springframework.web.bind.annotation.PostMapping;
9+
import org.springframework.web.bind.annotation.RequestBody;
10+
import org.springframework.web.bind.annotation.RequestMapping;
11+
import org.springframework.web.bind.annotation.RequestParam;
12+
import org.springframework.web.bind.annotation.RestController;
13+
314
import com.github.binarywang.demo.wx.mp.config.WxMpConfiguration;
415
import me.chanjar.weixin.mp.api.WxMpService;
516
import me.chanjar.weixin.mp.bean.message.WxMpXmlMessage;
617
import me.chanjar.weixin.mp.bean.message.WxMpXmlOutMessage;
7-
import org.apache.commons.lang3.StringUtils;
8-
import org.slf4j.Logger;
9-
import org.slf4j.LoggerFactory;
10-
import org.springframework.web.bind.annotation.*;
1118

1219
/**
1320
* @author Binary Wang(https://github.com/binarywang)
@@ -23,15 +30,18 @@ public String authGet(@PathVariable String appid,
2330
@RequestParam(name = "timestamp", required = false) String timestamp,
2431
@RequestParam(name = "nonce", required = false) String nonce,
2532
@RequestParam(name = "echostr", required = false) String echostr) {
26-
final WxMpService wxService = WxMpConfiguration.getMpServices().get(appid);
2733

2834
this.logger.info("\n接收到来自微信服务器的认证消息:[{}, {}, {}, {}]", signature,
2935
timestamp, nonce, echostr);
30-
3136
if (StringUtils.isAnyBlank(signature, timestamp, nonce, echostr)) {
3237
throw new IllegalArgumentException("请求参数非法,请核实!");
3338
}
3439

40+
final WxMpService wxService = WxMpConfiguration.getMpServices().get(appid);
41+
if (wxService == null) {
42+
throw new IllegalArgumentException(String.format("未找到对应appid=[%d]的配置,请核实!", appid));
43+
}
44+
3545
if (wxService.checkSignature(timestamp, nonce, signature)) {
3646
return echostr;
3747
}
@@ -45,12 +55,13 @@ public String post(@PathVariable String appid,
4555
@RequestParam("signature") String signature,
4656
@RequestParam("timestamp") String timestamp,
4757
@RequestParam("nonce") String nonce,
58+
@RequestParam("openid") String openid,
4859
@RequestParam(name = "encrypt_type", required = false) String encType,
4960
@RequestParam(name = "msg_signature", required = false) String msgSignature) {
5061
final WxMpService wxService = WxMpConfiguration.getMpServices().get(appid);
51-
this.logger.info("\n接收微信请求:[signature=[{}], encType=[{}], msgSignature=[{}],"
62+
this.logger.info("\n接收微信请求:[openid=[{}], [signature=[{}], encType=[{}], msgSignature=[{}],"
5263
+ " timestamp=[{}], nonce=[{}], requestBody=[\n{}\n] ",
53-
signature, encType, msgSignature, timestamp, nonce, requestBody);
64+
openid, signature, encType, msgSignature, timestamp, nonce, requestBody);
5465

5566
if (!wxService.checkSignature(timestamp, nonce, signature)) {
5667
throw new IllegalArgumentException("非法请求,可能属于伪造的请求!");

src/main/java/com/github/binarywang/demo/wx/mp/handler/SubscribeHandler.java

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,16 @@
11
package com.github.binarywang.demo.wx.mp.handler;
22

3+
import java.util.Map;
4+
5+
import org.springframework.stereotype.Component;
6+
37
import com.github.binarywang.demo.wx.mp.builder.TextBuilder;
48
import me.chanjar.weixin.common.error.WxErrorException;
59
import me.chanjar.weixin.common.session.WxSessionManager;
610
import me.chanjar.weixin.mp.api.WxMpService;
711
import me.chanjar.weixin.mp.bean.message.WxMpXmlMessage;
812
import me.chanjar.weixin.mp.bean.message.WxMpXmlOutMessage;
913
import me.chanjar.weixin.mp.bean.result.WxMpUser;
10-
import org.springframework.stereotype.Component;
11-
12-
import java.util.Map;
1314

1415
/**
1516
* @author Binary Wang(https://github.com/binarywang)
@@ -25,16 +26,22 @@ public WxMpXmlOutMessage handle(WxMpXmlMessage wxMessage,
2526
this.logger.info("新关注用户 OPENID: " + wxMessage.getFromUser());
2627

2728
// 获取微信用户基本信息
28-
WxMpUser userWxInfo = weixinService.getUserService()
29-
.userInfo(wxMessage.getFromUser(), null);
30-
31-
if (userWxInfo != null) {
32-
// TODO 可以添加关注用户到本地
29+
try {
30+
WxMpUser userWxInfo = weixinService.getUserService()
31+
.userInfo(wxMessage.getFromUser(), null);
32+
if (userWxInfo != null) {
33+
// TODO 可以添加关注用户到本地数据库
34+
}
35+
} catch (WxErrorException e) {
36+
if (e.getError().getErrorCode() == 48001) {
37+
this.logger.info("该公众号没有获取用户信息权限!");
38+
}
3339
}
3440

41+
3542
WxMpXmlOutMessage responseResult = null;
3643
try {
37-
responseResult = handleSpecial(wxMessage);
44+
responseResult = this.handleSpecial(wxMessage);
3845
} catch (Exception e) {
3946
this.logger.error(e.getMessage(), e);
4047
}

src/main/resources/META-INF/additional-spring-configuration-metadata.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
{
44
"name": "wx.mp.configs",
55
"type": "java.util.List",
6-
"description": "Description for wx.mp.configs. https://github.com/ettingshausen/weixin-java-mp-demo-springboot/blob/master/README.md."
6+
"description": "Description for wx.mp.configs."
77
}
88
]
99
}

0 commit comments

Comments
 (0)