diff --git a/README.md b/README.md index c10376e1..ef29200b 100644 --- a/README.md +++ b/README.md @@ -4,6 +4,10 @@ weixin4j 微信开发工具包 ------------- +项目说明 +------- +`weixin4j`是一个用java编写针对微信开发的工具包,包含`weixin4j-mp`(微信公众平台)、`weixin4j-qy`(微信企业号)和`weixin4j-netty`(微信被动消息服务器)三个工程 + 功能列表 ------- * **weixin4j-mp** @@ -19,14 +23,6 @@ weixin4j * **weixin4j-server** `netty服务器&消息分发` - -项目说明 -------- -+ `weixin4j`包含「微信公众平台」和「微信企业号」的API封装. - -+ API的成功调用依赖于正确的appid等数据,填写格式说明见API工程下的README.md文件. - -+ netty服务正在重构中 如何获取 ---------- @@ -45,9 +41,13 @@ weixin4j weixin4j-qy 1.4 -微信被动消息服务器 +微信被动消息服务器(本周内部署到中央库) - `正在重构中..` + + com.foxinmy + weixin4j-server + 0.0.1 + 以上依赖如果出现Missing artifact错误 请尝试在eclipse里这么做 @@ -69,10 +69,6 @@ https://github.com/foxinmy/weixin4j/releases 接下来 ------ -* 被动消息服务重构 - -![消息分发](http://7mj4zs.com1.z0.glb.clouddn.com/weixin4j.png) - * 公众号第三方服务应用 * 企业号第三方应用 & 企业号登陆授权 diff --git a/pom.xml b/pom.xml index 9ca26b4f..e6210ef9 100644 --- a/pom.xml +++ b/pom.xml @@ -43,15 +43,11 @@ weixin4j-base weixin4j-mp weixin4j-qy - weixin4j-server - 1.7 4.8.2 1.6.1 - 1.0.9 - 1.7.6 - 1.7.6 + 1.7.6 1.4.7 4.2.5 1.9 @@ -60,35 +56,25 @@ 1.1.6 2.6.0 3.9 - 4.0.23.Final 3.3.2 1.9 UTF-8 - 2.4 - 3.0 - 2.6 - 2.5 - 2.5.1 - 2.10.1 - 2.8.2 - 1.5 - 2.18 org.apache.maven.plugins maven-compiler-plugin - ${maven.compiler.plugin.version} + 3.0 - ${jdk.version} - ${jdk.version} + 1.7 + 1.7 org.apache.maven.plugins maven-resources-plugin - ${maven.resources.plugin.version} + 2.6 ${project.build.sourceEncoding} @@ -103,7 +89,7 @@ org.apache.maven.plugins maven-jar-plugin - ${maven.jar.plugin.version} + 2.5 default-jar @@ -129,29 +115,10 @@ - - org.apache.maven.plugins - maven-assembly-plugin - ${maven.assembly.plugin.version} - - - src/main/assembly.xml - - - - - make-assembly - package - - single - - - - org.apache.maven.plugins maven-source-plugin - ${maven.source.plugin.version} + 2.4 attach-sources @@ -164,7 +131,7 @@ org.apache.maven.plugins maven-javadoc-plugin - ${maven.javadoc.plugin.version} + 2.10.1 attach-javadocs @@ -186,12 +153,12 @@ org.apache.maven.plugins maven-deploy-plugin - ${maven.deploy.plugin.version} + 2.8.2 org.apache.maven.plugins maven-gpg-plugin - ${maven.gpg.plugin.version} + 1.5 verify @@ -204,7 +171,7 @@ org.apache.maven.plugins maven-surefire-plugin - ${maven.surefire.plugin.version} + 2.18 true diff --git a/weixin4j-mp/README.md b/weixin4j-mp/README.md index ab9562d8..db11ae53 100644 --- a/weixin4j-mp/README.md +++ b/weixin4j-mp/README.md @@ -41,7 +41,7 @@ weixin4j-mp 如何使用 -------- -1.API工程可以单独打包到其他项目中使用,需新增或拷贝`weixin.properties`文件到项目的`classpath`中 +1.需新增或拷贝`weixin.properties`文件到项目的`classpath`中 weixin.properties说明 @@ -74,7 +74,7 @@ weixin.properties说明 ca_file=/tmp/weixin/xxxxx.p12 #classpath路径下:ca_file=classpath:xxxxx.p12 - #微信登陆授权的重定向路径 + #微信登陆授权的重定向路径(使用OauthApi时需要填写) redirect_uri=http://xxx 2.实例化一个`WeixinProxy`对象,调用API,需要强调的是如果只传入appid,appsecret两个参数将无法调用支付相关接口 diff --git a/weixin4j-mp/pom.xml b/weixin4j-mp/pom.xml index 8680ece3..838670c6 100644 --- a/weixin4j-mp/pom.xml +++ b/weixin4j-mp/pom.xml @@ -50,7 +50,7 @@ org.slf4j slf4j-api - ${jcl.over.version} + ${jcl.version} jaxen diff --git a/weixin4j-qy/README.md b/weixin4j-qy/README.md index 300f67b6..501ec9ae 100644 --- a/weixin4j-qy/README.md +++ b/weixin4j-qy/README.md @@ -25,7 +25,7 @@ weixin4j-qy 如何使用 -------- -1.API工程可以单独打包到其他项目中使用,需新增或拷贝`weixin.properties`文件到项目的`classpath`中 +1.需新增或拷贝`weixin.properties`文件到项目的`classpath`中 weixin.properties说明 diff --git a/weixin4j-server/README.md b/weixin4j-server/README.md index e66d2626..9c5ceca2 100644 --- a/weixin4j-server/README.md +++ b/weixin4j-server/README.md @@ -7,61 +7,123 @@ weixin4j-server 功能列表 ------- -* `netty构建服务器` +* `netty服务器` * `消息分发` +* `消息拦截` + 如何使用 --------- -1.正确填写`weixin.properties`中的属性值 +------- +编写一个简单的服务启动类 -| 属性名 | 说明 | -| :---------- | :-------------- | -| account | 微信公众号信息 `json格式` | -| token_path | 使用FileTokenHolder时token保存的物理路径 | -| qr_path | 调用二维码接口时保存二维码图片的物理路径 | -| media_path | 调用媒体接口时保存媒体文件的物理路径 | -| bill_path | 调用下载对账单接口保存excel文件的物理路径 | -| ca_file | 调用某些接口(支付相关)强制需要auth的ca授权文件 | - -示例(properties中换行用右斜杆\\) - - account={"id":"appId","secret":"appSecret",\\ - "token":"开放者的token","openId":"公众号的openid 非必须",\\ - "encodingAesKey":"公众号设置了加密方式且为「安全模式」时需要填入",\\ - "mchId":"V3.x版本下的微信商户号",\\ - "partnerId":"V2版本下的财付通的商户号","partnerKey":"V2版本下的财付通商户权限密钥Key",\\ - "version":"针对微信支付的版本号(目前可能为2,3),如果不填则按照mchId非空与否来做判断",\\ - "paySignKey":"微信支付中调用API的密钥"} - - token_path=/tmp/weixin/token - qr_path=/tmp/weixin/qr - media_path=/tmp/weixin/media - bill_path=/tmp/weixin/bill - ca_file=/tmp/weixin/xxxxx.p12 | xxxxx.pfx - -2.在对应的action中实现自己的具体业务 如 TextAction 则表示收到文本消息 - - @Override - public ResponseMessage execute(TextMessage inMessage) { - return new ResponseMessage(new Text("Hello World!"), inMessage); + public class MessageServerStartup{ + public static void main(String[] args) { + new WeixinServerBootstrap("开发者token").pushMessageHandler( + DebugMessageHandler.global).startup(); + } } - -3.`mvn package`,得到一个zip的压缩包,解压(也可使用deploy.xml部署到远程服务器)到`启动目录` +以上代码就会启动一个适用于`明文模式`并总是调试输出微信请求信息的消息服务. -4.启动netty服务(`com.foxinmy.weixin4j.mp.startup.WeixinMpServerBootstrap`) +密文模式的服务启动类 + + public class MessageServerStartup{ + public static void main(String[] args) { + new WeixinServerBootstrap("appid","开发者token","加密密钥").pushMessageHandler( + DebugMessageHandler.global).startup(); + } + } + +只回复文本消息的服务启动类 + + public class MessageServerStartup{ + public static void main(String[] args) { + // 需要一个文本消息的handler + WeixinMessageHandler messageHandler = new WeixinMessageHandler() { + @Override + public WeixinResponse doHandle(WeixinRequest request, + WeixinMessage message) throws WeixinException { + return new TextResponse("HelloWorld!"); + } + + @Override + public boolean canHandle(WeixinRequest request, + WeixinMessage message) { + return message.getMsgType().equals("text"); + } + }; + // 当消息类型为文本(text)时回复「HelloWorld」, 否则回复调试消息 + new WeixinServerBootstrap("appid","开发者token","加密密钥").pushMessageHandler(messageHandler, + DebugMessageHandler.global).startup(); + } + } + +更多内容将会写在wiki里 + +assembly打包(辅助) +----------------- +[assembly](http://maven.apache.org/plugins/maven-assembly-plugin/assembly.html)是maven的一个打包插件,它可以创建一个包含脚本、配置文件以及所有运行时所依赖的元素(jar)Assembly插件能帮你构建一个完整的发布包. + +1.复制[assembly描述](./src/main/assembly.xml)和[启动脚本](./src/main/startup.sh)到自己工程的src/main目录下. + +2.在项目pom.xml中的/bulid/plugins节点新增如下配置 + + + org.apache.maven.plugins + maven-assembly-plugin + 2.5.1 + + + src/main/assembly.xml + + weixin-server + + + + make-assembly + package + + single + + + + +`descriptor`表示[assembly](./src/main/assembly.xml)文件的位置. + +`finalName`表示打包(zip)后的文件名,需配合启动脚本中`APP_HOME`的值使用. + +3.[启动脚本](./src/main/startup.sh)中`JAVA_HOME`为java运行环境(jre|jdk)的安装根目录,如果与脚本中的值不一致,可使用`ln -s t/usr/local/java 实际的目录`. + +4.[启动脚本](./src/main/startup.sh)中`APP_HOME`的值为服务的启动目录,相当于运行服务时的classpath目录. + +5.修改[启动脚本](./src/main/startup.sh)中`APP_MAINCLASS`的值为上述编写的netty服务启动类. + +6.执行`mvn package`命令后在target目录下得到一个zip的压缩包,在7或者8中选择一种方式启动服务. + +7.[上传zip包到服务器],解压包到启动目录(`APP_HOME`)的`上一级目录`后运行startup.sh脚本. sh startup.sh start - - > 1.服务的启动脚本[startup.sh](./src/main/startup.sh)需要被注意到,有`JAVA_HOME`和`APP_HOME`两个参数. - - > 2.其中`JAVA_HOME`参数值指的是java运行环境(jre|jdk)的安装根目录,如果与脚本中的值不一致,可以改更为实际的路径或者使用`ln -s t/usr/local/java target`软链接命令创建期望的链接. - - > 3.其中`APP_HOME`参数值指的是本服务的启动目录,此目录需要被正确事先创建好,同时[`deploy.xml`](./deploy.xml)远程部署命令也依赖于此. - - > 4.Ant远程部署[`deploy.xml`](./deploy.xml)的正确执行需要[`jsch`](http://www.jcraft.com/jsch/)包的支持,下载jar包将其引入执行Ant命令时的`classpath`中. - - > 5.一般来说*Action事件处理类中应该有自己的实际业务类(`service`)需要被注入,可以使用org.springframework.context.ApplicationContext#getBeansWithAnnotation(ActionAnnotation.class)函数获取Action集合后再来实现[`AbstractActionMapping`](https://github.com/foxinmy/weixin4j/blob/master/weixin4j-base/src/main/java/com/foxinmy/weixin4j/action/mapping/AbstractActionMapping.java). +8.ant远程部署 + + > 复制[`deploy.xml`](./deploy.xml)到自己工程的根目录下. + + > Ant远程部署[`deploy.xml`](./deploy.xml)的正确执行需要[`jsch`](http://www.jcraft.com/jsch/)包的支持,下载jar包将其引入执行Ant命令时的`classpath`中. + + > 正确填写`zip.name`、`host`、`pwd`、`main.dir`、`sub.dir`五个属性值. + + > 右键 Run as -> Ant Build + [更新LOG](./CHANGE.md) ----------------------- \ No newline at end of file +---------------------- + +相关参考 +------- + +![消息分发时序图](http://7mj4zs.com1.z0.glb.clouddn.com/weixin4j.png) + +[spring-webmvc:DispatcherServlet](https://github.com/spring-projects/spring-framework/blob/master/spring-webmvc/src/main/java/org/springframework/web/servlet/DispatcherServlet.java) + +[spring-webmvc:HandlerAdapter](https://github.com/spring-projects/spring-framework/blob/master/spring-webmvc/src/main/java/org/springframework/web/servlet/HandlerAdapter.java) + +[spring-webmvc:HandlerInterceptor](https://github.com/spring-projects/spring-framework/blob/master/spring-webmvc/src/main/java/org/springframework/web/servlet/HandlerInterceptor.java) \ No newline at end of file diff --git a/weixin4j-server/deploy.xml b/weixin4j-server/deploy.xml index a67919e9..f524dc3b 100644 --- a/weixin4j-server/deploy.xml +++ b/weixin4j-server/deploy.xml @@ -1,12 +1,12 @@ - + - + - + @@ -22,28 +22,28 @@ - + ${host}:停止[${sub.dir}]... - + ${host}:解压${zip.name}... - + ${host}:启动服务... - + target.home = ${target.home} file.zip = ${target.home}/${zip.name} diff --git a/weixin4j-server/pom.xml b/weixin4j-server/pom.xml index 85ff80a9..91370299 100644 --- a/weixin4j-server/pom.xml +++ b/weixin4j-server/pom.xml @@ -2,58 +2,210 @@ 4.0.0 - - com.foxinmy - weixin4j - 1.4 - + com.foxinmy weixin4j-server + 0.0.1 weixin4j-server https://github.com/foxinmy/weixin4j/tree/master/weixin4j-server 微信netty服务 + 2014 + + + Apache License, Version 2.0 + http://www.apache.org/licenses/LICENSE-2.0.txt + repo + A business-friendly OSS license + + + + https://github.com/foxinmy/weixin4j + scm:git:git://github.com/foxinmy/weixin4j.git + scm:git:git@github.com:foxinmy/weixin4j.git + HEAD + + + https://github.com/foxinmy/weixin4j/issues + + + + hujinyuhao@163.com + jinyu + jinyu + https://github.com/foxinmy + +8 + + java developer + + + + + UTF-8 + org.apache.maven.plugins - maven-assembly-plugin + maven-compiler-plugin + 3.0 - weixin-server + 1.7 + 1.7 org.apache.maven.plugins - maven-deploy-plugin + maven-resources-plugin + 2.6 - true + ${project.build.sourceEncoding} + + + pem + pfx + p12 + jks + + + org.apache.maven.plugins + maven-jar-plugin + 2.5 + + + default-jar + package + + jar + + + target/classes + + *.properties + *.xml + *.txt + **/*.md + + + false + + + + + + + + + org.apache.maven.plugins + maven-source-plugin + 2.4 + + + attach-sources + + jar-no-fork + + + + + + org.apache.maven.plugins + maven-javadoc-plugin + 2.10.1 + + + attach-javadocs + + jar + + + + + public + ${project.build.sourceEncoding} + ${project.build.sourceEncoding} + ${project.build.sourceEncoding} + + http://docs.oracle.com/javase/7/docs/api + + + + + org.apache.maven.plugins + maven-deploy-plugin + 2.8.2 + + + org.apache.maven.plugins + maven-gpg-plugin + 1.5 + + + verify + + sign + + + + + + org.apache.maven.plugins + maven-surefire-plugin + 2.18 + + true + + + + + + + src/main/java + + **/*.xml + **/*.properties + **/*.pem + **/*.p12 + **/*.pfx + + + + src/main/resources + + *.xml + *.properties + + + io.netty netty-all - ${netty.version} + 4.0.23.Final org.slf4j slf4j-api - ${jcl.over.version} + 1.7.6 junit junit + 4.8.2 + test org.apache.httpcomponents httpcore - ${httpclient.version} + 4.2.5 test org.apache.httpcomponents httpclient - ${httpclient.version} + 4.2.5 commons-codec @@ -67,4 +219,35 @@ test + + + release + + + + org.apache.maven.plugins + maven-source-plugin + + + org.apache.maven.plugins + maven-javadoc-plugin + + + org.apache.maven.plugins + maven-gpg-plugin + + + + + + + + oss-snapshot + https://oss.sonatype.org/content/repositories/snapshots/ + + + oss-release + https://oss.sonatype.org/service/local/staging/deploy/maven2/ + + \ No newline at end of file diff --git a/weixin4j-server/src/main/java/com/foxinmy/weixin4j/request/WeixinMessage.java b/weixin4j-server/src/main/java/com/foxinmy/weixin4j/request/WeixinMessage.java index 33ccbaba..be015608 100644 --- a/weixin4j-server/src/main/java/com/foxinmy/weixin4j/request/WeixinMessage.java +++ b/weixin4j-server/src/main/java/com/foxinmy/weixin4j/request/WeixinMessage.java @@ -6,7 +6,7 @@ import javax.xml.bind.annotation.XmlElement; import javax.xml.bind.annotation.XmlRootElement; /** - * 被动消息 + * 被动消息(待完善) * * @className WeixinMessage * @author jy diff --git a/weixin4j-server/src/main/startup.sh b/weixin4j-server/src/main/startup.sh index 781e9bcf..55e9be13 100644 --- a/weixin4j-server/src/main/startup.sh +++ b/weixin4j-server/src/main/startup.sh @@ -9,7 +9,7 @@ RUNNING_USER=root APP_HOME="/usr/local/weixin/weixin-server" #main class -APP_MAINCLASS=com.foxinmy.weixin4j.startup.WeixinServerBootstrap +APP_MAINCLASS=netty服务启动类 #classpath CLASSPATH=$APP_HOME/classes