From 141671c4265604c539bb013c0fbed0c577e9a035 Mon Sep 17 00:00:00 2001 From: yangqisheng Date: Thu, 9 Aug 2018 19:16:34 +0800 Subject: [PATCH] =?UTF-8?q?spring-boot=E9=9B=86=E6=88=90weixin4j=E5=BE=AE?= =?UTF-8?q?=E4=BF=A1=E5=BC=80=E5=8F=91=E8=80=85=E6=8E=A5=E5=85=A5=E6=A1=88?= =?UTF-8?q?=E4=BE=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- weixin4j-example-web/pom.xml | 78 ++++++++++++ .../weixin4j/example/Application.java | 19 +++ .../controller/WeixinJieruController.java | 74 +++++++++++ .../handler/AtsEventMessageHandler.java | 117 ++++++++++++++++++ .../handler/AtsNormalMessageHandler.java | 75 +++++++++++ .../src/main/resources/application.yml | 2 + .../src/main/resources/logback.xml | 89 +++++++++++++ .../src/main/resources/package.xml | 37 ++++++ .../src/main/resources/weixin4j.properties | 49 ++++++++ 9 files changed, 540 insertions(+) create mode 100644 weixin4j-example-web/pom.xml create mode 100644 weixin4j-example-web/src/main/java/com/ansitech/weixin4j/example/Application.java create mode 100644 weixin4j-example-web/src/main/java/com/ansitech/weixin4j/example/controller/WeixinJieruController.java create mode 100644 weixin4j-example-web/src/main/java/com/ansitech/weixin4j/example/handler/AtsEventMessageHandler.java create mode 100644 weixin4j-example-web/src/main/java/com/ansitech/weixin4j/example/handler/AtsNormalMessageHandler.java create mode 100644 weixin4j-example-web/src/main/resources/application.yml create mode 100644 weixin4j-example-web/src/main/resources/logback.xml create mode 100644 weixin4j-example-web/src/main/resources/package.xml create mode 100644 weixin4j-example-web/src/main/resources/weixin4j.properties diff --git a/weixin4j-example-web/pom.xml b/weixin4j-example-web/pom.xml new file mode 100644 index 0000000..0a52c0c --- /dev/null +++ b/weixin4j-example-web/pom.xml @@ -0,0 +1,78 @@ + + + 4.0.0 + com.ansitech + weixin4j-example-web + 1.0.0 + jar + + org.springframework.boot + spring-boot-starter-parent + 1.5.9.RELEASE + + + + + + org.springframework.boot + spring-boot-starter + + + org.springframework.boot + spring-boot-starter-web + + + org.weixin4j + weixin4j + 0.1.1 + + + + UTF-8 + 1.7 + 1.7 + + + + + + org.apache.maven.plugins + maven-jar-plugin + 2.6 + + + + + com.ansitech.weixin4j.example.Application + true + lib/ + + + ./ + + + + + + maven-assembly-plugin + + + false + + + src/main/resources/package.xml + + + + + make-assembly + package + + single + + + + + + + \ No newline at end of file diff --git a/weixin4j-example-web/src/main/java/com/ansitech/weixin4j/example/Application.java b/weixin4j-example-web/src/main/java/com/ansitech/weixin4j/example/Application.java new file mode 100644 index 0000000..0721309 --- /dev/null +++ b/weixin4j-example-web/src/main/java/com/ansitech/weixin4j/example/Application.java @@ -0,0 +1,19 @@ +package com.ansitech.weixin4j.example; + +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; +import org.springframework.context.annotation.ComponentScan; + +/** + * 程序入口 + * + * @author yangqisheng + */ +@SpringBootApplication +@ComponentScan +public class Application { + + public static void main(String[] args) { + SpringApplication.run(Application.class, args); + } +} diff --git a/weixin4j-example-web/src/main/java/com/ansitech/weixin4j/example/controller/WeixinJieruController.java b/weixin4j-example-web/src/main/java/com/ansitech/weixin4j/example/controller/WeixinJieruController.java new file mode 100644 index 0000000..7741be7 --- /dev/null +++ b/weixin4j-example-web/src/main/java/com/ansitech/weixin4j/example/controller/WeixinJieruController.java @@ -0,0 +1,74 @@ +package com.ansitech.weixin4j.example.controller; + +import java.io.IOException; +import javax.servlet.ServletInputStream; +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; +import org.springframework.stereotype.Controller; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RequestMethod; +import org.weixin4j.WeixinException; +import org.weixin4j.spi.HandlerFactory; +import org.weixin4j.spi.IMessageHandler; +import org.weixin4j.util.TokenUtil; + +/** + * 微信开发者接入 + * + * @author yangqisheng + */ +@Controller +@RequestMapping("/weixin/jieru") +public class WeixinJieruController { + + //开发者接入验证 + @RequestMapping(method = RequestMethod.GET) + public void get(HttpServletRequest request, HttpServletResponse response) throws IOException { + //消息来源可靠性验证 + String signature = request.getParameter("signature");// 微信加密签名 + String timestamp = request.getParameter("timestamp");// 时间戳 + String nonce = request.getParameter("nonce"); // 随机数 + //Token为weixin4j.properties中配置的Token + String token = TokenUtil.get(); + //1.验证消息真实性 + //http://mp.weixin.qq.com/wiki/index.php?title=验证消息真实性 + //成为开发者验证 + String echostr = request.getParameter("echostr"); + //确认此次GET请求来自微信服务器,原样返回echostr参数内容,则接入生效,成为开发者成功,否则接入失败 + if (TokenUtil.checkSignature(token, signature, timestamp, nonce)) { + response.getWriter().write(echostr); + } + } + + //接收微信消息 + @RequestMapping(method = RequestMethod.POST) + public void post(HttpServletRequest request, HttpServletResponse response) throws IOException { + //消息来源可靠性验证 + String signature = request.getParameter("signature");// 微信加密签名 + String timestamp = request.getParameter("timestamp");// 时间戳 + String nonce = request.getParameter("nonce"); // 随机数 + //Token为weixin4j.properties中配置的Token + String token = TokenUtil.get(); + //确认此次GET请求来自微信服务器,原样返回echostr参数内容,则接入生效,成为开发者成功,否则接入失败 + if (!TokenUtil.checkSignature(token, signature, timestamp, nonce)) { + //消息不可靠,直接返回 + response.getWriter().write(""); + return; + } + //用户每次向公众号发送消息、或者产生自定义菜单点击事件时,响应URL将得到推送 + try { + response.setCharacterEncoding("UTF-8"); + response.setContentType("text/xml"); + //获取POST流 + ServletInputStream in = request.getInputStream(); + //非注解方式,依然采用消息处理工厂模式调用 + IMessageHandler messageHandler = HandlerFactory.getMessageHandler(); + //处理输入消息,返回结果 + String xml = messageHandler.invoke(in); + //返回结果 + response.getWriter().write(xml); + } catch (IOException | WeixinException ex) { + response.getWriter().write(""); + } + } +} diff --git a/weixin4j-example-web/src/main/java/com/ansitech/weixin4j/example/handler/AtsEventMessageHandler.java b/weixin4j-example-web/src/main/java/com/ansitech/weixin4j/example/handler/AtsEventMessageHandler.java new file mode 100644 index 0000000..92ba219 --- /dev/null +++ b/weixin4j-example-web/src/main/java/com/ansitech/weixin4j/example/handler/AtsEventMessageHandler.java @@ -0,0 +1,117 @@ +package com.ansitech.weixin4j.example.handler; + +import org.weixin4j.model.message.OutputMessage; +import org.weixin4j.model.message.event.ClickEventMessage; +import org.weixin4j.model.message.event.LocationEventMessage; +import org.weixin4j.model.message.event.LocationSelectEventMessage; +import org.weixin4j.model.message.event.PicPhotoOrAlbumEventMessage; +import org.weixin4j.model.message.event.PicSysPhotoEventMessage; +import org.weixin4j.model.message.event.PicWeixinEventMessage; +import org.weixin4j.model.message.event.QrsceneScanEventMessage; +import org.weixin4j.model.message.event.QrsceneSubscribeEventMessage; +import org.weixin4j.model.message.event.ScanCodePushEventMessage; +import org.weixin4j.model.message.event.ScanCodeWaitMsgEventMessage; +import org.weixin4j.model.message.event.SubscribeEventMessage; +import org.weixin4j.model.message.event.UnSubscribeEventMessage; +import org.weixin4j.model.message.event.ViewEventMessage; +import org.weixin4j.model.message.output.TextOutputMessage; +import org.weixin4j.spi.IEventMessageHandler; + +/** + * 自定义事件消息处理器 + * + * @author yangqisheng + */ +public class AtsEventMessageHandler implements IEventMessageHandler { + + @Override + public OutputMessage subscribe(SubscribeEventMessage msg) { + TextOutputMessage out = new TextOutputMessage(); + out.setContent("感谢您的关注!"); + return out; + } + + @Override + public OutputMessage unSubscribe(UnSubscribeEventMessage msg) { + //取消关注 + return null; + } + + @Override + public OutputMessage qrsceneSubscribe(QrsceneSubscribeEventMessage msg) { + TextOutputMessage out = new TextOutputMessage(); + out.setContent("感谢您的关注!,来源:" + msg.getEventKey()); + return out; + } + + @Override + public OutputMessage qrsceneScan(QrsceneScanEventMessage msg) { + TextOutputMessage out = new TextOutputMessage(); + out.setContent("你的消息已经收到!"); + return out; + } + + @Override + public OutputMessage location(LocationEventMessage msg) { + TextOutputMessage out = new TextOutputMessage(); + out.setContent("你的消息已经收到!"); + return out; + } + + @Override + public OutputMessage click(ClickEventMessage msg) { + TextOutputMessage out = new TextOutputMessage(); + out.setContent("点击了菜单!"); + return out; + } + + @Override + public OutputMessage view(ViewEventMessage msg) { + TextOutputMessage out = new TextOutputMessage(); + out.setContent("点击了链接!"); + return out; + } + + @Override + public OutputMessage scanCodePush(ScanCodePushEventMessage msg) { + TextOutputMessage out = new TextOutputMessage(); + out.setContent("扫码!"); + return out; + } + + @Override + public OutputMessage scanCodeWaitMsg(ScanCodeWaitMsgEventMessage msg) { + TextOutputMessage out = new TextOutputMessage(); + out.setContent("扫码等待中!"); + return out; + } + + @Override + public OutputMessage picSysPhoto(PicSysPhotoEventMessage msg) { + TextOutputMessage out = new TextOutputMessage(); + out.setContent("发起拍照!"); + return out; + } + + @Override + public OutputMessage picPhotoOrAlbum(PicPhotoOrAlbumEventMessage msg) { + TextOutputMessage out = new TextOutputMessage(); + out.setContent("选择相册!"); + return out; + } + + @Override + public OutputMessage picWeixin(PicWeixinEventMessage msg) { + TextOutputMessage out = new TextOutputMessage(); + out.setContent("上次图片!"); + return out; + } + + @Override + public OutputMessage locationSelect(LocationSelectEventMessage msg) { + TextOutputMessage out = new TextOutputMessage(); + out.setContent("选择地理位置!"); + return out; + } + +} diff --git a/weixin4j-example-web/src/main/java/com/ansitech/weixin4j/example/handler/AtsNormalMessageHandler.java b/weixin4j-example-web/src/main/java/com/ansitech/weixin4j/example/handler/AtsNormalMessageHandler.java new file mode 100644 index 0000000..35dc226 --- /dev/null +++ b/weixin4j-example-web/src/main/java/com/ansitech/weixin4j/example/handler/AtsNormalMessageHandler.java @@ -0,0 +1,75 @@ +package com.ansitech.weixin4j.example.handler; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.weixin4j.model.message.OutputMessage; +import org.weixin4j.model.message.normal.ImageInputMessage; +import org.weixin4j.model.message.normal.LinkInputMessage; +import org.weixin4j.model.message.normal.LocationInputMessage; +import org.weixin4j.model.message.normal.ShortVideoInputMessage; +import org.weixin4j.model.message.normal.TextInputMessage; +import org.weixin4j.model.message.normal.VideoInputMessage; +import org.weixin4j.model.message.normal.VoiceInputMessage; +import org.weixin4j.model.message.output.TextOutputMessage; +import org.weixin4j.spi.INormalMessageHandler; + +/** + * 自定义普通消息处理器 + * + * @author yangqisheng + */ +public class AtsNormalMessageHandler implements INormalMessageHandler { + + protected final Logger LOG = LoggerFactory.getLogger(AtsNormalMessageHandler.class); + + @Override + public OutputMessage textTypeMsg(TextInputMessage msg) { + LOG.debug("文本消息:" + msg.getContent()); + TextOutputMessage out = new TextOutputMessage(); + out.setContent("您发的消息是:" + msg.getContent()); + return out; + } + + @Override + public OutputMessage imageTypeMsg(ImageInputMessage msg) { + TextOutputMessage out = new TextOutputMessage(); + out.setContent("你的消息已经收到!"); + return out; + } + + @Override + public OutputMessage voiceTypeMsg(VoiceInputMessage msg) { + TextOutputMessage out = new TextOutputMessage(); + out.setContent("你的消息已经收到!"); + return out; + } + + @Override + public OutputMessage videoTypeMsg(VideoInputMessage msg) { + TextOutputMessage out = new TextOutputMessage(); + out.setContent("你的消息已经收到!"); + return out; + } + + @Override + public OutputMessage shortvideoTypeMsg(ShortVideoInputMessage msg) { + TextOutputMessage out = new TextOutputMessage(); + out.setContent("你的消息已经收到!"); + return out; + } + + @Override + public OutputMessage locationTypeMsg(LocationInputMessage msg) { + TextOutputMessage out = new TextOutputMessage(); + out.setContent("你的消息已经收到!"); + return out; + } + + @Override + public OutputMessage linkTypeMsg(LinkInputMessage msg) { + TextOutputMessage out = new TextOutputMessage(); + out.setContent("你的消息已经收到!"); + return out; + } + +} diff --git a/weixin4j-example-web/src/main/resources/application.yml b/weixin4j-example-web/src/main/resources/application.yml new file mode 100644 index 0000000..47fbb02 --- /dev/null +++ b/weixin4j-example-web/src/main/resources/application.yml @@ -0,0 +1,2 @@ +server: + port: 8080 \ No newline at end of file diff --git a/weixin4j-example-web/src/main/resources/logback.xml b/weixin4j-example-web/src/main/resources/logback.xml new file mode 100644 index 0000000..82f8841 --- /dev/null +++ b/weixin4j-example-web/src/main/resources/logback.xml @@ -0,0 +1,89 @@ + + + epay-batch + + + + + %d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{50} - %msg%n + + + + + + ERROR + ACCEPT + DENY + + + ${LOG_HOME}/%d{yyyy-MM-dd}/ats-error.log + 30 + + + %d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{50} - %msg%n + + + + + + + INFO + ACCEPT + DENY + + + ${LOG_HOME}/%d{yyyy-MM-dd}/ats-info.log + 30 + + + %d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{50} - %msg%n + + + + + + + DEBUG + ACCEPT + DENY + + + ${LOG_HOME}/%d{yyyy-MM-dd}/ats-debug.log + 30 + + + %d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{50} - %msg%n + + + + + + + + + + + + + + + ${LOG_HOME}/%d{yyyy-MM-dd}/ats-mybatis.log + 3 + + + %d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{50} - %msg%n + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/weixin4j-example-web/src/main/resources/package.xml b/weixin4j-example-web/src/main/resources/package.xml new file mode 100644 index 0000000..3ac729d --- /dev/null +++ b/weixin4j-example-web/src/main/resources/package.xml @@ -0,0 +1,37 @@ + + + package + + + zip + + true + + + + src/main/bin + bin/ + + + + + + + ${project.build.directory} + / + + *.jar + + + + + + lib + runtime + + ${groupId}:${artifactId} + + + + \ No newline at end of file diff --git a/weixin4j-example-web/src/main/resources/weixin4j.properties b/weixin4j-example-web/src/main/resources/weixin4j.properties new file mode 100644 index 0000000..37d485b --- /dev/null +++ b/weixin4j-example-web/src/main/resources/weixin4j.properties @@ -0,0 +1,49 @@ +#\u5fae\u4fe1SDK\u914d\u7f6e\u6587\u4ef6 +#\u8bfb\u53d6\u89c4\u5219\uff1a\u4f18\u5148\u8bfb\u53d6System.getProperty() +#\u518d\u4eceweixin4j.properties\u8bfb\u53d6,key +#\u5982\u679cSystem.getProperty()\u4e0eweixin4j.properties\u90fd\u6ca1\u8bbe\u7f6e\uff0c\u5219\u9ed8\u8ba4\u672aNULL + +#\u5f00\u53d1\u8005\u8c03\u8bd5\u8bbe\u7f6e +weixin4j.debug=true +#\u516c\u4f17\u53f7Token +weixin4j.token=weixin4j + +#\u516c\u4f17\u53f7\u539f\u59cbID +weixin4j.oauth.originalid= +#\u5f00\u53d1\u8005\u7b2c\u4e09\u65b9\u7528\u6237\u552f\u4e00\u51ed\u8bc1 +weixin4j.oauth.appid= +#\u5f00\u53d1\u8005\u7b2c\u4e09\u65b9\u7528\u6237\u552f\u4e00\u51ed\u8bc1\u5bc6\u94a5 +weixin4j.oauth.secret= +#\u6d88\u606f\u52a0\u5bc6\u65b9\u5f0f 0:\u660e\u6587\u6a21\u5f0f(\u9ed8\u8ba4), 1:\u517c\u5bb9\u6a21\u5f0f, 2:\u5b89\u5168\u6a21\u5f0f(\u63a8\u8350) +weixin4j.oauth.encodingtype=0 +#\u6d88\u606f\u52a0\u5bc6\u5bc6\u94a5(43\u4f4d\u5b57\u7b26\u7ec4\u6210A-Za-z0-9) +weixin4j.oauth.encodingaeskey=0123456789abcedfghijklmnopqrstuvwxyzZXCVBNM +#\u7f51\u9875\u5b89\u5168\u6388\u6743URL +weixin4j.oauth.url= + +#\u516c\u4f17\u5e73\u53f0\u63a5\u53e3\u57df\u540d +#\u901a\u7528\u57df\u540d(api.weixin.qq.com)\uff0c\u4f7f\u7528\u8be5\u57df\u540d\u5c06\u8bbf\u95ee\u5b98\u65b9\u6307\u5b9a\u5c31\u8fd1\u7684\u63a5\u5165\u70b9\uff1b +#\u4e0a\u6d77\u57df\u540d(sh.api.weixin.qq.com)\uff0c\u4f7f\u7528\u8be5\u57df\u540d\u5c06\u8bbf\u95ee\u4e0a\u6d77\u7684\u63a5\u5165\u70b9\uff1b +#\u6df1\u5733\u57df\u540d(sz.api.weixin.qq.com)\uff0c\u4f7f\u7528\u8be5\u57df\u540d\u5c06\u8bbf\u95ee\u6df1\u5733\u7684\u63a5\u5165\u70b9\uff1b +#\u9999\u6e2f\u57df\u540d(hk.api.weixin.qq.com)\uff0c\u4f7f\u7528\u8be5\u57df\u540d\u5c06\u8bbf\u95ee\u9999\u6e2f\u7684\u63a5\u5165\u70b9\u3002 +weixin4j.api.domain=api.weixin.qq.com + +#\u5fae\u4fe1\u652f\u4ed8_\u5546\u6237ID +weixin4j.pay.partner.id= +#\u5fae\u4fe1\u652f\u4ed8_\u5546\u6237\u5bc6\u94a5 +weixin4j.pay.partner.key= +#\u5fae\u4fe1\u652f\u4ed8_\u901a\u77e5URL +weixin4j.pay.notify_url= + +#\u8fde\u63a5\u8d85\u65f6\u8bbe\u7f6e +weixin4j.http.connectionTimeout=25000 +#\u8bf7\u6c42\u8d85\u65f6\u8bbe\u7f6e +weixin4j.http.readTimeout=25000 +#\u8bc1\u4e66\u8def\u5f84 +weixin4j.http.cert.path= +weixin4j.http.cert.secret= + +#\u9ed8\u8ba4\u6d88\u606f\u5904\u7406\u51fd\u6570 +weixin4j.handler=org.weixin4j.spi.DefaultMessageHandler +weixin4j.message.handler.normal=org.weixin4j.demo.jieru.AtsNormalMessageHandler +weixin4j.message.handler.event=org.weixin4j.demo.jieru.AtsEventMessageHandler \ No newline at end of file