diff --git a/pom.xml b/pom.xml index c3a9fc5..23ed1bc 100644 --- a/pom.xml +++ b/pom.xml @@ -24,17 +24,6 @@ org.springframework.boot spring-boot-starter-data-redis - - redis.clients - jedis - 2.9.3 - - - ch.qos.logback - logback-classic - - - org.springframework.boot spring-boot-starter-mail @@ -43,12 +32,6 @@ org.springframework.boot spring-boot-starter-web - - - ch.qos.logback - logback-classic - - org.mybatis.spring.boot @@ -80,6 +63,10 @@ org.junit.vintage junit-vintage-engine + + com.vaadin.external.google + android-json + @@ -120,6 +107,12 @@ org.crazycake shiro-redis 3.2.3 + + + org.apache.shiro + shiro-core + + @@ -152,28 +145,12 @@ com.alibaba druid-spring-boot-starter 1.1.20 - - - ch.qos.logback - logback-classic - - org.springframework.boot spring-boot-configuration-processor true - - io.springfox - springfox-swagger2 - 2.9.2 - - - io.springfox - springfox-swagger-ui - 2.8.0 - org.springframework.boot spring-boot-starter-thymeleaf @@ -195,12 +172,33 @@ com.github.binarywang weixin-java-open - 3.7.0 + 3.9.0 + + + commons-codec + commons-codec + + + commons-io + commons-io + + + + + com.github.jedis-lock + jedis-lock + 1.0.0 com.baidu.aip java-sdk 4.11.3 + + + org.slf4j + slf4j-simple + + diff --git a/src/main/java/com/rymcu/vertical/wx/miniapp/config/WxMaConfiguration.java b/src/main/java/com/rymcu/vertical/wx/miniapp/config/WxMaConfiguration.java index e4498d0..691e6fa 100644 --- a/src/main/java/com/rymcu/vertical/wx/miniapp/config/WxMaConfiguration.java +++ b/src/main/java/com/rymcu/vertical/wx/miniapp/config/WxMaConfiguration.java @@ -3,13 +3,13 @@ package com.rymcu.vertical.wx.miniapp.config; import cn.binarywang.wx.miniapp.api.WxMaService; import cn.binarywang.wx.miniapp.api.impl.WxMaServiceImpl; import cn.binarywang.wx.miniapp.bean.WxMaKefuMessage; -import cn.binarywang.wx.miniapp.bean.WxMaTemplateData; -import cn.binarywang.wx.miniapp.bean.WxMaTemplateMessage; +import cn.binarywang.wx.miniapp.bean.WxMaSubscribeMessage; import cn.binarywang.wx.miniapp.config.impl.WxMaDefaultConfigImpl; import cn.binarywang.wx.miniapp.message.WxMaMessageHandler; import cn.binarywang.wx.miniapp.message.WxMaMessageRouter; import com.google.common.collect.Lists; import com.google.common.collect.Maps; +import lombok.extern.slf4j.Slf4j; import me.chanjar.weixin.common.bean.result.WxMediaUploadResult; import me.chanjar.weixin.common.error.WxErrorException; import org.springframework.beans.factory.annotation.Autowired; @@ -25,13 +25,14 @@ import java.util.stream.Collectors; /** * @author Binary Wang */ +@Slf4j @Configuration @EnableConfigurationProperties(WxMaProperties.class) public class WxMaConfiguration { - private WxMaProperties properties; + private final WxMaProperties properties; - private static Map routers = Maps.newHashMap(); - private static Map maServices = Maps.newHashMap(); + private static final Map routers = Maps.newHashMap(); + private static Map maServices; @Autowired public WxMaConfiguration(WxMaProperties properties) { @@ -59,67 +60,68 @@ public class WxMaConfiguration { } maServices = configs.stream() - .map(a -> { - WxMaDefaultConfigImpl config = new WxMaDefaultConfigImpl(); - config.setAppid(a.getAppId()); - config.setSecret(a.getSecret()); - config.setToken(a.getToken()); - config.setAesKey(a.getAesKey()); - config.setMsgDataFormat(a.getMsgDataFormat()); + .map(a -> { + WxMaDefaultConfigImpl config = new WxMaDefaultConfigImpl(); +// WxMaDefaultConfigImpl config = new WxMaRedisConfigImpl(new JedisPool()); + // 使用上面的配置时,需要同时引入jedis-lock的依赖,否则会报类无法找到的异常 + config.setAppid(a.getAppId()); + config.setSecret(a.getSecret()); + config.setToken(a.getToken()); + config.setAesKey(a.getAesKey()); + config.setMsgDataFormat(a.getMsgDataFormat()); - WxMaService service = new WxMaServiceImpl(); - service.setWxMaConfig(config); - routers.put(a.getAppId(), this.newRouter(service)); - return service; - }).collect(Collectors.toMap(s -> s.getWxMaConfig().getAppid(), a -> a)); + WxMaService service = new WxMaServiceImpl(); + service.setWxMaConfig(config); + routers.put(a.getAppId(), this.newRouter(service)); + return service; + }).collect(Collectors.toMap(s -> s.getWxMaConfig().getAppid(), a -> a)); } private WxMaMessageRouter newRouter(WxMaService service) { final WxMaMessageRouter router = new WxMaMessageRouter(service); router - .rule().handler(logHandler).next() - .rule().async(false).content("模板").handler(templateMsgHandler).end() - .rule().async(false).content("文本").handler(textHandler).end() - .rule().async(false).content("图片").handler(picHandler).end() - .rule().async(false).content("二维码").handler(qrcodeHandler).end(); + .rule().handler(logHandler).next() + .rule().async(false).content("订阅消息").handler(subscribeMsgHandler).end() + .rule().async(false).content("文本").handler(textHandler).end() + .rule().async(false).content("图片").handler(picHandler).end() + .rule().async(false).content("二维码").handler(qrcodeHandler).end(); return router; } - private final WxMaMessageHandler templateMsgHandler = (wxMessage, context, service, sessionManager) -> { - service.getMsgService().sendTemplateMsg(WxMaTemplateMessage.builder() - .templateId("此处更换为自己的模板id") - .formId("自己替换可用的formid") - .data(Lists.newArrayList( - new WxMaTemplateData("keyword1", "339208499", "#173177"))) - .toUser(wxMessage.getFromUser()) - .build()); + private final WxMaMessageHandler subscribeMsgHandler = (wxMessage, context, service, sessionManager) -> { + service.getMsgService().sendSubscribeMsg(WxMaSubscribeMessage.builder() + .templateId("此处更换为自己的模板id") + .data(Lists.newArrayList( + new WxMaSubscribeMessage.Data("keyword1", "339208499"))) + .toUser(wxMessage.getFromUser()) + .build()); return null; }; private final WxMaMessageHandler logHandler = (wxMessage, context, service, sessionManager) -> { - System.out.println("收到消息:" + wxMessage.toString()); + log.info("收到消息:" + wxMessage.toString()); service.getMsgService().sendKefuMsg(WxMaKefuMessage.newTextBuilder().content("收到信息为:" + wxMessage.toJson()) - .toUser(wxMessage.getFromUser()).build()); + .toUser(wxMessage.getFromUser()).build()); return null; }; private final WxMaMessageHandler textHandler = (wxMessage, context, service, sessionManager) -> { service.getMsgService().sendKefuMsg(WxMaKefuMessage.newTextBuilder().content("回复文本消息") - .toUser(wxMessage.getFromUser()).build()); + .toUser(wxMessage.getFromUser()).build()); return null; }; private final WxMaMessageHandler picHandler = (wxMessage, context, service, sessionManager) -> { try { WxMediaUploadResult uploadResult = service.getMediaService() - .uploadMedia("image", "png", - ClassLoader.getSystemResourceAsStream("tmp.png")); + .uploadMedia("image", "png", + ClassLoader.getSystemResourceAsStream("tmp.png")); service.getMsgService().sendKefuMsg( - WxMaKefuMessage - .newImageBuilder() - .mediaId(uploadResult.getMediaId()) - .toUser(wxMessage.getFromUser()) - .build()); + WxMaKefuMessage + .newImageBuilder() + .mediaId(uploadResult.getMediaId()) + .toUser(wxMessage.getFromUser()) + .build()); } catch (WxErrorException e) { e.printStackTrace(); } @@ -132,11 +134,11 @@ public class WxMaConfiguration { final File file = service.getQrcodeService().createQrcode("123", 430); WxMediaUploadResult uploadResult = service.getMediaService().uploadMedia("image", file); service.getMsgService().sendKefuMsg( - WxMaKefuMessage - .newImageBuilder() - .mediaId(uploadResult.getMediaId()) - .toUser(wxMessage.getFromUser()) - .build()); + WxMaKefuMessage + .newImageBuilder() + .mediaId(uploadResult.getMediaId()) + .toUser(wxMessage.getFromUser()) + .build()); } catch (WxErrorException e) { e.printStackTrace(); } diff --git a/src/main/java/com/rymcu/vertical/wx/mp/controller/WxRedirectController.java b/src/main/java/com/rymcu/vertical/wx/mp/controller/WxRedirectController.java index 518f9b9..6c63290 100644 --- a/src/main/java/com/rymcu/vertical/wx/mp/controller/WxRedirectController.java +++ b/src/main/java/com/rymcu/vertical/wx/mp/controller/WxRedirectController.java @@ -27,8 +27,8 @@ public class WxRedirectController { } try { - WxMpOAuth2AccessToken accessToken = wxService.oauth2getAccessToken(code); - WxMpUser user = wxService.oauth2getUserInfo(accessToken, null); + WxMpOAuth2AccessToken accessToken = wxService.getOAuth2Service().getAccessToken(code); + WxMpUser user = wxService.getOAuth2Service().getUserInfo(accessToken, null); map.put("user", user); } catch (WxErrorException e) { e.printStackTrace(); diff --git a/src/main/java/com/rymcu/vertical/wx/mp/controller/WxoAuthController.java b/src/main/java/com/rymcu/vertical/wx/mp/controller/WxoAuthController.java index e5c3c4d..84883af 100644 --- a/src/main/java/com/rymcu/vertical/wx/mp/controller/WxoAuthController.java +++ b/src/main/java/com/rymcu/vertical/wx/mp/controller/WxoAuthController.java @@ -44,7 +44,7 @@ public class WxoAuthController { baseUrl = new StringBuilder(domain).append(contextPath); } StringBuilder accessTokenUrl = baseUrl.append("/wx/oauth/" + appId + "/getAccessToken?redirectUrl=").append(URIUtil.encodeURIComponent(redirectUrl)); - String oauth2Url = wxMpService.oauth2buildAuthorizationUrl(accessTokenUrl.toString(), WxConsts.OAuth2Scope.SNSAPI_BASE, null); + String oauth2Url = wxMpService.getOAuth2Service().buildAuthorizationUrl(accessTokenUrl.toString(), WxConsts.OAuth2Scope.SNSAPI_BASE, null); return "redirect:" + oauth2Url; } @@ -52,8 +52,8 @@ public class WxoAuthController { @GetMapping("getAccessToken") public String getAccessToken(@PathVariable String appId, @RequestParam(name = "code") String code, @RequestParam(name = "redirectUrl") String redirectUrl) throws Exception { wxMpService.switchoverTo(appId); - WxMpOAuth2AccessToken oAuth2AccessToken = wxMpService.oauth2getAccessToken(code); - boolean valid = wxMpService.oauth2validateAccessToken(oAuth2AccessToken); + WxMpOAuth2AccessToken oAuth2AccessToken = wxMpService.getOAuth2Service().getAccessToken(code); + boolean valid = wxMpService.getOAuth2Service().validateAccessToken(oAuth2AccessToken); if (!valid) { throw new Exception("无权限"); } diff --git a/src/main/java/com/rymcu/vertical/wx/mp/handler/MsgHandler.java b/src/main/java/com/rymcu/vertical/wx/mp/handler/MsgHandler.java index c4a2cf0..28c6ac6 100644 --- a/src/main/java/com/rymcu/vertical/wx/mp/handler/MsgHandler.java +++ b/src/main/java/com/rymcu/vertical/wx/mp/handler/MsgHandler.java @@ -20,7 +20,7 @@ public class MsgHandler extends AbstractHandler { @Override public WxMpXmlOutMessage handle(WxMpXmlMessage wxMessage, - Map context, WxMpService weixinService, + Map context, WxMpService wxMpService, WxSessionManager sessionManager) { if (!wxMessage.getMsgType().equals(XmlMsgType.EVENT)) { @@ -33,9 +33,12 @@ public class MsgHandler extends AbstractHandler { .toUser(wxMessage.getFromUser()).build(); } + logger.info("\n接收到 {} 公众号请求消息,内容:{}", wxMpService.getWxMpConfigStorage().getAppId(), wxMessage); + + String content = "我们已经收到您的留言,稍后客服将会联系您!"; - return new TextBuilder().build(content, wxMessage, weixinService); + return new TextBuilder().build(content, wxMessage, wxMpService); } diff --git a/src/main/java/com/rymcu/vertical/wx/open/controller/WxOpenApiController.java b/src/main/java/com/rymcu/vertical/wx/open/controller/WxOpenApiController.java index 3473a02..f613a4f 100644 --- a/src/main/java/com/rymcu/vertical/wx/open/controller/WxOpenApiController.java +++ b/src/main/java/com/rymcu/vertical/wx/open/controller/WxOpenApiController.java @@ -40,7 +40,7 @@ public class WxOpenApiController { System.out.println("===================================Host:"); System.out.println(request.getHeader("host")); String host = request.getHeader("host"); - String url = "http://"+host+"/vertical-console/wx/open/auth/jump"; + String url = "http://"+host+"/open/auth/jump"; try { url = wxOpenServiceHandler.getWxOpenComponentService().getPreAuthUrl(url); // 添加来源,解决302跳转来源丢失的问题 diff --git a/src/main/java/com/rymcu/vertical/wx/open/controller/WxOpenNotifyController.java b/src/main/java/com/rymcu/vertical/wx/open/controller/WxOpenNotifyController.java index d7b2784..d56334c 100644 --- a/src/main/java/com/rymcu/vertical/wx/open/controller/WxOpenNotifyController.java +++ b/src/main/java/com/rymcu/vertical/wx/open/controller/WxOpenNotifyController.java @@ -23,9 +23,13 @@ public class WxOpenNotifyController { @Autowired protected WxOpenServiceHandler wxOpenService; - /**全网发布官方测试小程序 AppId*/ + /** + * 全网发布官方测试小程序 AppId + */ private final static String testMiniProgramAppId = "wxd101a85aa106f53e"; - /**全网发布官方测试公众号 AppId*/ + /** + * 全网发布官方测试公众号 AppId + */ private final static String testMpAppId = "wx570bc396a51b8ff8"; private final static String TESTCOMPONENT_MSG_TYPE_TEXT = "TESTCOMPONENT_MSG_TYPE_TEXT"; private final static String TESTCOMPONENT_MSG_TYPE_TEXT_callback = "TESTCOMPONENT_MSG_TYPE_TEXT_callback"; @@ -107,9 +111,9 @@ public class WxOpenNotifyController { } catch (WxErrorException e) { logger.error("callback", e); } - }else{ + } else { WxMpXmlOutMessage outMessage = wxOpenService.getWxOpenMessageRouter().route(inMessage, appId); - if(outMessage != null){ + if (outMessage != null) { out = WxOpenXmlMessage.wxMpOutXmlMessageToEncryptedXml(outMessage, wxOpenService.getWxOpenConfigStorage()); } }