mirror of
https://gitee.com/binary/weixin-java-tools.git
synced 2025-04-05 17:38:05 +08:00
调整企业号message相关类的包位置
This commit is contained in:
parent
239b8c505c
commit
49cf402a4e
@ -1,74 +1,74 @@
|
|||||||
package me.chanjar.weixin.cp.api;
|
package me.chanjar.weixin.cp.api;
|
||||||
|
|
||||||
import me.chanjar.weixin.common.bean.WxAccessToken;
|
import me.chanjar.weixin.common.bean.WxAccessToken;
|
||||||
import me.chanjar.weixin.common.util.http.apache.ApacheHttpClientBuilder;
|
import me.chanjar.weixin.common.util.http.apache.ApacheHttpClientBuilder;
|
||||||
|
|
||||||
import java.io.File;
|
import java.io.File;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 微信客户端配置存储
|
* 微信客户端配置存储
|
||||||
*
|
*
|
||||||
* @author Daniel Qian
|
* @author Daniel Qian
|
||||||
*/
|
*/
|
||||||
public interface WxCpConfigStorage {
|
public interface WxCpConfigStorage {
|
||||||
|
|
||||||
String getAccessToken();
|
String getAccessToken();
|
||||||
|
|
||||||
boolean isAccessTokenExpired();
|
boolean isAccessTokenExpired();
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 强制将access token过期掉
|
* 强制将access token过期掉
|
||||||
*/
|
*/
|
||||||
void expireAccessToken();
|
void expireAccessToken();
|
||||||
|
|
||||||
void updateAccessToken(WxAccessToken accessToken);
|
void updateAccessToken(WxAccessToken accessToken);
|
||||||
|
|
||||||
void updateAccessToken(String accessToken, int expiresIn);
|
void updateAccessToken(String accessToken, int expiresIn);
|
||||||
|
|
||||||
String getJsapiTicket();
|
String getJsapiTicket();
|
||||||
|
|
||||||
boolean isJsapiTicketExpired();
|
boolean isJsapiTicketExpired();
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 强制将jsapi ticket过期掉
|
* 强制将jsapi ticket过期掉
|
||||||
*/
|
*/
|
||||||
void expireJsapiTicket();
|
void expireJsapiTicket();
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 应该是线程安全的
|
* 应该是线程安全的
|
||||||
*
|
*
|
||||||
* @param jsapiTicket
|
* @param jsapiTicket
|
||||||
*/
|
*/
|
||||||
void updateJsapiTicket(String jsapiTicket, int expiresInSeconds);
|
void updateJsapiTicket(String jsapiTicket, int expiresInSeconds);
|
||||||
|
|
||||||
String getCorpId();
|
String getCorpId();
|
||||||
|
|
||||||
String getCorpSecret();
|
String getCorpSecret();
|
||||||
|
|
||||||
Integer getAgentId();
|
Integer getAgentId();
|
||||||
|
|
||||||
String getToken();
|
String getToken();
|
||||||
|
|
||||||
String getAesKey();
|
String getAesKey();
|
||||||
|
|
||||||
long getExpiresTime();
|
long getExpiresTime();
|
||||||
|
|
||||||
String getOauth2redirectUri();
|
String getOauth2redirectUri();
|
||||||
|
|
||||||
String getHttpProxyHost();
|
String getHttpProxyHost();
|
||||||
|
|
||||||
int getHttpProxyPort();
|
int getHttpProxyPort();
|
||||||
|
|
||||||
String getHttpProxyUsername();
|
String getHttpProxyUsername();
|
||||||
|
|
||||||
String getHttpProxyPassword();
|
String getHttpProxyPassword();
|
||||||
|
|
||||||
File getTmpDirFile();
|
File getTmpDirFile();
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* http client builder
|
* http client builder
|
||||||
*
|
*
|
||||||
* @return ApacheHttpClientBuilder
|
* @return ApacheHttpClientBuilder
|
||||||
*/
|
*/
|
||||||
ApacheHttpClientBuilder getApacheHttpClientBuilder();
|
ApacheHttpClientBuilder getApacheHttpClientBuilder();
|
||||||
}
|
}
|
@ -1,224 +1,224 @@
|
|||||||
package me.chanjar.weixin.cp.api;
|
package me.chanjar.weixin.cp.api;
|
||||||
|
|
||||||
import me.chanjar.weixin.common.bean.WxAccessToken;
|
import me.chanjar.weixin.common.bean.WxAccessToken;
|
||||||
import me.chanjar.weixin.common.util.ToStringUtils;
|
import me.chanjar.weixin.common.util.ToStringUtils;
|
||||||
import me.chanjar.weixin.common.util.http.apache.ApacheHttpClientBuilder;
|
import me.chanjar.weixin.common.util.http.apache.ApacheHttpClientBuilder;
|
||||||
|
|
||||||
import java.io.File;
|
import java.io.File;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 基于内存的微信配置provider,在实际生产环境中应该将这些配置持久化
|
* 基于内存的微信配置provider,在实际生产环境中应该将这些配置持久化
|
||||||
*
|
*
|
||||||
* @author Daniel Qian
|
* @author Daniel Qian
|
||||||
*/
|
*/
|
||||||
public class WxCpInMemoryConfigStorage implements WxCpConfigStorage {
|
public class WxCpInMemoryConfigStorage implements WxCpConfigStorage {
|
||||||
|
|
||||||
protected volatile String corpId;
|
protected volatile String corpId;
|
||||||
protected volatile String corpSecret;
|
protected volatile String corpSecret;
|
||||||
|
|
||||||
protected volatile String token;
|
protected volatile String token;
|
||||||
protected volatile String accessToken;
|
protected volatile String accessToken;
|
||||||
protected volatile String aesKey;
|
protected volatile String aesKey;
|
||||||
protected volatile Integer agentId;
|
protected volatile Integer agentId;
|
||||||
protected volatile long expiresTime;
|
protected volatile long expiresTime;
|
||||||
|
|
||||||
protected volatile String oauth2redirectUri;
|
protected volatile String oauth2redirectUri;
|
||||||
|
|
||||||
protected volatile String httpProxyHost;
|
protected volatile String httpProxyHost;
|
||||||
protected volatile int httpProxyPort;
|
protected volatile int httpProxyPort;
|
||||||
protected volatile String httpProxyUsername;
|
protected volatile String httpProxyUsername;
|
||||||
protected volatile String httpProxyPassword;
|
protected volatile String httpProxyPassword;
|
||||||
|
|
||||||
protected volatile String jsapiTicket;
|
protected volatile String jsapiTicket;
|
||||||
protected volatile long jsapiTicketExpiresTime;
|
protected volatile long jsapiTicketExpiresTime;
|
||||||
|
|
||||||
protected volatile File tmpDirFile;
|
protected volatile File tmpDirFile;
|
||||||
|
|
||||||
private volatile ApacheHttpClientBuilder apacheHttpClientBuilder;
|
private volatile ApacheHttpClientBuilder apacheHttpClientBuilder;
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public String getAccessToken() {
|
public String getAccessToken() {
|
||||||
return this.accessToken;
|
return this.accessToken;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setAccessToken(String accessToken) {
|
public void setAccessToken(String accessToken) {
|
||||||
this.accessToken = accessToken;
|
this.accessToken = accessToken;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public boolean isAccessTokenExpired() {
|
public boolean isAccessTokenExpired() {
|
||||||
return System.currentTimeMillis() > this.expiresTime;
|
return System.currentTimeMillis() > this.expiresTime;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void expireAccessToken() {
|
public void expireAccessToken() {
|
||||||
this.expiresTime = 0;
|
this.expiresTime = 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public synchronized void updateAccessToken(WxAccessToken accessToken) {
|
public synchronized void updateAccessToken(WxAccessToken accessToken) {
|
||||||
updateAccessToken(accessToken.getAccessToken(), accessToken.getExpiresIn());
|
updateAccessToken(accessToken.getAccessToken(), accessToken.getExpiresIn());
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public synchronized void updateAccessToken(String accessToken, int expiresInSeconds) {
|
public synchronized void updateAccessToken(String accessToken, int expiresInSeconds) {
|
||||||
this.accessToken = accessToken;
|
this.accessToken = accessToken;
|
||||||
this.expiresTime = System.currentTimeMillis() + (expiresInSeconds - 200) * 1000l;
|
this.expiresTime = System.currentTimeMillis() + (expiresInSeconds - 200) * 1000l;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public String getJsapiTicket() {
|
public String getJsapiTicket() {
|
||||||
return this.jsapiTicket;
|
return this.jsapiTicket;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setJsapiTicket(String jsapiTicket) {
|
public void setJsapiTicket(String jsapiTicket) {
|
||||||
this.jsapiTicket = jsapiTicket;
|
this.jsapiTicket = jsapiTicket;
|
||||||
}
|
}
|
||||||
|
|
||||||
public long getJsapiTicketExpiresTime() {
|
public long getJsapiTicketExpiresTime() {
|
||||||
return this.jsapiTicketExpiresTime;
|
return this.jsapiTicketExpiresTime;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setJsapiTicketExpiresTime(long jsapiTicketExpiresTime) {
|
public void setJsapiTicketExpiresTime(long jsapiTicketExpiresTime) {
|
||||||
this.jsapiTicketExpiresTime = jsapiTicketExpiresTime;
|
this.jsapiTicketExpiresTime = jsapiTicketExpiresTime;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public boolean isJsapiTicketExpired() {
|
public boolean isJsapiTicketExpired() {
|
||||||
return System.currentTimeMillis() > this.jsapiTicketExpiresTime;
|
return System.currentTimeMillis() > this.jsapiTicketExpiresTime;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public synchronized void updateJsapiTicket(String jsapiTicket, int expiresInSeconds) {
|
public synchronized void updateJsapiTicket(String jsapiTicket, int expiresInSeconds) {
|
||||||
this.jsapiTicket = jsapiTicket;
|
this.jsapiTicket = jsapiTicket;
|
||||||
// 预留200秒的时间
|
// 预留200秒的时间
|
||||||
this.jsapiTicketExpiresTime = System.currentTimeMillis() + (expiresInSeconds - 200) * 1000l;
|
this.jsapiTicketExpiresTime = System.currentTimeMillis() + (expiresInSeconds - 200) * 1000l;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void expireJsapiTicket() {
|
public void expireJsapiTicket() {
|
||||||
this.jsapiTicketExpiresTime = 0;
|
this.jsapiTicketExpiresTime = 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public String getCorpId() {
|
public String getCorpId() {
|
||||||
return this.corpId;
|
return this.corpId;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setCorpId(String corpId) {
|
public void setCorpId(String corpId) {
|
||||||
this.corpId = corpId;
|
this.corpId = corpId;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public String getCorpSecret() {
|
public String getCorpSecret() {
|
||||||
return this.corpSecret;
|
return this.corpSecret;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setCorpSecret(String corpSecret) {
|
public void setCorpSecret(String corpSecret) {
|
||||||
this.corpSecret = corpSecret;
|
this.corpSecret = corpSecret;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public String getToken() {
|
public String getToken() {
|
||||||
return this.token;
|
return this.token;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setToken(String token) {
|
public void setToken(String token) {
|
||||||
this.token = token;
|
this.token = token;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public long getExpiresTime() {
|
public long getExpiresTime() {
|
||||||
return this.expiresTime;
|
return this.expiresTime;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setExpiresTime(long expiresTime) {
|
public void setExpiresTime(long expiresTime) {
|
||||||
this.expiresTime = expiresTime;
|
this.expiresTime = expiresTime;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public String getAesKey() {
|
public String getAesKey() {
|
||||||
return this.aesKey;
|
return this.aesKey;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setAesKey(String aesKey) {
|
public void setAesKey(String aesKey) {
|
||||||
this.aesKey = aesKey;
|
this.aesKey = aesKey;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Integer getAgentId() {
|
public Integer getAgentId() {
|
||||||
return this.agentId;
|
return this.agentId;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setAgentId(Integer agentId) {
|
public void setAgentId(Integer agentId) {
|
||||||
this.agentId = agentId;
|
this.agentId = agentId;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public String getOauth2redirectUri() {
|
public String getOauth2redirectUri() {
|
||||||
return this.oauth2redirectUri;
|
return this.oauth2redirectUri;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setOauth2redirectUri(String oauth2redirectUri) {
|
public void setOauth2redirectUri(String oauth2redirectUri) {
|
||||||
this.oauth2redirectUri = oauth2redirectUri;
|
this.oauth2redirectUri = oauth2redirectUri;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public String getHttpProxyHost() {
|
public String getHttpProxyHost() {
|
||||||
return this.httpProxyHost;
|
return this.httpProxyHost;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setHttpProxyHost(String httpProxyHost) {
|
public void setHttpProxyHost(String httpProxyHost) {
|
||||||
this.httpProxyHost = httpProxyHost;
|
this.httpProxyHost = httpProxyHost;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public int getHttpProxyPort() {
|
public int getHttpProxyPort() {
|
||||||
return this.httpProxyPort;
|
return this.httpProxyPort;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setHttpProxyPort(int httpProxyPort) {
|
public void setHttpProxyPort(int httpProxyPort) {
|
||||||
this.httpProxyPort = httpProxyPort;
|
this.httpProxyPort = httpProxyPort;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public String getHttpProxyUsername() {
|
public String getHttpProxyUsername() {
|
||||||
return this.httpProxyUsername;
|
return this.httpProxyUsername;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setHttpProxyUsername(String httpProxyUsername) {
|
public void setHttpProxyUsername(String httpProxyUsername) {
|
||||||
this.httpProxyUsername = httpProxyUsername;
|
this.httpProxyUsername = httpProxyUsername;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public String getHttpProxyPassword() {
|
public String getHttpProxyPassword() {
|
||||||
return this.httpProxyPassword;
|
return this.httpProxyPassword;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setHttpProxyPassword(String httpProxyPassword) {
|
public void setHttpProxyPassword(String httpProxyPassword) {
|
||||||
this.httpProxyPassword = httpProxyPassword;
|
this.httpProxyPassword = httpProxyPassword;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public String toString() {
|
public String toString() {
|
||||||
return ToStringUtils.toSimpleString(this);
|
return ToStringUtils.toSimpleString(this);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public File getTmpDirFile() {
|
public File getTmpDirFile() {
|
||||||
return this.tmpDirFile;
|
return this.tmpDirFile;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setTmpDirFile(File tmpDirFile) {
|
public void setTmpDirFile(File tmpDirFile) {
|
||||||
this.tmpDirFile = tmpDirFile;
|
this.tmpDirFile = tmpDirFile;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public ApacheHttpClientBuilder getApacheHttpClientBuilder() {
|
public ApacheHttpClientBuilder getApacheHttpClientBuilder() {
|
||||||
return this.apacheHttpClientBuilder;
|
return this.apacheHttpClientBuilder;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setApacheHttpClientBuilder(ApacheHttpClientBuilder apacheHttpClientBuilder) {
|
public void setApacheHttpClientBuilder(ApacheHttpClientBuilder apacheHttpClientBuilder) {
|
||||||
this.apacheHttpClientBuilder = apacheHttpClientBuilder;
|
this.apacheHttpClientBuilder = apacheHttpClientBuilder;
|
||||||
}
|
}
|
||||||
}
|
}
|
@ -1,269 +1,269 @@
|
|||||||
package me.chanjar.weixin.cp.api;
|
package me.chanjar.weixin.cp.api;
|
||||||
|
|
||||||
import me.chanjar.weixin.common.bean.WxAccessToken;
|
import me.chanjar.weixin.common.bean.WxAccessToken;
|
||||||
import me.chanjar.weixin.common.util.http.apache.ApacheHttpClientBuilder;
|
import me.chanjar.weixin.common.util.http.apache.ApacheHttpClientBuilder;
|
||||||
import redis.clients.jedis.Jedis;
|
import redis.clients.jedis.Jedis;
|
||||||
import redis.clients.jedis.JedisPool;
|
import redis.clients.jedis.JedisPool;
|
||||||
import redis.clients.jedis.JedisPoolConfig;
|
import redis.clients.jedis.JedisPoolConfig;
|
||||||
|
|
||||||
import java.io.File;
|
import java.io.File;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Jedis client implementor for wechat config storage
|
* Jedis client implementor for wechat config storage
|
||||||
*
|
*
|
||||||
* @author gaigeshen
|
* @author gaigeshen
|
||||||
*/
|
*/
|
||||||
public class WxCpJedisConfigStorage implements WxCpConfigStorage {
|
public class WxCpJedisConfigStorage implements WxCpConfigStorage {
|
||||||
|
|
||||||
/* Redis keys here */
|
/* Redis keys here */
|
||||||
private static final String ACCESS_TOKEN_KEY = "WX_CP_ACCESS_TOKEN";
|
private static final String ACCESS_TOKEN_KEY = "WX_CP_ACCESS_TOKEN";
|
||||||
private static final String ACCESS_TOKEN_EXPIRES_TIME_KEY = "WX_CP_ACCESS_TOKEN_EXPIRES_TIME";
|
private static final String ACCESS_TOKEN_EXPIRES_TIME_KEY = "WX_CP_ACCESS_TOKEN_EXPIRES_TIME";
|
||||||
private static final String JS_API_TICKET_KEY = "WX_CP_JS_API_TICKET";
|
private static final String JS_API_TICKET_KEY = "WX_CP_JS_API_TICKET";
|
||||||
private static final String JS_API_TICKET_EXPIRES_TIME_KEY = "WX_CP_JS_API_TICKET_EXPIRES_TIME";
|
private static final String JS_API_TICKET_EXPIRES_TIME_KEY = "WX_CP_JS_API_TICKET_EXPIRES_TIME";
|
||||||
/* Redis clients pool */
|
/* Redis clients pool */
|
||||||
private final JedisPool jedisPool;
|
private final JedisPool jedisPool;
|
||||||
private volatile String corpId;
|
private volatile String corpId;
|
||||||
private volatile String corpSecret;
|
private volatile String corpSecret;
|
||||||
private volatile String token;
|
private volatile String token;
|
||||||
private volatile String aesKey;
|
private volatile String aesKey;
|
||||||
private volatile Integer agentId;
|
private volatile Integer agentId;
|
||||||
private volatile String oauth2redirectUri;
|
private volatile String oauth2redirectUri;
|
||||||
private volatile String httpProxyHost;
|
private volatile String httpProxyHost;
|
||||||
private volatile int httpProxyPort;
|
private volatile int httpProxyPort;
|
||||||
private volatile String httpProxyUsername;
|
private volatile String httpProxyUsername;
|
||||||
private volatile String httpProxyPassword;
|
private volatile String httpProxyPassword;
|
||||||
private volatile File tmpDirFile;
|
private volatile File tmpDirFile;
|
||||||
private volatile ApacheHttpClientBuilder apacheHttpClientBuilder;
|
private volatile ApacheHttpClientBuilder apacheHttpClientBuilder;
|
||||||
|
|
||||||
public WxCpJedisConfigStorage(String host, int port) {
|
public WxCpJedisConfigStorage(String host, int port) {
|
||||||
this.jedisPool = new JedisPool(host, port);
|
this.jedisPool = new JedisPool(host, port);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
public WxCpJedisConfigStorage(JedisPoolConfig poolConfig, String host, int port) {
|
public WxCpJedisConfigStorage(JedisPoolConfig poolConfig, String host, int port) {
|
||||||
this.jedisPool = new JedisPool(poolConfig, host, port);
|
this.jedisPool = new JedisPool(poolConfig, host, port);
|
||||||
}
|
}
|
||||||
|
|
||||||
public WxCpJedisConfigStorage(JedisPoolConfig poolConfig, String host, int port, int timeout, final String password) {
|
public WxCpJedisConfigStorage(JedisPoolConfig poolConfig, String host, int port, int timeout, final String password) {
|
||||||
this.jedisPool = new JedisPool(poolConfig, host, port, timeout, password);
|
this.jedisPool = new JedisPool(poolConfig, host, port, timeout, password);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* This method will be destroy jedis pool
|
* This method will be destroy jedis pool
|
||||||
*/
|
*/
|
||||||
public void destroy() {
|
public void destroy() {
|
||||||
this.jedisPool.destroy();
|
this.jedisPool.destroy();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public String getAccessToken() {
|
public String getAccessToken() {
|
||||||
try (Jedis jedis = this.jedisPool.getResource()) {
|
try (Jedis jedis = this.jedisPool.getResource()) {
|
||||||
return jedis.get(ACCESS_TOKEN_KEY);
|
return jedis.get(ACCESS_TOKEN_KEY);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public boolean isAccessTokenExpired() {
|
public boolean isAccessTokenExpired() {
|
||||||
try (Jedis jedis = this.jedisPool.getResource()) {
|
try (Jedis jedis = this.jedisPool.getResource()) {
|
||||||
String expiresTimeStr = jedis.get(ACCESS_TOKEN_EXPIRES_TIME_KEY);
|
String expiresTimeStr = jedis.get(ACCESS_TOKEN_EXPIRES_TIME_KEY);
|
||||||
|
|
||||||
if (expiresTimeStr != null) {
|
if (expiresTimeStr != null) {
|
||||||
Long expiresTime = Long.parseLong(expiresTimeStr);
|
Long expiresTime = Long.parseLong(expiresTimeStr);
|
||||||
return System.currentTimeMillis() > expiresTime;
|
return System.currentTimeMillis() > expiresTime;
|
||||||
}
|
}
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void expireAccessToken() {
|
public void expireAccessToken() {
|
||||||
try (Jedis jedis = this.jedisPool.getResource()) {
|
try (Jedis jedis = this.jedisPool.getResource()) {
|
||||||
jedis.set(ACCESS_TOKEN_EXPIRES_TIME_KEY, "0");
|
jedis.set(ACCESS_TOKEN_EXPIRES_TIME_KEY, "0");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public synchronized void updateAccessToken(WxAccessToken accessToken) {
|
public synchronized void updateAccessToken(WxAccessToken accessToken) {
|
||||||
this.updateAccessToken(accessToken.getAccessToken(), accessToken.getExpiresIn());
|
this.updateAccessToken(accessToken.getAccessToken(), accessToken.getExpiresIn());
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public synchronized void updateAccessToken(String accessToken, int expiresInSeconds) {
|
public synchronized void updateAccessToken(String accessToken, int expiresInSeconds) {
|
||||||
try (Jedis jedis = this.jedisPool.getResource()) {
|
try (Jedis jedis = this.jedisPool.getResource()) {
|
||||||
jedis.set(ACCESS_TOKEN_KEY, accessToken);
|
jedis.set(ACCESS_TOKEN_KEY, accessToken);
|
||||||
|
|
||||||
jedis.set(ACCESS_TOKEN_EXPIRES_TIME_KEY,
|
jedis.set(ACCESS_TOKEN_EXPIRES_TIME_KEY,
|
||||||
(System.currentTimeMillis() + (expiresInSeconds - 200) * 1000L) + "");
|
(System.currentTimeMillis() + (expiresInSeconds - 200) * 1000L) + "");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public String getJsapiTicket() {
|
public String getJsapiTicket() {
|
||||||
try (Jedis jedis = this.jedisPool.getResource()) {
|
try (Jedis jedis = this.jedisPool.getResource()) {
|
||||||
return jedis.get(JS_API_TICKET_KEY);
|
return jedis.get(JS_API_TICKET_KEY);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public boolean isJsapiTicketExpired() {
|
public boolean isJsapiTicketExpired() {
|
||||||
|
|
||||||
try (Jedis jedis = this.jedisPool.getResource()) {
|
try (Jedis jedis = this.jedisPool.getResource()) {
|
||||||
String expiresTimeStr = jedis.get(JS_API_TICKET_EXPIRES_TIME_KEY);
|
String expiresTimeStr = jedis.get(JS_API_TICKET_EXPIRES_TIME_KEY);
|
||||||
|
|
||||||
if (expiresTimeStr != null) {
|
if (expiresTimeStr != null) {
|
||||||
Long expiresTime = Long.parseLong(expiresTimeStr);
|
Long expiresTime = Long.parseLong(expiresTimeStr);
|
||||||
return System.currentTimeMillis() > expiresTime;
|
return System.currentTimeMillis() > expiresTime;
|
||||||
}
|
}
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void expireJsapiTicket() {
|
public void expireJsapiTicket() {
|
||||||
try (Jedis jedis = this.jedisPool.getResource()) {
|
try (Jedis jedis = this.jedisPool.getResource()) {
|
||||||
jedis.set(JS_API_TICKET_EXPIRES_TIME_KEY, "0");
|
jedis.set(JS_API_TICKET_EXPIRES_TIME_KEY, "0");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public synchronized void updateJsapiTicket(String jsapiTicket, int expiresInSeconds) {
|
public synchronized void updateJsapiTicket(String jsapiTicket, int expiresInSeconds) {
|
||||||
|
|
||||||
try (Jedis jedis = this.jedisPool.getResource()) {
|
try (Jedis jedis = this.jedisPool.getResource()) {
|
||||||
jedis.set(JS_API_TICKET_KEY, jsapiTicket);
|
jedis.set(JS_API_TICKET_KEY, jsapiTicket);
|
||||||
|
|
||||||
jedis.set(JS_API_TICKET_EXPIRES_TIME_KEY,
|
jedis.set(JS_API_TICKET_EXPIRES_TIME_KEY,
|
||||||
(System.currentTimeMillis() + (expiresInSeconds - 200) * 1000L + ""));
|
(System.currentTimeMillis() + (expiresInSeconds - 200) * 1000L + ""));
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public String getCorpId() {
|
public String getCorpId() {
|
||||||
return this.corpId;
|
return this.corpId;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setCorpId(String corpId) {
|
public void setCorpId(String corpId) {
|
||||||
this.corpId = corpId;
|
this.corpId = corpId;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public String getCorpSecret() {
|
public String getCorpSecret() {
|
||||||
return this.corpSecret;
|
return this.corpSecret;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setCorpSecret(String corpSecret) {
|
public void setCorpSecret(String corpSecret) {
|
||||||
this.corpSecret = corpSecret;
|
this.corpSecret = corpSecret;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Integer getAgentId() {
|
public Integer getAgentId() {
|
||||||
return this.agentId;
|
return this.agentId;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setAgentId(Integer agentId) {
|
public void setAgentId(Integer agentId) {
|
||||||
this.agentId = agentId;
|
this.agentId = agentId;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public String getToken() {
|
public String getToken() {
|
||||||
return this.token;
|
return this.token;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setToken(String token) {
|
public void setToken(String token) {
|
||||||
this.token = token;
|
this.token = token;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public String getAesKey() {
|
public String getAesKey() {
|
||||||
return this.aesKey;
|
return this.aesKey;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setAesKey(String aesKey) {
|
public void setAesKey(String aesKey) {
|
||||||
this.aesKey = aesKey;
|
this.aesKey = aesKey;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public long getExpiresTime() {
|
public long getExpiresTime() {
|
||||||
try (Jedis jedis = this.jedisPool.getResource()) {
|
try (Jedis jedis = this.jedisPool.getResource()) {
|
||||||
String expiresTimeStr = jedis.get(ACCESS_TOKEN_EXPIRES_TIME_KEY);
|
String expiresTimeStr = jedis.get(ACCESS_TOKEN_EXPIRES_TIME_KEY);
|
||||||
|
|
||||||
if (expiresTimeStr != null) {
|
if (expiresTimeStr != null) {
|
||||||
Long expiresTime = Long.parseLong(expiresTimeStr);
|
Long expiresTime = Long.parseLong(expiresTimeStr);
|
||||||
return expiresTime;
|
return expiresTime;
|
||||||
}
|
}
|
||||||
|
|
||||||
return 0L;
|
return 0L;
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public String getOauth2redirectUri() {
|
public String getOauth2redirectUri() {
|
||||||
return this.oauth2redirectUri;
|
return this.oauth2redirectUri;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setOauth2redirectUri(String oauth2redirectUri) {
|
public void setOauth2redirectUri(String oauth2redirectUri) {
|
||||||
this.oauth2redirectUri = oauth2redirectUri;
|
this.oauth2redirectUri = oauth2redirectUri;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public String getHttpProxyHost() {
|
public String getHttpProxyHost() {
|
||||||
return this.httpProxyHost;
|
return this.httpProxyHost;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setHttpProxyHost(String httpProxyHost) {
|
public void setHttpProxyHost(String httpProxyHost) {
|
||||||
this.httpProxyHost = httpProxyHost;
|
this.httpProxyHost = httpProxyHost;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public int getHttpProxyPort() {
|
public int getHttpProxyPort() {
|
||||||
return this.httpProxyPort;
|
return this.httpProxyPort;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setHttpProxyPort(int httpProxyPort) {
|
public void setHttpProxyPort(int httpProxyPort) {
|
||||||
this.httpProxyPort = httpProxyPort;
|
this.httpProxyPort = httpProxyPort;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public String getHttpProxyUsername() {
|
public String getHttpProxyUsername() {
|
||||||
return this.httpProxyUsername;
|
return this.httpProxyUsername;
|
||||||
}
|
}
|
||||||
|
|
||||||
// ============================ Setters below
|
// ============================ Setters below
|
||||||
|
|
||||||
public void setHttpProxyUsername(String httpProxyUsername) {
|
public void setHttpProxyUsername(String httpProxyUsername) {
|
||||||
this.httpProxyUsername = httpProxyUsername;
|
this.httpProxyUsername = httpProxyUsername;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public String getHttpProxyPassword() {
|
public String getHttpProxyPassword() {
|
||||||
return this.httpProxyPassword;
|
return this.httpProxyPassword;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setHttpProxyPassword(String httpProxyPassword) {
|
public void setHttpProxyPassword(String httpProxyPassword) {
|
||||||
this.httpProxyPassword = httpProxyPassword;
|
this.httpProxyPassword = httpProxyPassword;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public File getTmpDirFile() {
|
public File getTmpDirFile() {
|
||||||
return this.tmpDirFile;
|
return this.tmpDirFile;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setTmpDirFile(File tmpDirFile) {
|
public void setTmpDirFile(File tmpDirFile) {
|
||||||
this.tmpDirFile = tmpDirFile;
|
this.tmpDirFile = tmpDirFile;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public ApacheHttpClientBuilder getApacheHttpClientBuilder() {
|
public ApacheHttpClientBuilder getApacheHttpClientBuilder() {
|
||||||
return this.apacheHttpClientBuilder;
|
return this.apacheHttpClientBuilder;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setApacheHttpClientBuilder(ApacheHttpClientBuilder apacheHttpClientBuilder) {
|
public void setApacheHttpClientBuilder(ApacheHttpClientBuilder apacheHttpClientBuilder) {
|
||||||
this.apacheHttpClientBuilder = apacheHttpClientBuilder;
|
this.apacheHttpClientBuilder = apacheHttpClientBuilder;
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
@ -1,29 +1,29 @@
|
|||||||
package me.chanjar.weixin.cp.api;
|
package me.chanjar.weixin.cp.api;
|
||||||
|
|
||||||
import me.chanjar.weixin.common.exception.WxErrorException;
|
import me.chanjar.weixin.common.exception.WxErrorException;
|
||||||
import me.chanjar.weixin.common.session.WxSessionManager;
|
import me.chanjar.weixin.common.session.WxSessionManager;
|
||||||
import me.chanjar.weixin.cp.bean.WxCpXmlMessage;
|
import me.chanjar.weixin.cp.bean.WxCpXmlMessage;
|
||||||
import me.chanjar.weixin.cp.bean.WxCpXmlOutMessage;
|
import me.chanjar.weixin.cp.bean.WxCpXmlOutMessage;
|
||||||
|
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 处理微信推送消息的处理器接口
|
* 处理微信推送消息的处理器接口
|
||||||
*
|
*
|
||||||
* @author Daniel Qian
|
* @author Daniel Qian
|
||||||
*/
|
*/
|
||||||
public interface WxCpMessageHandler {
|
public interface WxCpMessageHandler {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param wxMessage
|
* @param wxMessage
|
||||||
* @param context 上下文,如果handler或interceptor之间有信息要传递,可以用这个
|
* @param context 上下文,如果handler或interceptor之间有信息要传递,可以用这个
|
||||||
* @param wxCpService
|
* @param wxCpService
|
||||||
* @param sessionManager
|
* @param sessionManager
|
||||||
* @return xml格式的消息,如果在异步规则里处理的话,可以返回null
|
* @return xml格式的消息,如果在异步规则里处理的话,可以返回null
|
||||||
*/
|
*/
|
||||||
WxCpXmlOutMessage handle(WxCpXmlMessage wxMessage,
|
WxCpXmlOutMessage handle(WxCpXmlMessage wxMessage,
|
||||||
Map<String, Object> context,
|
Map<String, Object> context,
|
||||||
WxCpService wxCpService,
|
WxCpService wxCpService,
|
||||||
WxSessionManager sessionManager) throws WxErrorException;
|
WxSessionManager sessionManager) throws WxErrorException;
|
||||||
|
|
||||||
}
|
}
|
@ -1,30 +1,30 @@
|
|||||||
package me.chanjar.weixin.cp.api;
|
package me.chanjar.weixin.cp.api;
|
||||||
|
|
||||||
import me.chanjar.weixin.common.exception.WxErrorException;
|
import me.chanjar.weixin.common.exception.WxErrorException;
|
||||||
import me.chanjar.weixin.common.session.WxSessionManager;
|
import me.chanjar.weixin.common.session.WxSessionManager;
|
||||||
import me.chanjar.weixin.cp.bean.WxCpXmlMessage;
|
import me.chanjar.weixin.cp.bean.WxCpXmlMessage;
|
||||||
|
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 微信消息拦截器,可以用来做验证
|
* 微信消息拦截器,可以用来做验证
|
||||||
*
|
*
|
||||||
* @author Daniel Qian
|
* @author Daniel Qian
|
||||||
*/
|
*/
|
||||||
public interface WxCpMessageInterceptor {
|
public interface WxCpMessageInterceptor {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 拦截微信消息
|
* 拦截微信消息
|
||||||
*
|
*
|
||||||
* @param wxMessage
|
* @param wxMessage
|
||||||
* @param context 上下文,如果handler或interceptor之间有信息要传递,可以用这个
|
* @param context 上下文,如果handler或interceptor之间有信息要传递,可以用这个
|
||||||
* @param wxCpService
|
* @param wxCpService
|
||||||
* @param sessionManager
|
* @param sessionManager
|
||||||
* @return true代表OK,false代表不OK
|
* @return true代表OK,false代表不OK
|
||||||
*/
|
*/
|
||||||
boolean intercept(WxCpXmlMessage wxMessage,
|
boolean intercept(WxCpXmlMessage wxMessage,
|
||||||
Map<String, Object> context,
|
Map<String, Object> context,
|
||||||
WxCpService wxCpService,
|
WxCpService wxCpService,
|
||||||
WxSessionManager sessionManager) throws WxErrorException;
|
WxSessionManager sessionManager) throws WxErrorException;
|
||||||
|
|
||||||
}
|
}
|
@ -1,15 +1,15 @@
|
|||||||
package me.chanjar.weixin.cp.api;
|
package me.chanjar.weixin.cp.api;
|
||||||
|
|
||||||
import me.chanjar.weixin.cp.bean.WxCpXmlMessage;
|
import me.chanjar.weixin.cp.bean.WxCpXmlMessage;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 消息匹配器,用在消息路由的时候
|
* 消息匹配器,用在消息路由的时候
|
||||||
*/
|
*/
|
||||||
public interface WxCpMessageMatcher {
|
public interface WxCpMessageMatcher {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 消息是否匹配某种模式
|
* 消息是否匹配某种模式
|
||||||
*/
|
*/
|
||||||
boolean match(WxCpXmlMessage message);
|
boolean match(WxCpXmlMessage message);
|
||||||
|
|
||||||
}
|
}
|
@ -1,249 +1,249 @@
|
|||||||
package me.chanjar.weixin.cp.api;
|
package me.chanjar.weixin.cp.api;
|
||||||
|
|
||||||
import me.chanjar.weixin.common.api.WxErrorExceptionHandler;
|
import me.chanjar.weixin.common.api.WxErrorExceptionHandler;
|
||||||
import me.chanjar.weixin.common.api.WxMessageDuplicateChecker;
|
import me.chanjar.weixin.common.api.WxMessageDuplicateChecker;
|
||||||
import me.chanjar.weixin.common.api.WxMessageInMemoryDuplicateChecker;
|
import me.chanjar.weixin.common.api.WxMessageInMemoryDuplicateChecker;
|
||||||
import me.chanjar.weixin.common.session.InternalSession;
|
import me.chanjar.weixin.common.session.InternalSession;
|
||||||
import me.chanjar.weixin.common.session.InternalSessionManager;
|
import me.chanjar.weixin.common.session.InternalSessionManager;
|
||||||
import me.chanjar.weixin.common.session.StandardSessionManager;
|
import me.chanjar.weixin.common.session.StandardSessionManager;
|
||||||
import me.chanjar.weixin.common.session.WxSessionManager;
|
import me.chanjar.weixin.common.session.WxSessionManager;
|
||||||
import me.chanjar.weixin.common.util.LogExceptionHandler;
|
import me.chanjar.weixin.common.util.LogExceptionHandler;
|
||||||
import me.chanjar.weixin.cp.bean.WxCpXmlMessage;
|
import me.chanjar.weixin.cp.bean.WxCpXmlMessage;
|
||||||
import me.chanjar.weixin.cp.bean.WxCpXmlOutMessage;
|
import me.chanjar.weixin.cp.bean.WxCpXmlOutMessage;
|
||||||
import org.slf4j.Logger;
|
import org.slf4j.Logger;
|
||||||
import org.slf4j.LoggerFactory;
|
import org.slf4j.LoggerFactory;
|
||||||
|
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.HashMap;
|
import java.util.HashMap;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
import java.util.concurrent.ExecutionException;
|
import java.util.concurrent.ExecutionException;
|
||||||
import java.util.concurrent.ExecutorService;
|
import java.util.concurrent.ExecutorService;
|
||||||
import java.util.concurrent.Executors;
|
import java.util.concurrent.Executors;
|
||||||
import java.util.concurrent.Future;
|
import java.util.concurrent.Future;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* <pre>
|
* <pre>
|
||||||
* 微信消息路由器,通过代码化的配置,把来自微信的消息交给handler处理
|
* 微信消息路由器,通过代码化的配置,把来自微信的消息交给handler处理
|
||||||
*
|
*
|
||||||
* 说明:
|
* 说明:
|
||||||
* 1. 配置路由规则时要按照从细到粗的原则,否则可能消息可能会被提前处理
|
* 1. 配置路由规则时要按照从细到粗的原则,否则可能消息可能会被提前处理
|
||||||
* 2. 默认情况下消息只会被处理一次,除非使用 {@link WxCpMessageRouterRule#next()}
|
* 2. 默认情况下消息只会被处理一次,除非使用 {@link WxCpMessageRouterRule#next()}
|
||||||
* 3. 规则的结束必须用{@link WxCpMessageRouterRule#end()}或者{@link WxCpMessageRouterRule#next()},否则不会生效
|
* 3. 规则的结束必须用{@link WxCpMessageRouterRule#end()}或者{@link WxCpMessageRouterRule#next()},否则不会生效
|
||||||
*
|
*
|
||||||
* 使用方法:
|
* 使用方法:
|
||||||
* WxCpMessageRouter router = new WxCpMessageRouter();
|
* WxCpMessageRouter router = new WxCpMessageRouter();
|
||||||
* router
|
* router
|
||||||
* .rule()
|
* .rule()
|
||||||
* .msgType("MSG_TYPE").event("EVENT").eventKey("EVENT_KEY").content("CONTENT")
|
* .msgType("MSG_TYPE").event("EVENT").eventKey("EVENT_KEY").content("CONTENT")
|
||||||
* .interceptor(interceptor, ...).handler(handler, ...)
|
* .interceptor(interceptor, ...).handler(handler, ...)
|
||||||
* .end()
|
* .end()
|
||||||
* .rule()
|
* .rule()
|
||||||
* // 另外一个匹配规则
|
* // 另外一个匹配规则
|
||||||
* .end()
|
* .end()
|
||||||
* ;
|
* ;
|
||||||
*
|
*
|
||||||
* // 将WxXmlMessage交给消息路由器
|
* // 将WxXmlMessage交给消息路由器
|
||||||
* router.route(message);
|
* router.route(message);
|
||||||
*
|
*
|
||||||
* </pre>
|
* </pre>
|
||||||
*
|
*
|
||||||
* @author Daniel Qian
|
* @author Daniel Qian
|
||||||
*/
|
*/
|
||||||
public class WxCpMessageRouter {
|
public class WxCpMessageRouter {
|
||||||
|
|
||||||
private static final int DEFAULT_THREAD_POOL_SIZE = 100;
|
private static final int DEFAULT_THREAD_POOL_SIZE = 100;
|
||||||
protected final Logger log = LoggerFactory.getLogger(WxCpMessageRouter.class);
|
protected final Logger log = LoggerFactory.getLogger(WxCpMessageRouter.class);
|
||||||
private final List<WxCpMessageRouterRule> rules = new ArrayList<>();
|
private final List<WxCpMessageRouterRule> rules = new ArrayList<>();
|
||||||
|
|
||||||
private final WxCpService wxCpService;
|
private final WxCpService wxCpService;
|
||||||
|
|
||||||
private ExecutorService executorService;
|
private ExecutorService executorService;
|
||||||
|
|
||||||
private WxMessageDuplicateChecker messageDuplicateChecker;
|
private WxMessageDuplicateChecker messageDuplicateChecker;
|
||||||
|
|
||||||
private WxSessionManager sessionManager;
|
private WxSessionManager sessionManager;
|
||||||
|
|
||||||
private WxErrorExceptionHandler exceptionHandler;
|
private WxErrorExceptionHandler exceptionHandler;
|
||||||
|
|
||||||
public WxCpMessageRouter(WxCpService wxCpService) {
|
public WxCpMessageRouter(WxCpService wxCpService) {
|
||||||
this.wxCpService = wxCpService;
|
this.wxCpService = wxCpService;
|
||||||
this.executorService = Executors.newFixedThreadPool(DEFAULT_THREAD_POOL_SIZE);
|
this.executorService = Executors.newFixedThreadPool(DEFAULT_THREAD_POOL_SIZE);
|
||||||
this.messageDuplicateChecker = new WxMessageInMemoryDuplicateChecker();
|
this.messageDuplicateChecker = new WxMessageInMemoryDuplicateChecker();
|
||||||
this.sessionManager = new StandardSessionManager();
|
this.sessionManager = new StandardSessionManager();
|
||||||
this.exceptionHandler = new LogExceptionHandler();
|
this.exceptionHandler = new LogExceptionHandler();
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* <pre>
|
* <pre>
|
||||||
* 设置自定义的 {@link ExecutorService}
|
* 设置自定义的 {@link ExecutorService}
|
||||||
* 如果不调用该方法,默认使用 Executors.newFixedThreadPool(100)
|
* 如果不调用该方法,默认使用 Executors.newFixedThreadPool(100)
|
||||||
* </pre>
|
* </pre>
|
||||||
*
|
*
|
||||||
* @param executorService
|
* @param executorService
|
||||||
*/
|
*/
|
||||||
public void setExecutorService(ExecutorService executorService) {
|
public void setExecutorService(ExecutorService executorService) {
|
||||||
this.executorService = executorService;
|
this.executorService = executorService;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* <pre>
|
* <pre>
|
||||||
* 设置自定义的 {@link me.chanjar.weixin.common.api.WxMessageDuplicateChecker}
|
* 设置自定义的 {@link me.chanjar.weixin.common.api.WxMessageDuplicateChecker}
|
||||||
* 如果不调用该方法,默认使用 {@link me.chanjar.weixin.common.api.WxMessageInMemoryDuplicateChecker}
|
* 如果不调用该方法,默认使用 {@link me.chanjar.weixin.common.api.WxMessageInMemoryDuplicateChecker}
|
||||||
* </pre>
|
* </pre>
|
||||||
*
|
*
|
||||||
* @param messageDuplicateChecker
|
* @param messageDuplicateChecker
|
||||||
*/
|
*/
|
||||||
public void setMessageDuplicateChecker(WxMessageDuplicateChecker messageDuplicateChecker) {
|
public void setMessageDuplicateChecker(WxMessageDuplicateChecker messageDuplicateChecker) {
|
||||||
this.messageDuplicateChecker = messageDuplicateChecker;
|
this.messageDuplicateChecker = messageDuplicateChecker;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* <pre>
|
* <pre>
|
||||||
* 设置自定义的{@link me.chanjar.weixin.common.session.WxSessionManager}
|
* 设置自定义的{@link me.chanjar.weixin.common.session.WxSessionManager}
|
||||||
* 如果不调用该方法,默认使用 {@link me.chanjar.weixin.common.session.StandardSessionManager}
|
* 如果不调用该方法,默认使用 {@link me.chanjar.weixin.common.session.StandardSessionManager}
|
||||||
* </pre>
|
* </pre>
|
||||||
*
|
*
|
||||||
* @param sessionManager
|
* @param sessionManager
|
||||||
*/
|
*/
|
||||||
public void setSessionManager(WxSessionManager sessionManager) {
|
public void setSessionManager(WxSessionManager sessionManager) {
|
||||||
this.sessionManager = sessionManager;
|
this.sessionManager = sessionManager;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* <pre>
|
* <pre>
|
||||||
* 设置自定义的{@link me.chanjar.weixin.common.api.WxErrorExceptionHandler}
|
* 设置自定义的{@link me.chanjar.weixin.common.api.WxErrorExceptionHandler}
|
||||||
* 如果不调用该方法,默认使用 {@link me.chanjar.weixin.common.util.LogExceptionHandler}
|
* 如果不调用该方法,默认使用 {@link me.chanjar.weixin.common.util.LogExceptionHandler}
|
||||||
* </pre>
|
* </pre>
|
||||||
*
|
*
|
||||||
* @param exceptionHandler
|
* @param exceptionHandler
|
||||||
*/
|
*/
|
||||||
public void setExceptionHandler(WxErrorExceptionHandler exceptionHandler) {
|
public void setExceptionHandler(WxErrorExceptionHandler exceptionHandler) {
|
||||||
this.exceptionHandler = exceptionHandler;
|
this.exceptionHandler = exceptionHandler;
|
||||||
}
|
}
|
||||||
|
|
||||||
List<WxCpMessageRouterRule> getRules() {
|
List<WxCpMessageRouterRule> getRules() {
|
||||||
return this.rules;
|
return this.rules;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 开始一个新的Route规则
|
* 开始一个新的Route规则
|
||||||
*/
|
*/
|
||||||
public WxCpMessageRouterRule rule() {
|
public WxCpMessageRouterRule rule() {
|
||||||
return new WxCpMessageRouterRule(this);
|
return new WxCpMessageRouterRule(this);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 处理微信消息
|
* 处理微信消息
|
||||||
*
|
*
|
||||||
* @param wxMessage
|
* @param wxMessage
|
||||||
* @param context
|
* @param context
|
||||||
*/
|
*/
|
||||||
public WxCpXmlOutMessage route(final WxCpXmlMessage wxMessage, final Map<String, Object> context) {
|
public WxCpXmlOutMessage route(final WxCpXmlMessage wxMessage, final Map<String, Object> context) {
|
||||||
if (isDuplicateMessage(wxMessage)) {
|
if (isDuplicateMessage(wxMessage)) {
|
||||||
// 如果是重复消息,那么就不做处理
|
// 如果是重复消息,那么就不做处理
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
final List<WxCpMessageRouterRule> matchRules = new ArrayList<>();
|
final List<WxCpMessageRouterRule> matchRules = new ArrayList<>();
|
||||||
// 收集匹配的规则
|
// 收集匹配的规则
|
||||||
for (final WxCpMessageRouterRule rule : this.rules) {
|
for (final WxCpMessageRouterRule rule : this.rules) {
|
||||||
if (rule.test(wxMessage)) {
|
if (rule.test(wxMessage)) {
|
||||||
matchRules.add(rule);
|
matchRules.add(rule);
|
||||||
if (!rule.isReEnter()) {
|
if (!rule.isReEnter()) {
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (matchRules.size() == 0) {
|
if (matchRules.size() == 0) {
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
WxCpXmlOutMessage res = null;
|
WxCpXmlOutMessage res = null;
|
||||||
final List<Future> futures = new ArrayList<>();
|
final List<Future> futures = new ArrayList<>();
|
||||||
for (final WxCpMessageRouterRule rule : matchRules) {
|
for (final WxCpMessageRouterRule rule : matchRules) {
|
||||||
// 返回最后一个非异步的rule的执行结果
|
// 返回最后一个非异步的rule的执行结果
|
||||||
if (rule.isAsync()) {
|
if (rule.isAsync()) {
|
||||||
futures.add(
|
futures.add(
|
||||||
this.executorService.submit(new Runnable() {
|
this.executorService.submit(new Runnable() {
|
||||||
@Override
|
@Override
|
||||||
public void run() {
|
public void run() {
|
||||||
rule.service(wxMessage, context, WxCpMessageRouter.this.wxCpService, WxCpMessageRouter.this.sessionManager, WxCpMessageRouter.this.exceptionHandler);
|
rule.service(wxMessage, context, WxCpMessageRouter.this.wxCpService, WxCpMessageRouter.this.sessionManager, WxCpMessageRouter.this.exceptionHandler);
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
);
|
);
|
||||||
} else {
|
} else {
|
||||||
res = rule.service(wxMessage, context, this.wxCpService, this.sessionManager, this.exceptionHandler);
|
res = rule.service(wxMessage, context, this.wxCpService, this.sessionManager, this.exceptionHandler);
|
||||||
// 在同步操作结束,session访问结束
|
// 在同步操作结束,session访问结束
|
||||||
this.log.debug("End session access: async=false, sessionId={}", wxMessage.getFromUserName());
|
this.log.debug("End session access: async=false, sessionId={}", wxMessage.getFromUserName());
|
||||||
sessionEndAccess(wxMessage);
|
sessionEndAccess(wxMessage);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (futures.size() > 0) {
|
if (futures.size() > 0) {
|
||||||
this.executorService.submit(new Runnable() {
|
this.executorService.submit(new Runnable() {
|
||||||
@Override
|
@Override
|
||||||
public void run() {
|
public void run() {
|
||||||
for (Future future : futures) {
|
for (Future future : futures) {
|
||||||
try {
|
try {
|
||||||
future.get();
|
future.get();
|
||||||
WxCpMessageRouter.this.log.debug("End session access: async=true, sessionId={}", wxMessage.getFromUserName());
|
WxCpMessageRouter.this.log.debug("End session access: async=true, sessionId={}", wxMessage.getFromUserName());
|
||||||
// 异步操作结束,session访问结束
|
// 异步操作结束,session访问结束
|
||||||
sessionEndAccess(wxMessage);
|
sessionEndAccess(wxMessage);
|
||||||
} catch (InterruptedException e) {
|
} catch (InterruptedException e) {
|
||||||
WxCpMessageRouter.this.log.error("Error happened when wait task finish", e);
|
WxCpMessageRouter.this.log.error("Error happened when wait task finish", e);
|
||||||
} catch (ExecutionException e) {
|
} catch (ExecutionException e) {
|
||||||
WxCpMessageRouter.this.log.error("Error happened when wait task finish", e);
|
WxCpMessageRouter.this.log.error("Error happened when wait task finish", e);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
return res;
|
return res;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 处理微信消息
|
* 处理微信消息
|
||||||
*
|
*
|
||||||
* @param wxMessage
|
* @param wxMessage
|
||||||
*/
|
*/
|
||||||
public WxCpXmlOutMessage route(final WxCpXmlMessage wxMessage) {
|
public WxCpXmlOutMessage route(final WxCpXmlMessage wxMessage) {
|
||||||
return this.route(wxMessage, new HashMap<String, Object>());
|
return this.route(wxMessage, new HashMap<String, Object>());
|
||||||
}
|
}
|
||||||
|
|
||||||
protected boolean isDuplicateMessage(WxCpXmlMessage wxMessage) {
|
protected boolean isDuplicateMessage(WxCpXmlMessage wxMessage) {
|
||||||
|
|
||||||
String messageId = "";
|
String messageId = "";
|
||||||
if (wxMessage.getMsgId() == null) {
|
if (wxMessage.getMsgId() == null) {
|
||||||
messageId = String.valueOf(wxMessage.getCreateTime())
|
messageId = String.valueOf(wxMessage.getCreateTime())
|
||||||
+ "-" + String.valueOf(wxMessage.getAgentId() == null ? "" : wxMessage.getAgentId())
|
+ "-" + String.valueOf(wxMessage.getAgentId() == null ? "" : wxMessage.getAgentId())
|
||||||
+ "-" + wxMessage.getFromUserName()
|
+ "-" + wxMessage.getFromUserName()
|
||||||
+ "-" + String.valueOf(wxMessage.getEventKey() == null ? "" : wxMessage.getEventKey())
|
+ "-" + String.valueOf(wxMessage.getEventKey() == null ? "" : wxMessage.getEventKey())
|
||||||
+ "-" + String.valueOf(wxMessage.getEvent() == null ? "" : wxMessage.getEvent())
|
+ "-" + String.valueOf(wxMessage.getEvent() == null ? "" : wxMessage.getEvent())
|
||||||
;
|
;
|
||||||
} else {
|
} else {
|
||||||
messageId = String.valueOf(wxMessage.getMsgId());
|
messageId = String.valueOf(wxMessage.getMsgId());
|
||||||
}
|
}
|
||||||
|
|
||||||
return this.messageDuplicateChecker.isDuplicate(messageId);
|
return this.messageDuplicateChecker.isDuplicate(messageId);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 对session的访问结束
|
* 对session的访问结束
|
||||||
*
|
*
|
||||||
* @param wxMessage
|
* @param wxMessage
|
||||||
*/
|
*/
|
||||||
protected void sessionEndAccess(WxCpXmlMessage wxMessage) {
|
protected void sessionEndAccess(WxCpXmlMessage wxMessage) {
|
||||||
|
|
||||||
InternalSession session = ((InternalSessionManager) this.sessionManager).findSession(wxMessage.getFromUserName());
|
InternalSession session = ((InternalSessionManager) this.sessionManager).findSession(wxMessage.getFromUserName());
|
||||||
if (session != null) {
|
if (session != null) {
|
||||||
session.endAccess();
|
session.endAccess();
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
@ -1,321 +1,321 @@
|
|||||||
package me.chanjar.weixin.cp.api;
|
package me.chanjar.weixin.cp.api;
|
||||||
|
|
||||||
import me.chanjar.weixin.common.api.WxErrorExceptionHandler;
|
import me.chanjar.weixin.common.api.WxErrorExceptionHandler;
|
||||||
import me.chanjar.weixin.common.exception.WxErrorException;
|
import me.chanjar.weixin.common.exception.WxErrorException;
|
||||||
import me.chanjar.weixin.common.session.WxSessionManager;
|
import me.chanjar.weixin.common.session.WxSessionManager;
|
||||||
import me.chanjar.weixin.cp.bean.WxCpXmlMessage;
|
import me.chanjar.weixin.cp.bean.WxCpXmlMessage;
|
||||||
import me.chanjar.weixin.cp.bean.WxCpXmlOutMessage;
|
import me.chanjar.weixin.cp.bean.WxCpXmlOutMessage;
|
||||||
|
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.HashMap;
|
import java.util.HashMap;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
import java.util.regex.Pattern;
|
import java.util.regex.Pattern;
|
||||||
|
|
||||||
public class WxCpMessageRouterRule {
|
public class WxCpMessageRouterRule {
|
||||||
|
|
||||||
private final WxCpMessageRouter routerBuilder;
|
private final WxCpMessageRouter routerBuilder;
|
||||||
|
|
||||||
private boolean async = true;
|
private boolean async = true;
|
||||||
|
|
||||||
private String fromUser;
|
private String fromUser;
|
||||||
|
|
||||||
private String msgType;
|
private String msgType;
|
||||||
|
|
||||||
private String event;
|
private String event;
|
||||||
|
|
||||||
private String eventKey;
|
private String eventKey;
|
||||||
|
|
||||||
private String content;
|
private String content;
|
||||||
|
|
||||||
private String rContent;
|
private String rContent;
|
||||||
|
|
||||||
private WxCpMessageMatcher matcher;
|
private WxCpMessageMatcher matcher;
|
||||||
|
|
||||||
private boolean reEnter = false;
|
private boolean reEnter = false;
|
||||||
|
|
||||||
private Integer agentId;
|
private Integer agentId;
|
||||||
|
|
||||||
private List<WxCpMessageHandler> handlers = new ArrayList<>();
|
private List<WxCpMessageHandler> handlers = new ArrayList<>();
|
||||||
|
|
||||||
private List<WxCpMessageInterceptor> interceptors = new ArrayList<>();
|
private List<WxCpMessageInterceptor> interceptors = new ArrayList<>();
|
||||||
|
|
||||||
protected WxCpMessageRouterRule(WxCpMessageRouter routerBuilder) {
|
protected WxCpMessageRouterRule(WxCpMessageRouter routerBuilder) {
|
||||||
this.routerBuilder = routerBuilder;
|
this.routerBuilder = routerBuilder;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 设置是否异步执行,默认是true
|
* 设置是否异步执行,默认是true
|
||||||
*
|
*
|
||||||
* @param async
|
* @param async
|
||||||
*/
|
*/
|
||||||
public WxCpMessageRouterRule async(boolean async) {
|
public WxCpMessageRouterRule async(boolean async) {
|
||||||
this.async = async;
|
this.async = async;
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 如果agentId匹配
|
* 如果agentId匹配
|
||||||
*
|
*
|
||||||
* @param agentId
|
* @param agentId
|
||||||
*/
|
*/
|
||||||
public WxCpMessageRouterRule agentId(Integer agentId) {
|
public WxCpMessageRouterRule agentId(Integer agentId) {
|
||||||
this.agentId = agentId;
|
this.agentId = agentId;
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 如果msgType等于某值
|
* 如果msgType等于某值
|
||||||
*
|
*
|
||||||
* @param msgType
|
* @param msgType
|
||||||
*/
|
*/
|
||||||
public WxCpMessageRouterRule msgType(String msgType) {
|
public WxCpMessageRouterRule msgType(String msgType) {
|
||||||
this.msgType = msgType;
|
this.msgType = msgType;
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 如果event等于某值
|
* 如果event等于某值
|
||||||
*
|
*
|
||||||
* @param event
|
* @param event
|
||||||
*/
|
*/
|
||||||
public WxCpMessageRouterRule event(String event) {
|
public WxCpMessageRouterRule event(String event) {
|
||||||
this.event = event;
|
this.event = event;
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 如果eventKey等于某值
|
* 如果eventKey等于某值
|
||||||
*
|
*
|
||||||
* @param eventKey
|
* @param eventKey
|
||||||
*/
|
*/
|
||||||
public WxCpMessageRouterRule eventKey(String eventKey) {
|
public WxCpMessageRouterRule eventKey(String eventKey) {
|
||||||
this.eventKey = eventKey;
|
this.eventKey = eventKey;
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 如果content等于某值
|
* 如果content等于某值
|
||||||
*
|
*
|
||||||
* @param content
|
* @param content
|
||||||
*/
|
*/
|
||||||
public WxCpMessageRouterRule content(String content) {
|
public WxCpMessageRouterRule content(String content) {
|
||||||
this.content = content;
|
this.content = content;
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 如果content匹配该正则表达式
|
* 如果content匹配该正则表达式
|
||||||
*
|
*
|
||||||
* @param regex
|
* @param regex
|
||||||
*/
|
*/
|
||||||
public WxCpMessageRouterRule rContent(String regex) {
|
public WxCpMessageRouterRule rContent(String regex) {
|
||||||
this.rContent = regex;
|
this.rContent = regex;
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 如果fromUser等于某值
|
* 如果fromUser等于某值
|
||||||
*
|
*
|
||||||
* @param fromUser
|
* @param fromUser
|
||||||
*/
|
*/
|
||||||
public WxCpMessageRouterRule fromUser(String fromUser) {
|
public WxCpMessageRouterRule fromUser(String fromUser) {
|
||||||
this.fromUser = fromUser;
|
this.fromUser = fromUser;
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 如果消息匹配某个matcher,用在用户需要自定义更复杂的匹配规则的时候
|
* 如果消息匹配某个matcher,用在用户需要自定义更复杂的匹配规则的时候
|
||||||
*
|
*
|
||||||
* @param matcher
|
* @param matcher
|
||||||
*/
|
*/
|
||||||
public WxCpMessageRouterRule matcher(WxCpMessageMatcher matcher) {
|
public WxCpMessageRouterRule matcher(WxCpMessageMatcher matcher) {
|
||||||
this.matcher = matcher;
|
this.matcher = matcher;
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 设置微信消息拦截器
|
* 设置微信消息拦截器
|
||||||
*
|
*
|
||||||
* @param interceptor
|
* @param interceptor
|
||||||
*/
|
*/
|
||||||
public WxCpMessageRouterRule interceptor(WxCpMessageInterceptor interceptor) {
|
public WxCpMessageRouterRule interceptor(WxCpMessageInterceptor interceptor) {
|
||||||
return interceptor(interceptor, (WxCpMessageInterceptor[]) null);
|
return interceptor(interceptor, (WxCpMessageInterceptor[]) null);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 设置微信消息拦截器
|
* 设置微信消息拦截器
|
||||||
*
|
*
|
||||||
* @param interceptor
|
* @param interceptor
|
||||||
* @param otherInterceptors
|
* @param otherInterceptors
|
||||||
*/
|
*/
|
||||||
public WxCpMessageRouterRule interceptor(WxCpMessageInterceptor interceptor, WxCpMessageInterceptor... otherInterceptors) {
|
public WxCpMessageRouterRule interceptor(WxCpMessageInterceptor interceptor, WxCpMessageInterceptor... otherInterceptors) {
|
||||||
this.interceptors.add(interceptor);
|
this.interceptors.add(interceptor);
|
||||||
if (otherInterceptors != null && otherInterceptors.length > 0) {
|
if (otherInterceptors != null && otherInterceptors.length > 0) {
|
||||||
for (WxCpMessageInterceptor i : otherInterceptors) {
|
for (WxCpMessageInterceptor i : otherInterceptors) {
|
||||||
this.interceptors.add(i);
|
this.interceptors.add(i);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 设置微信消息处理器
|
* 设置微信消息处理器
|
||||||
*
|
*
|
||||||
* @param handler
|
* @param handler
|
||||||
*/
|
*/
|
||||||
public WxCpMessageRouterRule handler(WxCpMessageHandler handler) {
|
public WxCpMessageRouterRule handler(WxCpMessageHandler handler) {
|
||||||
return handler(handler, (WxCpMessageHandler[]) null);
|
return handler(handler, (WxCpMessageHandler[]) null);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 设置微信消息处理器
|
* 设置微信消息处理器
|
||||||
*
|
*
|
||||||
* @param handler
|
* @param handler
|
||||||
* @param otherHandlers
|
* @param otherHandlers
|
||||||
*/
|
*/
|
||||||
public WxCpMessageRouterRule handler(WxCpMessageHandler handler, WxCpMessageHandler... otherHandlers) {
|
public WxCpMessageRouterRule handler(WxCpMessageHandler handler, WxCpMessageHandler... otherHandlers) {
|
||||||
this.handlers.add(handler);
|
this.handlers.add(handler);
|
||||||
if (otherHandlers != null && otherHandlers.length > 0) {
|
if (otherHandlers != null && otherHandlers.length > 0) {
|
||||||
for (WxCpMessageHandler i : otherHandlers) {
|
for (WxCpMessageHandler i : otherHandlers) {
|
||||||
this.handlers.add(i);
|
this.handlers.add(i);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 规则结束,代表如果一个消息匹配该规则,那么它将不再会进入其他规则
|
* 规则结束,代表如果一个消息匹配该规则,那么它将不再会进入其他规则
|
||||||
*/
|
*/
|
||||||
public WxCpMessageRouter end() {
|
public WxCpMessageRouter end() {
|
||||||
this.routerBuilder.getRules().add(this);
|
this.routerBuilder.getRules().add(this);
|
||||||
return this.routerBuilder;
|
return this.routerBuilder;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 规则结束,但是消息还会进入其他规则
|
* 规则结束,但是消息还会进入其他规则
|
||||||
*/
|
*/
|
||||||
public WxCpMessageRouter next() {
|
public WxCpMessageRouter next() {
|
||||||
this.reEnter = true;
|
this.reEnter = true;
|
||||||
return end();
|
return end();
|
||||||
}
|
}
|
||||||
|
|
||||||
protected boolean test(WxCpXmlMessage wxMessage) {
|
protected boolean test(WxCpXmlMessage wxMessage) {
|
||||||
return
|
return
|
||||||
(this.fromUser == null || this.fromUser.equals(wxMessage.getFromUserName()))
|
(this.fromUser == null || this.fromUser.equals(wxMessage.getFromUserName()))
|
||||||
&&
|
&&
|
||||||
(this.agentId == null || this.agentId.equals(wxMessage.getAgentId()))
|
(this.agentId == null || this.agentId.equals(wxMessage.getAgentId()))
|
||||||
&&
|
&&
|
||||||
(this.msgType == null || this.msgType.equals(wxMessage.getMsgType()))
|
(this.msgType == null || this.msgType.equals(wxMessage.getMsgType()))
|
||||||
&&
|
&&
|
||||||
(this.event == null || this.event.equals(wxMessage.getEvent()))
|
(this.event == null || this.event.equals(wxMessage.getEvent()))
|
||||||
&&
|
&&
|
||||||
(this.eventKey == null || this.eventKey.equals(wxMessage.getEventKey()))
|
(this.eventKey == null || this.eventKey.equals(wxMessage.getEventKey()))
|
||||||
&&
|
&&
|
||||||
(this.content == null || this.content
|
(this.content == null || this.content
|
||||||
.equals(wxMessage.getContent() == null ? null : wxMessage.getContent().trim()))
|
.equals(wxMessage.getContent() == null ? null : wxMessage.getContent().trim()))
|
||||||
&&
|
&&
|
||||||
(this.rContent == null || Pattern
|
(this.rContent == null || Pattern
|
||||||
.matches(this.rContent, wxMessage.getContent() == null ? "" : wxMessage.getContent().trim()))
|
.matches(this.rContent, wxMessage.getContent() == null ? "" : wxMessage.getContent().trim()))
|
||||||
&&
|
&&
|
||||||
(this.matcher == null || this.matcher.match(wxMessage))
|
(this.matcher == null || this.matcher.match(wxMessage))
|
||||||
;
|
;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 处理微信推送过来的消息
|
* 处理微信推送过来的消息
|
||||||
*
|
*
|
||||||
* @param wxMessage
|
* @param wxMessage
|
||||||
* @return true 代表继续执行别的router,false 代表停止执行别的router
|
* @return true 代表继续执行别的router,false 代表停止执行别的router
|
||||||
*/
|
*/
|
||||||
protected WxCpXmlOutMessage service(WxCpXmlMessage wxMessage,
|
protected WxCpXmlOutMessage service(WxCpXmlMessage wxMessage,
|
||||||
Map<String, Object> context,
|
Map<String, Object> context,
|
||||||
WxCpService wxCpService,
|
WxCpService wxCpService,
|
||||||
WxSessionManager sessionManager,
|
WxSessionManager sessionManager,
|
||||||
WxErrorExceptionHandler exceptionHandler) {
|
WxErrorExceptionHandler exceptionHandler) {
|
||||||
|
|
||||||
if (context == null) {
|
if (context == null) {
|
||||||
context = new HashMap<>();
|
context = new HashMap<>();
|
||||||
}
|
}
|
||||||
|
|
||||||
try {
|
try {
|
||||||
// 如果拦截器不通过
|
// 如果拦截器不通过
|
||||||
for (WxCpMessageInterceptor interceptor : this.interceptors) {
|
for (WxCpMessageInterceptor interceptor : this.interceptors) {
|
||||||
if (!interceptor.intercept(wxMessage, context, wxCpService, sessionManager)) {
|
if (!interceptor.intercept(wxMessage, context, wxCpService, sessionManager)) {
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// 交给handler处理
|
// 交给handler处理
|
||||||
WxCpXmlOutMessage res = null;
|
WxCpXmlOutMessage res = null;
|
||||||
for (WxCpMessageHandler handler : this.handlers) {
|
for (WxCpMessageHandler handler : this.handlers) {
|
||||||
// 返回最后handler的结果
|
// 返回最后handler的结果
|
||||||
res = handler.handle(wxMessage, context, wxCpService, sessionManager);
|
res = handler.handle(wxMessage, context, wxCpService, sessionManager);
|
||||||
}
|
}
|
||||||
return res;
|
return res;
|
||||||
|
|
||||||
} catch (WxErrorException e) {
|
} catch (WxErrorException e) {
|
||||||
exceptionHandler.handle(e);
|
exceptionHandler.handle(e);
|
||||||
}
|
}
|
||||||
|
|
||||||
return null;
|
return null;
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setFromUser(String fromUser) {
|
public void setFromUser(String fromUser) {
|
||||||
this.fromUser = fromUser;
|
this.fromUser = fromUser;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setMsgType(String msgType) {
|
public void setMsgType(String msgType) {
|
||||||
this.msgType = msgType;
|
this.msgType = msgType;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setEvent(String event) {
|
public void setEvent(String event) {
|
||||||
this.event = event;
|
this.event = event;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setEventKey(String eventKey) {
|
public void setEventKey(String eventKey) {
|
||||||
this.eventKey = eventKey;
|
this.eventKey = eventKey;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setContent(String content) {
|
public void setContent(String content) {
|
||||||
this.content = content;
|
this.content = content;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setrContent(String rContent) {
|
public void setrContent(String rContent) {
|
||||||
this.rContent = rContent;
|
this.rContent = rContent;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setMatcher(WxCpMessageMatcher matcher) {
|
public void setMatcher(WxCpMessageMatcher matcher) {
|
||||||
this.matcher = matcher;
|
this.matcher = matcher;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setAgentId(Integer agentId) {
|
public void setAgentId(Integer agentId) {
|
||||||
this.agentId = agentId;
|
this.agentId = agentId;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setHandlers(List<WxCpMessageHandler> handlers) {
|
public void setHandlers(List<WxCpMessageHandler> handlers) {
|
||||||
this.handlers = handlers;
|
this.handlers = handlers;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setInterceptors(List<WxCpMessageInterceptor> interceptors) {
|
public void setInterceptors(List<WxCpMessageInterceptor> interceptors) {
|
||||||
this.interceptors = interceptors;
|
this.interceptors = interceptors;
|
||||||
}
|
}
|
||||||
|
|
||||||
public boolean isAsync() {
|
public boolean isAsync() {
|
||||||
return this.async;
|
return this.async;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setAsync(boolean async) {
|
public void setAsync(boolean async) {
|
||||||
this.async = async;
|
this.async = async;
|
||||||
}
|
}
|
||||||
|
|
||||||
public boolean isReEnter() {
|
public boolean isReEnter() {
|
||||||
return this.reEnter;
|
return this.reEnter;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setReEnter(boolean reEnter) {
|
public void setReEnter(boolean reEnter) {
|
||||||
this.reEnter = reEnter;
|
this.reEnter = reEnter;
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
@ -1,67 +0,0 @@
|
|||||||
package me.chanjar.weixin.cp.api;
|
|
||||||
|
|
||||||
import com.google.inject.Inject;
|
|
||||||
import me.chanjar.weixin.common.exception.WxErrorException;
|
|
||||||
import me.chanjar.weixin.cp.api.impl.WxCpServiceImpl;
|
|
||||||
import me.chanjar.weixin.cp.bean.WxCpDepart;
|
|
||||||
import org.testng.Assert;
|
|
||||||
import org.testng.annotations.Guice;
|
|
||||||
import org.testng.annotations.Test;
|
|
||||||
|
|
||||||
import java.util.List;
|
|
||||||
|
|
||||||
import static org.testng.Assert.assertNotNull;
|
|
||||||
import static org.testng.Assert.assertTrue;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 测试部门接口
|
|
||||||
*
|
|
||||||
* @author Daniel Qian
|
|
||||||
*/
|
|
||||||
@Test(groups = "departAPI")
|
|
||||||
@Guice(modules = ApiTestModule.class)
|
|
||||||
public class WxCpDepartAPITest {
|
|
||||||
|
|
||||||
@Inject
|
|
||||||
protected WxCpServiceImpl wxCpService;
|
|
||||||
|
|
||||||
protected WxCpDepart depart;
|
|
||||||
|
|
||||||
@Test
|
|
||||||
public void testDepartCreate() throws WxErrorException {
|
|
||||||
WxCpDepart cpDepart = new WxCpDepart();
|
|
||||||
cpDepart.setName("子部门" + System.currentTimeMillis());
|
|
||||||
cpDepart.setParentId(1);
|
|
||||||
cpDepart.setOrder(1);
|
|
||||||
Integer departId = this.wxCpService.departCreate(cpDepart);
|
|
||||||
System.out.println(departId);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test//(dependsOnMethods = "testDepartCreate")
|
|
||||||
public void testDepartGet() throws WxErrorException {
|
|
||||||
System.out.println("=================获取部门");
|
|
||||||
List<WxCpDepart> departList = this.wxCpService.departGet();
|
|
||||||
assertNotNull(departList);
|
|
||||||
assertTrue(departList.size() > 0);
|
|
||||||
for (WxCpDepart g : departList) {
|
|
||||||
this.depart = g;
|
|
||||||
System.out.println(this.depart.getId() + ":" + this.depart.getName());
|
|
||||||
assertNotNull(g.getName());
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test(dependsOnMethods = {"testDepartGet", "testDepartCreate"})
|
|
||||||
public void testDepartUpdate() throws WxErrorException {
|
|
||||||
System.out.println("=================更新部门");
|
|
||||||
this.depart.setName("子部门改名" + System.currentTimeMillis());
|
|
||||||
this.wxCpService.departUpdate(this.depart);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test(dependsOnMethods = "testDepartUpdate")
|
|
||||||
public void testDepartDelete() throws WxErrorException {
|
|
||||||
System.out.println("=================删除部门");
|
|
||||||
System.out.println(this.depart.getId() + ":" + this.depart.getName());
|
|
||||||
this.wxCpService.departDelete(this.depart.getId());
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
Loading…
Reference in New Issue
Block a user