⬆️ 升级微信服务框架依赖

This commit is contained in:
x ronger 2020-11-17 00:20:40 +08:00
parent ea8164e7e7
commit 9ad7fc3c68
7 changed files with 98 additions and 91 deletions

66
pom.xml
View File

@ -24,17 +24,6 @@
<groupId>org.springframework.boot</groupId> <groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId> <artifactId>spring-boot-starter-data-redis</artifactId>
</dependency> </dependency>
<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
<version>2.9.3</version>
<exclusions>
<exclusion>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency> <dependency>
<groupId>org.springframework.boot</groupId> <groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-mail</artifactId> <artifactId>spring-boot-starter-mail</artifactId>
@ -43,12 +32,6 @@
<dependency> <dependency>
<groupId>org.springframework.boot</groupId> <groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId> <artifactId>spring-boot-starter-web</artifactId>
<exclusions>
<exclusion>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
</exclusion>
</exclusions>
</dependency> </dependency>
<dependency> <dependency>
<groupId>org.mybatis.spring.boot</groupId> <groupId>org.mybatis.spring.boot</groupId>
@ -80,6 +63,10 @@
<groupId>org.junit.vintage</groupId> <groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId> <artifactId>junit-vintage-engine</artifactId>
</exclusion> </exclusion>
<exclusion>
<groupId>com.vaadin.external.google</groupId>
<artifactId>android-json</artifactId>
</exclusion>
</exclusions> </exclusions>
</dependency> </dependency>
<dependency> <dependency>
@ -120,6 +107,12 @@
<groupId>org.crazycake</groupId> <groupId>org.crazycake</groupId>
<artifactId>shiro-redis</artifactId> <artifactId>shiro-redis</artifactId>
<version>3.2.3</version> <version>3.2.3</version>
<exclusions>
<exclusion>
<groupId>org.apache.shiro</groupId>
<artifactId>shiro-core</artifactId>
</exclusion>
</exclusions>
</dependency> </dependency>
<!--apache相关依赖--> <!--apache相关依赖-->
<dependency> <dependency>
@ -152,28 +145,12 @@
<groupId>com.alibaba</groupId> <groupId>com.alibaba</groupId>
<artifactId>druid-spring-boot-starter</artifactId> <artifactId>druid-spring-boot-starter</artifactId>
<version>1.1.20</version> <version>1.1.20</version>
<exclusions>
<exclusion>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
</exclusion>
</exclusions>
</dependency> </dependency>
<dependency> <dependency>
<groupId>org.springframework.boot</groupId> <groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId> <artifactId>spring-boot-configuration-processor</artifactId>
<optional>true</optional> <optional>true</optional>
</dependency> </dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>2.9.2</version>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>2.8.0</version>
</dependency>
<dependency> <dependency>
<groupId>org.springframework.boot</groupId> <groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId> <artifactId>spring-boot-starter-thymeleaf</artifactId>
@ -195,12 +172,33 @@
<dependency> <dependency>
<groupId>com.github.binarywang</groupId> <groupId>com.github.binarywang</groupId>
<artifactId>weixin-java-open</artifactId> <artifactId>weixin-java-open</artifactId>
<version>3.7.0</version> <version>3.9.0</version>
<exclusions>
<exclusion>
<groupId>commons-codec</groupId>
<artifactId>commons-codec</artifactId>
</exclusion>
<exclusion>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>com.github.jedis-lock</groupId>
<artifactId>jedis-lock</artifactId>
<version>1.0.0</version>
</dependency> </dependency>
<dependency> <dependency>
<groupId>com.baidu.aip</groupId> <groupId>com.baidu.aip</groupId>
<artifactId>java-sdk</artifactId> <artifactId>java-sdk</artifactId>
<version>4.11.3</version> <version>4.11.3</version>
<exclusions>
<exclusion>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-simple</artifactId>
</exclusion>
</exclusions>
</dependency> </dependency>
</dependencies> </dependencies>

View File

