Skip to content

Commit c51e792

Browse files
committed
多公众号支持改造
1 parent 7c15eaf commit c51e792

File tree

8 files changed

+165
-182
lines changed

8 files changed

+165
-182
lines changed

README.md

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,23 @@
55
-----------------------
66

77
## 使用步骤:
8-
1. 配置:复制 `/src/main/resources/application.yml.template` 或修改其扩展名生成 `application.yml` 文件,根据自己需要填写相关配置(需要注意的是:yml文件内的属性冒号后面的文字之前需要加空格,可参考已有配置,否则属性会设置不成功);
9-
1. 运行Java程序:`WxMpDemoApplication`
10-
1. 打开shell或cmd,进入ngrok目录,运行 `ngrok -config ngrok.cfg -subdomain my-domain 8080` 如果运行失败,请更换my-domain为其它字符串,直至连接成功;
11-
1. 配置微信公众号中的接口地址:http://my-domain.tunnel.qydev.com/wechat/portal (注意my-domain要跟上面的一致,需要符合微信官方的要求);
12-
1. 根据自己需要修改各个handler的实现,加入自己的业务逻辑。
8+
1. 配置:复制 `/src/main/resources/application.yml.template` 或修改其扩展名生成 `application.yml` 文件,根据自己需要填写相关配置(需要注意的是:yml文件内的属性冒号后面的文字之前需要加空格,可参考已有配置,否则属性会设置不成功);
9+
2. 主要配置说明如下:
10+
```
11+
wx:
12+
mp:
13+
configs:
14+
- appId: 1111 (一个公众号的appid)
15+
secret: 1111(公众号的appsecret)
16+
token: 111 (接口配置里的Token值)
17+
aesKey: 111 (接口配置里的EncodingAESKey值)
18+
- appId: 2222 (另一个公众号的appid,以下同上)
19+
secret: 1111
20+
token: 111
21+
aesKey: 111
22+
```
23+
3. 运行Java程序:`WxMpDemoApplication`
24+
4. 打开shell或cmd,进入ngrok目录,运行 `ngrok -config ngrok.cfg -subdomain my-domain 8080` 如果运行失败,请更换my-domain为其它字符串,直至连接成功;
25+
5. 配置微信公众号中的接口地址:http://my-domain.tunnel.qydev.com/wx/portal/xxxxx (注意xxxxx为对应公众号的appid值,my-domain要跟上面的一致,需要符合微信官方的要求);
26+
6. 根据自己需要修改各个handler的实现,加入自己的业务逻辑。
1327

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

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

33
import com.github.binarywang.demo.wx.mp.handler.*;
4-
import me.chanjar.weixin.mp.api.WxMpConfigStorage;
4+
import com.google.common.collect.Maps;
55
import me.chanjar.weixin.mp.api.WxMpInMemoryConfigStorage;
66
import me.chanjar.weixin.mp.api.WxMpMessageRouter;
77
import me.chanjar.weixin.mp.api.WxMpService;
88
import me.chanjar.weixin.mp.api.impl.WxMpServiceImpl;
99
import me.chanjar.weixin.mp.constant.WxMpEventConstants;
1010
import org.springframework.beans.factory.annotation.Autowired;
11-
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
12-
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
1311
import org.springframework.boot.context.properties.EnableConfigurationProperties;
1412
import org.springframework.context.annotation.Bean;
1513
import org.springframework.context.annotation.Configuration;
1614

15+
import java.util.Map;
16+
import java.util.stream.Collectors;
17+
1718
import static me.chanjar.weixin.common.api.WxConsts.*;
1819

