新增weixin4j-example示例工程

This commit is contained in:
jinyu 2016-04-10 14:46:37 +08:00
parent 0a35cf9d9b
commit 525246212b
13 changed files with 522 additions and 34 deletions

View File

@ -44,6 +44,7 @@
<module>weixin4j-mp</module>
<module>weixin4j-qy</module>
<module>weixin4j-server</module>
<module>weixin4j-example</module>
</modules>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>

32
weixin4j-example/.gitignore vendored Normal file
View File

@ -0,0 +1,32 @@
# Mobile Tools for Java (J2ME)
.mtj.tmp/
# Package Files #
*.jar
*.war
*.ear
# virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml
hs_err_pid*
*~
# eclipse ignore
*.settings/*
/.project
/.classpath
/.tomcatplugin
# idea ignore
/.idea
*.iml
# maven ignore
target/*
# other ignore
*.log
*.tmp
Thumbs.db
/target/
.DS_Store

38
weixin4j-example/pom.xml Normal file
View File

@ -0,0 +1,38 @@
<?xml version="1.0"?>
<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>com.foxinmy</groupId>
<artifactId>weixin4j</artifactId>
<version>1.6.8</version>
</parent>
<artifactId>weixin4j-example</artifactId>
<version>1.0</version>
<name>weixin4j-example</name>
<url>https://github.com/foxinmy/weixin4j/tree/master/weixin4j-example</url>
<description>weixin4j示例</description>
<dependencies>
<dependency>
<groupId>com.foxinmy</groupId>
<artifactId>weixin4j-mp</artifactId>
<version>1.6.8</version>
</dependency>
<dependency>
<groupId>com.foxinmy</groupId>
<artifactId>weixin4j-qy</artifactId>
<version>1.6.8</version>
</dependency>
<dependency>
<groupId>com.foxinmy</groupId>
<artifactId>weixin4j-server</artifactId>
<version>1.1.7</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>4.2.1.RELEASE</version>
<scope>test</scope>
</dependency>
</dependencies>
</project>

View File

@ -0,0 +1,90 @@
package com.foximy.weixin4j.example.server;
import io.netty.util.internal.logging.InternalLoggerFactory;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import com.foxinmy.weixin4j.exception.WeixinException;
import com.foxinmy.weixin4j.spring.SpringBeanFactory;
import com.foxinmy.weixin4j.startup.WeixinServerBootstrap;
import com.foxinmy.weixin4j.util.AesToken;
/**
* 微信消息服务:需要另外开启一个线程去启动服务,比如在spring mvc中
*
* @className Weixin4jServerStartupWithThread
* @author jy
* @date 2015年5月7日
* @since JDK 1.7
* @see
*/
public class Weixin4jServerStartupWithThread implements ApplicationContextAware {
/**
* 服务监听的端口号,目前微信只支持80端口,可以考虑用nginx做转发到此端口
*/
private final int port;
/**
* 服务器token信息
*/
/**
* 明文模式:String aesToken = ""; 密文模式:AesToken aesToken = new
* AesToken("公众号appid", "公众号token","公众号加密/解密消息的密钥");
*/
private final AesToken aesToken;
/**
* 处理微信消息的全限包名(也可通过addHandler方式一个一个添加)
*/
private final String handlerPackage;
/**
* 用spring去获取bean
*/
private ApplicationContext applicationContext;
private Weixin4jServerStartupWithThread(int port, AesToken aesToken,
String handlerPackage) {
this.port = port;
this.aesToken = aesToken;
this.handlerPackage = handlerPackage;
}
@Override
public void setApplicationContext(ApplicationContext applicationContext)
throws BeansException {
this.applicationContext = applicationContext;
}
private ExecutorService executor;
/**
* 启动函数
*
* @throws WeixinException
*/
public void start() {
executor = Executors.newCachedThreadPool();
executor.execute(new Runnable() {
@Override
public void run() {
try {
new WeixinServerBootstrap(aesToken)
.handlerPackagesToScan(handlerPackage)
.resolveBeanFactory(
new SpringBeanFactory(applicationContext))
.openAlwaysResponse().startup(port);
} catch (WeixinException e) {
InternalLoggerFactory.getInstance(getClass()).error(
"weixin4j server startup:FAIL", e);
}
}
});
}
public void stop() {
executor.shutdown();
}
}

