Skip to content

Commit

Permalink
initial
Browse files Browse the repository at this point in the history
  • Loading branch information
binarywang committed Oct 27, 2016
0 parents commit 830689e
Show file tree
Hide file tree
Showing 23 changed files with 1,090 additions and 0 deletions.
5 changes: 5 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
/target/
/.settings/
*.project
*.classpath
application.properties
75 changes: 75 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.4.1.RELEASE</version>
</parent>

<groupId>com.github.binarywang.wechat</groupId>
<version>1.0.0-SNAPSHOT</version>
<artifactId>weixin-mp-demo-springboot</artifactId>
<packaging>jar</packaging>

<name>spring-boot-demo-for-wechat-mp</name>
<description>Spring Boot Demo with wechat MP</description>

<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.build.locales>zh_CN</project.build.locales>
<project.build.jdk>1.7</project.build.jdk>
<weixin-java-mp.version>2.2.4</weixin-java-mp.version>
</properties>

<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>

<dependency>
<groupId>com.github.binarywang</groupId>
<artifactId>weixin-java-mp</artifactId>
<version>${weixin-java-mp.version}</version>
</dependency>

<!-- Testing Dependencies -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
</dependency>
</dependencies>

<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>${project.build.jdk}</source>
<target>${project.build.jdk}</target>
<encoding>${project.build.sourceEncoding}</encoding>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<configuration>
<groups>com.testwithspring.starter.springboot.UnitTest</groups>
</configuration>
</plugin>
</plugins>
</build>

</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package com.github.binarywang.demo.wechat;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

