diff --git a/.gitignore b/.gitignore index a2b51e4a..fbf348ae 100644 --- a/.gitignore +++ b/.gitignore @@ -26,6 +26,7 @@ test-config.xml /gradle/ *.bat /gradlew +**/build/ # OSX # Icon must end with two \r diff --git a/weixin-java-cp/src/main/java/me/chanjar/weixin/cp/api/WxCpConfigStorage.java b/weixin-java-cp/src/main/java/me/chanjar/weixin/cp/api/WxCpConfigStorage.java index b334b85b..eb1e57ca 100644 --- a/weixin-java-cp/src/main/java/me/chanjar/weixin/cp/api/WxCpConfigStorage.java +++ b/weixin-java-cp/src/main/java/me/chanjar/weixin/cp/api/WxCpConfigStorage.java @@ -45,7 +45,7 @@ public interface WxCpConfigStorage { String getCorpSecret(); - String getAgentId(); + Integer getAgentId(); String getToken(); diff --git a/weixin-java-cp/src/main/java/me/chanjar/weixin/cp/api/WxCpInMemoryConfigStorage.java b/weixin-java-cp/src/main/java/me/chanjar/weixin/cp/api/WxCpInMemoryConfigStorage.java index fb7bacff..867dd6bb 100644 --- a/weixin-java-cp/src/main/java/me/chanjar/weixin/cp/api/WxCpInMemoryConfigStorage.java +++ b/weixin-java-cp/src/main/java/me/chanjar/weixin/cp/api/WxCpInMemoryConfigStorage.java @@ -19,7 +19,7 @@ public class WxCpInMemoryConfigStorage implements WxCpConfigStorage { protected volatile String token; protected volatile String accessToken; protected volatile String aesKey; - protected volatile String agentId; + protected volatile Integer agentId; protected volatile long expiresTime; protected volatile String oauth2redirectUri; @@ -146,11 +146,11 @@ public class WxCpInMemoryConfigStorage implements WxCpConfigStorage { } @Override - public String getAgentId() { + public Integer getAgentId() { return this.agentId; } - public void setAgentId(String agentId) { + public void setAgentId(Integer agentId) { this.agentId = agentId; } diff --git a/weixin-java-cp/src/main/java/me/chanjar/weixin/cp/api/WxCpJedisConfigStorage.java b/weixin-java-cp/src/main/java/me/chanjar/weixin/cp/api/WxCpJedisConfigStorage.java index 5209479f..d9cfc06f 100644 --- a/weixin-java-cp/src/main/java/me/chanjar/weixin/cp/api/WxCpJedisConfigStorage.java +++ b/weixin-java-cp/src/main/java/me/chanjar/weixin/cp/api/WxCpJedisConfigStorage.java @@ -1,267 +1,267 @@ -package me.chanjar.weixin.cp.api; - -import java.io.File; - -import me.chanjar.weixin.common.bean.WxAccessToken; -import me.chanjar.weixin.common.util.http.ApacheHttpClientBuilder; -import redis.clients.jedis.Jedis; -import redis.clients.jedis.JedisPool; - -/** - * Jedis client implementor for wechat config storage - * - * @author gaigeshen - */ -public class WxCpJedisConfigStorage implements WxCpConfigStorage { - - /* Redis keys here */ - 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 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 volatile String corpId; - private volatile String corpSecret; - - private volatile String token; - private volatile String aesKey; - private volatile String agentId; - - private volatile String oauth2redirectUri; - - private volatile String httpProxyHost; - private volatile int httpProxyPort; - private volatile String httpProxyUsername; - private volatile String httpProxyPassword; - - private volatile File tmpDirFile; - - private volatile ApacheHttpClientBuilder apacheHttpClientBuilder; - - /* Redis clients pool */ - private final JedisPool jedisPool; - - public WxCpJedisConfigStorage(String host, int port) { - this.jedisPool = new JedisPool(host, port); - } - - /** - * - * This method will be destroy jedis pool - */ - public void destroy() { - this.jedisPool.destroy(); - } - - @Override - public String getAccessToken() { - try (Jedis jedis = this.jedisPool.getResource()) { - return jedis.get(ACCESS_TOKEN_KEY); - } - } - - @Override - public boolean isAccessTokenExpired() { - try (Jedis jedis = this.jedisPool.getResource()) { - String expiresTimeStr = jedis.get(ACCESS_TOKEN_EXPIRES_TIME_KEY); - - if (expiresTimeStr != null) { - Long expiresTime = Long.parseLong(expiresTimeStr); - return System.currentTimeMillis() > expiresTime; - } - - return true; - - } - } - - @Override - public void expireAccessToken() { - try (Jedis jedis = this.jedisPool.getResource()) { - jedis.set(ACCESS_TOKEN_EXPIRES_TIME_KEY, "0"); - } - } - - @Override - public synchronized void updateAccessToken(WxAccessToken accessToken) { - this.updateAccessToken(accessToken.getAccessToken(), accessToken.getExpiresIn()); - } - - @Override - public synchronized void updateAccessToken(String accessToken, int expiresInSeconds) { - try (Jedis jedis = this.jedisPool.getResource()) { - jedis.set(ACCESS_TOKEN_KEY, accessToken); - - jedis.set(ACCESS_TOKEN_EXPIRES_TIME_KEY, - (System.currentTimeMillis() + (expiresInSeconds - 200) * 1000L) + ""); - } - } - - @Override - public String getJsapiTicket() { - try (Jedis jedis = this.jedisPool.getResource()) { - return jedis.get(JS_API_TICKET_KEY); - } - } - - @Override - public boolean isJsapiTicketExpired() { - - try (Jedis jedis = this.jedisPool.getResource()) { - String expiresTimeStr = jedis.get(JS_API_TICKET_EXPIRES_TIME_KEY); - - if (expiresTimeStr != null) { - Long expiresTime = Long.parseLong(expiresTimeStr); - return System.currentTimeMillis() > expiresTime; - } - - return true; - - } - } - - @Override - public void expireJsapiTicket() { - try (Jedis jedis = this.jedisPool.getResource()) { - jedis.set(JS_API_TICKET_EXPIRES_TIME_KEY, "0"); - } - } - - @Override - public synchronized void updateJsapiTicket(String jsapiTicket, int expiresInSeconds) { - - try (Jedis jedis = this.jedisPool.getResource()) { - jedis.set(JS_API_TICKET_KEY, jsapiTicket); - - jedis.set(JS_API_TICKET_EXPIRES_TIME_KEY, - (System.currentTimeMillis() + (expiresInSeconds - 200) * 1000L + "")); - } - - } - - @Override - public String getCorpId() { - return this.corpId; - } - - @Override - public String getCorpSecret() { - return this.corpSecret; - } - - @Override - public String getAgentId() { - return this.agentId; - } - - @Override - public String getToken() { - return this.token; - } - - @Override - public String getAesKey() { - return this.aesKey; - } - - @Override - public long getExpiresTime() { - try (Jedis jedis = this.jedisPool.getResource()) { - String expiresTimeStr = jedis.get(ACCESS_TOKEN_EXPIRES_TIME_KEY); - - if (expiresTimeStr != null) { - Long expiresTime = Long.parseLong(expiresTimeStr); - return expiresTime; - } - - return 0L; - - } - } - - @Override - public String getOauth2redirectUri() { - return this.oauth2redirectUri; - } - - @Override - public String getHttpProxyHost() { - return this.httpProxyHost; - } - - @Override - public int getHttpProxyPort() { - return this.httpProxyPort; - } - - @Override - public String getHttpProxyUsername() { - return this.httpProxyUsername; - } - - @Override - public String getHttpProxyPassword() { - return this.httpProxyPassword; - } - - @Override - public File getTmpDirFile() { - return this.tmpDirFile; - } - - @Override - public ApacheHttpClientBuilder getApacheHttpClientBuilder() { - return this.apacheHttpClientBuilder; - } - - public void setCorpId(String corpId) { - this.corpId = corpId; - } - - public void setCorpSecret(String corpSecret) { - this.corpSecret = corpSecret; - } - - public void setToken(String token) { - this.token = token; - } - - public void setAesKey(String aesKey) { - this.aesKey = aesKey; - } - - public void setAgentId(String agentId) { - this.agentId = agentId; - } - - // ============================ Setters below - - public void setOauth2redirectUri(String oauth2redirectUri) { - this.oauth2redirectUri = oauth2redirectUri; - } - - public void setHttpProxyHost(String httpProxyHost) { - this.httpProxyHost = httpProxyHost; - } - - public void setHttpProxyPort(int httpProxyPort) { - this.httpProxyPort = httpProxyPort; - } - - public void setHttpProxyUsername(String httpProxyUsername) { - this.httpProxyUsername = httpProxyUsername; - } - - public void setHttpProxyPassword(String httpProxyPassword) { - this.httpProxyPassword = httpProxyPassword; - } - - public void setTmpDirFile(File tmpDirFile) { - this.tmpDirFile = tmpDirFile; - } - - public void setApacheHttpClientBuilder(ApacheHttpClientBuilder apacheHttpClientBuilder) { - this.apacheHttpClientBuilder = apacheHttpClientBuilder; - } - -} +package me.chanjar.weixin.cp.api; + +import java.io.File; + +import me.chanjar.weixin.common.bean.WxAccessToken; +import me.chanjar.weixin.common.util.http.ApacheHttpClientBuilder; +import redis.clients.jedis.Jedis; +import redis.clients.jedis.JedisPool; + +/** + * Jedis client implementor for wechat config storage + * + * @author gaigeshen + */ +public class WxCpJedisConfigStorage implements WxCpConfigStorage { + + /* Redis keys here */ + 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 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 volatile String corpId; + private volatile String corpSecret; + + private volatile String token; + private volatile String aesKey; + private volatile Integer agentId; + + private volatile String oauth2redirectUri; + + private volatile String httpProxyHost; + private volatile int httpProxyPort; + private volatile String httpProxyUsername; + private volatile String httpProxyPassword; + + private volatile File tmpDirFile; + + private volatile ApacheHttpClientBuilder apacheHttpClientBuilder; + + /* Redis clients pool */ + private final JedisPool jedisPool; + + public WxCpJedisConfigStorage(String host, int port) { + this.jedisPool = new JedisPool(host, port); + } + + /** + * + * This method will be destroy jedis pool + */ + public void destroy() { + this.jedisPool.destroy(); + } + + @Override + public String getAccessToken() { + try (Jedis jedis = this.jedisPool.getResource()) { + return jedis.get(ACCESS_TOKEN_KEY); + } + } + + @Override + public boolean isAccessTokenExpired() { + try (Jedis jedis = this.jedisPool.getResource()) { + String expiresTimeStr = jedis.get(ACCESS_TOKEN_EXPIRES_TIME_KEY); + + if (expiresTimeStr != null) { + Long expiresTime = Long.parseLong(expiresTimeStr); + return System.currentTimeMillis() > expiresTime; + } + + return true; + + } + } + + @Override + public void expireAccessToken() { + try (Jedis jedis = this.jedisPool.getResource()) { + jedis.set(ACCESS_TOKEN_EXPIRES_TIME_KEY, "0"); + } + } + + @Override + public synchronized void updateAccessToken(WxAccessToken accessToken) { + this.updateAccessToken(accessToken.getAccessToken(), accessToken.getExpiresIn()); + } + + @Override + public synchronized void updateAccessToken(String accessToken, int expiresInSeconds) { + try (Jedis jedis = this.jedisPool.getResource()) { + jedis.set(ACCESS_TOKEN_KEY, accessToken); + + jedis.set(ACCESS_TOKEN_EXPIRES_TIME_KEY, + (System.currentTimeMillis() + (expiresInSeconds - 200) * 1000L) + ""); + } + } + + @Override + public String getJsapiTicket() { + try (Jedis jedis = this.jedisPool.getResource()) { + return jedis.get(JS_API_TICKET_KEY); + } + } + + @Override + public boolean isJsapiTicketExpired() { + + try (Jedis jedis = this.jedisPool.getResource()) { + String expiresTimeStr = jedis.get(JS_API_TICKET_EXPIRES_TIME_KEY); + + if (expiresTimeStr != null) { + Long expiresTime = Long.parseLong(expiresTimeStr); + return System.currentTimeMillis() > expiresTime; + } + + return true; + + } + } + + @Override + public void expireJsapiTicket() { + try (Jedis jedis = this.jedisPool.getResource()) { + jedis.set(JS_API_TICKET_EXPIRES_TIME_KEY, "0"); + } + } + + @Override + public synchronized void updateJsapiTicket(String jsapiTicket, int expiresInSeconds) { + + try (Jedis jedis = this.jedisPool.getResource()) { + jedis.set(JS_API_TICKET_KEY, jsapiTicket); + + jedis.set(JS_API_TICKET_EXPIRES_TIME_KEY, + (System.currentTimeMillis() + (expiresInSeconds - 200) * 1000L + "")); + } + + } + + @Override + public String getCorpId() { + return this.corpId; + } + + @Override + public String getCorpSecret() { + return this.corpSecret; + } + + @Override + public Integer getAgentId() { + return this.agentId; + } + + @Override + public String getToken() { + return this.token; + } + + @Override + public String getAesKey() { + return this.aesKey; + } + + @Override + public long getExpiresTime() { + try (Jedis jedis = this.jedisPool.getResource()) { + String expiresTimeStr = jedis.get(ACCESS_TOKEN_EXPIRES_TIME_KEY); + + if (expiresTimeStr != null) { + Long expiresTime = Long.parseLong(expiresTimeStr); + return expiresTime; + } + + return 0L; + + } + } + + @Override + public String getOauth2redirectUri() { + return this.oauth2redirectUri; + } + + @Override + public String getHttpProxyHost() { + return this.httpProxyHost; + } + + @Override + public int getHttpProxyPort() { + return this.httpProxyPort; + } + + @Override + public String getHttpProxyUsername() { + return this.httpProxyUsername; + } + + @Override + public String getHttpProxyPassword() { + return this.httpProxyPassword; + } + + @Override + public File getTmpDirFile() { + return this.tmpDirFile; + } + + @Override + public ApacheHttpClientBuilder getApacheHttpClientBuilder() { + return this.apacheHttpClientBuilder; + } + + public void setCorpId(String corpId) { + this.corpId = corpId; + } + + public void setCorpSecret(String corpSecret) { + this.corpSecret = corpSecret; + } + + public void setToken(String token) { + this.token = token; + } + + public void setAesKey(String aesKey) { + this.aesKey = aesKey; + } + + public void setAgentId(Integer agentId) { + this.agentId = agentId; + } + + // ============================ Setters below + + public void setOauth2redirectUri(String oauth2redirectUri) { + this.oauth2redirectUri = oauth2redirectUri; + } + + public void setHttpProxyHost(String httpProxyHost) { + this.httpProxyHost = httpProxyHost; + } + + public void setHttpProxyPort(int httpProxyPort) { + this.httpProxyPort = httpProxyPort; + } + + public void setHttpProxyUsername(String httpProxyUsername) { + this.httpProxyUsername = httpProxyUsername; + } + + public void setHttpProxyPassword(String httpProxyPassword) { + this.httpProxyPassword = httpProxyPassword; + } + + public void setTmpDirFile(File tmpDirFile) { + this.tmpDirFile = tmpDirFile; + } + + public void setApacheHttpClientBuilder(ApacheHttpClientBuilder apacheHttpClientBuilder) { + this.apacheHttpClientBuilder = apacheHttpClientBuilder; + } + +} diff --git a/weixin-java-cp/src/main/java/me/chanjar/weixin/cp/api/WxCpService.java b/weixin-java-cp/src/main/java/me/chanjar/weixin/cp/api/WxCpService.java index e6f8f2ad..b5bf50c9 100644 --- a/weixin-java-cp/src/main/java/me/chanjar/weixin/cp/api/WxCpService.java +++ b/weixin-java-cp/src/main/java/me/chanjar/weixin/cp/api/WxCpService.java @@ -160,7 +160,7 @@ public interface WxCpService { * * @param menu * @throws WxErrorException - * @see #menuCreate(String, me.chanjar.weixin.common.bean.menu.WxMenu) + * @see #menuCreate(Integer, WxMenu) */ void menuCreate(WxMenu menu) throws WxErrorException; @@ -177,7 +177,7 @@ public interface WxCpService { * @throws WxErrorException * @see #menuCreate(me.chanjar.weixin.common.bean.menu.WxMenu) */ - void menuCreate(String agentId, WxMenu menu) throws WxErrorException; + void menuCreate(Integer agentId, WxMenu menu) throws WxErrorException; /** *
@@ -188,7 +188,7 @@ public interface WxCpService {
    * 
* * @throws WxErrorException - * @see #menuDelete(String) + * @see #menuDelete(Integer) */ void menuDelete() throws WxErrorException; @@ -204,7 +204,7 @@ public interface WxCpService { * @throws WxErrorException * @see #menuDelete() */ - void menuDelete(String agentId) throws WxErrorException; + void menuDelete(Integer agentId) throws WxErrorException; /** *
@@ -215,7 +215,7 @@ public interface WxCpService {
    * 
* * @throws WxErrorException - * @see #menuGet(String) + * @see #menuGet(Integer) */ WxMenu menuGet() throws WxErrorException; @@ -231,7 +231,7 @@ public interface WxCpService { * @throws WxErrorException * @see #menuGet() */ - WxMenu menuGet(String agentId) throws WxErrorException; + WxMenu menuGet(Integer agentId) throws WxErrorException; /** *
@@ -396,12 +396,12 @@ public interface WxCpService {
    *  
    * 构造oauth2授权的url连接
    * 
- * + * * @param state * @return url */ String oauth2buildAuthorizationUrl(String state); - + /** *
    * 构造oauth2授权的url连接
@@ -425,7 +425,7 @@ public interface WxCpService {
    *
    * @param code
    * @return [userid, deviceid]
-   * @see #oauth2getUserInfo(String, String)
+   * @see #oauth2getUserInfo(Integer, String)
    */
   String[] oauth2getUserInfo(String code) throws WxErrorException;
 
@@ -443,7 +443,7 @@ public interface WxCpService {
    * @return [userid, deviceid]
    * @see #oauth2getUserInfo(String)
    */
-  String[] oauth2getUserInfo(String agentId, String code) throws WxErrorException;
+  String[] oauth2getUserInfo(Integer agentId, String code) throws WxErrorException;
 
 
   /**
diff --git a/weixin-java-cp/src/main/java/me/chanjar/weixin/cp/api/WxCpServiceImpl.java b/weixin-java-cp/src/main/java/me/chanjar/weixin/cp/api/WxCpServiceImpl.java
index b044b468..30f9914c 100644
--- a/weixin-java-cp/src/main/java/me/chanjar/weixin/cp/api/WxCpServiceImpl.java
+++ b/weixin-java-cp/src/main/java/me/chanjar/weixin/cp/api/WxCpServiceImpl.java
@@ -191,7 +191,7 @@ public class WxCpServiceImpl implements WxCpService {
   }
 
   @Override
-  public void menuCreate(String agentId, WxMenu menu) throws WxErrorException {
+  public void menuCreate(Integer agentId, WxMenu menu) throws WxErrorException {
     String url = "https://qyapi.weixin.qq.com/cgi-bin/menu/create?agentid="
  + this.configStorage.getAgentId();
     post(url, menu.toJson());
@@ -203,7 +203,7 @@ public class WxCpServiceImpl implements WxCpService {
   }
 
   @Override
-  public void menuDelete(String agentId) throws WxErrorException {
+  public void menuDelete(Integer agentId) throws WxErrorException {
     String url = "https://qyapi.weixin.qq.com/cgi-bin/menu/delete?agentid=" + agentId;
     get(url, null);
   }
@@ -214,7 +214,7 @@ public class WxCpServiceImpl implements WxCpService {
   }
 
   @Override
-  public WxMenu menuGet(String agentId) throws WxErrorException {
+  public WxMenu menuGet(Integer agentId) throws WxErrorException {
     String url = "https://qyapi.weixin.qq.com/cgi-bin/menu/get?agentid=" + agentId;
     try {
       String resultContent = get(url, null);
@@ -487,7 +487,7 @@ this.configStorage.getOauth2redirectUri(),
   }
 
   @Override
-  public String[] oauth2getUserInfo(String agentId, String code) throws WxErrorException {
+  public String[] oauth2getUserInfo(Integer agentId, String code) throws WxErrorException {
     String url = "https://qyapi.weixin.qq.com/cgi-bin/user/getuserinfo?"
             + "code=" + code
             + "&agentid=" + agentId;
diff --git a/weixin-java-cp/src/main/java/me/chanjar/weixin/cp/bean/WxCpMessage.java b/weixin-java-cp/src/main/java/me/chanjar/weixin/cp/bean/WxCpMessage.java
index e2422e3b..c25c170b 100644
--- a/weixin-java-cp/src/main/java/me/chanjar/weixin/cp/bean/WxCpMessage.java
+++ b/weixin-java-cp/src/main/java/me/chanjar/weixin/cp/bean/WxCpMessage.java
@@ -1,17 +1,12 @@
 package me.chanjar.weixin.cp.bean;
 
+import me.chanjar.weixin.cp.bean.messagebuilder.*;
+import me.chanjar.weixin.cp.util.json.WxCpGsonBuilder;
+
 import java.io.Serializable;
 import java.util.ArrayList;
 import java.util.List;
 
-import me.chanjar.weixin.cp.bean.messagebuilder.FileBuilder;
-import me.chanjar.weixin.cp.bean.messagebuilder.ImageBuilder;
-import me.chanjar.weixin.cp.bean.messagebuilder.NewsBuilder;
-import me.chanjar.weixin.cp.bean.messagebuilder.TextBuilder;
-import me.chanjar.weixin.cp.bean.messagebuilder.VideoBuilder;
-import me.chanjar.weixin.cp.bean.messagebuilder.VoiceBuilder;
-import me.chanjar.weixin.cp.util.json.WxCpGsonBuilder;
-
 /**
  * 消息
  *
@@ -23,7 +18,7 @@ public class WxCpMessage implements Serializable {
   private String toUser;
   private String toParty;
   private String toTag;
-  private String agentId;
+  private Integer agentId;
   private String msgType;
   private String content;
   private String mediaId;
@@ -101,11 +96,11 @@ public class WxCpMessage implements Serializable {
     this.toTag = toTag;
   }
 
-  public String getAgentId() {
+  public Integer getAgentId() {
     return this.agentId;
   }
 
-  public void setAgentId(String agentId) {
+  public void setAgentId(Integer agentId) {
     this.agentId = agentId;
   }
 
diff --git a/weixin-java-cp/src/main/java/me/chanjar/weixin/cp/bean/messagebuilder/BaseBuilder.java b/weixin-java-cp/src/main/java/me/chanjar/weixin/cp/bean/messagebuilder/BaseBuilder.java
index 035777d8..7800f1c6 100644
--- a/weixin-java-cp/src/main/java/me/chanjar/weixin/cp/bean/messagebuilder/BaseBuilder.java
+++ b/weixin-java-cp/src/main/java/me/chanjar/weixin/cp/bean/messagebuilder/BaseBuilder.java
@@ -5,13 +5,13 @@ import me.chanjar.weixin.cp.bean.WxCpMessage;
 
 public class BaseBuilder {
   protected String msgType;
-  protected String agentId;
+  protected Integer agentId;
   protected String toUser;
   protected String toParty;
   protected String toTag;
   protected String safe;
 
-  public T agentId(String agentId) {
+  public T agentId(Integer agentId) {
     this.agentId = agentId;
     return (T) this;
   }