提交 b39565a9 编写于 作者: 如梦技术's avatar 如梦技术 🐛

v1.3.0

上级 184ed687
......@@ -52,8 +52,8 @@ dream:
appSecret: 11ed9e2b8e3e3c131e7be320a42b2b5a
token: 123456
wxa-config:
appid: wx4f53594f9a6b3dcb
appSecret: eec6482ba3804df05bd10895bace0579
app-id: wx4f53594f9a6b3dcb
app-secret: eec6482ba3804df05bd10895bace0579
```
- cache使用spring的cache,需要`@EnableCaching`开启。
......
......@@ -12,15 +12,15 @@ apply plugin: 'maven'
apply plugin: 'signing'
apply plugin: 'propdeps'
apply plugin: 'propdeps-maven'
apply plugin: 'maven-publish'
apply plugin: 'io.spring.dependency-management'
ext {
javaVersion = JavaVersion.VERSION_1_8
springVersion = "4.3.14.RELEASE"
springBootVersion = "1.5.10.RELEASE"
jfinalVersion = "3.3"
jfinalWeixinVersion = "1.9"
junitVersion = "4.12"
springVersion = "4.3.18.RELEASE"
springBootVersion = "1.5.15.RELEASE"
jfinalVersion = "3.4"
jfinalWeixinVersion = "2.1"
lombokVersion = '1.16.20'
}
......@@ -39,7 +39,7 @@ dependencies {
provided "org.springframework.boot:spring-boot-starter-aop"
optional "org.springframework.boot:spring-boot-configuration-processor"
provided "org.projectlombok:lombok:${lombokVersion}"
testCompile "junit:junit:${junitVersion}"
testCompile "org.springframework.boot:spring-boot-starter-test"
}
dependencyManagement {
......@@ -53,6 +53,21 @@ task sourcesJar(type: Jar, dependsOn: classes) {
from sourceSets.main.allSource
}
jar {
into("META-INF/maven/$project.group/$project.name") {
from { generatePomFileForMavenJavaPublication }
rename ".*", "pom.xml"
}
}
publishing {
publications {
mavenJava(MavenPublication) {
from components.java
}
}
}
javadoc {
options {
encoding "UTF-8"
......
package net.dreamlu.weixin.aspect;
import com.jfinal.weixin.sdk.api.ApiConfigKit;
import lombok.extern.slf4j.Slf4j;
import lombok.AllArgsConstructor;
import net.dreamlu.weixin.annotation.WxApi;
import net.dreamlu.weixin.properties.DreamWeixinProperties;
import org.aspectj.lang.ProceedingJoinPoint;
......@@ -15,15 +15,10 @@ import javax.servlet.http.HttpServletRequest;
@Aspect
@Order
@Slf4j
@AllArgsConstructor
public class WxApiAspect {
private final DreamWeixinProperties weixinProperties;
public WxApiAspect(DreamWeixinProperties weixinProperties) {
this.weixinProperties = weixinProperties;
}
@Around("@annotation(wxApi)")
public Object aroundWxApi(ProceedingJoinPoint point, WxApi wxApi) throws Throwable {
// 目前不支持多小程序,所以不用判断是否为小程序
......
package net.dreamlu.weixin.cache;
import com.jfinal.weixin.sdk.cache.IAccessTokenCache;
import lombok.AllArgsConstructor;
import org.springframework.cache.Cache;
@AllArgsConstructor
public class SpringAccessTokenCache implements IAccessTokenCache {
private final String ACCESS_TOKEN_PREFIX = "dream-weixin:token:";
private final static String ACCESS_TOKEN_PREFIX = "dream-weixin:token:";
private final Cache cache;
public SpringAccessTokenCache(Cache cache) {
this.cache = cache;
}
@Override
public String get(String key) {
return cache.get(ACCESS_TOKEN_PREFIX + key, String.class);
......
package net.dreamlu.weixin.config;
import lombok.AllArgsConstructor;
import net.dreamlu.weixin.aspect.WxApiAspect;
import net.dreamlu.weixin.cache.SpringAccessTokenCache;
import net.dreamlu.weixin.properties.DreamWeixinProperties;
import net.dreamlu.weixin.spring.MsgInterceptor;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.boot.autoconfigure.AutoConfigureAfter;
import org.springframework.cache.Cache;
import org.springframework.cache.CacheManager;
import org.springframework.context.annotation.Bean;
......@@ -13,34 +14,24 @@ import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
@Configuration
@EnableConfigurationProperties(DreamWeixinProperties.class)
@AutoConfigureAfter(DreamWeixinProperties.class)
@AllArgsConstructor
public class DreamWeixinAutoConfiguration {
private final CacheManager cacheManager;
private final DreamWeixinProperties weixinProperties;
public DreamWeixinAutoConfiguration(CacheManager cacheManager, DreamWeixinProperties weixinProperties) {
this.cacheManager = cacheManager;
this.weixinProperties = weixinProperties;
}
@Bean
public WeixinAppConfig weixinAppConfig() {
return new WeixinAppConfig(weixinProperties);
}
@Bean
public SpringAccessTokenCache springAccessTokenCache() {
Cache cache = cacheManager.getCache(weixinProperties.getAccessTokenCache());
public SpringAccessTokenCache springAccessTokenCache(DreamWeixinProperties properties) {
Cache cache = cacheManager.getCache(properties.getAccessTokenCache());
return new SpringAccessTokenCache(cache);
}
@Bean
public WxApiAspect wxApiAspect() {
return new WxApiAspect(weixinProperties);
public WxApiAspect wxApiAspect(DreamWeixinProperties properties) {
return new WxApiAspect(properties);
}
@Configuration
public class MsgConfiguration extends WebMvcConfigurerAdapter {
public static class MsgConfiguration extends WebMvcConfigurerAdapter {
private final DreamWeixinProperties properties;
public MsgConfiguration(DreamWeixinProperties properties) {
......
......@@ -4,31 +4,46 @@ import com.jfinal.weixin.sdk.api.ApiConfig;
import com.jfinal.weixin.sdk.api.ApiConfigKit;
import com.jfinal.wxaapp.WxaConfig;
import com.jfinal.wxaapp.WxaConfigKit;
import lombok.AllArgsConstructor;
import net.dreamlu.weixin.properties.DreamWeixinProperties;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.boot.autoconfigure.AutoConfigureAfter;
import org.springframework.context.annotation.Configuration;
import java.util.List;
@Configuration
@AutoConfigureAfter(DreamWeixinAutoConfiguration.class)
@AllArgsConstructor
public class WeixinAppConfig implements InitializingBean {
private final DreamWeixinProperties weixinProperties;
private final DreamWeixinProperties weixinProperties;
public WeixinAppConfig(DreamWeixinProperties weixinProperties) {
this.weixinProperties = weixinProperties;
}
@Override
public void afterPropertiesSet() throws Exception {
boolean isdev = weixinProperties.isDevMode();
ApiConfigKit.setDevMode(isdev);
List<DreamWeixinProperties.ApiConfig> list = weixinProperties.getWxConfigs();
for (DreamWeixinProperties.ApiConfig apiConfig : list) {
ApiConfig config = new ApiConfig();
config.setAppId(apiConfig.getAppId());
config.setAppSecret(apiConfig.getAppSecret());
config.setToken(apiConfig.getToken());
config.setEncodingAesKey(apiConfig.getEncodingAesKey());
config.setEncryptMessage(apiConfig.isMessageEncrypt());
ApiConfigKit.putApiConfig(config);
}
DreamWeixinProperties.WxaConfig wxaConfig = weixinProperties.getWxaConfig();
WxaConfig config = new WxaConfig();
config.setAppId(wxaConfig.getAppId());
config.setAppSecret(wxaConfig.getAppSecret());
config.setToken(wxaConfig.getToken());
config.setEncodingAesKey(wxaConfig.getEncodingAesKey());
config.setMessageEncrypt(wxaConfig.isMessageEncrypt());
WxaConfigKit.setDevMode(isdev);
WxaConfigKit.setWxaConfig(config);
if (WxaMsgParser.JSON == weixinProperties.getWxaMsgParser()) {
WxaConfigKit.useJsonMsgParser();
}
}
@Override
public void afterPropertiesSet() throws Exception {
boolean isdev = weixinProperties.isDevMode();
ApiConfigKit.setDevMode(isdev);
List<ApiConfig> list = weixinProperties.getWxConfigs();
for (ApiConfig apiConfig : list) {
ApiConfigKit.putApiConfig(apiConfig);
}
WxaConfig wxaConfig = weixinProperties.getWxaConfig();
WxaConfigKit.setDevMode(isdev);
WxaConfigKit.setWxaConfig(wxaConfig);
if (WxaMsgParser.JSON == weixinProperties.getWxaMsgParser()) {
WxaConfigKit.useJsonMsgParser();
}
}
}
package net.dreamlu.weixin.properties;
import com.jfinal.weixin.sdk.api.ApiConfig;
import com.jfinal.wxaapp.WxaConfig;
import lombok.Getter;
import lombok.Setter;
import net.dreamlu.weixin.config.WxaMsgParser;
......@@ -10,55 +8,63 @@ import org.springframework.boot.context.properties.ConfigurationProperties;
import java.util.ArrayList;
import java.util.List;
@Getter
@Setter
@ConfigurationProperties("dream.weixin")
public class DreamWeixinProperties {
/**
* JFinal filter拦截的路由,默认:/weixin/*
*/
@Getter
@Setter
private String urlPatterns = "/weixin/*";
/**
* 拦截的路由,默认:/weixin/*
*/
private String urlPatterns = "/weixin/*";
/**
* 是否开发模式,默认:false
*/
@Getter
@Setter
private boolean devMode = false;
/**
* 是否开发模式,默认:false
*/
private boolean devMode = false;
/**
* Spring cache name,需要开启spring cache,默认:dreamWeixinCache
*/
@Getter
@Setter
private String accessTokenCache = "dreamWeixinCache";
/**
* Spring cache name,需要开启spring cache,默认:dreamWeixinCache
*/
private String accessTokenCache = "dreamWeixinCache";
/**
* 多公众号url挂参,默认:appId
*/
@Getter
@Setter
private String appIdKey = "appId";
/**
* 多公众号url挂参,默认:appId
*/
private String appIdKey = "appId";
/**
* 多公众号配置
*/
@Getter
private List<ApiConfig> wxConfigs = new ArrayList<ApiConfig>();
/**
* 多公众号配置
*/
private List<ApiConfig> wxConfigs = new ArrayList<>();
/**
* 小程序配置
*/
@Getter
@Setter
private WxaConfig wxaConfig = new WxaConfig();
/**
* 小程序配置
*/
private WxaConfig wxaConfig = new WxaConfig();
/**
* 小程序消息解析,默认xml,支持json和xml
*/
@Getter
@Setter
private WxaMsgParser wxaMsgParser = WxaMsgParser.XML;
/**
* 小程序消息解析,默认xml,支持json和xml
*/
private WxaMsgParser wxaMsgParser = WxaMsgParser.XML;
@Getter
@Setter
public static class ApiConfig {
private String token;
private String appId;
private String appSecret;
private String encodingAesKey;
private boolean messageEncrypt = false; // 消息加密与否
}
@Getter
@Setter
public static class WxaConfig {
private String appId;
private String appSecret;
private String token;
private String encodingAesKey;
private boolean messageEncrypt = false; // 消息加密与否
}
}
......@@ -16,6 +16,7 @@ import com.jfinal.weixin.sdk.msg.out.OutTextMsg;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
......@@ -34,9 +35,10 @@ public abstract class MsgController {
/**
* weixin 公众号服务器调用唯一入口,即在开发者中心输入的 URL 必须要指向此 action
* @param imXmlMsg imXmlMsg
* @return {ResponseEntity}
*/
@RequestMapping("")
public void index(@RequestBody String imXmlMsg) {
public ResponseEntity<Void> index(@RequestBody String imXmlMsg) {
// 开发模式输出微信服务发送过来的 xml 消息
if (ApiConfigKit.isDevMode()) {
System.out.println("接收消息:");
......@@ -133,6 +135,7 @@ public abstract class MsgController {
logger.error("未能识别的消息类型。 消息 xml 内容为:\n" + imXmlMsg);
processIsNotDefinedMsg((InNotDefinedMsg) msg);
}
return ResponseEntity.ok().build();
}
/**
......
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
net.dreamlu.weixin.properties.DreamWeixinProperties,\
net.dreamlu.weixin.config.WeixinAppConfig,\
net.dreamlu.weixin.config.DreamWeixinAutoConfiguration
provides: jfinal, jfinal-weixin, cglib
provides: jfinal, jfinal-weixin
package com.example.demo;
import com.jfinal.weixin.sdk.jfinal.MsgControllerAdapter;
import com.jfinal.weixin.sdk.msg.in.InTextMsg;
import com.jfinal.weixin.sdk.msg.in.event.InFollowEvent;
import com.jfinal.weixin.sdk.msg.in.event.InMenuEvent;
......
......@@ -8,5 +8,5 @@ dream:
appSecret: 11ed9e2b8e3e3c131e7be320a42b2b5a
token: 123456
wxa-config:
appid: wx4f53594f9a6b3dcb
appSecret: eec6482ba3804df05bd10895bace0579
\ No newline at end of file
app-id: wx4f53594f9a6b3dcb
app-secret: eec6482ba3804df05bd10895bace0579
\ No newline at end of file
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册