/**
*
* @author Binary Wang
*/
@SpringBootApplication
public class WechatMpDemoApplication {

public static void main(String[] args) {
SpringApplication.run(WechatMpDemoApplication.class, args);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package com.github.binarywang.demo.wechat.builder;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import me.chanjar.weixin.mp.api.WxMpService;
import me.chanjar.weixin.mp.bean.WxMpXmlMessage;
import me.chanjar.weixin.mp.bean.WxMpXmlOutMessage;

/**
*
* @author Binary Wang
*
*/
public abstract class AbstractBuilder {
protected final Logger logger = LoggerFactory.getLogger(getClass());

public abstract WxMpXmlOutMessage build(String content,
WxMpXmlMessage wxMessage, WxMpService service);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package com.github.binarywang.demo.wechat.builder;

import me.chanjar.weixin.mp.api.WxMpService;
import me.chanjar.weixin.mp.bean.WxMpXmlMessage;
import me.chanjar.weixin.mp.bean.WxMpXmlOutImageMessage;
import me.chanjar.weixin.mp.bean.WxMpXmlOutMessage;

/**
*
* @author Binary Wang
*
*/
public class ImageBuilder extends AbstractBuilder {

@Override
public WxMpXmlOutMessage build(String content, WxMpXmlMessage wxMessage,
WxMpService service) {

WxMpXmlOutImageMessage m = WxMpXmlOutMessage.IMAGE().mediaId(content)
.fromUser(wxMessage.getToUser()).toUser(wxMessage.getFromUser())
.build();

return m;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package com.github.binarywang.demo.wechat.builder;

import me.chanjar.weixin.mp.api.WxMpService;
import me.chanjar.weixin.mp.bean.WxMpXmlMessage;
import me.chanjar.weixin.mp.bean.WxMpXmlOutMessage;
import me.chanjar.weixin.mp.bean.WxMpXmlOutTextMessage;

/**
*
* @author Binary Wang
*
*/
public class TextBuilder extends AbstractBuilder {

@Override
public WxMpXmlOutMessage build(String content, WxMpXmlMessage wxMessage,
WxMpService service) {
WxMpXmlOutTextMessage m = WxMpXmlOutMessage.TEXT().content(content)
.fromUser(wxMessage.getToUser()).toUser(wxMessage.getFromUser())
.build();
return m;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,172 @@
package com.github.binarywang.demo.wechat.config;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import com.github.binarywang.demo.wechat.handler.AbstractHandler;
import com.github.binarywang.demo.wechat.handler.KfSessionHandler;
import com.github.binarywang.demo.wechat.handler.LocationHandler;
import com.github.binarywang.demo.wechat.handler.LogHandler;
import com.github.binarywang.demo.wechat.handler.MenuHandler;
import com.github.binarywang.demo.wechat.handler.MsgHandler;
import com.github.binarywang.demo.wechat.handler.NullHandler;
import com.github.binarywang.demo.wechat.handler.StoreCheckNotifyHandler;
import com.github.binarywang.demo.wechat.handler.SubscribeHandler;
import com.github.binarywang.demo.wechat.handler.UnsubscribeHandler;

import me.chanjar.weixin.common.api.WxConsts;
import me.chanjar.weixin.mp.api.WxMpConfigStorage;
import me.chanjar.weixin.mp.api.WxMpInMemoryConfigStorage;
import me.chanjar.weixin.mp.api.WxMpMessageRouter;
import me.chanjar.weixin.mp.api.WxMpService;
import me.chanjar.weixin.mp.api.impl.WxMpServiceImpl;

/**
* wechat mp configuration
*
* @author Binary Wang
*/
@Configuration
@ConditionalOnClass(WxMpService.class)
@EnableConfigurationProperties(WechatMpProperties.class)
public class WechatMpConfiguration {
@Autowired
private WechatMpProperties properties;

@Bean
@ConditionalOnMissingBean
public WxMpConfigStorage configStorage() {
WxMpInMemoryConfigStorage configStorage = new WxMpInMemoryConfigStorage();
configStorage.setAppId(this.properties.getAppId());
configStorage.setSecret(this.properties.getSecret());
configStorage.setToken(this.properties.getToken());
configStorage.setAesKey(this.properties.getAesKey());
configStorage.setPartnerId(this.properties.getPartnerId());
configStorage.setPartnerKey(this.properties.getPartnerKey());
return configStorage;
}

@Bean
@ConditionalOnMissingBean
public WxMpService wxMpService(WxMpConfigStorage configStorage) {
WxMpService wxMpService = new WxMpServiceImpl();
wxMpService.setWxMpConfigStorage(configStorage);
return wxMpService;
}

@Bean
public WxMpMessageRouter router(WxMpService wxMpService) {
final WxMpMessageRouter newRouter = new WxMpMessageRouter(wxMpService);

// 记录所有事件的日志
newRouter.rule().handler(this.logHandler).next();

// 接收客服会话管理事件
newRouter.rule().async(false).msgType(WxConsts.XML_MSG_EVENT)
.event(WxConsts.EVT_KF_CREATE_SESSION)
.handler(this.kfSessionHandler).end();
newRouter.rule().async(false).msgType(WxConsts.XML_MSG_EVENT)
.event(WxConsts.EVT_KF_CLOSE_SESSION).handler(this.kfSessionHandler)
.end();
newRouter.rule().async(false).msgType(WxConsts.XML_MSG_EVENT)
.event(WxConsts.EVT_KF_SWITCH_SESSION)
.handler(this.kfSessionHandler).end();

// 门店审核事件
newRouter.rule().async(false).msgType(WxConsts.XML_MSG_EVENT)
.event(WxConsts.EVT_POI_CHECK_NOTIFY)
.handler(this.storeCheckNotifyHandler).end();

// 自定义菜单事件
newRouter.rule().async(false).msgType(WxConsts.XML_MSG_EVENT)
.event(WxConsts.BUTTON_CLICK).handler(this.getMenuHandler()).end();

// 点击菜单连接事件
newRouter.rule().async(false).msgType(WxConsts.XML_MSG_EVENT)
.event(WxConsts.BUTTON_VIEW).handler(this.nullHandler).end();

// 关注事件
newRouter.rule().async(false).msgType(WxConsts.XML_MSG_EVENT)
.event(WxConsts.EVT_SUBSCRIBE).handler(this.getSubscribeHandler())
.end();

// 取消关注事件
newRouter.rule().async(false).msgType(WxConsts.XML_MSG_EVENT)
.event(WxConsts.EVT_UNSUBSCRIBE)
.handler(this.getUnsubscribeHandler()).end();

// 上报地理位置事件
newRouter.rule().async(false).msgType(WxConsts.XML_MSG_EVENT)
.event(WxConsts.EVT_LOCATION).handler(this.getLocationHandler())
.end();

// 接收地理位置消息
newRouter.rule().async(false).msgType(WxConsts.XML_MSG_LOCATION)
.handler(this.getLocationHandler()).end();

// 扫码事件
newRouter.rule().async(false).msgType(WxConsts.XML_MSG_EVENT)
.event(WxConsts.EVT_SCAN).handler(this.getScanHandler()).end();

// 默认
newRouter.rule().async(false).handler(this.getMsgHandler()).end();

return newRouter;
}

@Autowired
private LocationHandler locationHandler;

@Autowired
private MenuHandler menuHandler;

@Autowired
private MsgHandler msgHandler;

@Autowired
protected LogHandler logHandler;

@Autowired
protected NullHandler nullHandler;

@Autowired
protected KfSessionHandler kfSessionHandler;

@Autowired
protected StoreCheckNotifyHandler storeCheckNotifyHandler;

@Autowired
private UnsubscribeHandler unsubscribeHandler;

@Autowired
private SubscribeHandler subscribeHandler;

protected MenuHandler getMenuHandler() {
return this.menuHandler;
}

protected SubscribeHandler getSubscribeHandler() {
return this.subscribeHandler;
}

protected UnsubscribeHandler getUnsubscribeHandler() {
return this.unsubscribeHandler;
}

protected AbstractHandler getLocationHandler() {
return this.locationHandler;
}

protected MsgHandler getMsgHandler() {
return this.msgHandler;
}

protected AbstractHandler getScanHandler() {
return null;
}

}
Loading

0 comments on commit 830689e

Please sign in to comment.