Skip to content

Commit 830689e

Browse files
committed
initial
0 parents  commit 830689e

23 files changed

+1090
-0
lines changed

.gitignore

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
/target/
2+
/.settings/
3+
*.project
4+
*.classpath
5+
application.properties

pom.xml

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
4+
<modelVersion>4.0.0</modelVersion>
5+
6+
<parent>
7+
<groupId>org.springframework.boot</groupId>
8+
<artifactId>spring-boot-starter-parent</artifactId>
9+
<version>1.4.1.RELEASE</version>
10+
</parent>
11+
12+
<groupId>com.github.binarywang.wechat</groupId>
13+
<version>1.0.0-SNAPSHOT</version>
14+
<artifactId>weixin-mp-demo-springboot</artifactId>
15+
<packaging>jar</packaging>
16+
17+
<name>spring-boot-demo-for-wechat-mp</name>
18+
<description>Spring Boot Demo with wechat MP</description>
19+
20+
<properties>
21+
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
22+
<project.build.locales>zh_CN</project.build.locales>
23+
<project.build.jdk>1.7</project.build.jdk>
24+
<weixin-java-mp.version>2.2.4</weixin-java-mp.version>
25+
</properties>
26+
27+
<dependencies>
28+
<dependency>
29+
<groupId>org.springframework.boot</groupId>
30+
<artifactId>spring-boot-starter-web</artifactId>
31+
</dependency>
32+
<dependency>
33+
<groupId>org.springframework.boot</groupId>
34+
<artifactId>spring-boot-starter-thymeleaf</artifactId>
35+
</dependency>
36+
37+
<dependency>
38+
<groupId>com.github.binarywang</groupId>
39+
<artifactId>weixin-java-mp</artifactId>
40+
<version>${weixin-java-mp.version}</version>
41+
</dependency>
42+
43+
<!-- Testing Dependencies -->
44+
<dependency>
45+
<groupId>org.springframework.boot</groupId>
46+
<artifactId>spring-boot-starter-test</artifactId>
47+
</dependency>
48+
</dependencies>
49+
50+
<build>
51+
<plugins>
52+
<plugin>
53+
<groupId>org.springframework.boot</groupId>
54+
<artifactId>spring-boot-maven-plugin</artifactId>
55+
</plugin>
56+
<plugin>
57+
<groupId>org.apache.maven.plugins</groupId>
58+
<artifactId>maven-compiler-plugin</artifactId>
59+
<configuration>
60+
<source>${project.build.jdk}</source>
61+
<target>${project.build.jdk}</target>
62+
<encoding>${project.build.sourceEncoding}</encoding>
63+
</configuration>
64+
</plugin>
65+
<plugin>
66+
<groupId>org.apache.maven.plugins</groupId>
67+
<artifactId>maven-surefire-plugin</artifactId>
68+
<configuration>
69+
<groups>com.testwithspring.starter.springboot.UnitTest</groups>
70+
</configuration>
71+
</plugin>
72+
</plugins>
73+
</build>
74+
75+
</project>
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package com.github.binarywang.demo.wechat;
2+
3+
import org.springframework.boot.SpringApplication;
4+
import org.springframework.boot.autoconfigure.SpringBootApplication;
5+
6+
/**
7+
*
8+
* @author Binary Wang
9+
*/
10+
@SpringBootApplication
11+
public class WechatMpDemoApplication {
12+
13+
public static void main(String[] args) {
14+
SpringApplication.run(WechatMpDemoApplication.class, args);
15+
}
16+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package com.github.binarywang.demo.wechat.builder;
2+
3+
import org.slf4j.Logger;
4+
import org.slf4j.LoggerFactory;
5+
6+
import me.chanjar.weixin.mp.api.WxMpService;
7+
import me.chanjar.weixin.mp.bean.WxMpXmlMessage;
8+
import me.chanjar.weixin.mp.bean.WxMpXmlOutMessage;
9+
10+
/**
11+
*
12+
* @author Binary Wang
13+
*
14+
*/
15+
public abstract class AbstractBuilder {
16+
protected final Logger logger = LoggerFactory.getLogger(getClass());
17+
18+
public abstract WxMpXmlOutMessage build(String content,
19+
WxMpXmlMessage wxMessage, WxMpService service);
20+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package com.github.binarywang.demo.wechat.builder;
2+
3+
import me.chanjar.weixin.mp.api.WxMpService;
4+
import me.chanjar.weixin.mp.bean.WxMpXmlMessage;
5+
import me.chanjar.weixin.mp.bean.WxMpXmlOutImageMessage;
6+
import me.chanjar.weixin.mp.bean.WxMpXmlOutMessage;
7+
8+
/**
9+
*
10+
* @author Binary Wang
11+
*
12+
*/
13+
public class ImageBuilder extends AbstractBuilder {
14+
15+
@Override
16+
public WxMpXmlOutMessage build(String content, WxMpXmlMessage wxMessage,
17+
WxMpService service) {
18+
19+
WxMpXmlOutImageMessage m = WxMpXmlOutMessage.IMAGE().mediaId(content)
20+
.fromUser(wxMessage.getToUser()).toUser(wxMessage.getFromUser())
21+
.build();
22+
23+
return m;
24+
}
25+
26+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package com.github.binarywang.demo.wechat.builder;
2+
3+
import me.chanjar.weixin.mp.api.WxMpService;
4+
import me.chanjar.weixin.mp.bean.WxMpXmlMessage;
5+
import me.chanjar.weixin.mp.bean.WxMpXmlOutMessage;
6+
import me.chanjar.weixin.mp.bean.WxMpXmlOutTextMessage;
7+
8+
/**
9+
*
10+
* @author Binary Wang
11+
*
12+
*/
13+
public class TextBuilder extends AbstractBuilder {
14+
15+
@Override
16+
public WxMpXmlOutMessage build(String content, WxMpXmlMessage wxMessage,
17+
WxMpService service) {
18+
WxMpXmlOutTextMessage m = WxMpXmlOutMessage.TEXT().content(content)
19+
.fromUser(wxMessage.getToUser()).toUser(wxMessage.getFromUser())
20+
.build();
21+
return m;
22+
}
23+
24+
}
Lines changed: 172 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,172 @@
1+
package com.github.binarywang.demo.wechat.config;
2+
3+
import org.springframework.beans.factory.annotation.Autowired;
4+
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
5+
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
6+
import org.springframework.boot.context.properties.EnableConfigurationProperties;
7+
import org.springframework.context.annotation.Bean;
8+
import org.springframework.context.annotation.Configuration;
9+
10+
import com.github.binarywang.demo.wechat.handler.AbstractHandler;
11+
import com.github.binarywang.demo.wechat.handler.KfSessionHandler;
12+
import com.github.binarywang.demo.wechat.handler.LocationHandler;
13+
import com.github.binarywang.demo.wechat.handler.LogHandler;
14+
import com.github.binarywang.demo.wechat.handler.MenuHandler;
15+
import com.github.binarywang.demo.wechat.handler.MsgHandler;
16+
import com.github.binarywang.demo.wechat.handler.NullHandler;
17+
import com.github.binarywang.demo.wechat.handler.StoreCheckNotifyHandler;
18+
import com.github.binarywang.demo.wechat.handler.SubscribeHandler;
19+
import com.github.binarywang.demo.wechat.handler.UnsubscribeHandler;
20+
21+
import me.chanjar.weixin.common.api.WxConsts;
22+
import me.chanjar.weixin.mp.api.WxMpConfigStorage;
23+
import me.chanjar.weixin.mp.api.WxMpInMemoryConfigStorage;
24+
import me.chanjar.weixin.mp.api.WxMpMessageRouter;
25+
import me.chanjar.weixin.mp.api.WxMpService;
26+
import me.chanjar.weixin.mp.api.impl.WxMpServiceImpl;
27+
28+
/**
29+
* wechat mp configuration
30+
*
31+
* @author Binary Wang
32+
*/
33+
@Configuration
34+
@ConditionalOnClass(WxMpService.class)
35+
@EnableConfigurationProperties(WechatMpProperties.class)
36+
public class WechatMpConfiguration {
37+
@Autowired
38+
private WechatMpProperties properties;
39+
40+
@Bean
41+
@ConditionalOnMissingBean
42+
public WxMpConfigStorage configStorage() {
43+
WxMpInMemoryConfigStorage configStorage = new WxMpInMemoryConfigStorage();
44+
configStorage.setAppId(this.properties.getAppId());
45+
configStorage.setSecret(this.properties.getSecret());
46+
configStorage.setToken(this.properties.getToken());
47+
configStorage.setAesKey(this.properties.getAesKey());
48+
configStorage.setPartnerId(this.properties.getPartnerId());
49+
configStorage.setPartnerKey(this.properties.getPartnerKey());
50+
return configStorage;
51+
}
52+
53+
@Bean
54+
@ConditionalOnMissingBean
55+
public WxMpService wxMpService(WxMpConfigStorage configStorage) {
56+
WxMpService wxMpService = new WxMpServiceImpl();
57+
wxMpService.setWxMpConfigStorage(configStorage);
58+
return wxMpService;
59+
}
60+
61+
@Bean
62+
public WxMpMessageRouter router(WxMpService wxMpService) {
63+
final WxMpMessageRouter newRouter = new WxMpMessageRouter(wxMpService);
64+
65+
// 记录所有事件的日志
66+
newRouter.rule().handler(this.logHandler).next();
67+
68+
// 接收客服会话管理事件
69+
newRouter.rule().async(false).msgType(WxConsts.XML_MSG_EVENT)
70+
.event(WxConsts.EVT_KF_CREATE_SESSION)
71+
.handler(this.kfSessionHandler).end();
72+
newRouter.rule().async(false).msgType(WxConsts.XML_MSG_EVENT)
73+
.event(WxConsts.EVT_KF_CLOSE_SESSION).handler(this.kfSessionHandler)
74+
.end();
75+
newRouter.rule().async(false).msgType(WxConsts.XML_MSG_EVENT)
76+
.event(WxConsts.EVT_KF_SWITCH_SESSION)
77+
.handler(this.kfSessionHandler).end();
78+
79+
// 门店审核事件
80+
newRouter.rule().async(false).msgType(WxConsts.XML_MSG_EVENT)
81+
.event(WxConsts.EVT_POI_CHECK_NOTIFY)
82+
.handler(this.storeCheckNotifyHandler).end();
83+
84+
// 自定义菜单事件
85+
newRouter.rule().async(false).msgType(WxConsts.XML_MSG_EVENT)
86+
.event(WxConsts.BUTTON_CLICK).handler(this.getMenuHandler()).end();
87+
88+
// 点击菜单连接事件
89+
newRouter.rule().async(false).msgType(WxConsts.XML_MSG_EVENT)
90+
.event(WxConsts.BUTTON_VIEW).handler(this.nullHandler).end();
91+
92+
// 关注事件
93+
newRouter.rule().async(false).msgType(WxConsts.XML_MSG_EVENT)
94+
.event(WxConsts.EVT_SUBSCRIBE).handler(this.getSubscribeHandler())
95+
.end();
96+
97+
// 取消关注事件
98+
newRouter.rule().async(false).msgType(WxConsts.XML_MSG_EVENT)
99+
.event(WxConsts.EVT_UNSUBSCRIBE)
100+
.handler(this.getUnsubscribeHandler()).end();
101+
102+
// 上报地理位置事件
103+
newRouter.rule().async(false).msgType(WxConsts.XML_MSG_EVENT)
104+
.event(WxConsts.EVT_LOCATION).handler(this.getLocationHandler())
105+
.end();
106+
107+
// 接收地理位置消息
108+
newRouter.rule().async(false).msgType(WxConsts.XML_MSG_LOCATION)
109+
.handler(this.getLocationHandler()).end();
110+
111+
// 扫码事件
112+
newRouter.rule().async(false).msgType(WxConsts.XML_MSG_EVENT)
113+
.event(WxConsts.EVT_SCAN).handler(this.getScanHandler()).end();
114+
115+
// 默认
116+
newRouter.rule().async(false).handler(this.getMsgHandler()).end();
117+
118+
return newRouter;
119+
}
120+
121+
@Autowired
122+
private LocationHandler locationHandler;
123+
124+
@Autowired
125+
private MenuHandler menuHandler;
126+
127+
@Autowired
128+
private MsgHandler msgHandler;
129+
130+
@Autowired
131+
protected LogHandler logHandler;
132+
133+
@Autowired
134+
protected NullHandler nullHandler;
135+
136+
@Autowired
137+
protected KfSessionHandler kfSessionHandler;
138+
139+
@Autowired
140+
protected StoreCheckNotifyHandler storeCheckNotifyHandler;
141+
142+
@Autowired
143+
private UnsubscribeHandler unsubscribeHandler;
144+
145+
@Autowired
146+
private SubscribeHandler subscribeHandler;
147+
148+
protected MenuHandler getMenuHandler() {
149+
return this.menuHandler;
150+
}
151+
152+
protected SubscribeHandler getSubscribeHandler() {
153+
return this.subscribeHandler;
154+
}
155+
156+
protected UnsubscribeHandler getUnsubscribeHandler() {
157+
return this.unsubscribeHandler;
158+
}
159+
160+
protected AbstractHandler getLocationHandler() {
161+
return this.locationHandler;
162+
}
163+
164+
protected MsgHandler getMsgHandler() {
165+
return this.msgHandler;
166+
}
167+
168+
protected AbstractHandler getScanHandler() {
169+
return null;
170+
}
171+
172+
}

0 commit comments

Comments
 (0)