View File

@ -0,0 +1,52 @@
package com.foximy.weixin4j.example.server;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.foxinmy.weixin4j.exception.WeixinException;
import com.foxinmy.weixin4j.spring.SpringBeanFactory;
import com.foxinmy.weixin4j.startup.WeixinServerBootstrap;
/**
* 微信消息服务:单独作为一个服务jar包启动
*
* @className Weixin4jServerStartupWithoutThread
* @author jy
* @date 2015年5月7日
* @since JDK 1.7
* @see
*/
public class Weixin4jServerStartupWithoutThread {
/**
* 服务监听的端口号,目前微信只支持80端口,可以考虑用nginx做转发到此端口
*/
private static int port = 10000;
/**
* 服务器token信息
*/
/**
* 明文模式:String aesToken = ""; 密文模式:AesToken aesToken = new
* AesToken("公众号appid", "公众号token","公众号加密/解密消息的密钥");
*/
private static String aesToken = "weixin4j";
/**
* 处理微信消息的全限包名(也可通过addHandler方式一个一个添加)
*/
private static String handlerPackage = "com.foximy.weixin4j.example.server.handler";
/**
* 入口函数 可使用assembly插件打成可执行zip包:https://github.com/foxinmy/weixin4j/wiki/
* assembly%E6%89%93%E5%8C%85
*
* @param args
* @throws WeixinException
*/
public static void main(String[] args) throws WeixinException {
ApplicationContext applicationContext = new ClassPathXmlApplicationContext(
new String[] { "classpath:/spring-bean.xml" });
new WeixinServerBootstrap(aesToken)
.handlerPackagesToScan(handlerPackage).openAlwaysResponse()
.resolveBeanFactory(new SpringBeanFactory(applicationContext))
.startup(port);
}
}

View File

@ -0,0 +1,42 @@
package com.foximy.weixin4j.example.server.handler;
import com.foxinmy.weixin4j.exception.WeixinException;
import com.foxinmy.weixin4j.handler.MessageHandlerAdapter;
import com.foxinmy.weixin4j.message.TextMessage;
import com.foxinmy.weixin4j.request.WeixinRequest;
import com.foxinmy.weixin4j.response.TextResponse;
import com.foxinmy.weixin4j.response.WeixinResponse;
/**
* 输入 hello 回复 world
*
* @className HelloMessageHandler
* @author jy
* @date 2015年12月27日
* @since JDK 1.7
* @see
*/
public class HelloMessageHandler extends MessageHandlerAdapter<TextMessage> {
@Override
public boolean canHandle0(WeixinRequest request, TextMessage message)
throws WeixinException {
return message.getContent().equalsIgnoreCase("hello");
}
@Override
public WeixinResponse doHandle0(WeixinRequest request, TextMessage message)
throws WeixinException {
/**
* 返回用户world文本
*/
return new TextResponse("world");
}
/**
* 提高权重 > TextMessageHandler
*/
@Override
public int weight() {
return 2;
}
}

View File

@ -0,0 +1,27 @@
package com.foximy.weixin4j.example.server.handler;
import com.foxinmy.weixin4j.exception.WeixinException;
import com.foxinmy.weixin4j.handler.MessageHandlerAdapter;
import com.foxinmy.weixin4j.mp.event.ScribeEventMessage;
import com.foxinmy.weixin4j.request.WeixinRequest;
import com.foxinmy.weixin4j.response.TextResponse;
import com.foxinmy.weixin4j.response.WeixinResponse;
/**
* 处理关注消息
*
* @className SubscribeMessageHandler
* @author jy
* @date 2015年12月3日
* @since JDK 1.7
* @see
*/
public class SubscribeMessageHandler extends
MessageHandlerAdapter<ScribeEventMessage> {
@Override
public WeixinResponse doHandle0(WeixinRequest arg0, ScribeEventMessage arg1)
throws WeixinException {
return new TextResponse("欢迎关注~");
}
}

