From d29cc984bcddd7738b0e4fed23ad5e3894f5fe5e Mon Sep 17 00:00:00 2001 From: BinaryWang Date: Wed, 6 Jul 2016 16:02:37 +0800 Subject: [PATCH] =?UTF-8?q?=E5=88=9D=E6=AD=A5=E5=AE=9E=E7=8E=B0=E4=B8=80?= =?UTF-8?q?=E4=B8=AA=E4=BD=BF=E7=94=A8Spring=E6=A1=86=E6=9E=B6=E7=9A=84Dem?= =?UTF-8?q?o=20Web=E6=A8=A1=E5=9D=97?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- spring-demo/pom.xml | 25 ---- weixin-java-demo-with-spring/.gitignore | 1 + weixin-java-demo-with-spring/README.md | 7 + weixin-java-demo-with-spring/pom.xml | 91 ++++++++++++ .../demo/spring/aop/ControllerLogAspect.java | 80 +++++++++++ .../demo/spring/builder/AbstractBuilder.java | 21 +++ .../demo/spring/builder/ImageBuilder.java | 27 ++++ .../demo/spring/builder/TextBuilder.java | 25 ++++ .../demo/spring/config/WxAccountEnum.java | 41 ++++++ .../demo/spring/config/WxConfig.java | 21 +++ .../demo/spring/config/WxGzh1Config.java | 42 ++++++ .../demo/spring/config/WxGzh2Config.java | 42 ++++++ .../AbstractWxPortalController.java | 62 +++++++++ .../controller/Gzh1WxPortalController.java | 26 ++++ .../controller/Gzh2WxPortalController.java | 26 ++++ .../binarywang/demo/spring/dto/WxMenuKey.java | 44 ++++++ .../demo/spring/handler/AbstractHandler.java | 23 ++++ .../demo/spring/handler/LocationHandler.java | 47 +++++++ .../demo/spring/handler/MenuHandler.java | 71 ++++++++++ .../demo/spring/handler/MsgHandler.java | 48 +++++++ .../demo/spring/handler/NullHandler.java | 34 +++++ .../demo/spring/handler/ScanHandler.java | 10 ++ .../demo/spring/handler/SubscribeHandler.java | 62 +++++++++ .../spring/handler/UnsubscribeHandler.java | 27 ++++ .../demo/spring/service/BaseWxService.java | 129 ++++++++++++++++++ .../demo/spring/service/Gzh1WxService.java | 65 +++++++++ .../demo/spring/service/Gzh2WxService.java | 66 +++++++++ .../src/main/resources/.gitignore | 2 + .../src/main/resources/applicationContext.xml | 17 +++ .../src/main/resources/log4j.properties | 8 ++ .../main/resources/spring-service-bean.xml | 29 ++++ .../main/resources/spring-servlet-common.xml | 43 ++++++ .../resources/wx-gzh1.properties.template | 3 + .../resources/wx-gzh2.properties.template | 3 + .../src/main/webapp/WEB-INF/web.xml | 8 -- 35 files changed, 1243 insertions(+), 33 deletions(-) delete mode 100644 spring-demo/pom.xml create mode 100644 weixin-java-demo-with-spring/.gitignore create mode 100644 weixin-java-demo-with-spring/README.md create mode 100644 weixin-java-demo-with-spring/pom.xml create mode 100644 weixin-java-demo-with-spring/src/main/java/com/github/binarywang/demo/spring/aop/ControllerLogAspect.java create mode 100644 weixin-java-demo-with-spring/src/main/java/com/github/binarywang/demo/spring/builder/AbstractBuilder.java create mode 100644 weixin-java-demo-with-spring/src/main/java/com/github/binarywang/demo/spring/builder/ImageBuilder.java create mode 100644 weixin-java-demo-with-spring/src/main/java/com/github/binarywang/demo/spring/builder/TextBuilder.java create mode 100644 weixin-java-demo-with-spring/src/main/java/com/github/binarywang/demo/spring/config/WxAccountEnum.java create mode 100644 weixin-java-demo-with-spring/src/main/java/com/github/binarywang/demo/spring/config/WxConfig.java create mode 100644 weixin-java-demo-with-spring/src/main/java/com/github/binarywang/demo/spring/config/WxGzh1Config.java create mode 100644 weixin-java-demo-with-spring/src/main/java/com/github/binarywang/demo/spring/config/WxGzh2Config.java create mode 100644 weixin-java-demo-with-spring/src/main/java/com/github/binarywang/demo/spring/controller/AbstractWxPortalController.java create mode 100644 weixin-java-demo-with-spring/src/main/java/com/github/binarywang/demo/spring/controller/Gzh1WxPortalController.java create mode 100644 weixin-java-demo-with-spring/src/main/java/com/github/binarywang/demo/spring/controller/Gzh2WxPortalController.java create mode 100644 weixin-java-demo-with-spring/src/main/java/com/github/binarywang/demo/spring/dto/WxMenuKey.java create mode 100644 weixin-java-demo-with-spring/src/main/java/com/github/binarywang/demo/spring/handler/AbstractHandler.java create mode 100644 weixin-java-demo-with-spring/src/main/java/com/github/binarywang/demo/spring/handler/LocationHandler.java create mode 100644 weixin-java-demo-with-spring/src/main/java/com/github/binarywang/demo/spring/handler/MenuHandler.java create mode 100644 weixin-java-demo-with-spring/src/main/java/com/github/binarywang/demo/spring/handler/MsgHandler.java create mode 100644 weixin-java-demo-with-spring/src/main/java/com/github/binarywang/demo/spring/handler/NullHandler.java create mode 100644 weixin-java-demo-with-spring/src/main/java/com/github/binarywang/demo/spring/handler/ScanHandler.java create mode 100644 weixin-java-demo-with-spring/src/main/java/com/github/binarywang/demo/spring/handler/SubscribeHandler.java create mode 100644 weixin-java-demo-with-spring/src/main/java/com/github/binarywang/demo/spring/handler/UnsubscribeHandler.java create mode 100644 weixin-java-demo-with-spring/src/main/java/com/github/binarywang/demo/spring/service/BaseWxService.java create mode 100644 weixin-java-demo-with-spring/src/main/java/com/github/binarywang/demo/spring/service/Gzh1WxService.java create mode 100644 weixin-java-demo-with-spring/src/main/java/com/github/binarywang/demo/spring/service/Gzh2WxService.java create mode 100644 weixin-java-demo-with-spring/src/main/resources/.gitignore create mode 100644 weixin-java-demo-with-spring/src/main/resources/applicationContext.xml create mode 100644 weixin-java-demo-with-spring/src/main/resources/log4j.properties create mode 100644 weixin-java-demo-with-spring/src/main/resources/spring-service-bean.xml create mode 100644 weixin-java-demo-with-spring/src/main/resources/spring-servlet-common.xml create mode 100644 weixin-java-demo-with-spring/src/main/resources/wx-gzh1.properties.template create mode 100644 weixin-java-demo-with-spring/src/main/resources/wx-gzh2.properties.template rename {spring-demo => weixin-java-demo-with-spring}/src/main/webapp/WEB-INF/web.xml (84%) diff --git a/spring-demo/pom.xml b/spring-demo/pom.xml deleted file mode 100644 index 815ce765..00000000 --- a/spring-demo/pom.xml +++ /dev/null @@ -1,25 +0,0 @@ - - - 4.0.0 - - com.github.binarywang - weixin-java-parent - 1.3.6-SNAPSHOT - - spring-demo - war - WeiXin Java Tools - demo with spring - spring demo - https://github.com/binarywang/weixin-java-tools - - - - com.github.binarywang - weixin-java-common - ${project.version} - - - - diff --git a/weixin-java-demo-with-spring/.gitignore b/weixin-java-demo-with-spring/.gitignore new file mode 100644 index 00000000..22f500b0 --- /dev/null +++ b/weixin-java-demo-with-spring/.gitignore @@ -0,0 +1 @@ +*.pmd diff --git a/weixin-java-demo-with-spring/README.md b/weixin-java-demo-with-spring/README.md new file mode 100644 index 00000000..1268e693 --- /dev/null +++ b/weixin-java-demo-with-spring/README.md @@ -0,0 +1,7 @@ +#### 本Demo使用Spring MVC 框架实现微信公众号后台管理功能,支持多公众号,欢迎帮忙维护添加新功能,或提供更好的实现。 +### 1. 配置 + 复制/src/main/resources/wx-gzh1.properties.template 生成wx-gzh1.properties 文件,填写相关配置; + 复制/src/main/resources/wx-gzh2.properties.template 生成wx-gzh2.properties 文件,填写相关配置. + +### 2. 使用maven运行demo程序 + mvn jetty:run diff --git a/weixin-java-demo-with-spring/pom.xml b/weixin-java-demo-with-spring/pom.xml new file mode 100644 index 00000000..469ac9e2 --- /dev/null +++ b/weixin-java-demo-with-spring/pom.xml @@ -0,0 +1,91 @@ + + + 4.0.0 + + com.github.binarywang + weixin-java-parent + 1.3.6-SNAPSHOT + + weixin-java-demo-with-spring + war + WeiXin Java Tools - DemoWithSpring + spring demo + https://github.com/binarywang/weixin-java-tools + + 4.3.0.RELEASE + 4.1.0.RELEASE + 19.0 + 1.7.2 + 1.8.9 + 1.2.6 + 3.1 + 9.3.10.v20160621 + + + + + com.github.binarywang + weixin-java-mp + ${project.version} + + + + com.alibaba + fastjson + ${fastjson.version} + + + org.apache.commons + commons-lang3 + ${commons-lang3.version} + + + com.google.guava + guava + ${guava.version} + + + org.slf4j + slf4j-log4j12 + ${slf4j.version} + + + org.springframework + spring-webmvc + ${spring.version} + + + org.springframework + spring-web + ${spring.version} + + + org.springframework.security + spring-security-core + ${spring-security.version} + + + + org.aspectj + aspectjrt + ${aspectj.version} + + + org.aspectj + aspectjweaver + ${aspectj.version} + + + + + + + org.eclipse.jetty + jetty-maven-plugin + ${jetty-maven-plugin.version} + + + + diff --git a/weixin-java-demo-with-spring/src/main/java/com/github/binarywang/demo/spring/aop/ControllerLogAspect.java b/weixin-java-demo-with-spring/src/main/java/com/github/binarywang/demo/spring/aop/ControllerLogAspect.java new file mode 100644 index 00000000..8a836908 --- /dev/null +++ b/weixin-java-demo-with-spring/src/main/java/com/github/binarywang/demo/spring/aop/ControllerLogAspect.java @@ -0,0 +1,80 @@ +package com.github.binarywang.demo.spring.aop; + +import org.apache.commons.lang3.ArrayUtils; +import org.aspectj.lang.JoinPoint; +import org.aspectj.lang.annotation.After; +import org.aspectj.lang.annotation.Aspect; +import org.aspectj.lang.annotation.Before; +import org.aspectj.lang.annotation.Pointcut; +import org.aspectj.lang.reflect.CodeSignature; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.security.core.Authentication; +import org.springframework.security.core.context.SecurityContextHolder; +import org.springframework.stereotype.Component; + +/** + * + * @author Binary Wang + * + */ +@Aspect +@Component +public class ControllerLogAspect { + private Logger logger = LoggerFactory.getLogger(getClass()); + + @Pointcut("within(com.github.binarywang.demo.spring..*.controller..*)") + public void inController() { + } + + @Pointcut("execution(public * com.github.binarywang.demo.spring..*.controller..*.*(..))") + public void controller() { + } + + @Before("inController()") + public void writeBeforeLog(JoinPoint jp) { + this.debugInController(jp, "Start"); + } + + @After("inController()") + public void writeAfterLog(JoinPoint jp) { + this.debugInController(jp, "End"); + } + + private void debugInController(JoinPoint jp, String msg) { + String userName = getLoginUserName(); + + this.logger.debug("\n【{}】{}.{}() {} ", userName, + jp.getTarget() + .getClass().getSimpleName(), jp.getSignature().getName(), msg); + } + + private static String getLoginUserName() { + Authentication authentication = SecurityContextHolder.getContext() + .getAuthentication(); + if (authentication != null) { + return authentication.getName(); + } + + return "Anonymous"; + } + + @Before("controller()") + public void writeParams(JoinPoint jp) { + String[] names = ((CodeSignature) jp.getSignature()) + .getParameterNames(); + Object[] args = jp.getArgs(); + + if (ArrayUtils.isEmpty(names)) { + return; + } + + StringBuilder sb = new StringBuilder("Arguments: "); + for (int i = 0; i < names.length; i++) { + sb.append(names[i] + " = " + args[i] + ","); + } + + debugInController(jp, sb.toString()); + } + +} diff --git a/weixin-java-demo-with-spring/src/main/java/com/github/binarywang/demo/spring/builder/AbstractBuilder.java b/weixin-java-demo-with-spring/src/main/java/com/github/binarywang/demo/spring/builder/AbstractBuilder.java new file mode 100644 index 00000000..bf019b4b --- /dev/null +++ b/weixin-java-demo-with-spring/src/main/java/com/github/binarywang/demo/spring/builder/AbstractBuilder.java @@ -0,0 +1,21 @@ +package com.github.binarywang.demo.spring.builder; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import com.github.binarywang.demo.spring.service.BaseWxService; + +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, BaseWxService service) ; +} diff --git a/weixin-java-demo-with-spring/src/main/java/com/github/binarywang/demo/spring/builder/ImageBuilder.java b/weixin-java-demo-with-spring/src/main/java/com/github/binarywang/demo/spring/builder/ImageBuilder.java new file mode 100644 index 00000000..88267125 --- /dev/null +++ b/weixin-java-demo-with-spring/src/main/java/com/github/binarywang/demo/spring/builder/ImageBuilder.java @@ -0,0 +1,27 @@ +package com.github.binarywang.demo.spring.builder; + +import com.github.binarywang.demo.spring.service.BaseWxService; + +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, + BaseWxService service) { + + WxMpXmlOutImageMessage m = WxMpXmlOutMessage.IMAGE().mediaId(content) + .fromUser(wxMessage.getToUserName()).toUser(wxMessage.getFromUserName()) + .build(); + + return m; + } + +} diff --git a/weixin-java-demo-with-spring/src/main/java/com/github/binarywang/demo/spring/builder/TextBuilder.java b/weixin-java-demo-with-spring/src/main/java/com/github/binarywang/demo/spring/builder/TextBuilder.java new file mode 100644 index 00000000..551f6ca7 --- /dev/null +++ b/weixin-java-demo-with-spring/src/main/java/com/github/binarywang/demo/spring/builder/TextBuilder.java @@ -0,0 +1,25 @@ +package com.github.binarywang.demo.spring.builder; + +import com.github.binarywang.demo.spring.service.BaseWxService; + +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, + BaseWxService service) { + WxMpXmlOutTextMessage m = WxMpXmlOutMessage.TEXT().content(content) + .fromUser(wxMessage.getToUserName()).toUser(wxMessage.getFromUserName()) + .build(); + return m; + } + +} diff --git a/weixin-java-demo-with-spring/src/main/java/com/github/binarywang/demo/spring/config/WxAccountEnum.java b/weixin-java-demo-with-spring/src/main/java/com/github/binarywang/demo/spring/config/WxAccountEnum.java new file mode 100644 index 00000000..c82aab9e --- /dev/null +++ b/weixin-java-demo-with-spring/src/main/java/com/github/binarywang/demo/spring/config/WxAccountEnum.java @@ -0,0 +1,41 @@ +package com.github.binarywang.demo.spring.config; + +/** + * 公众号标识的枚举类 + * @author Binary Wang + * + */ +public enum WxAccountEnum { + GZH1(1, "公众号1"), + GZH2(2, "公众号2"); + + private int pubid; + private String name; + + private WxAccountEnum(int pubid, String name) { + this.name = name; + this.pubid = pubid; + } + + public int getPubid() { + return this.pubid; + } + + public String getName() { + return this.name; + } + + public static int queryPubid(String wxCode) { + return WxAccountEnum.valueOf(wxCode.toUpperCase()).getPubid(); + } + + public static String queryWxCode(int pubid) { + for (WxAccountEnum e : values()) { + if (e.getPubid() == pubid) { + return e.name().toLowerCase(); + } + } + + return null; + } +} diff --git a/weixin-java-demo-with-spring/src/main/java/com/github/binarywang/demo/spring/config/WxConfig.java b/weixin-java-demo-with-spring/src/main/java/com/github/binarywang/demo/spring/config/WxConfig.java new file mode 100644 index 00000000..a65d4000 --- /dev/null +++ b/weixin-java-demo-with-spring/src/main/java/com/github/binarywang/demo/spring/config/WxConfig.java @@ -0,0 +1,21 @@ +package com.github.binarywang.demo.spring.config; + +/** + * 微信配置的抽象实现类 + * @author Binary Wang + * + */ +public abstract class WxConfig { + public abstract String getToken(); + + public abstract String getAppid(); + + public abstract String getAppsecret(); + + public abstract WxAccountEnum getWxAccountEnum(); + + public int getPubId() { + return getWxAccountEnum().getPubid(); + } + +} diff --git a/weixin-java-demo-with-spring/src/main/java/com/github/binarywang/demo/spring/config/WxGzh1Config.java b/weixin-java-demo-with-spring/src/main/java/com/github/binarywang/demo/spring/config/WxGzh1Config.java new file mode 100644 index 00000000..fee34f43 --- /dev/null +++ b/weixin-java-demo-with-spring/src/main/java/com/github/binarywang/demo/spring/config/WxGzh1Config.java @@ -0,0 +1,42 @@ +package com.github.binarywang.demo.spring.config; + +import org.springframework.beans.factory.annotation.Value; +import org.springframework.context.annotation.Configuration; + +/** + * + * @author Binary Wang + * + */ +@Configuration +public class WxGzh1Config extends WxConfig { + @Value("#{gzh1WxProperties.wx_token}") + private String token; + + @Value("#{gzh1WxProperties.wx_appid}") + private String appid; + + @Value("#{gzh1WxProperties.wx_appsecret}") + private String appsecret; + + @Override + public String getToken() { + return this.token; + } + + @Override + public String getAppid() { + return this.appid; + } + + @Override + public String getAppsecret() { + return this.appsecret; + } + + @Override + public WxAccountEnum getWxAccountEnum() { + return WxAccountEnum.GZH1; + } + +} diff --git a/weixin-java-demo-with-spring/src/main/java/com/github/binarywang/demo/spring/config/WxGzh2Config.java b/weixin-java-demo-with-spring/src/main/java/com/github/binarywang/demo/spring/config/WxGzh2Config.java new file mode 100644 index 00000000..d284630a --- /dev/null +++ b/weixin-java-demo-with-spring/src/main/java/com/github/binarywang/demo/spring/config/WxGzh2Config.java @@ -0,0 +1,42 @@ +package com.github.binarywang.demo.spring.config; + +import org.springframework.beans.factory.annotation.Value; +import org.springframework.context.annotation.Configuration; + +/** + * + * @author Binary Wang + * + */ +@Configuration +public class WxGzh2Config extends WxConfig { + @Value("#{gzh2WxProperties.wx_token}") + private String token; + + @Value("#{gzh2WxProperties.wx_appid}") + private String appid; + + @Value("#{gzh2WxProperties.wx_appsecret}") + private String appsecret; + + @Override + public String getToken() { + return this.token; + } + + @Override + public String getAppid() { + return this.appid; + } + + @Override + public String getAppsecret() { + return this.appsecret; + } + + @Override + public WxAccountEnum getWxAccountEnum() { + return WxAccountEnum.GZH2; + } + +} diff --git a/weixin-java-demo-with-spring/src/main/java/com/github/binarywang/demo/spring/controller/AbstractWxPortalController.java b/weixin-java-demo-with-spring/src/main/java/com/github/binarywang/demo/spring/controller/AbstractWxPortalController.java new file mode 100644 index 00000000..8bae4669 --- /dev/null +++ b/weixin-java-demo-with-spring/src/main/java/com/github/binarywang/demo/spring/controller/AbstractWxPortalController.java @@ -0,0 +1,62 @@ +package com.github.binarywang.demo.spring.controller; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.web.bind.annotation.RequestBody; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RequestMethod; +import org.springframework.web.bind.annotation.RequestParam; +import org.springframework.web.bind.annotation.ResponseBody; + +import com.github.binarywang.demo.spring.service.BaseWxService; + +import me.chanjar.weixin.mp.bean.WxMpXmlMessage; +import me.chanjar.weixin.mp.bean.WxMpXmlOutMessage; + +/** + * + * @author Binary Wang + * + */ +public abstract class AbstractWxPortalController { + private final Logger logger = LoggerFactory.getLogger(this.getClass()); + + @RequestMapping(method = RequestMethod.GET, produces = "text/plain;charset=utf-8") + public @ResponseBody String authGet( + @RequestParam("signature") String signature, + @RequestParam("timestamp") String timestamp, + @RequestParam("nonce") String nonce, + @RequestParam("echostr") String echostr) { + this.logger.info("接收到来自微信服务器的认证消息"); + + if (this.getWxService().checkSignature(timestamp, nonce, signature)) { + return echostr; + } + + return "非法请求"; + } + + @RequestMapping(method = RequestMethod.POST, produces = "application/xml; charset=UTF-8") + public @ResponseBody String post(@RequestBody String requestBody) { + + this.logger.debug("\n接收微信请求:{} ", requestBody); + + BaseWxService wxService = this.getWxService(); + + WxMpXmlOutMessage out = wxService + .route(WxMpXmlMessage.fromXml(requestBody)); + + if (out == null) { + return ""; + } + + String outXml = out.toXml(); + + this.logger.debug("\n组装回复信息:{}", outXml); + + return outXml; + } + + protected abstract BaseWxService getWxService(); + +} diff --git a/weixin-java-demo-with-spring/src/main/java/com/github/binarywang/demo/spring/controller/Gzh1WxPortalController.java b/weixin-java-demo-with-spring/src/main/java/com/github/binarywang/demo/spring/controller/Gzh1WxPortalController.java new file mode 100644 index 00000000..cea13152 --- /dev/null +++ b/weixin-java-demo-with-spring/src/main/java/com/github/binarywang/demo/spring/controller/Gzh1WxPortalController.java @@ -0,0 +1,26 @@ +package com.github.binarywang.demo.spring.controller; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RestController; + +import com.github.binarywang.demo.spring.service.BaseWxService; +import com.github.binarywang.demo.spring.service.Gzh1WxService; + +/** + * 第一个公众号的微信交互接口 + * @author Binary Wang + * + */ +@RestController +@RequestMapping("/api/gzh1/portal") +public class Gzh1WxPortalController extends AbstractWxPortalController{ + @Autowired + private Gzh1WxService wxService; + + @Override + protected BaseWxService getWxService() { + return this.wxService; + } + +} diff --git a/weixin-java-demo-with-spring/src/main/java/com/github/binarywang/demo/spring/controller/Gzh2WxPortalController.java b/weixin-java-demo-with-spring/src/main/java/com/github/binarywang/demo/spring/controller/Gzh2WxPortalController.java new file mode 100644 index 00000000..d982ad46 --- /dev/null +++ b/weixin-java-demo-with-spring/src/main/java/com/github/binarywang/demo/spring/controller/Gzh2WxPortalController.java @@ -0,0 +1,26 @@ +package com.github.binarywang.demo.spring.controller; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RestController; + +import com.github.binarywang.demo.spring.service.BaseWxService; +import com.github.binarywang.demo.spring.service.Gzh2WxService; + +/** + * 第二个公众号的微信交互接口 + * @author Binary Wang + * + */ +@RestController +@RequestMapping("/api/gzh2/portal") +public class Gzh2WxPortalController extends AbstractWxPortalController{ + @Autowired + private Gzh2WxService wxService; + + @Override + protected BaseWxService getWxService() { + return this.wxService; + } + +} diff --git a/weixin-java-demo-with-spring/src/main/java/com/github/binarywang/demo/spring/dto/WxMenuKey.java b/weixin-java-demo-with-spring/src/main/java/com/github/binarywang/demo/spring/dto/WxMenuKey.java new file mode 100644 index 00000000..99b4cb68 --- /dev/null +++ b/weixin-java-demo-with-spring/src/main/java/com/github/binarywang/demo/spring/dto/WxMenuKey.java @@ -0,0 +1,44 @@ +package com.github.binarywang.demo.spring.dto; + +import org.apache.commons.lang3.builder.ToStringBuilder; + +/** + * 菜单的dto对象 + * @author Binary Wang + * + */ +public class WxMenuKey { + private String type; + private String content; + + @Override + public String toString() { + return ToStringBuilder.reflectionToString(this); + } + + public WxMenuKey() { + + } + + public WxMenuKey(String type, String content) { + this.type = type; + this.content = content; + } + + public String getType() { + return this.type; + } + + public void setType(String type) { + this.type = type; + } + + public String getContent() { + return this.content; + } + + public void setContent(String content) { + this.content = content; + } + +} diff --git a/weixin-java-demo-with-spring/src/main/java/com/github/binarywang/demo/spring/handler/AbstractHandler.java b/weixin-java-demo-with-spring/src/main/java/com/github/binarywang/demo/spring/handler/AbstractHandler.java new file mode 100644 index 00000000..7e4e1642 --- /dev/null +++ b/weixin-java-demo-with-spring/src/main/java/com/github/binarywang/demo/spring/handler/AbstractHandler.java @@ -0,0 +1,23 @@ +package com.github.binarywang.demo.spring.handler; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import com.github.binarywang.demo.spring.config.WxConfig; +import com.google.gson.Gson; + +import me.chanjar.weixin.mp.api.WxMpMessageHandler; + +/** + * + * @author Binary Wang + * + */ +public abstract class AbstractHandler implements WxMpMessageHandler { + + protected Logger logger = LoggerFactory.getLogger(getClass()); + protected final Gson gson = new Gson(); + + protected abstract WxConfig getWxConfig(); + +} diff --git a/weixin-java-demo-with-spring/src/main/java/com/github/binarywang/demo/spring/handler/LocationHandler.java b/weixin-java-demo-with-spring/src/main/java/com/github/binarywang/demo/spring/handler/LocationHandler.java new file mode 100644 index 00000000..2a03ee76 --- /dev/null +++ b/weixin-java-demo-with-spring/src/main/java/com/github/binarywang/demo/spring/handler/LocationHandler.java @@ -0,0 +1,47 @@ +package com.github.binarywang.demo.spring.handler; + +import java.util.Map; + +import com.github.binarywang.demo.spring.builder.TextBuilder; + +import me.chanjar.weixin.common.api.WxConsts; +import me.chanjar.weixin.common.exception.WxErrorException; +import me.chanjar.weixin.common.session.WxSessionManager; +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 LocationHandler extends AbstractHandler { + + @Override + public WxMpXmlOutMessage handle(WxMpXmlMessage wxMessage, + Map context, WxMpService wxMpService, + WxSessionManager sessionManager) throws WxErrorException { + if (wxMessage.getMsgType().equals(WxConsts.XML_MSG_LOCATION)) { + //TODO 接收处理用户发送的地理位置消息 + try { + String content = "感谢反馈,您的的地理位置已收到!"; + return new TextBuilder().build(content, wxMessage, null); + } catch (Exception e) { + this.logger.error("位置消息接收处理失败", e); + return null; + } + } + + //上报地理位置事件 + this.logger.info("\n上报地理位置 。。。 "); + this.logger.info("\n纬度 : " + wxMessage.getLatitude()); + this.logger.info("\n经度 : " + wxMessage.getLongitude()); + this.logger.info("\n精度 : " + String.valueOf(wxMessage.getPrecision())); + + //TODO 可以将用户地理位置信息保存到本地数据库,以便以后使用 + + return null; + } + +} diff --git a/weixin-java-demo-with-spring/src/main/java/com/github/binarywang/demo/spring/handler/MenuHandler.java b/weixin-java-demo-with-spring/src/main/java/com/github/binarywang/demo/spring/handler/MenuHandler.java new file mode 100644 index 00000000..45d5c978 --- /dev/null +++ b/weixin-java-demo-with-spring/src/main/java/com/github/binarywang/demo/spring/handler/MenuHandler.java @@ -0,0 +1,71 @@ +package com.github.binarywang.demo.spring.handler; + +import java.util.Map; + +import com.alibaba.fastjson.JSON; +import com.github.binarywang.demo.spring.builder.AbstractBuilder; +import com.github.binarywang.demo.spring.builder.ImageBuilder; +import com.github.binarywang.demo.spring.builder.TextBuilder; +import com.github.binarywang.demo.spring.dto.WxMenuKey; +import com.github.binarywang.demo.spring.service.BaseWxService; + +import me.chanjar.weixin.common.api.WxConsts; +import me.chanjar.weixin.common.session.WxSessionManager; +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 MenuHandler extends AbstractHandler { + + @Override + public WxMpXmlOutMessage handle(WxMpXmlMessage wxMessage, + Map context, WxMpService wxMpService, + WxSessionManager sessionManager) { + BaseWxService weixinService = (BaseWxService) wxMpService; + + String key = wxMessage.getEventKey(); + WxMenuKey menuKey = null; + try { + menuKey = JSON.parseObject(key, WxMenuKey.class); + } catch (Exception e) { + return WxMpXmlOutMessage.TEXT().content(key) + .fromUser(wxMessage.getToUserName()) + .toUser(wxMessage.getFromUserName()).build(); + } + + AbstractBuilder builder = null; + switch (menuKey.getType()) { + case WxConsts.XML_MSG_TEXT: + builder = new TextBuilder(); + break; + case WxConsts.XML_MSG_IMAGE: + builder = new ImageBuilder(); + break; + case WxConsts.XML_MSG_VOICE: + break; + case WxConsts.XML_MSG_VIDEO: + break; + case WxConsts.XML_MSG_NEWS: + break; + default: + break; + } + + if (builder != null) { + try { + return builder.build(menuKey.getContent(), wxMessage, weixinService); + } catch (Exception e) { + this.logger.error(e.getMessage(), e); + } + } + + return null; + + } + +} diff --git a/weixin-java-demo-with-spring/src/main/java/com/github/binarywang/demo/spring/handler/MsgHandler.java b/weixin-java-demo-with-spring/src/main/java/com/github/binarywang/demo/spring/handler/MsgHandler.java new file mode 100644 index 00000000..47ad929f --- /dev/null +++ b/weixin-java-demo-with-spring/src/main/java/com/github/binarywang/demo/spring/handler/MsgHandler.java @@ -0,0 +1,48 @@ +package com.github.binarywang.demo.spring.handler; + +import java.util.Map; + +import org.apache.commons.lang3.StringUtils; + +import com.github.binarywang.demo.spring.builder.TextBuilder; +import com.github.binarywang.demo.spring.service.BaseWxService; + +import me.chanjar.weixin.common.api.WxConsts; +import me.chanjar.weixin.common.session.WxSessionManager; +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 MsgHandler extends AbstractHandler { + + @Override + public WxMpXmlOutMessage handle(WxMpXmlMessage wxMessage, + Map context, WxMpService wxMpService, + WxSessionManager sessionManager) { + + BaseWxService weixinService = (BaseWxService) wxMpService; + + if (!wxMessage.getMsgType().equals(WxConsts.XML_MSG_EVENT)) { + //TODO 可以选择将消息保存到本地 + } + + //当用户输入关键词如“你好”,“在吗”等并且有客服在线时,把消息转发给在线客服 + if (StringUtils.startsWithAny(wxMessage.getContent(), "你好", "在吗") + && weixinService.isCustomerServiceOnline()) { + return WxMpXmlOutMessage + .TRANSFER_CUSTOMER_SERVICE().fromUser(wxMessage.getToUserName()) + .toUser(wxMessage.getFromUserName()).build(); + } + + //TODO 组装回复消息 + String content = "回复信息内容"; + return new TextBuilder().build(content, wxMessage, weixinService); + + } + +} diff --git a/weixin-java-demo-with-spring/src/main/java/com/github/binarywang/demo/spring/handler/NullHandler.java b/weixin-java-demo-with-spring/src/main/java/com/github/binarywang/demo/spring/handler/NullHandler.java new file mode 100644 index 00000000..ac828f52 --- /dev/null +++ b/weixin-java-demo-with-spring/src/main/java/com/github/binarywang/demo/spring/handler/NullHandler.java @@ -0,0 +1,34 @@ +package com.github.binarywang.demo.spring.handler; + +import java.util.Map; + +import org.springframework.stereotype.Component; + +import com.github.binarywang.demo.spring.config.WxConfig; + +import me.chanjar.weixin.common.session.WxSessionManager; +import me.chanjar.weixin.mp.api.WxMpService; +import me.chanjar.weixin.mp.bean.WxMpXmlMessage; +import me.chanjar.weixin.mp.bean.WxMpXmlOutMessage; + +/** + * + * @author Binary Wang + * + */ +@Component +public class NullHandler extends AbstractHandler { + + @Override + public WxMpXmlOutMessage handle(WxMpXmlMessage wxMessage, + Map context, WxMpService wxMpService, + WxSessionManager sessionManager) { + return null; + } + + @Override + protected WxConfig getWxConfig() { + return null; + } + +} diff --git a/weixin-java-demo-with-spring/src/main/java/com/github/binarywang/demo/spring/handler/ScanHandler.java b/weixin-java-demo-with-spring/src/main/java/com/github/binarywang/demo/spring/handler/ScanHandler.java new file mode 100644 index 00000000..91267d4d --- /dev/null +++ b/weixin-java-demo-with-spring/src/main/java/com/github/binarywang/demo/spring/handler/ScanHandler.java @@ -0,0 +1,10 @@ +package com.github.binarywang.demo.spring.handler; + +/** + * + * @author Binary Wang + * + */ +public abstract class ScanHandler extends AbstractHandler { + +} diff --git a/weixin-java-demo-with-spring/src/main/java/com/github/binarywang/demo/spring/handler/SubscribeHandler.java b/weixin-java-demo-with-spring/src/main/java/com/github/binarywang/demo/spring/handler/SubscribeHandler.java new file mode 100644 index 00000000..f5bd6460 --- /dev/null +++ b/weixin-java-demo-with-spring/src/main/java/com/github/binarywang/demo/spring/handler/SubscribeHandler.java @@ -0,0 +1,62 @@ +package com.github.binarywang.demo.spring.handler; + +import java.util.Map; + +import com.github.binarywang.demo.spring.builder.TextBuilder; +import com.github.binarywang.demo.spring.service.BaseWxService; + +import me.chanjar.weixin.common.exception.WxErrorException; +import me.chanjar.weixin.common.session.WxSessionManager; +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.result.WxMpUser; + +/** + * + * @author Binary Wang + * + */ +public abstract class SubscribeHandler extends AbstractHandler { + + @Override + public WxMpXmlOutMessage handle(WxMpXmlMessage wxMessage, Map context, WxMpService wxMpService, + WxSessionManager sessionManager) throws WxErrorException { + + this.logger.info("新关注用户 OPENID: " + wxMessage.getFromUserName()); + + BaseWxService weixinService = (BaseWxService) wxMpService; + + // 获取微信用户基本信息 + WxMpUser userWxInfo = weixinService.userInfo(wxMessage.getFromUserName(), null); + + if (userWxInfo != null) { + // TODO 可以添加关注用户到本地 + } + + WxMpXmlOutMessage responsResult = null; + try { + responsResult = handleSpecial(wxMessage); + } catch (Exception e) { + this.logger.error(e.getMessage(), e); + } + + if (responsResult != null) { + return responsResult; + } + + try { + return new TextBuilder().build("感谢关注", wxMessage, weixinService); + } catch (Exception e) { + this.logger.error(e.getMessage(), e); + } + + return null; + } + + /** + * 处理特殊请求,比如如果是扫码进来的,可以做相应处理 + */ + protected abstract WxMpXmlOutMessage handleSpecial(WxMpXmlMessage wxMessage) throws Exception; + +} diff --git a/weixin-java-demo-with-spring/src/main/java/com/github/binarywang/demo/spring/handler/UnsubscribeHandler.java b/weixin-java-demo-with-spring/src/main/java/com/github/binarywang/demo/spring/handler/UnsubscribeHandler.java new file mode 100644 index 00000000..44daaa18 --- /dev/null +++ b/weixin-java-demo-with-spring/src/main/java/com/github/binarywang/demo/spring/handler/UnsubscribeHandler.java @@ -0,0 +1,27 @@ +package com.github.binarywang.demo.spring.handler; + +import java.util.Map; + +import me.chanjar.weixin.common.session.WxSessionManager; +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 UnsubscribeHandler extends AbstractHandler { + + @Override + public WxMpXmlOutMessage handle(WxMpXmlMessage wxMessage, + Map context, WxMpService wxMpService, + WxSessionManager sessionManager) { + String openId = wxMessage.getFromUserName(); + this.logger.info("取消关注用户 OPENID: " + openId); + // TODO 可以更新本地数据库为取消关注状态 + return null; + } + +} diff --git a/weixin-java-demo-with-spring/src/main/java/com/github/binarywang/demo/spring/service/BaseWxService.java b/weixin-java-demo-with-spring/src/main/java/com/github/binarywang/demo/spring/service/BaseWxService.java new file mode 100644 index 00000000..a05abf8c --- /dev/null +++ b/weixin-java-demo-with-spring/src/main/java/com/github/binarywang/demo/spring/service/BaseWxService.java @@ -0,0 +1,129 @@ +package com.github.binarywang.demo.spring.service; + +import javax.annotation.PostConstruct; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.beans.factory.annotation.Autowired; + +import com.github.binarywang.demo.spring.config.WxConfig; +import com.github.binarywang.demo.spring.handler.AbstractHandler; +import com.github.binarywang.demo.spring.handler.MenuHandler; +import com.github.binarywang.demo.spring.handler.MsgHandler; +import com.github.binarywang.demo.spring.handler.NullHandler; +import com.github.binarywang.demo.spring.handler.SubscribeHandler; +import com.github.binarywang.demo.spring.handler.UnsubscribeHandler; +import com.google.gson.JsonArray; +import com.google.gson.JsonParser; + +import me.chanjar.weixin.common.api.WxConsts; +import me.chanjar.weixin.mp.api.WxMpInMemoryConfigStorage; +import me.chanjar.weixin.mp.api.WxMpMessageRouter; +import me.chanjar.weixin.mp.api.WxMpServiceImpl; +import me.chanjar.weixin.mp.bean.WxMpXmlMessage; +import me.chanjar.weixin.mp.bean.WxMpXmlOutMessage; + +/** + * + * @author Binary Wang + * + */ +public abstract class BaseWxService extends WxMpServiceImpl { + private final Logger logger = LoggerFactory.getLogger(this.getClass()); + + @Autowired + protected NullHandler nullHandler; + + private WxMpMessageRouter router; + + protected abstract WxConfig getServerConfig(); + + protected abstract MenuHandler getMenuHandler(); + + protected abstract SubscribeHandler getSubscribeHandler(); + + protected abstract UnsubscribeHandler getUnsubscribeHandler(); + + protected abstract AbstractHandler getLocationHandler(); + + protected abstract MsgHandler getMsgHandler(); + + protected abstract AbstractHandler getScanHandler(); + + @PostConstruct + public void init() { + final WxMpInMemoryConfigStorage config = new WxMpInMemoryConfigStorage(); + config.setAppId(this.getServerConfig().getAppid());// 设置微信公众号的appid + config.setSecret(this.getServerConfig().getAppsecret());// 设置微信公众号的app corpSecret + config.setToken(this.getServerConfig().getToken());// 设置微信公众号的token + super.setWxMpConfigStorage(config); + + this.refreshRouter(); + } + + private void refreshRouter() { + + final WxMpMessageRouter newRouter = new WxMpMessageRouter(this); + + // 自定义菜单事件 + newRouter.rule().async(false).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(); + + this.router = newRouter; + } + + public WxMpXmlOutMessage route(WxMpXmlMessage message) { + try { + final WxMpXmlOutMessage responseMessage = this.router.route(message); + return responseMessage; + } catch (Exception e) { + this.logger.error(e.getMessage(), e); + } + + return null; + } + + public boolean isCustomerServiceOnline() { + try { + String url = "https://api.weixin.qq.com/cgi-bin/customservice/getonlinekflist"; + String executeResult = this.get(url, null); + JsonArray jsonArray = new JsonParser().parse(executeResult) + .getAsJsonObject().get("kf_online_list").getAsJsonArray(); + return jsonArray.size() > 0; + } catch (Exception e) { + this.logger.error("获取客服在线状态异常: " + e.getMessage(), e); + } + + return false; + } + +} diff --git a/weixin-java-demo-with-spring/src/main/java/com/github/binarywang/demo/spring/service/Gzh1WxService.java b/weixin-java-demo-with-spring/src/main/java/com/github/binarywang/demo/spring/service/Gzh1WxService.java new file mode 100644 index 00000000..74e0e566 --- /dev/null +++ b/weixin-java-demo-with-spring/src/main/java/com/github/binarywang/demo/spring/service/Gzh1WxService.java @@ -0,0 +1,65 @@ +package com.github.binarywang.demo.spring.service; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Service; + +import com.github.binarywang.demo.spring.config.WxGzh1Config; +import com.github.binarywang.demo.spring.config.WxConfig; +import com.github.binarywang.demo.spring.handler.AbstractHandler; +import com.github.binarywang.demo.spring.handler.MenuHandler; +import com.github.binarywang.demo.spring.handler.MsgHandler; +import com.github.binarywang.demo.spring.handler.SubscribeHandler; +import com.github.binarywang.demo.spring.handler.UnsubscribeHandler; + +/** + * + * @author Binary Wang + * + */ +@Service +public class Gzh1WxService extends BaseWxService { + @Autowired + private WxGzh1Config wxConfig; + + @Override + protected WxConfig getServerConfig() { + return this.wxConfig; + } + + @Override + protected MenuHandler getMenuHandler() { + // TODO Auto-generated method stub + return null; + } + + @Override + protected SubscribeHandler getSubscribeHandler() { + // TODO Auto-generated method stub + return null; + } + + @Override + protected UnsubscribeHandler getUnsubscribeHandler() { + // TODO Auto-generated method stub + return null; + } + + @Override + protected AbstractHandler getLocationHandler() { + // TODO Auto-generated method stub + return null; + } + + @Override + protected MsgHandler getMsgHandler() { + // TODO Auto-generated method stub + return null; + } + + @Override + protected AbstractHandler getScanHandler() { + // TODO Auto-generated method stub + return null; + } + +} diff --git a/weixin-java-demo-with-spring/src/main/java/com/github/binarywang/demo/spring/service/Gzh2WxService.java b/weixin-java-demo-with-spring/src/main/java/com/github/binarywang/demo/spring/service/Gzh2WxService.java new file mode 100644 index 00000000..beaeafbf --- /dev/null +++ b/weixin-java-demo-with-spring/src/main/java/com/github/binarywang/demo/spring/service/Gzh2WxService.java @@ -0,0 +1,66 @@ +package com.github.binarywang.demo.spring.service; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Service; + +import com.github.binarywang.demo.spring.config.WxGzh2Config; +import com.github.binarywang.demo.spring.config.WxConfig; +import com.github.binarywang.demo.spring.handler.AbstractHandler; +import com.github.binarywang.demo.spring.handler.MenuHandler; +import com.github.binarywang.demo.spring.handler.MsgHandler; +import com.github.binarywang.demo.spring.handler.SubscribeHandler; +import com.github.binarywang.demo.spring.handler.UnsubscribeHandler; + +/** + * + * @author Binary Wang + * + */ +@Service +public class Gzh2WxService extends BaseWxService { + + @Autowired + private WxGzh2Config wxConfig; + + @Override + protected WxConfig getServerConfig() { + return this.wxConfig; + } + + @Override + protected MenuHandler getMenuHandler() { + // TODO Auto-generated method stub + return null; + } + + @Override + protected SubscribeHandler getSubscribeHandler() { + // TODO Auto-generated method stub + return null; + } + + @Override + protected UnsubscribeHandler getUnsubscribeHandler() { + // TODO Auto-generated method stub + return null; + } + + @Override + protected AbstractHandler getLocationHandler() { + // TODO Auto-generated method stub + return null; + } + + @Override + protected MsgHandler getMsgHandler() { + // TODO Auto-generated method stub + return null; + } + + @Override + protected AbstractHandler getScanHandler() { + // TODO Auto-generated method stub + return null; + } + +} diff --git a/weixin-java-demo-with-spring/src/main/resources/.gitignore b/weixin-java-demo-with-spring/src/main/resources/.gitignore new file mode 100644 index 00000000..a8697d73 --- /dev/null +++ b/weixin-java-demo-with-spring/src/main/resources/.gitignore @@ -0,0 +1,2 @@ +/wx-gzh1.properties +/wx-gzh2.properties diff --git a/weixin-java-demo-with-spring/src/main/resources/applicationContext.xml b/weixin-java-demo-with-spring/src/main/resources/applicationContext.xml new file mode 100644 index 00000000..f4954624 --- /dev/null +++ b/weixin-java-demo-with-spring/src/main/resources/applicationContext.xml @@ -0,0 +1,17 @@ + + + + + + + + + \ No newline at end of file diff --git a/weixin-java-demo-with-spring/src/main/resources/log4j.properties b/weixin-java-demo-with-spring/src/main/resources/log4j.properties new file mode 100644 index 00000000..0f4d6778 --- /dev/null +++ b/weixin-java-demo-with-spring/src/main/resources/log4j.properties @@ -0,0 +1,8 @@ +log4j.rootLogger=DEBUG, stdout + +log4j.appender.stdout=org.apache.log4j.ConsoleAppender +log4j.appender.stdout.Target=System.out +log4j.appender.stdout.layout=org.apache.log4j.EnhancedPatternLayout +log4j.appender.stdout.layout.ConversionPattern=[%-5p] %d{yyyy-MM-dd HH:mm:ss,SSS} [%t]:%c:%L#%M() %m%n + +log4j.logger.org.apache.http.impl.conn.PoolingHttpClientConnectionManager=INFO \ No newline at end of file diff --git a/weixin-java-demo-with-spring/src/main/resources/spring-service-bean.xml b/weixin-java-demo-with-spring/src/main/resources/spring-service-bean.xml new file mode 100644 index 00000000..bc5bbf9a --- /dev/null +++ b/weixin-java-demo-with-spring/src/main/resources/spring-service-bean.xml @@ -0,0 +1,29 @@ + + + + + + + + + + + + + UTF-8 + + + + + + + + \ No newline at end of file diff --git a/weixin-java-demo-with-spring/src/main/resources/spring-servlet-common.xml b/weixin-java-demo-with-spring/src/main/resources/spring-servlet-common.xml new file mode 100644 index 00000000..3686b979 --- /dev/null +++ b/weixin-java-demo-with-spring/src/main/resources/spring-servlet-common.xml @@ -0,0 +1,43 @@ + + + + + + + + + + + + + + + + + + + + text/html;charset=UTF-8 + text/plain;charset=UTF-8 + + + + + + + + + + + diff --git a/weixin-java-demo-with-spring/src/main/resources/wx-gzh1.properties.template b/weixin-java-demo-with-spring/src/main/resources/wx-gzh1.properties.template new file mode 100644 index 00000000..76afcd9e --- /dev/null +++ b/weixin-java-demo-with-spring/src/main/resources/wx-gzh1.properties.template @@ -0,0 +1,3 @@ +wx_appid= +wx_appsecret= +wx_token= diff --git a/weixin-java-demo-with-spring/src/main/resources/wx-gzh2.properties.template b/weixin-java-demo-with-spring/src/main/resources/wx-gzh2.properties.template new file mode 100644 index 00000000..76afcd9e --- /dev/null +++ b/weixin-java-demo-with-spring/src/main/resources/wx-gzh2.properties.template @@ -0,0 +1,3 @@ +wx_appid= +wx_appsecret= +wx_token= diff --git a/spring-demo/src/main/webapp/WEB-INF/web.xml b/weixin-java-demo-with-spring/src/main/webapp/WEB-INF/web.xml similarity index 84% rename from spring-demo/src/main/webapp/WEB-INF/web.xml rename to weixin-java-demo-with-spring/src/main/webapp/WEB-INF/web.xml index 6242f8f1..fd9cce94 100644 --- a/spring-demo/src/main/webapp/WEB-INF/web.xml +++ b/weixin-java-demo-with-spring/src/main/webapp/WEB-INF/web.xml @@ -20,14 +20,6 @@ encodingFilter /* - - springSecurityFilterChain - org.springframework.web.filter.DelegatingFilterProxy - - - springSecurityFilterChain - /v1/admin/* - org.springframework.web.context.ContextLoaderListener