1920
/**
@@ -22,55 +23,72 @@
2223
* @author Binary Wang(https://github.com/binarywang)
2324
*/
2425
@Configuration
25-
@ConditionalOnClass(WxMpService.class)
2626
@EnableConfigurationProperties(WxMpProperties.class)
2727
public class WxMpConfiguration {
28-
@Autowired
29-
protected LogHandler logHandler;
30-
@Autowired
31-
protected NullHandler nullHandler;
32-
@Autowired
33-
protected KfSessionHandler kfSessionHandler;
34-
@Autowired
35-
protected StoreCheckNotifyHandler storeCheckNotifyHandler;
36-
@Autowired
37-
private WxMpProperties properties;
38-
@Autowired
28+
private LogHandler logHandler;
29+
private NullHandler nullHandler;
30+
private KfSessionHandler kfSessionHandler;
31+
private StoreCheckNotifyHandler storeCheckNotifyHandler;
3932
private LocationHandler locationHandler;
40-
@Autowired
4133
private MenuHandler menuHandler;
42-
@Autowired
4334
private MsgHandler msgHandler;
44-
@Autowired
4535
private UnsubscribeHandler unsubscribeHandler;
46-
@Autowired
4736
private SubscribeHandler subscribeHandler;
4837

49-
@Bean
50-
@ConditionalOnMissingBean
51-
public WxMpConfigStorage configStorage() {
52-
WxMpInMemoryConfigStorage configStorage = new WxMpInMemoryConfigStorage();
53-
configStorage.setAppId(this.properties.getAppId());
54-
configStorage.setSecret(this.properties.getSecret());
55-
configStorage.setToken(this.properties.getToken());
56-
configStorage.setAesKey(this.properties.getAesKey());
57-
return configStorage;
38+
private WxMpProperties properties;
39+
40+
private static Map<String, WxMpMessageRouter> routers = Maps.newHashMap();
41+
private static Map<String, WxMpService> mpServices = Maps.newHashMap();
42+
43+
@Autowired
44+
public WxMpConfiguration(LogHandler logHandler, NullHandler nullHandler, KfSessionHandler kfSessionHandler,
45+
StoreCheckNotifyHandler storeCheckNotifyHandler, LocationHandler locationHandler,
46+
MenuHandler menuHandler, MsgHandler msgHandler, UnsubscribeHandler unsubscribeHandler,
47+
SubscribeHandler subscribeHandler, WxMpProperties properties) {
48+
this.logHandler = logHandler;
49+
this.nullHandler = nullHandler;
50+
this.kfSessionHandler = kfSessionHandler;
51+
this.storeCheckNotifyHandler = storeCheckNotifyHandler;
52+
this.locationHandler = locationHandler;
53+
this.menuHandler = menuHandler;
54+
this.msgHandler = msgHandler;
55+
this.unsubscribeHandler = unsubscribeHandler;
56+
this.subscribeHandler = subscribeHandler;
57+
this.properties = properties;
5858
}
5959

60-
@Bean
61-
@ConditionalOnMissingBean
62-
public WxMpService wxMpService(WxMpConfigStorage configStorage) {
63-
// WxMpService wxMpService = new me.chanjar.weixin.mp.api.impl.WxMpServiceOkHttpImpl();
64-
// WxMpService wxMpService = new me.chanjar.weixin.mp.api.impl.WxMpServiceJoddHttpImpl();
65-
// WxMpService wxMpService = new me.chanjar.weixin.mp.api.impl.WxMpServiceHttpClientImpl();
66-
67-
WxMpService wxMpService = new WxMpServiceImpl();
68-
wxMpService.setWxMpConfigStorage(configStorage);
69-
return wxMpService;
60+
public static Map<String, WxMpMessageRouter> getRouters() {
61+
return routers;
62+
}
63+
64+
public static Map<String, WxMpService> getMpServices() {
65+
return mpServices;
7066
}
7167

7268
@Bean
73-
public WxMpMessageRouter router(WxMpService wxMpService) {
69+
public Object services() {
70+
mpServices = this.properties.getConfigs()
71+
.stream()
72+
.map(a -> {
73+
WxMpInMemoryConfigStorage configStorage = new WxMpInMemoryConfigStorage();
74+
configStorage.setAppId(a.getAppId());
75+
configStorage.setSecret(a.getSecret());
76+
configStorage.setToken(a.getToken());
77+
configStorage.setAesKey(a.getAesKey());
78+
79+
// WxMpService wxMpService = new me.chanjar.weixin.mp.api.impl.WxMpServiceOkHttpImpl();
80+
// WxMpService wxMpService = new me.chanjar.weixin.mp.api.impl.WxMpServiceJoddHttpImpl();
81+
// WxMpService wxMpService = new me.chanjar.weixin.mp.api.impl.WxMpServiceHttpClientImpl();
82+
WxMpService service = new WxMpServiceImpl();
83+
service.setWxMpConfigStorage(configStorage);
84+
routers.put(a.getAppId(), this.newRouter(service));
85+
return service;
86+
}).collect(Collectors.toMap(s -> s.getWxMpConfigStorage().getAppId(), a -> a));
87+
88+
return Boolean.TRUE;
89+
}
90+
91+
private WxMpMessageRouter newRouter(WxMpService wxMpService) {
7492
final WxMpMessageRouter newRouter = new WxMpMessageRouter(wxMpService);
7593

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

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

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

33
import com.github.binarywang.demo.wx.mp.utils.JsonUtils;
4-
import org.apache.commons.lang3.builder.ToStringBuilder;
5-
import org.apache.commons.lang3.builder.ToStringStyle;
4+
import lombok.Data;
65
import org.springframework.boot.context.properties.ConfigurationProperties;
76

7+
import java.util.List;
8+
89
/**
910
* wechat mp properties
1011
*
1112
* @author Binary Wang(https://github.com/binarywang)
1213
*/
14+
@Data
1315
@ConfigurationProperties(prefix = "wx.mp")
1416
public class WxMpProperties {
15-
/**
16-
* 设置微信公众号的appid
17-
*/
18-
private String appId;
19-
20-
/**
21-
* 设置微信公众号的app secret
22-
*/
23-
private String secret;
24-
25-
/**
26-
* 设置微信公众号的token
27-
*/
28-
private String token;
29-
30-
/**
31-
* 设置微信公众号的EncodingAESKey
32-
*/
33-
private String aesKey;
34-
35-
public String getAppId() {
36-
return this.appId;
37-
}
38-
39-
public void setAppId(String appId) {
40-
this.appId = appId;
41-
}
42-
43-
public String getSecret() {
44-
return this.secret;
45-
}
46-
47-
public void setSecret(String secret) {
48-
this.secret = secret;
49-
}
50-
51-
public String getToken() {
52-
return this.token;
53-
}
54-
55-
public void setToken(String token) {
56-
this.token = token;
57-
}
58-
59-
public String getAesKey() {
60-
return this.aesKey;
61-
}
62-
63-
public void setAesKey(String aesKey) {
64-
this.aesKey = aesKey;
17+
private List<MpConfig> configs;
18+
19+
@Data
20+
public static class MpConfig {
21+
/**
22+
* 设置微信公众号的appid
23+
*/
24+
private String appId;
25+
26+
/**
27+
* 设置微信公众号的app secret
28+
*/
29+
private String secret;
30+
31+
/**
32+
* 设置微信公众号的token
33+
*/
34+
private String token;
35+
36+
/**
37+
* 设置微信公众号的EncodingAESKey
38+
*/
39+
private String aesKey;
6540
}
6641

6742
@Override

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

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

3+
import com.github.binarywang.demo.wx.mp.config.WxMpConfiguration;
34
import me.chanjar.weixin.common.bean.menu.WxMenu;
45
import me.chanjar.weixin.common.bean.menu.WxMenuButton;
56
import me.chanjar.weixin.common.error.WxErrorException;
6-
import me.chanjar.weixin.mp.api.WxMpMenuService;
7-
import me.chanjar.weixin.mp.api.WxMpService;
87
import me.chanjar.weixin.mp.bean.menu.WxMpGetSelfMenuInfoResult;
98
import me.chanjar.weixin.mp.bean.menu.WxMpMenu;
10-
import org.springframework.beans.factory.annotation.Autowired;
119
import org.springframework.web.bind.annotation.*;
1210

1311
import static me.chanjar.weixin.common.api.WxConsts.MenuButtonType;
1412

1513
/**
16-
* <pre>
17-
* 注意:此contorller 实现WxMpMenuService接口,仅是为了演示如何调用所有菜单相关操作接口,
18-
* 实际项目中无需这样,根据自己需要添加对应接口即可
19-
* </pre>
20-
*
2114
* @author Binary Wang(https://github.com/binarywang)
2215
*/
2316
@RestController
24-
@RequestMapping("/wechat/menu")
25-
public class WxMenuController implements WxMpMenuService {
26-
27-
@Autowired
28-
private WxMpService wxService;
17+
@RequestMapping("/wx/menu/{appid}")
18+
public class WxMenuController {
2919

3020
/**
3121
* <pre>
@@ -35,17 +25,15 @@ public class WxMenuController implements WxMpMenuService {
3525
* 详情请见:https://mp.weixin.qq.com/wiki?t=resource/res_main&id=mp1455782296&token=&lang=zh_CN
3626
* </pre>
3727
*
38-
* @param menu
3928
* @return 如果是个性化菜单,则返回menuid,否则返回null
4029
*/
41-
@Override
4230
@PostMapping("/create")
43-
public String menuCreate(@RequestBody WxMenu menu) throws WxErrorException {
44-
return this.wxService.getMenuService().menuCreate(menu);
31+
public String menuCreate(@PathVariable String appid, @RequestBody WxMenu menu) throws WxErrorException {
32+
return WxMpConfiguration.getMpServices().get(appid).getMenuService().menuCreate(menu);
4533
}
4634

4735
@GetMapping("/create")
48-
public String menuCreateSample() throws WxErrorException {
36+
public String menuCreateSample(@PathVariable String appid) throws WxErrorException {
4937
WxMenu menu = new WxMenu();
5038
WxMenuButton button1 = new WxMenuButton();
5139
button1.setType(MenuButtonType.CLICK);
@@ -85,7 +73,7 @@ public String menuCreateSample() throws WxErrorException {
8573
button3.getSubButtons().add(button32);
8674
button3.getSubButtons().add(button33);
8775

88-
return this.wxService.getMenuService().menuCreate(menu);
76+
return WxMpConfiguration.getMpServices().get(appid).getMenuService().menuCreate(menu);
8977
}
9078

9179
/**
@@ -99,10 +87,9 @@ public String menuCreateSample() throws WxErrorException {
9987
* @param json
10088
* @return 如果是个性化菜单,则返回menuid,否则返回null
10189
*/
102-
@Override
10390
@GetMapping("/create/{json}")
104-
public String menuCreate(@PathVariable String json) throws WxErrorException {
105-
return this.wxService.getMenuService().menuCreate(json);
91+
public String menuCreate(@PathVariable String appid, @PathVariable String json) throws WxErrorException {
92+
return WxMpConfiguration.getMpServices().get(appid).getMenuService().menuCreate(json);
10693
}
10794

10895
/**
@@ -111,10 +98,9 @@ public String menuCreate(@PathVariable String json) throws WxErrorException {
11198
* 详情请见: https://mp.weixin.qq.com/wiki?t=resource/res_main&id=mp1421141015&token=&lang=zh_CN
11299
* </pre>
113100
*/
114-
@Override
115101
@GetMapping("/delete")
116-
public void menuDelete() throws WxErrorException {
117-
this.wxService.getMenuService().menuDelete();
102+
public void menuDelete(@PathVariable String appid) throws WxErrorException {
103+
WxMpConfiguration.getMpServices().get(appid).getMenuService().menuDelete();
118104
}
119105

120106
/**
@@ -125,10 +111,9 @@ public void menuDelete() throws WxErrorException {
125111
*
126112
* @param menuId 个性化菜单的menuid
127113
*/
128-
@Override
129114
@GetMapping("/delete/{menuId}")
130-
public void menuDelete(@PathVariable String menuId) throws WxErrorException {
131-
this.wxService.getMenuService().menuDelete(menuId);
115+
public void menuDelete(@PathVariable String appid, @PathVariable String menuId) throws WxErrorException {
116+
WxMpConfiguration.getMpServices().get(appid).getMenuService().menuDelete(menuId);
132117
}
133118

134119
/**
@@ -137,10 +122,9 @@ public void menuDelete(@PathVariable String menuId) throws WxErrorException {
137122
* 详情请见: https://mp.weixin.qq.com/wiki?t=resource/res_main&id=mp1421141014&token=&lang=zh_CN
138123
* </pre>
139124
*/
140-
@Override
141125
@GetMapping("/get")
142-
public WxMpMenu menuGet() throws WxErrorException {
143-
return this.wxService.getMenuService().menuGet();
126+
public WxMpMenu menuGet(@PathVariable String appid) throws WxErrorException {
127+
return WxMpConfiguration.getMpServices().get(appid).getMenuService().menuGet();
144128
}
145129

146130
/**
@@ -151,10 +135,9 @@ public WxMpMenu menuGet() throws WxErrorException {
151135
*
152136
* @param userid 可以是粉丝的OpenID,也可以是粉丝的微信号。
153137
*/
154-
@Override
155138
@GetMapping("/menuTryMatch/{userid}")
156-
public WxMenu menuTryMatch(@PathVariable String userid) throws WxErrorException {
157-
return this.wxService.getMenuService().menuTryMatch(userid);
139+
public WxMenu menuTryMatch(@PathVariable String appid, @PathVariable String userid) throws WxErrorException {
140+
return WxMpConfiguration.getMpServices().get(appid).getMenuService().menuTryMatch(userid);
158141
}
159142

160143
/**
@@ -172,9 +155,8 @@ public WxMenu menuTryMatch(@PathVariable String userid) throws WxErrorException
172155
* https://api.weixin.qq.com/cgi-bin/get_current_selfmenu_info?access_token=ACCESS_TOKEN
173156
* </pre>
174157
*/
175-
@Override
176158
@GetMapping("/getSelfMenuInfo")
177-
public WxMpGetSelfMenuInfoResult getSelfMenuInfo() throws WxErrorException {
178-
return this.wxService.getMenuService().getSelfMenuInfo();
159+
public WxMpGetSelfMenuInfoResult getSelfMenuInfo(@PathVariable String appid) throws WxErrorException {
160+
return WxMpConfiguration.getMpServices().get(appid).getMenuService().getSelfMenuInfo();
179161
}
180162
}

0 commit comments

Comments
 (0)