View File

@ -0,0 +1,26 @@
package com.foximy.weixin4j.example.server.handler;
import com.foxinmy.weixin4j.exception.WeixinException;
import com.foxinmy.weixin4j.handler.MessageHandlerAdapter;
import com.foxinmy.weixin4j.message.TextMessage;
import com.foxinmy.weixin4j.request.WeixinRequest;
import com.foxinmy.weixin4j.response.TextResponse;
import com.foxinmy.weixin4j.response.WeixinResponse;
/**
* 文本消息处理
*
* @className TextMessageHandler
* @author jy
* @date 2015年11月18日
* @since JDK 1.7
* @see
*/
public class TextMessageHandler extends MessageHandlerAdapter<TextMessage> {
@Override
public WeixinResponse doHandle0(WeixinRequest request, TextMessage message)
throws WeixinException {
return new TextResponse("收到了文本消息");
}
}

View File

@ -0,0 +1,29 @@
package com.foximy.weixin4j.example.server.handler;
import com.foxinmy.weixin4j.exception.WeixinException;
import com.foxinmy.weixin4j.handler.MessageHandlerAdapter;
import com.foxinmy.weixin4j.message.VoiceMessage;
import com.foxinmy.weixin4j.request.WeixinRequest;
import com.foxinmy.weixin4j.response.TextResponse;
import com.foxinmy.weixin4j.response.WeixinResponse;
/**
* 只处理语音消息
*
* @className VoiceMessageHandler
* @author jy
* @date 2015年11月18日
* @since JDK 1.7
* @see
*/
public class VoiceMessageHandler extends MessageHandlerAdapter<VoiceMessage> {
@Override
public WeixinResponse doHandle0(WeixinRequest request, VoiceMessage message)
throws WeixinException {
/**
* 返回一段文字给用户
*/
return new TextResponse("你讲了一句话");
}
}

View File

