Skip to content

Commit 4a4314c

Browse files
committed
优化demo
1 parent 9420d85 commit 4a4314c

File tree

2 files changed

+17
-7
lines changed

2 files changed

+17
-7
lines changed

pom.xml

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

2020
<properties>
21-
<weixin-java-miniapp.version>3.9.0</weixin-java-miniapp.version>
21+
<weixin-java-miniapp.version>3.9.4.B</weixin-java-miniapp.version>
2222

2323
<maven.compiler.source>1.8</maven.compiler.source>
2424
<maven.compiler.target>1.8</maven.compiler.target>
@@ -67,6 +67,10 @@
6767
<artifactId>lombok</artifactId>
6868
<scope>provided</scope>
6969
</dependency>
70+
<dependency>
71+
<groupId>redis.clients</groupId>
72+
<artifactId>jedis</artifactId>
73+
</dependency>
7074
</dependencies>
7175

7276
<build>

src/main/java/com/github/binarywang/demo/wx/miniapp/config/WxMaConfiguration.java

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,19 @@
55
import cn.binarywang.wx.miniapp.bean.WxMaKefuMessage;
66
import cn.binarywang.wx.miniapp.bean.WxMaSubscribeMessage;
77
import cn.binarywang.wx.miniapp.config.impl.WxMaDefaultConfigImpl;
8+
import cn.binarywang.wx.miniapp.config.impl.WxMaRedisConfigImpl;
89
import cn.binarywang.wx.miniapp.message.WxMaMessageHandler;
910
import cn.binarywang.wx.miniapp.message.WxMaMessageRouter;
1011
import com.google.common.collect.Lists;
1112
import com.google.common.collect.Maps;
13+
import lombok.extern.slf4j.Slf4j;
1214
import me.chanjar.weixin.common.bean.result.WxMediaUploadResult;
1315
import me.chanjar.weixin.common.error.WxErrorException;
16+
import me.chanjar.weixin.common.error.WxRuntimeException;
1417
import org.springframework.beans.factory.annotation.Autowired;
1518
import org.springframework.boot.context.properties.EnableConfigurationProperties;
1619
import org.springframework.context.annotation.Configuration;
20+
import redis.clients.jedis.JedisPool;
1721

1822
import javax.annotation.PostConstruct;
1923
import java.io.File;
@@ -24,13 +28,14 @@
2428
/**
2529
* @author <a href="https://github.com/binarywang">Binary Wang</a>
2630
*/
31+
@Slf4j
2732
@Configuration
2833
@EnableConfigurationProperties(WxMaProperties.class)
2934
public class WxMaConfiguration {
30-
private WxMaProperties properties;
35+
private final WxMaProperties properties;
3136

32-
private static Map<String, WxMaMessageRouter> routers = Maps.newHashMap();
33-
private static Map<String, WxMaService> maServices = Maps.newHashMap();
37+
private static final Map<String, WxMaMessageRouter> routers = Maps.newHashMap();
38+
private static Map<String, WxMaService> maServices;
3439

3540
@Autowired
3641
public WxMaConfiguration(WxMaProperties properties) {
@@ -54,12 +59,13 @@ public static WxMaMessageRouter getRouter(String appid) {
5459
public void init() {
5560
List<WxMaProperties.Config> configs = this.properties.getConfigs();
5661
if (configs == null) {
57-
throw new RuntimeException("大哥,拜托先看下项目首页的说明(readme文件),添加下相关配置,注意别配错了!");
62+
throw new WxRuntimeException("大哥,拜托先看下项目首页的说明(readme文件),添加下相关配置,注意别配错了!");
5863
}
5964

6065
maServices = configs.stream()
6166
.map(a -> {
62-
WxMaDefaultConfigImpl config = new WxMaDefaultConfigImpl();
67+
// WxMaDefaultConfigImpl config = new WxMaDefaultConfigImpl();
68+
WxMaDefaultConfigImpl config = new WxMaRedisConfigImpl(new JedisPool());
6369
config.setAppid(a.getAppid());
6470
config.setSecret(a.getSecret());
6571
config.setToken(a.getToken());
@@ -95,7 +101,7 @@ private WxMaMessageRouter newRouter(WxMaService service) {
95101
};
96102

97103
private final WxMaMessageHandler logHandler = (wxMessage, context, service, sessionManager) -> {
98-
System.out.println("收到消息:" + wxMessage.toString());
104+
log.info("收到消息:" + wxMessage.toString());
99105
service.getMsgService().sendKefuMsg(WxMaKefuMessage.newTextBuilder().content("收到信息为:" + wxMessage.toJson())
100106
.toUser(wxMessage.getFromUser()).build());
101107
return null;

0 commit comments

Comments
 (0)