Skip to content

Commit 608348d

Browse files
committed
update sdk to 3.3.7.B
1 parent d28f4af commit 608348d

File tree

6 files changed

+88
-83
lines changed

6 files changed

+88
-83
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,4 @@
55
application.yml
66
/.idea/
77
*.iml
8+
/ngrok/

pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
<description>Spring Boot Demo with wechat MP</description>
1919

2020
<properties>
21-
<weixin-java-mp.version>3.3.0</weixin-java-mp.version>
21+
<weixin-java-mp.version>3.3.7.B</weixin-java-mp.version>
2222

2323
<maven.compiler.source>1.8</maven.compiler.source>
2424
<maven.compiler.target>1.8</maven.compiler.target>

src/main/java/com/github/binarywang/demo/wx/mp/config/WxMpConfiguration.java

Lines changed: 24 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,20 @@
11
package com.github.binarywang.demo.wx.mp.config;
22

3-
import java.util.List;
4-
import java.util.Map;
5-
import java.util.stream.Collectors;
6-
import javax.annotation.PostConstruct;
7-
8-
import org.springframework.beans.factory.annotation.Autowired;
9-
import org.springframework.boot.context.properties.EnableConfigurationProperties;
10-
import org.springframework.context.annotation.Configuration;
11-
12-
import com.github.binarywang.demo.wx.mp.handler.KfSessionHandler;
13-
import com.github.binarywang.demo.wx.mp.handler.LocationHandler;
14-
import com.github.binarywang.demo.wx.mp.handler.LogHandler;
15-
import com.github.binarywang.demo.wx.mp.handler.MenuHandler;
16-
import com.github.binarywang.demo.wx.mp.handler.MsgHandler;
17-
import com.github.binarywang.demo.wx.mp.handler.NullHandler;
18-
import com.github.binarywang.demo.wx.mp.handler.ScanHandler;
19-
import com.github.binarywang.demo.wx.mp.handler.StoreCheckNotifyHandler;
20-
import com.github.binarywang.demo.wx.mp.handler.SubscribeHandler;
21-
import com.github.binarywang.demo.wx.mp.handler.UnsubscribeHandler;
3+
import com.github.binarywang.demo.wx.mp.handler.*;
224
import com.google.common.collect.Maps;
235
import me.chanjar.weixin.mp.api.WxMpInMemoryConfigStorage;
246
import me.chanjar.weixin.mp.api.WxMpMessageRouter;
257
import me.chanjar.weixin.mp.api.WxMpService;
268
import me.chanjar.weixin.mp.api.impl.WxMpServiceImpl;
279
import me.chanjar.weixin.mp.constant.WxMpEventConstants;
10+
import org.springframework.beans.factory.annotation.Autowired;
11+
import org.springframework.boot.context.properties.EnableConfigurationProperties;
12+
import org.springframework.context.annotation.Bean;
13+
import org.springframework.context.annotation.Configuration;
14+
15+
import java.util.List;
16+
import java.util.Map;
17+
import java.util.stream.Collectors;
2818

2919
import static me.chanjar.weixin.common.api.WxConsts.*;
3020

@@ -70,37 +60,29 @@ public WxMpConfiguration(LogHandler logHandler, NullHandler nullHandler, KfSessi
7060
this.properties = properties;
7161
}
7262