@ -0,0 +1,102 @@
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx" xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.1.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.1.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.1.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.1.xsd">
<!-- 微信接口代理start -->
<bean id="weixinProxy" class="com.foxinmy.weixin4j.mp.WeixinProxy">
<constructor-arg>
<bean class="com.foxinmy.weixin4j.util.Weixin4jSettings">
<!-- 公众号信息:不声明则默认使用weixin4j.properties配置的weixin4j.account字段 -->
<constructor-arg>
<bean class="com.foxinmy.weixin4j.model.WeixinAccount">
<constructor-arg index="0" value="公众账号的appid" />
<constructor-arg index="1" value="公众账号的appsecret" />
</bean>
</constructor-arg>
<!-- token存储:不声明则默认使用文件方式(FileTokenStorager)保存 -->
<property name="tokenStorager">
<!-- redis保存token -->
<bean class="com.foxinmy.weixin4j.token.RedisTokenStorager">
<constructor-arg type="redis.clients.jedis.JedisPool">
<bean class="redis.clients.jedis.JedisPool">
<constructor-arg index="0">
<bean class="redis.clients.jedis.JedisPoolConfig">
<property name="maxTotal" value="${redis.maxTotal}" />
<property name="maxIdle" value="${redis.maxIdle}" />
<property name="maxWaitMillis" value="${redis.maxWaitMillis}" />
<property name="testOnBorrow" value="${redis.testOnBorrow}" />
</bean>
</constructor-arg>
<constructor-arg index="1" value="${redis.host}" />
<constructor-arg index="2" value="${redis.port}" />
<constructor-arg index="3" value="${redis.timeout}" />
</bean>
</constructor-arg>
</bean>
</property>
<!-- http参数:http请求时的参数,比如代理、超时等配置信息,暂时还未实现 -->
<property name="httpParams">
<bean class="com.foxinmy.weixin4j.http.HttpParams" />
</property>
<!-- 临时目录:weixin4j调用某些接口时需要用到的临时目录,不声明则获取顺序为:weixin4j.properties#weixin4j.tmpdir->`java.io.tmp` -->
<property name="tmpdir" value="/tmp/weixin4j" />
</bean>
</constructor-arg>
</bean>
<!-- 微信接口代理end -->
<!-- 微信支付接口代理start -->
<bean id="weixinPayProxy" class="com.foxinmy.weixin4j.payment.WeixinPayProxy">
<constructor-arg>
<bean class="com.foxinmy.weixin4j.util.Weixin4jSettings">
<!-- 商户信息:不声明则默认使用weixin4j.properties配置的weixin4j.account字段 -->
<constructor-arg>
<bean class="com.foxinmy.weixin4j.model.WeixinPayAccount">
<constructor-arg index="0" value="公众账号的appid" />
<constructor-arg index="1" value="商户平台的支付密钥:paySignkey" />
<constructor-arg index="2" value="商户平台的商户ID:mchId" />
</bean>
</constructor-arg>
<!-- ca证书:调用某些支付接口(如退款、红包)需要用到的证书文件,不声明则获取顺序为:weixin4j.properties#weixin4j.certificate.file->`classpath:ca.p12` -->
<property name="certificateFile" value="/path/to/certificate/file" />
<!-- http参数:http请求时的参数,比如代理、超时等配置信息,暂时还未实现 -->
<property name="httpParams">
<bean class="com.foxinmy.weixin4j.http.HttpParams" />
</property>
<!-- 临时目录:weixin4j调用某些接口时需要用到的临时目录,不声明则获取顺序为:weixin4j.properties#weixin4j.tmpdir->`java.io.tmp` -->
<property name="tmpdir" value="/tmp/weixin4j" />
</bean>
</constructor-arg>
</bean>
<!-- 微信支付接口代理end -->
<!-- 微信消息服务start -->
<bean
class="com.foximy.weixin4j.example.server.Weixin4jServerStartupWithThread"
init-method="start" destroy-method="stop">
<!-- 端口号 微信暂时只支持80端口 所以需要自己把微信被动消息请求转发(nginx等)到这个端口上来 -->
<constructor-arg index="0" value="10000" />
<!-- token信息 -->
<constructor-arg index="1">
<!-- 明文模式 -->
<bean class="com.foxinmy.weixin4j.util.AesToken">
<constructor-arg index="0" value="weixin4j" />
</bean>
<!-- 加密模式 -->
<!-- bean class="com.foxinmy.weixin4j.util.AesToken"> <constructor-arg
index="0" value="公众号的应用ID(appid/corpid)" /> <constructor-arg index="1" value="开发者Token"
/> <constructor-arg index="2" value="解密的EncodingAESKey" /> </bean -->
</constructor-arg>
<!-- 处理微信消息的全限包名 -->
<constructor-arg index="2"
value="com.foximy.weixin4j.example.server.handler" />
</bean>
<!-- 微信消息服务end -->
</beans>

View File