@ -3,13 +3,13 @@ package com.rymcu.vertical.wx.miniapp.config;
import cn.binarywang.wx.miniapp.api.WxMaService; import cn.binarywang.wx.miniapp.api.WxMaService;
import cn.binarywang.wx.miniapp.api.impl.WxMaServiceImpl; import cn.binarywang.wx.miniapp.api.impl.WxMaServiceImpl;
import cn.binarywang.wx.miniapp.bean.WxMaKefuMessage; import cn.binarywang.wx.miniapp.bean.WxMaKefuMessage;
import cn.binarywang.wx.miniapp.bean.WxMaTemplateData; import cn.binarywang.wx.miniapp.bean.WxMaSubscribeMessage;
import cn.binarywang.wx.miniapp.bean.WxMaTemplateMessage;
import cn.binarywang.wx.miniapp.config.impl.WxMaDefaultConfigImpl; import cn.binarywang.wx.miniapp.config.impl.WxMaDefaultConfigImpl;
import cn.binarywang.wx.miniapp.message.WxMaMessageHandler; import cn.binarywang.wx.miniapp.message.WxMaMessageHandler;
import cn.binarywang.wx.miniapp.message.WxMaMessageRouter; import cn.binarywang.wx.miniapp.message.WxMaMessageRouter;
import com.google.common.collect.Lists; import com.google.common.collect.Lists;
import com.google.common.collect.Maps; import com.google.common.collect.Maps;
import lombok.extern.slf4j.Slf4j;
import me.chanjar.weixin.common.bean.result.WxMediaUploadResult; import me.chanjar.weixin.common.bean.result.WxMediaUploadResult;
import me.chanjar.weixin.common.error.WxErrorException; import me.chanjar.weixin.common.error.WxErrorException;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
@ -25,13 +25,14 @@ import java.util.stream.Collectors;
/** /**
* @author <a href="https://github.com/binarywang">Binary Wang</a> * @author <a href="https://github.com/binarywang">Binary Wang</a>
*/ */
@Slf4j
@Configuration @Configuration
@EnableConfigurationProperties(WxMaProperties.class) @EnableConfigurationProperties(WxMaProperties.class)
public class WxMaConfiguration { public class WxMaConfiguration {
private WxMaProperties properties; private final WxMaProperties properties;
private static Map<String, WxMaMessageRouter> routers = Maps.newHashMap(); private static final Map<String, WxMaMessageRouter> routers = Maps.newHashMap();
private static Map<String, WxMaService> maServices = Maps.newHashMap(); private static Map<String, WxMaService> maServices;
@Autowired @Autowired
public WxMaConfiguration(WxMaProperties properties) { public WxMaConfiguration(WxMaProperties properties) {
@ -61,6 +62,8 @@ public class WxMaConfiguration {
maServices = configs.stream() maServices = configs.stream()
.map(a -> { .map(a -> {
WxMaDefaultConfigImpl config = new WxMaDefaultConfigImpl(); WxMaDefaultConfigImpl config = new WxMaDefaultConfigImpl();
// WxMaDefaultConfigImpl config = new WxMaRedisConfigImpl(new JedisPool());
// 使用上面的配置时需要同时引入jedis-lock的依赖否则会报类无法找到的异常
config.setAppid(a.getAppId()); config.setAppid(a.getAppId());
config.setSecret(a.getSecret()); config.setSecret(a.getSecret());
config.setToken(a.getToken()); config.setToken(a.getToken());
@ -78,26 +81,25 @@ public class WxMaConfiguration {
final WxMaMessageRouter router = new WxMaMessageRouter(service); final WxMaMessageRouter router = new WxMaMessageRouter(service);
router router
.rule().handler(logHandler).next() .rule().handler(logHandler).next()
.rule().async(false).content("模板").handler(templateMsgHandler).end() .rule().async(false).content("订阅消息").handler(subscribeMsgHandler).end()
.rule().async(false).content("文本").handler(textHandler).end() .rule().async(false).content("文本").handler(textHandler).end()
.rule().async(false).content("图片").handler(picHandler).end() .rule().async(false).content("图片").handler(picHandler).end()
.rule().async(false).content("二维码").handler(qrcodeHandler).end(); .rule().async(false).content("二维码").handler(qrcodeHandler).end();
return router; return router;
} }
private final WxMaMessageHandler templateMsgHandler = (wxMessage, context, service, sessionManager) -> { private final WxMaMessageHandler subscribeMsgHandler = (wxMessage, context, service, sessionManager) -> {
service.getMsgService().sendTemplateMsg(WxMaTemplateMessage.builder() service.getMsgService().sendSubscribeMsg(WxMaSubscribeMessage.builder()
.templateId("此处更换为自己的模板id") .templateId("此处更换为自己的模板id")
.formId("自己替换可用的formid")
.data(Lists.newArrayList( .data(Lists.newArrayList(
new WxMaTemplateData("keyword1", "339208499", "#173177"))) new WxMaSubscribeMessage.Data("keyword1", "339208499")))
.toUser(wxMessage.getFromUser()) .toUser(wxMessage.getFromUser())
.build()); .build());
return null; return null;
}; };
private final WxMaMessageHandler logHandler = (wxMessage, context, service, sessionManager) -> { 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()) service.getMsgService().sendKefuMsg(WxMaKefuMessage.newTextBuilder().content("收到信息为:" + wxMessage.toJson())
.toUser(wxMessage.getFromUser()).build()); .toUser(wxMessage.getFromUser()).build());
return null; return null;

View File

@ -27,8 +27,8 @@ public class WxRedirectController {
} }
try { try {
WxMpOAuth2AccessToken accessToken = wxService.oauth2getAccessToken(code); WxMpOAuth2AccessToken accessToken = wxService.getOAuth2Service().getAccessToken(code);
WxMpUser user = wxService.oauth2getUserInfo(accessToken, null); WxMpUser user = wxService.getOAuth2Service().getUserInfo(accessToken, null);
map.put("user", user); map.put("user", user);
} catch (WxErrorException e) { } catch (WxErrorException e) {
e.printStackTrace(); e.printStackTrace();

View File

@ -44,7 +44,7 @@ public class WxoAuthController {
baseUrl = new StringBuilder(domain).append(contextPath); baseUrl = new StringBuilder(domain).append(contextPath);
} }
StringBuilder accessTokenUrl = baseUrl.append("/wx/oauth/" + appId + "/getAccessToken?redirectUrl=").append(URIUtil.encodeURIComponent(redirectUrl)); 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; return "redirect:" + oauth2Url;
} }
@ -52,8 +52,8 @@ public class WxoAuthController {
@GetMapping("getAccessToken") @GetMapping("getAccessToken")
public String getAccessToken(@PathVariable String appId, @RequestParam(name = "code") String code, @RequestParam(name = "redirectUrl") String redirectUrl) throws Exception { public String getAccessToken(@PathVariable String appId, @RequestParam(name = "code") String code, @RequestParam(name = "redirectUrl") String redirectUrl) throws Exception {
wxMpService.switchoverTo(appId); wxMpService.switchoverTo(appId);
WxMpOAuth2AccessToken oAuth2AccessToken = wxMpService.oauth2getAccessToken(code); WxMpOAuth2AccessToken oAuth2AccessToken = wxMpService.getOAuth2Service().getAccessToken(code);
boolean valid = wxMpService.oauth2validateAccessToken(oAuth2AccessToken); boolean valid = wxMpService.getOAuth2Service().validateAccessToken(oAuth2AccessToken);
if (!valid) { if (!valid) {
throw new Exception("无权限"); throw new Exception("无权限");
} }

View File

@ -20,7 +20,7 @@ public class MsgHandler extends AbstractHandler {
@Override @Override
public WxMpXmlOutMessage handle(WxMpXmlMessage wxMessage, public WxMpXmlOutMessage handle(WxMpXmlMessage wxMessage,
Map<String, Object> context, WxMpService weixinService, Map<String, Object> context, WxMpService wxMpService,
WxSessionManager sessionManager) { WxSessionManager sessionManager) {
if (!wxMessage.getMsgType().equals(XmlMsgType.EVENT)) { if (!wxMessage.getMsgType().equals(XmlMsgType.EVENT)) {
@ -33,9 +33,12 @@ public class MsgHandler extends AbstractHandler {
.toUser(wxMessage.getFromUser()).build(); .toUser(wxMessage.getFromUser()).build();
} }
logger.info("\n接收到 {} 公众号请求消息,内容:{}", wxMpService.getWxMpConfigStorage().getAppId(), wxMessage);
String content = "我们已经收到您的留言,稍后客服将会联系您!"; String content = "我们已经收到您的留言,稍后客服将会联系您!";
return new TextBuilder().build(content, wxMessage, weixinService); return new TextBuilder().build(content, wxMessage, wxMpService);
} }

View File

@ -40,7 +40,7 @@ public class WxOpenApiController {
System.out.println("===================================Host:"); System.out.println("===================================Host:");
System.out.println(request.getHeader("host")); System.out.println(request.getHeader("host"));
String host = 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 { try {
url = wxOpenServiceHandler.getWxOpenComponentService().getPreAuthUrl(url); url = wxOpenServiceHandler.getWxOpenComponentService().getPreAuthUrl(url);
// 添加来源解决302跳转来源丢失的问题 // 添加来源解决302跳转来源丢失的问题

View File

@ -23,9 +23,13 @@ public class WxOpenNotifyController {
@Autowired @Autowired
protected WxOpenServiceHandler wxOpenService; protected WxOpenServiceHandler wxOpenService;
/**全网发布官方测试小程序 AppId*/ /**
* 全网发布官方测试小程序 AppId
*/
private final static String testMiniProgramAppId = "wxd101a85aa106f53e"; private final static String testMiniProgramAppId = "wxd101a85aa106f53e";
/**全网发布官方测试公众号 AppId*/ /**
* 全网发布官方测试公众号 AppId
*/
private final static String testMpAppId = "wx570bc396a51b8ff8"; 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 = "TESTCOMPONENT_MSG_TYPE_TEXT";
private final static String TESTCOMPONENT_MSG_TYPE_TEXT_callback = "TESTCOMPONENT_MSG_TYPE_TEXT_callback"; private final static String TESTCOMPONENT_MSG_TYPE_TEXT_callback = "TESTCOMPONENT_MSG_TYPE_TEXT_callback";