73-
public static Map<String, WxMpMessageRouter> getRouters() {
74-
return routers;
75-
}
76-
77-
public static Map<String, WxMpService> getMpServices() {
78-
return mpServices;
79-
}
80-
81-
@PostConstruct
82-
public void initServices() {
63+
@Bean
64+
public WxMpService wxMpService() {
8365
// 代码里 getConfigs()处报错的同学,请注意仔细阅读项目说明,你的IDE需要引入lombok插件!!!!
8466
final List<WxMpProperties.MpConfig> configs = this.properties.getConfigs();
8567
if (configs == null) {
8668
throw new RuntimeException("大哥,拜托先看下项目首页的说明(readme文件),添加下相关配置,注意别配错了!");
8769
}
8870

89-
mpServices = configs.stream().map(a -> {
90-
WxMpInMemoryConfigStorage configStorage = new WxMpInMemoryConfigStorage();
91-
configStorage.setAppId(a.getAppId());
92-
configStorage.setSecret(a.getSecret());
93-
configStorage.setToken(a.getToken());
94-
configStorage.setAesKey(a.getAesKey());
95-
96-
WxMpService service = new WxMpServiceImpl();
97-
service.setWxMpConfigStorage(configStorage);
98-
routers.put(a.getAppId(), this.newRouter(service));
99-
return service;
100-
}).collect(Collectors.toMap(s -> s.getWxMpConfigStorage().getAppId(), a -> a, (o, n) -> o));
71+
WxMpService service = new WxMpServiceImpl();
72+
service.setMultiConfigStorages(configs
73+
.stream().map(a -> {
74+
WxMpInMemoryConfigStorage configStorage = new WxMpInMemoryConfigStorage();
75+
configStorage.setAppId(a.getAppId());
76+
configStorage.setSecret(a.getSecret());
77+
configStorage.setToken(a.getToken());
78+
configStorage.setAesKey(a.getAesKey());
79+
return configStorage;
80+
}).collect(Collectors.toMap(WxMpInMemoryConfigStorage::getAppId, a -> a, (o, n) -> o)));
81+
return service;
10182
}
10283

103-
private WxMpMessageRouter newRouter(WxMpService wxMpService) {
84+
@Bean
85+
public WxMpMessageRouter messageRouter(WxMpService wxMpService) {
10486
final WxMpMessageRouter newRouter = new WxMpMessageRouter(wxMpService);
10587

10688
// 记录所有事件的日志 (异步执行)

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

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

3-
import java.net.MalformedURLException;
4-
import java.net.URL;
5-
import javax.servlet.http.HttpServletRequest;
6-
7-
import org.springframework.web.bind.annotation.GetMapping;
8-
import org.springframework.web.bind.annotation.PathVariable;
9-
import org.springframework.web.bind.annotation.PostMapping;
10-
import org.springframework.web.bind.annotation.RequestBody;
11-
import org.springframework.web.bind.annotation.RequestMapping;
12-
import org.springframework.web.bind.annotation.RestController;
13-
import org.springframework.web.context.request.RequestContextHolder;
14-
import org.springframework.web.context.request.ServletRequestAttributes;
15-
16-
import com.github.binarywang.demo.wx.mp.config.WxMpConfiguration;
173
import me.chanjar.weixin.common.api.WxConsts;
184
import me.chanjar.weixin.common.bean.menu.WxMenu;
195
import me.chanjar.weixin.common.bean.menu.WxMenuButton;
206
import me.chanjar.weixin.common.error.WxErrorException;
7+
import me.chanjar.weixin.mp.api.WxMpService;
218
import me.chanjar.weixin.mp.bean.menu.WxMpGetSelfMenuInfoResult;
229
import me.chanjar.weixin.mp.bean.menu.WxMpMenu;
10+
import org.springframework.beans.factory.annotation.Autowired;
11+
import org.springframework.web.bind.annotation.*;
12+
import org.springframework.web.context.request.RequestContextHolder;
13+
import org.springframework.web.context.request.ServletRequestAttributes;
14+
15+
import javax.servlet.http.HttpServletRequest;
16+
import java.net.MalformedURLException;
17+
import java.net.URL;
2318

2419
import static me.chanjar.weixin.common.api.WxConsts.MenuButtonType;
2520

@@ -29,6 +24,12 @@
2924
@RestController
3025
@RequestMapping("/wx/menu/{appid}")
3126
public class WxMenuController {
27+
private WxMpService wxService;
28+
29+
@Autowired
30+
public WxMenuController(WxMpService wxService) {
31+
this.wxService = wxService;
32+
}
3233

3334
/**
3435
* <pre>
@@ -42,7 +43,7 @@ public class WxMenuController {
4243
*/
4344
@PostMapping("/create")
4445
public String menuCreate(@PathVariable String appid, @RequestBody WxMenu menu) throws WxErrorException {
45-
return WxMpConfiguration.getMpServices().get(appid).getMenuService().menuCreate(menu);
46+
return this.wxService.switchover1(appid).getMenuService().menuCreate(menu);
4647
}
4748

4849
@GetMapping("/create")
@@ -91,10 +92,9 @@ public String menuCreateSample(@PathVariable String appid) throws WxErrorExcepti
9192
if (servletRequestAttributes != null) {
9293
HttpServletRequest request = servletRequestAttributes.getRequest();
9394
URL requestURL = new URL(request.getRequestURL().toString());
94-
String url = WxMpConfiguration.getMpServices().get(appid)
95-
.oauth2buildAuthorizationUrl(
96-
String.format("%s://%s/wx/redirect/%s/greet", requestURL.getProtocol(), requestURL.getHost(), appid),
97-
WxConsts.OAuth2Scope.SNSAPI_USERINFO, null);
95+
String url = this.wxService.switchover1(appid).oauth2buildAuthorizationUrl(
96+
String.format("%s://%s/wx/redirect/%s/greet", requestURL.getProtocol(), requestURL.getHost(), appid),
97+
WxConsts.OAuth2Scope.SNSAPI_USERINFO, null);
9898
button34.setUrl(url);
9999
}
100100

@@ -103,7 +103,8 @@ public String menuCreateSample(@PathVariable String appid) throws WxErrorExcepti
103103
button3.getSubButtons().add(button33);
104104
button3.getSubButtons().add(button34);
105105

106-
return WxMpConfiguration.getMpServices().get(appid).getMenuService().menuCreate(menu);
106+
this.wxService.switchover(appid);
107+
return this.wxService.getMenuService().menuCreate(menu);
107108
}
108109

109110
/**
@@ -114,12 +115,11 @@ public String menuCreateSample(@PathVariable String appid) throws WxErrorExcepti
114115
* 详情请见:https://mp.weixin.qq.com/wiki?t=resource/res_main&id=mp1455782296&token=&lang=zh_CN
115116
* </pre>
116117
*
117-
* @param json
118118
* @return 如果是个性化菜单,则返回menuid,否则返回null
119119
*/
120120
@PostMapping("/createByJson")
121121
public String menuCreate(@PathVariable String appid, @RequestBody String json) throws WxErrorException {
122-
return WxMpConfiguration.getMpServices().get(appid).getMenuService().menuCreate(json);
122+
return this.wxService.switchover1(appid).getMenuService().menuCreate(json);
123123
}
124124

125125
/**
@@ -130,7 +130,7 @@ public String menuCreate(@PathVariable String appid, @RequestBody String json) t
130130
*/
131131
@GetMapping("/delete")
132132
public void menuDelete(@PathVariable String appid) throws WxErrorException {
133-
WxMpConfiguration.getMpServices().get(appid).getMenuService().menuDelete();
133+
this.wxService.switchover1(appid).getMenuService().menuDelete();
134134
}
135135

136136
/**
@@ -143,7 +143,7 @@ public void menuDelete(@PathVariable String appid) throws WxErrorException {
143143
*/
144144
@GetMapping("/delete/{menuId}")
145145
public void menuDelete(@PathVariable String appid, @PathVariable String menuId) throws WxErrorException {
146-
WxMpConfiguration.getMpServices().get(appid).getMenuService().menuDelete(menuId);
146+
this.wxService.switchover1(appid).getMenuService().menuDelete(menuId);
147147
}
148148

149149
/**
@@ -154,7 +154,7 @@ public void menuDelete(@PathVariable String appid, @PathVariable String menuId)
154154
*/
155155
@GetMapping("/get")
156156
public WxMpMenu menuGet(@PathVariable String appid) throws WxErrorException {
157-
return WxMpConfiguration.getMpServices().get(appid).getMenuService().menuGet();
157+
return this.wxService.switchover1(appid).getMenuService().menuGet();
158158
}
159159

160160
/**
@@ -167,7 +167,7 @@ public WxMpMenu menuGet(@PathVariable String appid) throws WxErrorException {
167167
*/
168168
@GetMapping("/menuTryMatch/{userid}")
169169
public WxMenu menuTryMatch(@PathVariable String appid, @PathVariable String userid) throws WxErrorException {
170-
return WxMpConfiguration.getMpServices().get(appid).getMenuService().menuTryMatch(userid);
170+
return this.wxService.switchover1(appid).getMenuService().menuTryMatch(userid);
171171
}
172172

173173
/**
@@ -187,6 +187,6 @@ public WxMenu menuTryMatch(@PathVariable String appid, @PathVariable String user
187187
*/
188188
@GetMapping("/getSelfMenuInfo")
189189
public WxMpGetSelfMenuInfoResult getSelfMenuInfo(@PathVariable String appid) throws WxErrorException {
190-
return WxMpConfiguration.getMpServices().get(appid).getMenuService().getSelfMenuInfo();
190+
return this.wxService.switchover1(appid).getMenuService().getSelfMenuInfo();
191191
}
192192
}

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

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

3+
import me.chanjar.weixin.mp.api.WxMpMessageRouter;
34
import org.apache.commons.lang3.StringUtils;
45
import org.slf4j.Logger;
56
import org.slf4j.LoggerFactory;
7+
import org.springframework.beans.factory.annotation.Autowired;
68
import org.springframework.web.bind.annotation.GetMapping;
79
import org.springframework.web.bind.annotation.PathVariable;
810
import org.springframework.web.bind.annotation.PostMapping;
@@ -11,7 +13,6 @@
1113
import org.springframework.web.bind.annotation.RequestParam;
1214
import org.springframework.web.bind.annotation.RestController;
1315

14-
import com.github.binarywang.demo.wx.mp.config.WxMpConfiguration;
1516
import me.chanjar.weixin.mp.api.WxMpService;
1617
import me.chanjar.weixin.mp.bean.message.WxMpXmlMessage;
1718
import me.chanjar.weixin.mp.bean.message.WxMpXmlOutMessage;
@@ -24,6 +25,16 @@
2425
public class WxPortalController {
2526
private final Logger logger = LoggerFactory.getLogger(this.getClass());
2627

28+
private WxMpService wxService;
29+
30+
private WxMpMessageRouter messageRouter;
31+
32+
@Autowired
33+
public WxPortalController(WxMpService wxService, WxMpMessageRouter messageRouter) {
34+
this.wxService = wxService;
35+
this.messageRouter = messageRouter;
36+
}
37+
2738
@GetMapping(produces = "text/plain;charset=utf-8")
2839
public String authGet(@PathVariable String appid,
2940
@RequestParam(name = "signature", required = false) String signature,
@@ -37,9 +48,8 @@ public String authGet(@PathVariable String appid,
3748
throw new IllegalArgumentException("请求参数非法,请核实!");
3849
}
3950

40-
final WxMpService wxService = WxMpConfiguration.getMpServices().get(appid);
41-
if (wxService == null) {
42-
throw new IllegalArgumentException(String.format("未找到对应appid=[%d]的配置,请核实!", appid));
51+
if (!this.wxService.switchover(appid)) {
52+
throw new IllegalArgumentException(String.format("未找到对应appid=[%s]的配置,请核实!", appid));
4353
}
4454

4555
if (wxService.checkSignature(timestamp, nonce, signature)) {
@@ -58,11 +68,14 @@ public String post(@PathVariable String appid,
5868
@RequestParam("openid") String openid,
5969
@RequestParam(name = "encrypt_type", required = false) String encType,
6070
@RequestParam(name = "msg_signature", required = false) String msgSignature) {
61-
final WxMpService wxService = WxMpConfiguration.getMpServices().get(appid);
6271
this.logger.info("\n接收微信请求:[openid=[{}], [signature=[{}], encType=[{}], msgSignature=[{}],"
6372
+ " timestamp=[{}], nonce=[{}], requestBody=[\n{}\n] ",
6473
openid, signature, encType, msgSignature, timestamp, nonce, requestBody);
6574

75+
if (!this.wxService.switchover(appid)) {
76+
throw new IllegalArgumentException(String.format("未找到对应appid=[%s]的配置,请核实!", appid));
77+
}
78+
6679
if (!wxService.checkSignature(timestamp, nonce, signature)) {
6780
throw new IllegalArgumentException("非法请求,可能属于伪造的请求!");
6881
}
@@ -71,7 +84,7 @@ public String post(@PathVariable String appid,
7184
if (encType == null) {
7285
// 明文传输的消息
7386
WxMpXmlMessage inMessage = WxMpXmlMessage.fromXml(requestBody);
74-
WxMpXmlOutMessage outMessage = this.route(inMessage, appid);
87+
WxMpXmlOutMessage outMessage = this.route(inMessage);
7588
if (outMessage == null) {
7689
return "";
7790
}
@@ -82,7 +95,7 @@ public String post(@PathVariable String appid,
8295
WxMpXmlMessage inMessage = WxMpXmlMessage.fromEncryptedXml(requestBody, wxService.getWxMpConfigStorage(),
8396
timestamp, nonce, msgSignature);
8497
this.logger.debug("\n消息解密后内容为:\n{} ", inMessage.toString());
85-
WxMpXmlOutMessage outMessage = this.route(inMessage, appid);
98+
WxMpXmlOutMessage outMessage = this.route(inMessage);
8699
if (outMessage == null) {
87100
return "";
88101
}
@@ -94,9 +107,9 @@ public String post(@PathVariable String appid,
94107
return out;
95108
}
96109

97-
private WxMpXmlOutMessage route(WxMpXmlMessage message, String appid) {
110+
private WxMpXmlOutMessage route(WxMpXmlMessage message) {
98111
try {
99-
return WxMpConfiguration.getRouters().get(appid).route(message);
112+
return this.messageRouter.route(message);
100113
} catch (Exception e) {
101114
this.logger.error("路由消息时出现异常!", e);
102115
}

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

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,28 +5,37 @@
55
import me.chanjar.weixin.mp.api.WxMpService;
66
import me.chanjar.weixin.mp.bean.result.WxMpOAuth2AccessToken;
77
import me.chanjar.weixin.mp.bean.result.WxMpUser;
8+
import org.springframework.beans.factory.annotation.Autowired;
89
import org.springframework.stereotype.Controller;
910
import org.springframework.ui.ModelMap;
1011
import org.springframework.web.bind.annotation.PathVariable;
1112
import org.springframework.web.bind.annotation.RequestMapping;
1213
import org.springframework.web.bind.annotation.RequestParam;
1314

15+
import javax.xml.ws.Action;
16+
1417
/**
1518
* @author Edward
1619
*/
1720
@Controller
1821
@RequestMapping("/wx/redirect/{appid}")
1922
public class WxRedirectController {
23+
private WxMpService wxService;
2024

25+
@Autowired
26+
public WxRedirectController(WxMpService wxService) {
27+
this.wxService = wxService;
28+
}
2129

2230
@RequestMapping("/greet")
2331
public String greetUser(@PathVariable String appid, @RequestParam String code, ModelMap map) {
24-
25-
WxMpService mpService = WxMpConfiguration.getMpServices().get(appid);
32+
if (!this.wxService.switchover(appid)) {
33+
throw new IllegalArgumentException(String.format("未找到对应appid=[%s]的配置,请核实!", appid));
34+
}
2635

2736
try {
28-
WxMpOAuth2AccessToken accessToken = mpService.oauth2getAccessToken(code);
29-
WxMpUser user = mpService.oauth2getUserInfo(accessToken, null);
37+
WxMpOAuth2AccessToken accessToken = wxService.oauth2getAccessToken(code);
38+
WxMpUser user = wxService.oauth2getUserInfo(accessToken, null);
3039
map.put("user", user);
3140
} catch (WxErrorException e) {
3241
e.printStackTrace();

0 commit comments

Comments
 (0)