@ -0,0 +1,18 @@
# \u516c\u4f17\u53f7\u4fe1\u606f \u8bf7\u6309\u9700\u586b\u5199
weixin4j.account={"id":"wx4ab8f8de58159a57","secret":"1d4eb0f4bf556aaed539f30ed05ca795",\
"mchId":"\u5fae\u4fe1\u5546\u6237\u53f7 \u5fae\u4fe1\u652f\u4ed8\u65f6\u9700\u8981\u586b\u5165",\
"certificateKey":"\u52a0\u8f7d\u652f\u4ed8\u8bc1\u4e66\u6587\u4ef6\u7684\u5bc6\u7801 \u5982\u679c\u4e0d\u586b\u5199\u5219\u9ed8\u8ba4\u83b7\u53d6mchId\u4f5c\u4e3a\u5bc6\u7801",\
"paySignKey":"\u5fae\u4fe1\u652f\u4ed8\u4e2d\u8c03\u7528API\u7684\u5bc6\u94a5 \u5fae\u4fe1\u652f\u4ed8\u65f6\u9700\u8981\u586b\u5165"}
# weixin4j\u7684\u4e34\u65f6\u76ee\u5f55
# \u53ef\u80fd\u5b58\u653etoken\u6587\u4ef6\u3001\u4e8c\u7ef4\u7801\u6587\u4ef6\u3001\u5a92\u4f53\u6587\u4ef6\u3001\u5bf9\u8d26\u5355\u6587\u4ef6\u7b49
# \u4e3a\u7a7a\u65f6\u5219\u83b7\u53d6java.io.tmpdir\u4e34\u65f6\u76ee\u5f55
weixin4j.tmpdir=
# \u5fae\u4fe1\u652f\u4ed8\u67d0\u4e9b\u63a5\u53e3\u9700\u8981\u7684ca\u8bc1\u4e66\u5b58\u653e\u7684\u5b8c\u6574\u8def\u5f84
# classpath\u8def\u5f84\u4e0b\u53ef\u4ee5\u8fd9\u4e48\u5199
# weixin4j.certificate.file=classpath:xxxxx.p12
# \u4e3a\u7a7a\u65f6\u5219\u83b7\u53d6classpath\u6839\u76ee\u5f55\u4e0b\u7684ca.p12\u6587\u4ef6
weixin4j.certificate.file=/tmp/weixin4j/xxxxx.p12
# \u7528\u6237oauth\u6388\u6743\u540e\u91cd\u5b9a\u5411\u7684url(\u5728\u4f7f\u7528OauthApi\u65f6\u586b\u5199)
weixin4j.user.oauth.redirect.uri=

View File

@ -72,7 +72,7 @@ public class WeixinMessageDecoder extends
&& encryptType == EncryptType.AES) {
if (ServerToolkits.isBlank(aesToken.getAesKey())) {
throw new WeixinException(
"AESEncodingKey not be null in AES mode");
"EncodingAESKey not be empty in safety(AES) mode");
}
EncryptMessageHandler encryptHandler = EncryptMessageHandler
.parser(messageContent);

View File

@ -2,8 +2,11 @@ package com.foxinmy.weixin4j.server.test;
import java.util.Set;
import org.springframework.context.ApplicationContext;
import io.netty.channel.ChannelHandlerContext;
import com.foxinmy.weixin4j.dispatcher.BeanFactory;
import com.foxinmy.weixin4j.exception.WeixinException;
import com.foxinmy.weixin4j.handler.DebugMessageHandler;
import com.foxinmy.weixin4j.handler.MessageHandlerAdapter;
@ -11,11 +14,13 @@ import com.foxinmy.weixin4j.handler.MultipleMessageHandlerAdapter;
import com.foxinmy.weixin4j.handler.WeixinMessageHandler;
import com.foxinmy.weixin4j.interceptor.WeixinMessageInterceptor;
import com.foxinmy.weixin4j.message.TextMessage;
import com.foxinmy.weixin4j.message.VoiceMessage;
import com.foxinmy.weixin4j.mp.event.ScanEventMessage;
import com.foxinmy.weixin4j.request.WeixinMessage;
import com.foxinmy.weixin4j.request.WeixinRequest;
import com.foxinmy.weixin4j.response.TextResponse;
import com.foxinmy.weixin4j.response.WeixinResponse;
import com.foxinmy.weixin4j.spring.SpringBeanFactory;
import com.foxinmy.weixin4j.startup.WeixinServerBootstrap;
/**
@ -37,53 +42,88 @@ public class MessageServerStartup {
final String aesKey = "";
/**
* 明文模式
* 调试输出用户发来的消息
*
* @throws WeixinException
*/
public void test1() throws WeixinException {
// 所有请求都回复调试的文本消息
// 明文模式
new WeixinServerBootstrap(token).addHandler(DebugMessageHandler.global)
.startup();
}
/**
* 密文模式
*
* @throws WeixinException
*/
public void test2() throws WeixinException {
// 所有请求都回复调试的文本消息
// 密文模式
new WeixinServerBootstrap(weixinId, token, aesKey).addHandler(
DebugMessageHandler.global).startup();
}
/**
* 针对特定消息回复
* 针对特定消息类型
*
* @throws WeixinException
*/
public void test3() throws WeixinException {
public void test2() throws WeixinException {
// 针对文本消息回复
WeixinMessageHandler messageHandler = new MessageHandlerAdapter<TextMessage>() {
WeixinMessageHandler textMessageHandler = new MessageHandlerAdapter<TextMessage>() {
@Override
public WeixinResponse doHandle0(WeixinRequest request,
TextMessage message) throws WeixinException {
return new TextResponse("HelloWorld!");
}
};
// 当消息类型为文本(text)时回复HelloWorld, 否则回复调试消息
// 针对语音消息回复
WeixinMessageHandler voiceMessageHandler = new MessageHandlerAdapter<VoiceMessage>() {
@Override
public WeixinResponse doHandle0(WeixinRequest request,
VoiceMessage message) throws WeixinException {
return new TextResponse("HelloWorld!");
}
};
// 当消息类型为文本(text)或者语音时回复HelloWorld, 否则回复调试消息
new WeixinServerBootstrap(weixinId, token, aesKey).addHandler(
messageHandler, DebugMessageHandler.global).startup();
textMessageHandler, voiceMessageHandler,
DebugMessageHandler.global).startup();
}
/**
* 多种消息类型处理
*
* @throws WeixinException
*/
public void test3() throws WeixinException {
@SuppressWarnings("unchecked")
MultipleMessageHandlerAdapter messageHandler = new MultipleMessageHandlerAdapter(
ScanEventMessage.class, TextMessage.class) {
@Override
public WeixinResponse doHandle(WeixinRequest request,
WeixinMessage message, Set<String> nodeNames)
throws WeixinException {
return new TextResponse("处理了扫描和文字消息");
}
};
new WeixinServerBootstrap(token).addHandler(messageHandler,
DebugMessageHandler.global).startup();
}
/**
* 扫描包添加handler
*
* @throws WeixinException
*/
public void test4() throws WeixinException {
// 扫描包加载消息处理器
// handler处理所在的包名(子包也会扫描)
String packageToScan = "com.foxinmy.weixin4j.handler";
// handler默认使用 Class.newInstance
// 方式实例化,如果handler中含有service等类需要注入,可以声明一个BeanFactory,如SpringBeanFactory
ApplicationContext applicationContext = null; // spring容器
BeanFactory beanFactory = new SpringBeanFactory(applicationContext);
new WeixinServerBootstrap(token).handlerPackagesToScan(packageToScan)
.startup();
.openAlwaysResponse().resolveBeanFactory(beanFactory).startup();
}
/**
* 拦截器应用
*
* @throws WeixinException
*/
public void test5() throws WeixinException {
// 拦截所有请求
WeixinMessageInterceptor interceptor = new WeixinMessageInterceptor() {
@ -120,21 +160,12 @@ public class MessageServerStartup {
.openAlwaysResponse().startup();
}
@SuppressWarnings("unchecked")
public void test6() throws WeixinException {
MultipleMessageHandlerAdapter messageHandler = new MultipleMessageHandlerAdapter(
ScanEventMessage.class, TextMessage.class) {
@Override
public WeixinResponse doHandle(WeixinRequest request,
WeixinMessage message, Set<String> nodeNames)
throws WeixinException {
return new TextResponse("处理了扫描和文字消息");
}
};
new WeixinServerBootstrap(token).addHandler(messageHandler,
DebugMessageHandler.global).startup();
}
/**
* main方法入口
*
* @param args
* @throws Exception
*/
public static void main(String[] args) throws Exception {
new MessageServerStartup().test1();
}