diff --git a/weixin-java-cp/src/main/java/me/chanjar/weixin/cp/api/WxCpAgentService.java b/weixin-java-cp/src/main/java/me/chanjar/weixin/cp/api/WxCpAgentService.java index 30214a478f..7dad7b6c76 100644 --- a/weixin-java-cp/src/main/java/me/chanjar/weixin/cp/api/WxCpAgentService.java +++ b/weixin-java-cp/src/main/java/me/chanjar/weixin/cp/api/WxCpAgentService.java @@ -15,9 +15,9 @@ * @author huansinho */ public interface WxCpAgentService { - String GET_AGENT = "https://qyapi.weixin.qq.com/cgi-bin/agent/get?agentid=%d"; - String AGENT_SET = "https://qyapi.weixin.qq.com/cgi-bin/agent/set"; - String AGENT_LIST = "https://qyapi.weixin.qq.com/cgi-bin/agent/list"; + String GET_AGENT = "/cgi-bin/agent/get?agentid=%d"; + String AGENT_SET = "/cgi-bin/agent/set"; + String AGENT_LIST = "/cgi-bin/agent/list"; /** *
diff --git a/weixin-java-cp/src/main/java/me/chanjar/weixin/cp/api/WxCpChatService.java b/weixin-java-cp/src/main/java/me/chanjar/weixin/cp/api/WxCpChatService.java
index bbfc0fdd48..58ab329f01 100644
--- a/weixin-java-cp/src/main/java/me/chanjar/weixin/cp/api/WxCpChatService.java
+++ b/weixin-java-cp/src/main/java/me/chanjar/weixin/cp/api/WxCpChatService.java
@@ -12,10 +12,10 @@
* @author gaigeshen
*/
public interface WxCpChatService {
- String APPCHAT_CREATE = "https://qyapi.weixin.qq.com/cgi-bin/appchat/create";
- String APPCHAT_UPDATE = "https://qyapi.weixin.qq.com/cgi-bin/appchat/update";
- String APPCHAT_GET_CHATID = "https://qyapi.weixin.qq.com/cgi-bin/appchat/get?chatid=";
- String APPCHAT_SEND = "https://qyapi.weixin.qq.com/cgi-bin/appchat/send";
+ String APPCHAT_CREATE = "/cgi-bin/appchat/create";
+ String APPCHAT_UPDATE = "/cgi-bin/appchat/update";
+ String APPCHAT_GET_CHATID = "/cgi-bin/appchat/get?chatid=";
+ String APPCHAT_SEND = "/cgi-bin/appchat/send";
@Deprecated
String chatCreate(String name, String owner, List users, String chatId) throws WxErrorException;
diff --git a/weixin-java-cp/src/main/java/me/chanjar/weixin/cp/api/WxCpDepartmentService.java b/weixin-java-cp/src/main/java/me/chanjar/weixin/cp/api/WxCpDepartmentService.java
index 3f80d693ad..8aa7ca353a 100644
--- a/weixin-java-cp/src/main/java/me/chanjar/weixin/cp/api/WxCpDepartmentService.java
+++ b/weixin-java-cp/src/main/java/me/chanjar/weixin/cp/api/WxCpDepartmentService.java
@@ -1,10 +1,10 @@
package me.chanjar.weixin.cp.api;
-import java.util.List;
-
import me.chanjar.weixin.common.error.WxErrorException;
import me.chanjar.weixin.cp.bean.WxCpDepart;
+import java.util.List;
+
/**
*
* 部门管理接口
@@ -14,10 +14,10 @@
* @author Binary Wang
*/
public interface WxCpDepartmentService {
- String DEPARTMENT_CREATE = "https://qyapi.weixin.qq.com/cgi-bin/department/create";
- String DEPARTMENT_UPDATE = "https://qyapi.weixin.qq.com/cgi-bin/department/update";
- String DEPARTMENT_DELETE = "https://qyapi.weixin.qq.com/cgi-bin/department/delete?id=%d";
- String DEPARTMENT_LIST = "https://qyapi.weixin.qq.com/cgi-bin/department/list";
+ String DEPARTMENT_CREATE = "/cgi-bin/department/create";
+ String DEPARTMENT_UPDATE = "/cgi-bin/department/update";
+ String DEPARTMENT_DELETE = "/cgi-bin/department/delete?id=%d";
+ String DEPARTMENT_LIST = "/cgi-bin/department/list";
/**
*
diff --git a/weixin-java-cp/src/main/java/me/chanjar/weixin/cp/api/WxCpMediaService.java b/weixin-java-cp/src/main/java/me/chanjar/weixin/cp/api/WxCpMediaService.java
index 31ce6b5991..67c67ec625 100644
--- a/weixin-java-cp/src/main/java/me/chanjar/weixin/cp/api/WxCpMediaService.java
+++ b/weixin-java-cp/src/main/java/me/chanjar/weixin/cp/api/WxCpMediaService.java
@@ -1,12 +1,12 @@
package me.chanjar.weixin.cp.api;
+import me.chanjar.weixin.common.bean.result.WxMediaUploadResult;
+import me.chanjar.weixin.common.error.WxErrorException;
+
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
-import me.chanjar.weixin.common.bean.result.WxMediaUploadResult;
-import me.chanjar.weixin.common.error.WxErrorException;
-
/**
*
* 媒体管理接口.
@@ -16,10 +16,10 @@
* @author Binary Wang
*/
public interface WxCpMediaService {
- String MEDIA_GET_URL = "https://qyapi.weixin.qq.com/cgi-bin/media/get";
- String MEDIA_UPLOAD_URL = "https://qyapi.weixin.qq.com/cgi-bin/media/upload?type=";
- String IMG_UPLOAD_URL = "https://qyapi.weixin.qq.com/cgi-bin/media/uploadimg";
- String JSSDK_MEDIA_GET_URL = "https://qyapi.weixin.qq.com/cgi-bin/media/get/jssdk";
+ String MEDIA_GET_URL = "/cgi-bin/media/get";
+ String MEDIA_UPLOAD_URL = "/cgi-bin/media/upload?type=";
+ String IMG_UPLOAD_URL = "/cgi-bin/media/uploadimg";
+ String JSSDK_MEDIA_GET_URL = "/cgi-bin/media/get/jssdk";
/**
*
diff --git a/weixin-java-cp/src/main/java/me/chanjar/weixin/cp/api/WxCpMenuService.java b/weixin-java-cp/src/main/java/me/chanjar/weixin/cp/api/WxCpMenuService.java
index be7b4b7e55..7c0a01e595 100644
--- a/weixin-java-cp/src/main/java/me/chanjar/weixin/cp/api/WxCpMenuService.java
+++ b/weixin-java-cp/src/main/java/me/chanjar/weixin/cp/api/WxCpMenuService.java
@@ -12,9 +12,9 @@
* @author Binary Wang
*/
public interface WxCpMenuService {
- String MENU_CREATE = "https://qyapi.weixin.qq.com/cgi-bin/menu/create?agentid=%d";
- String MENU_DELETE = "https://qyapi.weixin.qq.com/cgi-bin/menu/delete?agentid=%d";
- String MENU_GET = "https://qyapi.weixin.qq.com/cgi-bin/menu/get?agentid=%d";
+ String MENU_CREATE = "/cgi-bin/menu/create?agentid=%d";
+ String MENU_DELETE = "/cgi-bin/menu/delete?agentid=%d";
+ String MENU_GET = "/cgi-bin/menu/get?agentid=%d";
/**
*
diff --git a/weixin-java-cp/src/main/java/me/chanjar/weixin/cp/api/WxCpOAuth2Service.java b/weixin-java-cp/src/main/java/me/chanjar/weixin/cp/api/WxCpOAuth2Service.java
index c6d9063fbb..39d2163e7b 100644
--- a/weixin-java-cp/src/main/java/me/chanjar/weixin/cp/api/WxCpOAuth2Service.java
+++ b/weixin-java-cp/src/main/java/me/chanjar/weixin/cp/api/WxCpOAuth2Service.java
@@ -13,8 +13,8 @@
* @author Binary Wang
*/
public interface WxCpOAuth2Service {
- String URL_GET_USER_INFO = "https://qyapi.weixin.qq.com/cgi-bin/user/getuserinfo?code=%s&agentid=%d";
- String URL_GET_USER_DETAIL = "https://qyapi.weixin.qq.com/cgi-bin/user/getuserdetail";
+ String URL_GET_USER_INFO = "/cgi-bin/user/getuserinfo?code=%s&agentid=%d";
+ String URL_GET_USER_DETAIL = "/cgi-bin/user/getuserdetail";
String URL_OAUTH_2_AUTHORIZE = "https://open.weixin.qq.com/connect/oauth2/authorize";
/**
diff --git a/weixin-java-cp/src/main/java/me/chanjar/weixin/cp/api/WxCpOaService.java b/weixin-java-cp/src/main/java/me/chanjar/weixin/cp/api/WxCpOaService.java
index 26922c0eee..5cca9e7788 100644
--- a/weixin-java-cp/src/main/java/me/chanjar/weixin/cp/api/WxCpOaService.java
+++ b/weixin-java-cp/src/main/java/me/chanjar/weixin/cp/api/WxCpOaService.java
@@ -16,10 +16,10 @@
* @date 2019-04-06 10:52
*/
public interface WxCpOaService {
- String GET_CHECKIN_DATA = "https://qyapi.weixin.qq.com/cgi-bin/checkin/getcheckindata";
- String GET_CHECKIN_OPTION = "https://qyapi.weixin.qq.com/cgi-bin/checkin/getcheckinoption";
- String GET_APPROVAL_DATA = "https://qyapi.weixin.qq.com/cgi-bin/corp/getapprovaldata";
- String GET_DIAL_RECORD = "https://qyapi.weixin.qq.com/cgi-bin/dial/get_dial_record";
+ String GET_CHECKIN_DATA = "/cgi-bin/checkin/getcheckindata";
+ String GET_CHECKIN_OPTION = "/cgi-bin/checkin/getcheckinoption";
+ String GET_APPROVAL_DATA = "/cgi-bin/corp/getapprovaldata";
+ String GET_DIAL_RECORD = "/cgi-bin/dial/get_dial_record";
/**
*
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 13d2e9dc4a..12ae987b1b 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
@@ -17,15 +17,15 @@
* @author chanjaster
*/
public interface WxCpService {
- String GET_JSAPI_TICKET = "https://qyapi.weixin.qq.com/cgi-bin/get_jsapi_ticket";
- String GET_AGENT_CONFIG_TICKET = "https://qyapi.weixin.qq.com/cgi-bin/ticket/get?&type=agent_config";
- String MESSAGE_SEND = "https://qyapi.weixin.qq.com/cgi-bin/message/send";
- String GET_CALLBACK_IP = "https://qyapi.weixin.qq.com/cgi-bin/getcallbackip";
- String BATCH_REPLACE_PARTY = "https://qyapi.weixin.qq.com/cgi-bin/batch/replaceparty";
- String BATCH_REPLACE_USER = "https://qyapi.weixin.qq.com/cgi-bin/batch/replaceuser";
- String BATCH_GET_RESULT = "https://qyapi.weixin.qq.com/cgi-bin/batch/getresult?jobid=";
- String JSCODE_TO_SESSION_URL = "https://qyapi.weixin.qq.com/cgi-bin/miniprogram/jscode2session";
- String GET_TOKEN = "https://qyapi.weixin.qq.com/cgi-bin/gettoken?&corpid=%s&corpsecret=%s";
+ String GET_JSAPI_TICKET = "/cgi-bin/get_jsapi_ticket";
+ String GET_AGENT_CONFIG_TICKET = "/cgi-bin/ticket/get?&type=agent_config";
+ String MESSAGE_SEND = "/cgi-bin/message/send";
+ String GET_CALLBACK_IP = "/cgi-bin/getcallbackip";
+ String BATCH_REPLACE_PARTY = "/cgi-bin/batch/replaceparty";
+ String BATCH_REPLACE_USER = "/cgi-bin/batch/replaceuser";
+ String BATCH_GET_RESULT = "/cgi-bin/batch/getresult?jobid=";
+ String JSCODE_TO_SESSION_URL = "/cgi-bin/miniprogram/jscode2session";
+ String GET_TOKEN = "/cgi-bin/gettoken?&corpid=%s&corpsecret=%s";
/**
*
diff --git a/weixin-java-cp/src/main/java/me/chanjar/weixin/cp/api/WxCpTagService.java b/weixin-java-cp/src/main/java/me/chanjar/weixin/cp/api/WxCpTagService.java
index a570a80b10..ee1b526d64 100644
--- a/weixin-java-cp/src/main/java/me/chanjar/weixin/cp/api/WxCpTagService.java
+++ b/weixin-java-cp/src/main/java/me/chanjar/weixin/cp/api/WxCpTagService.java
@@ -17,13 +17,13 @@
* @author Binary Wang
*/
public interface WxCpTagService {
- String TAG_CREATE = "https://qyapi.weixin.qq.com/cgi-bin/tag/create";
- String TAG_UPDATE = "https://qyapi.weixin.qq.com/cgi-bin/tag/update";
- String TAG_DELETE = "https://qyapi.weixin.qq.com/cgi-bin/tag/delete?tagid=%s";
- String TAG_LIST = "https://qyapi.weixin.qq.com/cgi-bin/tag/list";
- String TAG_GET = "https://qyapi.weixin.qq.com/cgi-bin/tag/get?tagid=%s";
- String TAG_ADDTAGUSERS = "https://qyapi.weixin.qq.com/cgi-bin/tag/addtagusers";
- String TAG_DELTAGUSERS = "https://qyapi.weixin.qq.com/cgi-bin/tag/deltagusers";
+ String TAG_CREATE = "/cgi-bin/tag/create";
+ String TAG_UPDATE = "/cgi-bin/tag/update";
+ String TAG_DELETE = "/cgi-bin/tag/delete?tagid=%s";
+ String TAG_LIST = "/cgi-bin/tag/list";
+ String TAG_GET = "/cgi-bin/tag/get?tagid=%s";
+ String TAG_ADDTAGUSERS = "/cgi-bin/tag/addtagusers";
+ String TAG_DELTAGUSERS = "/cgi-bin/tag/deltagusers";
/**
* 创建标签.
diff --git a/weixin-java-cp/src/main/java/me/chanjar/weixin/cp/api/WxCpTaskCardService.java b/weixin-java-cp/src/main/java/me/chanjar/weixin/cp/api/WxCpTaskCardService.java
index 1e1077014f..b6ebdc1202 100644
--- a/weixin-java-cp/src/main/java/me/chanjar/weixin/cp/api/WxCpTaskCardService.java
+++ b/weixin-java-cp/src/main/java/me/chanjar/weixin/cp/api/WxCpTaskCardService.java
@@ -14,7 +14,7 @@
* @date 2019-05-16
*/
public interface WxCpTaskCardService {
- String MESSAGE_UPDATE_TASKCARD = "https://qyapi.weixin.qq.com/cgi-bin/message/update_taskcard";
+ String MESSAGE_UPDATE_TASKCARD = "/cgi-bin/message/update_taskcard";
/**
*
diff --git a/weixin-java-cp/src/main/java/me/chanjar/weixin/cp/api/WxCpTpService.java b/weixin-java-cp/src/main/java/me/chanjar/weixin/cp/api/WxCpTpService.java
index 6e0d63fb2c..6c52bcfde6 100644
--- a/weixin-java-cp/src/main/java/me/chanjar/weixin/cp/api/WxCpTpService.java
+++ b/weixin-java-cp/src/main/java/me/chanjar/weixin/cp/api/WxCpTpService.java
@@ -15,10 +15,10 @@
* @author zhenjun cai
*/
public interface WxCpTpService {
- String JSCODE_TO_SESSION_URL = "https://qyapi.weixin.qq.com/cgi-bin/service/miniprogram/jscode2session";
- String GET_CORP_TOKEN = "https://qyapi.weixin.qq.com/cgi-bin/service/get_corp_token";
- String GET_PERMANENT_CODE = "https://qyapi.weixin.qq.com/cgi-bin/service/get_permanent_code";
- String GET_SUITE_TOKEN = "https://qyapi.weixin.qq.com/cgi-bin/service/get_suite_token";
+ String JSCODE_TO_SESSION_URL = "/cgi-bin/service/miniprogram/jscode2session";
+ String GET_CORP_TOKEN = "/cgi-bin/service/get_corp_token";
+ String GET_PERMANENT_CODE = "/cgi-bin/service/get_permanent_code";
+ String GET_SUITE_TOKEN = "/cgi-bin/service/get_suite_token";
/**
*
diff --git a/weixin-java-cp/src/main/java/me/chanjar/weixin/cp/api/WxCpUserService.java b/weixin-java-cp/src/main/java/me/chanjar/weixin/cp/api/WxCpUserService.java
index 091c687595..4289ae94c7 100644
--- a/weixin-java-cp/src/main/java/me/chanjar/weixin/cp/api/WxCpUserService.java
+++ b/weixin-java-cp/src/main/java/me/chanjar/weixin/cp/api/WxCpUserService.java
@@ -1,13 +1,13 @@
package me.chanjar.weixin.cp.api;
-import java.util.List;
-import java.util.Map;
-
import me.chanjar.weixin.common.error.WxErrorException;
import me.chanjar.weixin.cp.bean.WxCpInviteResult;
import me.chanjar.weixin.cp.bean.WxCpUser;
import me.chanjar.weixin.cp.bean.WxCpUserExternalContactInfo;
+import java.util.List;
+import java.util.Map;
+
/**
*
* 用户管理接口
@@ -17,18 +17,18 @@
* @author Binary Wang
*/
public interface WxCpUserService {
- String URL_AUTHENTICATE = "https://qyapi.weixin.qq.com/cgi-bin/user/authsucc?userid=";
- String URL_USER_CREATE = "https://qyapi.weixin.qq.com/cgi-bin/user/create";
- String URL_USER_UPDATE = "https://qyapi.weixin.qq.com/cgi-bin/user/update";
- String URL_USER_DELETE = "https://qyapi.weixin.qq.com/cgi-bin/user/delete?userid=";
- String URL_USER_BATCH_DELETE = "https://qyapi.weixin.qq.com/cgi-bin/user/batchdelete";
- String URL_USER_GET = "https://qyapi.weixin.qq.com/cgi-bin/user/get?userid=";
- String URL_USER_LIST = "https://qyapi.weixin.qq.com/cgi-bin/user/list?department_id=";
- String URL_USER_SIMPLE_LIST = "https://qyapi.weixin.qq.com/cgi-bin/user/simplelist?department_id=";
- String URL_BATCH_INVITE = "https://qyapi.weixin.qq.com/cgi-bin/batch/invite";
- String URL_CONVERT_TO_OPENID = "https://qyapi.weixin.qq.com/cgi-bin/user/convert_to_openid";
- String URL_CONVERT_TO_USERID = "https://qyapi.weixin.qq.com/cgi-bin/user/convert_to_userid";
- String URL_GET_EXTERNAL_CONTACT = "https://qyapi.weixin.qq.com/cgi-bin/crm/get_external_contact?external_userid=";
+ String URL_AUTHENTICATE = "/cgi-bin/user/authsucc?userid=";
+ String URL_USER_CREATE = "/cgi-bin/user/create";
+ String URL_USER_UPDATE = "/cgi-bin/user/update";
+ String URL_USER_DELETE = "/cgi-bin/user/delete?userid=";
+ String URL_USER_BATCH_DELETE = "/cgi-bin/user/batchdelete";
+ String URL_USER_GET = "/cgi-bin/user/get?userid=";
+ String URL_USER_LIST = "/cgi-bin/user/list?department_id=";
+ String URL_USER_SIMPLE_LIST = "/cgi-bin/user/simplelist?department_id=";
+ String URL_BATCH_INVITE = "/cgi-bin/batch/invite";
+ String URL_CONVERT_TO_OPENID = "/cgi-bin/user/convert_to_openid";
+ String URL_CONVERT_TO_USERID = "/cgi-bin/user/convert_to_userid";
+ String URL_GET_EXTERNAL_CONTACT = "/cgi-bin/crm/get_external_contact?external_userid=";
/**
*
diff --git a/weixin-java-cp/src/main/java/me/chanjar/weixin/cp/api/impl/BaseWxCpServiceImpl.java b/weixin-java-cp/src/main/java/me/chanjar/weixin/cp/api/impl/BaseWxCpServiceImpl.java
index d9c88b47a5..e0bab1d90e 100644
--- a/weixin-java-cp/src/main/java/me/chanjar/weixin/cp/api/impl/BaseWxCpServiceImpl.java
+++ b/weixin-java-cp/src/main/java/me/chanjar/weixin/cp/api/impl/BaseWxCpServiceImpl.java
@@ -18,7 +18,17 @@
import me.chanjar.weixin.common.util.http.RequestHttp;
import me.chanjar.weixin.common.util.http.SimpleGetRequestExecutor;
import me.chanjar.weixin.common.util.http.SimplePostRequestExecutor;
-import me.chanjar.weixin.cp.api.*;
+import me.chanjar.weixin.cp.api.WxCpAgentService;
+import me.chanjar.weixin.cp.api.WxCpChatService;
+import me.chanjar.weixin.cp.api.WxCpDepartmentService;
+import me.chanjar.weixin.cp.api.WxCpMediaService;
+import me.chanjar.weixin.cp.api.WxCpMenuService;
+import me.chanjar.weixin.cp.api.WxCpOAuth2Service;
+import me.chanjar.weixin.cp.api.WxCpOaService;
+import me.chanjar.weixin.cp.api.WxCpService;
+import me.chanjar.weixin.cp.api.WxCpTagService;
+import me.chanjar.weixin.cp.api.WxCpTaskCardService;
+import me.chanjar.weixin.cp.api.WxCpUserService;
import me.chanjar.weixin.cp.bean.WxCpMaJsCode2SessionResult;
import me.chanjar.weixin.cp.bean.WxCpMessage;
import me.chanjar.weixin.cp.bean.WxCpMessageSendResult;
@@ -37,16 +47,16 @@
public abstract class BaseWxCpServiceImpl implements WxCpService, RequestHttp {
protected final Logger log = LoggerFactory.getLogger(this.getClass());
- private WxCpUserService userService = new WxCpUserServiceImpl(this);
- private WxCpChatService chatService = new WxCpChatServiceImpl(this);
+ private WxCpUserService userService = new WxCpUserServiceImpl(this);
+ private WxCpChatService chatService = new WxCpChatServiceImpl(this);
private WxCpDepartmentService departmentService = new WxCpDepartmentServiceImpl(this);
- private WxCpMediaService mediaService = new WxCpMediaServiceImpl(this);
- private WxCpMenuService menuService = new WxCpMenuServiceImpl(this);
- private WxCpOAuth2Service oauth2Service = new WxCpOAuth2ServiceImpl(this);
- private WxCpTagService tagService = new WxCpTagServiceImpl(this);
- private WxCpAgentService agentService = new WxCpAgentServiceImpl(this);
- private WxCpOaService oaService = new WxCpOaServiceImpl(this);
- private WxCpTaskCardService taskCardService = new WxCpTaskCardServiceImpl(this);
+ private WxCpMediaService mediaService = new WxCpMediaServiceImpl(this);
+ private WxCpMenuService menuService = new WxCpMenuServiceImpl(this);
+ private WxCpOAuth2Service oauth2Service = new WxCpOAuth2ServiceImpl(this);
+ private WxCpTagService tagService = new WxCpTagServiceImpl(this);
+ private WxCpAgentService agentService = new WxCpAgentServiceImpl(this);
+ private WxCpOaService oaService = new WxCpOaServiceImpl(this);
+ private WxCpTaskCardService taskCardService = new WxCpTaskCardServiceImpl(this);
/**
* 全局的是否正在刷新access token的锁
@@ -104,7 +114,7 @@ public String getAgentJsapiTicket(boolean forceRefresh) throws WxErrorException
if (this.configStorage.isAgentJsapiTicketExpired()) {
synchronized (this.globalAgentJsapiTicketRefreshLock) {
if (this.configStorage.isAgentJsapiTicketExpired()) {
- String responseContent = this.get(WxCpService.GET_AGENT_CONFIG_TICKET, null);
+ String responseContent = this.get(this.configStorage.getApiUrl(WxCpService.GET_AGENT_CONFIG_TICKET), null);
JsonObject jsonObject = new JsonParser().parse(responseContent).getAsJsonObject();
this.configStorage.updateAgentJsapiTicket(jsonObject.get("ticket").getAsString(),
jsonObject.get("expires_in").getAsInt());
@@ -129,7 +139,7 @@ public String getJsapiTicket(boolean forceRefresh) throws WxErrorException {
if (this.configStorage.isJsapiTicketExpired()) {
synchronized (this.globalJsapiTicketRefreshLock) {
if (this.configStorage.isJsapiTicketExpired()) {
- String responseContent = this.get(WxCpService.GET_JSAPI_TICKET, null);
+ String responseContent = this.get(this.configStorage.getApiUrl(WxCpService.GET_JSAPI_TICKET), null);
JsonObject tmpJsonObject = new JsonParser().parse(responseContent).getAsJsonObject();
this.configStorage.updateJsapiTicket(tmpJsonObject.get("ticket").getAsString(),
tmpJsonObject.get("expires_in").getAsInt());
@@ -170,7 +180,7 @@ public WxCpMessageSendResult messageSend(WxCpMessage message) throws WxErrorExce
message.setAgentId(this.getWxCpConfigStorage().getAgentId());
}
- return WxCpMessageSendResult.fromJson(this.post(WxCpService.MESSAGE_SEND, message.toJson()));
+ return WxCpMessageSendResult.fromJson(this.post(this.configStorage.getApiUrl(WxCpService.MESSAGE_SEND), message.toJson()));
}
@Override
@@ -179,13 +189,13 @@ public WxCpMaJsCode2SessionResult jsCode2Session(String jsCode) throws WxErrorEx
params.put("js_code", jsCode);
params.put("grant_type", "authorization_code");
- String result = this.get(JSCODE_TO_SESSION_URL, Joiner.on("&").withKeyValueSeparator("=").join(params));
+ String result = this.get(this.configStorage.getApiUrl(JSCODE_TO_SESSION_URL), Joiner.on("&").withKeyValueSeparator("=").join(params));
return WxCpMaJsCode2SessionResult.fromJson(result);
}
@Override
public String[] getCallbackIp() throws WxErrorException {
- String responseContent = get(WxCpService.GET_CALLBACK_IP, null);
+ String responseContent = get(this.configStorage.getApiUrl(WxCpService.GET_CALLBACK_IP), null);
JsonElement tmpJsonElement = new JsonParser().parse(responseContent);
JsonArray jsonArray = tmpJsonElement.getAsJsonObject().get("ip_list").getAsJsonArray();
String[] ips = new String[jsonArray.size()];
@@ -329,19 +339,19 @@ public WxSessionManager getSessionManager() {
public String replaceParty(String mediaId) throws WxErrorException {
JsonObject jsonObject = new JsonObject();
jsonObject.addProperty("media_id", mediaId);
- return post(WxCpService.BATCH_REPLACE_PARTY, jsonObject.toString());
+ return post(this.configStorage.getApiUrl(WxCpService.BATCH_REPLACE_PARTY), jsonObject.toString());
}
@Override
public String replaceUser(String mediaId) throws WxErrorException {
JsonObject jsonObject = new JsonObject();
jsonObject.addProperty("media_id", mediaId);
- return post(WxCpService.BATCH_REPLACE_USER, jsonObject.toString());
+ return post(this.configStorage.getApiUrl(WxCpService.BATCH_REPLACE_USER), jsonObject.toString());
}
@Override
public String getTaskResult(String joinId) throws WxErrorException {
- String url = WxCpService.BATCH_GET_RESULT + joinId;
+ String url = this.configStorage.getApiUrl(WxCpService.BATCH_GET_RESULT + joinId);
return get(url, null);
}
diff --git a/weixin-java-cp/src/main/java/me/chanjar/weixin/cp/api/impl/BaseWxCpTpServiceImpl.java b/weixin-java-cp/src/main/java/me/chanjar/weixin/cp/api/impl/BaseWxCpTpServiceImpl.java
index 837c355e4d..f3714516cf 100644
--- a/weixin-java-cp/src/main/java/me/chanjar/weixin/cp/api/impl/BaseWxCpTpServiceImpl.java
+++ b/weixin-java-cp/src/main/java/me/chanjar/weixin/cp/api/impl/BaseWxCpTpServiceImpl.java
@@ -1,244 +1,244 @@
-package me.chanjar.weixin.cp.api.impl;
-
-import java.io.File;
-import java.io.IOException;
-import java.util.HashMap;
-import java.util.Map;
-
-import org.slf4j.Logger;
-import org.slf4j.LoggerFactory;
-
-import com.google.common.base.Joiner;
-import com.google.gson.JsonObject;
-import com.google.gson.JsonParser;
-
-import me.chanjar.weixin.common.WxType;
-import me.chanjar.weixin.common.bean.WxAccessToken;
-import me.chanjar.weixin.common.error.WxError;
-import me.chanjar.weixin.common.error.WxErrorException;
-import me.chanjar.weixin.common.util.DataUtils;
-import me.chanjar.weixin.common.util.crypto.SHA1;
-import me.chanjar.weixin.common.util.http.RequestExecutor;
-import me.chanjar.weixin.common.util.http.RequestHttp;
-import me.chanjar.weixin.common.util.http.SimpleGetRequestExecutor;
-import me.chanjar.weixin.common.util.http.SimplePostRequestExecutor;
-import me.chanjar.weixin.cp.api.WxCpService;
-import me.chanjar.weixin.cp.api.WxCpTpService;
-import me.chanjar.weixin.cp.bean.WxCpMaJsCode2SessionResult;
-import me.chanjar.weixin.cp.bean.WxCpTpCorp;
-import me.chanjar.weixin.cp.config.WxCpTpConfigStorage;
-
-/**
- * @author zhenjun cai
- */
-public abstract class BaseWxCpTpServiceImpl implements WxCpTpService, RequestHttp {
- protected final Logger log = LoggerFactory.getLogger(this.getClass());
-
- /**
- * 全局的是否正在刷新access token的锁
- */
- protected final Object globalSuiteAccessTokenRefreshLock = new Object();
-
- /**
- * 全局的是否正在刷新jsapi_ticket的锁
- */
- protected final Object globalSuiteTicketRefreshLock = new Object();
-
-
- protected WxCpTpConfigStorage configStorage;
-
-
- /**
- * 临时文件目录
- */
- private File tmpDirFile;
- private int retrySleepMillis = 1000;
- private int maxRetryTimes = 5;
-
- @Override
- public boolean checkSignature(String msgSignature, String timestamp, String nonce, String data) {
- try {
- return SHA1.gen(this.configStorage.getToken(), timestamp, nonce, data)
- .equals(msgSignature);
- } catch (Exception e) {
- this.log.error("Checking signature failed, and the reason is :" + e.getMessage());
- return false;
- }
- }
-
- @Override
- public String getSuiteAccessToken() throws WxErrorException {
- return getSuiteAccessToken(false);
- }
-
- @Override
- public String getSuiteTicket() throws WxErrorException {
- return getSuiteTicket(false);
- }
-
- @Override
- public String getSuiteTicket(boolean forceRefresh) throws WxErrorException {
-// suite ticket由微信服务器推送,不能强制刷新
-// if (forceRefresh) {
-// this.configStorage.expireSuiteTicket();
-// }
-
- if (this.configStorage.isSuiteTicketExpired()) {
-// 本地suite ticket 不存在或者过期
- WxError wxError = WxError.fromJson("{\"errcode\":40085, \"errmsg\":\"invaild suite ticket\"}", WxType.CP);
- throw new WxErrorException(wxError);
- }
- return this.configStorage.getSuiteTicket();
- }
-
-
- @Override
- public WxCpMaJsCode2SessionResult jsCode2Session(String jsCode) throws WxErrorException {
- Map params = new HashMap<>(2);
- params.put("js_code", jsCode);
- params.put("grant_type", "authorization_code");
-
- String result = this.get(JSCODE_TO_SESSION_URL, Joiner.on("&").withKeyValueSeparator("=").join(params));
- return WxCpMaJsCode2SessionResult.fromJson(result);
- }
-
-
- @Override
- public WxAccessToken getCorpToken(String authCorpid, String permanentCode) throws WxErrorException {
- JsonObject jsonObject = new JsonObject();
- jsonObject.addProperty("auth_corpid", authCorpid);
- jsonObject.addProperty("permanent_code", permanentCode);
- String result = post(GET_CORP_TOKEN, jsonObject.toString());
-
- return WxAccessToken.fromJson(result);
- }
-
-
- @Override
- public WxCpTpCorp getPermanentCode(String authCode) throws WxErrorException {
- JsonObject jsonObject = new JsonObject();
- jsonObject.addProperty("auth_code", authCode);
-
- String result = post(GET_PERMANENT_CODE, jsonObject.toString());
- jsonObject = new JsonParser().parse(result).getAsJsonObject();
- WxCpTpCorp wxCpTpCorp = WxCpTpCorp.fromJson(jsonObject.get("auth_corp_info").getAsString());
- wxCpTpCorp.setPermanentCode(jsonObject.get("permanent_code").getAsString());
- return wxCpTpCorp;
- }
-
- @Override
- public String get(String url, String queryParam) throws WxErrorException {
- return execute(SimpleGetRequestExecutor.create(this), url, queryParam);
- }
-
- @Override
- public String post(String url, String postData) throws WxErrorException {
- return execute(SimplePostRequestExecutor.create(this), url, postData);
- }
-
- /**
- * 向微信端发送请求,在这里执行的策略是当发生access_token过期时才去刷新,然后重新执行请求,而不是全局定时请求.
- */
- @Override
- public T execute(RequestExecutor executor, String uri, E data) throws WxErrorException {
- int retryTimes = 0;
- do {
- try {
- return this.executeInternal(executor, uri, data);
- } catch (WxErrorException e) {
- if (retryTimes + 1 > this.maxRetryTimes) {
- this.log.warn("重试达到最大次数【{}】", this.maxRetryTimes);
- //最后一次重试失败后,直接抛出异常,不再等待
- throw new RuntimeException("微信服务端异常,超出重试次数");
- }
-
- WxError error = e.getError();
- /*
- * -1 系统繁忙, 1000ms后重试
- */
- if (error.getErrorCode() == -1) {
- int sleepMillis = this.retrySleepMillis * (1 << retryTimes);
- try {
- this.log.debug("微信系统繁忙,{} ms 后重试(第{}次)", sleepMillis, retryTimes + 1);
- Thread.sleep(sleepMillis);
- } catch (InterruptedException e1) {
- Thread.currentThread().interrupt();
- }
- } else {
- throw e;
- }
- }
- } while (retryTimes++ < this.maxRetryTimes);
-
- this.log.warn("重试达到最大次数【{}】", this.maxRetryTimes);
- throw new RuntimeException("微信服务端异常,超出重试次数");
- }
-
- protected T executeInternal(RequestExecutor executor, String uri, E data) throws WxErrorException {
- E dataForLog = DataUtils.handleDataWithSecret(data);
-
- if (uri.contains("suite_access_token=")) {
- throw new IllegalArgumentException("uri参数中不允许有suite_access_token: " + uri);
- }
- String suiteAccessToken = getSuiteAccessToken(false);
-
- String uriWithAccessToken = uri + (uri.contains("?") ? "&" : "?") + "suite_access_token=" + suiteAccessToken;
-
- try {
- T result = executor.execute(uriWithAccessToken, data);
- this.log.debug("\n【请求地址】: {}\n【请求参数】:{}\n【响应数据】:{}", uriWithAccessToken, dataForLog, result);
- return result;
- } catch (WxErrorException e) {
- WxError error = e.getError();
- /*
- * 发生以下情况时尝试刷新suite_access_token
- * 42009 suite_access_token已过期
- */
- if (error.getErrorCode() == 42009) {
- // 强制设置wxCpTpConfigStorage它的suite access token过期了,这样在下一次请求里就会刷新suite access token
- this.configStorage.expireSuiteAccessToken();
- return execute(executor, uri, data);
- }
-
- if (error.getErrorCode() != 0) {
- this.log.error("\n【请求地址】: {}\n【请求参数】:{}\n【错误信息】:{}", uriWithAccessToken, dataForLog, error);
- throw new WxErrorException(error, e);
- }
- return null;
- } catch (IOException e) {
- this.log.error("\n【请求地址】: {}\n【请求参数】:{}\n【异常信息】:{}", uriWithAccessToken, dataForLog, e.getMessage());
- throw new RuntimeException(e);
- }
- }
-
- @Override
- public void setWxCpTpConfigStorage(WxCpTpConfigStorage wxConfigProvider) {
- this.configStorage = wxConfigProvider;
- this.initHttp();
- }
-
- @Override
- public void setRetrySleepMillis(int retrySleepMillis) {
- this.retrySleepMillis = retrySleepMillis;
- }
-
-
- @Override
- public void setMaxRetryTimes(int maxRetryTimes) {
- this.maxRetryTimes = maxRetryTimes;
- }
-
- public File getTmpDirFile() {
- return this.tmpDirFile;
- }
-
- public void setTmpDirFile(File tmpDirFile) {
- this.tmpDirFile = tmpDirFile;
- }
-
- @Override
- public RequestHttp, ?> getRequestHttp() {
- return this;
- }
-
-}
+package me.chanjar.weixin.cp.api.impl;
+
+import java.io.File;
+import java.io.IOException;
+import java.util.HashMap;
+import java.util.Map;
+
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import com.google.common.base.Joiner;
+import com.google.gson.JsonObject;
+import com.google.gson.JsonParser;
+
+import me.chanjar.weixin.common.WxType;
+import me.chanjar.weixin.common.bean.WxAccessToken;
+import me.chanjar.weixin.common.error.WxError;
+import me.chanjar.weixin.common.error.WxErrorException;
+import me.chanjar.weixin.common.util.DataUtils;
+import me.chanjar.weixin.common.util.crypto.SHA1;
+import me.chanjar.weixin.common.util.http.RequestExecutor;
+import me.chanjar.weixin.common.util.http.RequestHttp;
+import me.chanjar.weixin.common.util.http.SimpleGetRequestExecutor;
+import me.chanjar.weixin.common.util.http.SimplePostRequestExecutor;
+import me.chanjar.weixin.cp.api.WxCpService;
+import me.chanjar.weixin.cp.api.WxCpTpService;
+import me.chanjar.weixin.cp.bean.WxCpMaJsCode2SessionResult;
+import me.chanjar.weixin.cp.bean.WxCpTpCorp;
+import me.chanjar.weixin.cp.config.WxCpTpConfigStorage;
+
+/**
+ * @author zhenjun cai
+ */
+public abstract class BaseWxCpTpServiceImpl implements WxCpTpService, RequestHttp {
+ protected final Logger log = LoggerFactory.getLogger(this.getClass());
+
+ /**
+ * 全局的是否正在刷新access token的锁
+ */
+ protected final Object globalSuiteAccessTokenRefreshLock = new Object();
+
+ /**
+ * 全局的是否正在刷新jsapi_ticket的锁
+ */
+ protected final Object globalSuiteTicketRefreshLock = new Object();
+
+
+ protected WxCpTpConfigStorage configStorage;
+
+
+ /**
+ * 临时文件目录
+ */
+ private File tmpDirFile;
+ private int retrySleepMillis = 1000;
+ private int maxRetryTimes = 5;
+
+ @Override
+ public boolean checkSignature(String msgSignature, String timestamp, String nonce, String data) {
+ try {
+ return SHA1.gen(this.configStorage.getToken(), timestamp, nonce, data)
+ .equals(msgSignature);
+ } catch (Exception e) {
+ this.log.error("Checking signature failed, and the reason is :" + e.getMessage());
+ return false;
+ }
+ }
+
+ @Override
+ public String getSuiteAccessToken() throws WxErrorException {
+ return getSuiteAccessToken(false);
+ }
+
+ @Override
+ public String getSuiteTicket() throws WxErrorException {
+ return getSuiteTicket(false);
+ }
+
+ @Override
+ public String getSuiteTicket(boolean forceRefresh) throws WxErrorException {
+// suite ticket由微信服务器推送,不能强制刷新
+// if (forceRefresh) {
+// this.configStorage.expireSuiteTicket();
+// }
+
+ if (this.configStorage.isSuiteTicketExpired()) {
+// 本地suite ticket 不存在或者过期
+ WxError wxError = WxError.fromJson("{\"errcode\":40085, \"errmsg\":\"invaild suite ticket\"}", WxType.CP);
+ throw new WxErrorException(wxError);
+ }
+ return this.configStorage.getSuiteTicket();
+ }
+
+
+ @Override
+ public WxCpMaJsCode2SessionResult jsCode2Session(String jsCode) throws WxErrorException {
+ Map params = new HashMap<>(2);
+ params.put("js_code", jsCode);
+ params.put("grant_type", "authorization_code");
+
+ String result = this.get(configStorage.getApiUrl(JSCODE_TO_SESSION_URL), Joiner.on("&").withKeyValueSeparator("=").join(params));
+ return WxCpMaJsCode2SessionResult.fromJson(result);
+ }
+
+
+ @Override
+ public WxAccessToken getCorpToken(String authCorpid, String permanentCode) throws WxErrorException {
+ JsonObject jsonObject = new JsonObject();
+ jsonObject.addProperty("auth_corpid", authCorpid);
+ jsonObject.addProperty("permanent_code", permanentCode);
+ String result = post(configStorage.getApiUrl(GET_CORP_TOKEN), jsonObject.toString());
+
+ return WxAccessToken.fromJson(result);
+ }
+
+
+ @Override
+ public WxCpTpCorp getPermanentCode(String authCode) throws WxErrorException {
+ JsonObject jsonObject = new JsonObject();
+ jsonObject.addProperty("auth_code", authCode);
+
+ String result = post(configStorage.getApiUrl(GET_PERMANENT_CODE), jsonObject.toString());
+ jsonObject = new JsonParser().parse(result).getAsJsonObject();
+ WxCpTpCorp wxCpTpCorp = WxCpTpCorp.fromJson(jsonObject.get("auth_corp_info").getAsString());
+ wxCpTpCorp.setPermanentCode(jsonObject.get("permanent_code").getAsString());
+ return wxCpTpCorp;
+ }
+
+ @Override
+ public String get(String url, String queryParam) throws WxErrorException {
+ return execute(SimpleGetRequestExecutor.create(this), url, queryParam);
+ }
+
+ @Override
+ public String post(String url, String postData) throws WxErrorException {
+ return execute(SimplePostRequestExecutor.create(this), url, postData);
+ }
+
+ /**
+ * 向微信端发送请求,在这里执行的策略是当发生access_token过期时才去刷新,然后重新执行请求,而不是全局定时请求.
+ */
+ @Override
+ public T execute(RequestExecutor executor, String uri, E data) throws WxErrorException {
+ int retryTimes = 0;
+ do {
+ try {
+ return this.executeInternal(executor, uri, data);
+ } catch (WxErrorException e) {
+ if (retryTimes + 1 > this.maxRetryTimes) {
+ this.log.warn("重试达到最大次数【{}】", this.maxRetryTimes);
+ //最后一次重试失败后,直接抛出异常,不再等待
+ throw new RuntimeException("微信服务端异常,超出重试次数");
+ }
+
+ WxError error = e.getError();
+ /*
+ * -1 系统繁忙, 1000ms后重试
+ */
+ if (error.getErrorCode() == -1) {
+ int sleepMillis = this.retrySleepMillis * (1 << retryTimes);
+ try {
+ this.log.debug("微信系统繁忙,{} ms 后重试(第{}次)", sleepMillis, retryTimes + 1);
+ Thread.sleep(sleepMillis);
+ } catch (InterruptedException e1) {
+ Thread.currentThread().interrupt();
+ }
+ } else {
+ throw e;
+ }
+ }
+ } while (retryTimes++ < this.maxRetryTimes);
+
+ this.log.warn("重试达到最大次数【{}】", this.maxRetryTimes);
+ throw new RuntimeException("微信服务端异常,超出重试次数");
+ }
+
+ protected T executeInternal(RequestExecutor executor, String uri, E data) throws WxErrorException {
+ E dataForLog = DataUtils.handleDataWithSecret(data);
+
+ if (uri.contains("suite_access_token=")) {
+ throw new IllegalArgumentException("uri参数中不允许有suite_access_token: " + uri);
+ }
+ String suiteAccessToken = getSuiteAccessToken(false);
+
+ String uriWithAccessToken = uri + (uri.contains("?") ? "&" : "?") + "suite_access_token=" + suiteAccessToken;
+
+ try {
+ T result = executor.execute(uriWithAccessToken, data);
+ this.log.debug("\n【请求地址】: {}\n【请求参数】:{}\n【响应数据】:{}", uriWithAccessToken, dataForLog, result);
+ return result;
+ } catch (WxErrorException e) {
+ WxError error = e.getError();
+ /*
+ * 发生以下情况时尝试刷新suite_access_token
+ * 42009 suite_access_token已过期
+ */
+ if (error.getErrorCode() == 42009) {
+ // 强制设置wxCpTpConfigStorage它的suite access token过期了,这样在下一次请求里就会刷新suite access token
+ this.configStorage.expireSuiteAccessToken();
+ return execute(executor, uri, data);
+ }
+
+ if (error.getErrorCode() != 0) {
+ this.log.error("\n【请求地址】: {}\n【请求参数】:{}\n【错误信息】:{}", uriWithAccessToken, dataForLog, error);
+ throw new WxErrorException(error, e);
+ }
+ return null;
+ } catch (IOException e) {
+ this.log.error("\n【请求地址】: {}\n【请求参数】:{}\n【异常信息】:{}", uriWithAccessToken, dataForLog, e.getMessage());
+ throw new RuntimeException(e);
+ }
+ }
+
+ @Override
+ public void setWxCpTpConfigStorage(WxCpTpConfigStorage wxConfigProvider) {
+ this.configStorage = wxConfigProvider;
+ this.initHttp();
+ }
+
+ @Override
+ public void setRetrySleepMillis(int retrySleepMillis) {
+ this.retrySleepMillis = retrySleepMillis;
+ }
+
+
+ @Override
+ public void setMaxRetryTimes(int maxRetryTimes) {
+ this.maxRetryTimes = maxRetryTimes;
+ }
+
+ public File getTmpDirFile() {
+ return this.tmpDirFile;
+ }
+
+ public void setTmpDirFile(File tmpDirFile) {
+ this.tmpDirFile = tmpDirFile;
+ }
+
+ @Override
+ public RequestHttp, ?> getRequestHttp() {
+ return this;
+ }
+
+}
diff --git a/weixin-java-cp/src/main/java/me/chanjar/weixin/cp/api/impl/WxCpAgentServiceImpl.java b/weixin-java-cp/src/main/java/me/chanjar/weixin/cp/api/impl/WxCpAgentServiceImpl.java
index 10c417729e..2dc5ca8755 100644
--- a/weixin-java-cp/src/main/java/me/chanjar/weixin/cp/api/impl/WxCpAgentServiceImpl.java
+++ b/weixin-java-cp/src/main/java/me/chanjar/weixin/cp/api/impl/WxCpAgentServiceImpl.java
@@ -1,11 +1,8 @@
package me.chanjar.weixin.cp.api.impl;
-import java.util.List;
-
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import com.google.gson.reflect.TypeToken;
-
import me.chanjar.weixin.common.error.WxError;
import me.chanjar.weixin.common.error.WxErrorException;
import me.chanjar.weixin.cp.api.WxCpAgentService;
@@ -13,6 +10,8 @@
import me.chanjar.weixin.cp.bean.WxCpAgent;
import me.chanjar.weixin.cp.util.json.WxCpGsonBuilder;
+import java.util.List;
+
/**
*
@@ -37,13 +36,14 @@ public WxCpAgent get(Integer agentId) throws WxErrorException {
throw new IllegalArgumentException("缺少agentid参数");
}
- String responseContent = this.mainService.get(String.format(WxCpAgentService.GET_AGENT, agentId), null);
+ String responseContent = this.mainService.get(String.format(this.mainService.getWxCpConfigStorage().getApiUrl(WxCpAgentService.GET_AGENT), agentId), null);
return WxCpAgent.fromJson(responseContent);
}
@Override
public void set(WxCpAgent agentInfo) throws WxErrorException {
- String responseContent = this.mainService.post(WxCpAgentService.AGENT_SET, agentInfo.toJson());
+ String url = this.mainService.getWxCpConfigStorage().getApiUrl(WxCpAgentService.AGENT_SET);
+ String responseContent = this.mainService.post(url, agentInfo.toJson());
JsonObject jsonObject = JSON_PARSER.parse(responseContent).getAsJsonObject();
if (jsonObject.get("errcode").getAsInt() != 0) {
throw new WxErrorException(WxError.fromJson(responseContent));
@@ -52,7 +52,8 @@ public void set(WxCpAgent agentInfo) throws WxErrorException {
@Override
public List list() throws WxErrorException {
- String responseContent = this.mainService.get(WxCpAgentService.AGENT_LIST, null);
+ String url = this.mainService.getWxCpConfigStorage().getApiUrl(WxCpAgentService.AGENT_LIST);
+ String responseContent = this.mainService.get(url, null);
JsonObject jsonObject = JSON_PARSER.parse(responseContent).getAsJsonObject();
if (jsonObject.get("errcode").getAsInt() != 0) {
throw new WxErrorException(WxError.fromJson(responseContent));
diff --git a/weixin-java-cp/src/main/java/me/chanjar/weixin/cp/api/impl/WxCpChatServiceImpl.java b/weixin-java-cp/src/main/java/me/chanjar/weixin/cp/api/impl/WxCpChatServiceImpl.java
index 0b1fb59381..1bf809502b 100644
--- a/weixin-java-cp/src/main/java/me/chanjar/weixin/cp/api/impl/WxCpChatServiceImpl.java
+++ b/weixin-java-cp/src/main/java/me/chanjar/weixin/cp/api/impl/WxCpChatServiceImpl.java
@@ -47,7 +47,7 @@ public String chatCreate(String name, String owner, List users, String c
if (StringUtils.isNotBlank(chatId)) {
data.put("chatid", chatId);
}
- String result = this.cpService.post(APPCHAT_CREATE, WxGsonBuilder.create().toJson(data));
+ String result = this.cpService.post(this.cpService.getWxCpConfigStorage().getApiUrl(APPCHAT_CREATE), WxGsonBuilder.create().toJson(data));
return new JsonParser().parse(result).getAsJsonObject().get("chatid").getAsString();
}
@@ -76,7 +76,7 @@ public void chatUpdate(String chatId, String name, String owner, List us
data.put("del_user_list", usersToDelete);
}
- this.cpService.post(APPCHAT_UPDATE, WxGsonBuilder.create().toJson(data));
+ this.cpService.post(this.cpService.getWxCpConfigStorage().getApiUrl(APPCHAT_UPDATE), WxGsonBuilder.create().toJson(data));
}
@Override
@@ -86,7 +86,7 @@ public void update(String chatId, String name, String owner, List usersT
@Override
public WxCpChat chatGet(String chatId) throws WxErrorException {
- String result = this.cpService.get(APPCHAT_GET_CHATID + chatId, null);
+ String result = this.cpService.get(this.cpService.getWxCpConfigStorage().getApiUrl(APPCHAT_GET_CHATID + chatId), null);
return WxCpGsonBuilder.create()
.fromJson(JSON_PARSER.parse(result).getAsJsonObject().getAsJsonObject("chat_info").toString(), WxCpChat.class);
}
@@ -98,7 +98,7 @@ public WxCpChat get(String chatId) throws WxErrorException {
@Override
public void sendMsg(WxCpAppChatMessage message) throws WxErrorException {
- this.cpService.post(WxCpChatService.APPCHAT_SEND, message.toJson());
+ this.cpService.post(this.cpService.getWxCpConfigStorage().getApiUrl(WxCpChatService.APPCHAT_SEND), message.toJson());
}
}
diff --git a/weixin-java-cp/src/main/java/me/chanjar/weixin/cp/api/impl/WxCpDepartmentServiceImpl.java b/weixin-java-cp/src/main/java/me/chanjar/weixin/cp/api/impl/WxCpDepartmentServiceImpl.java
index 09ded1b8b3..ce6f02ae5f 100644
--- a/weixin-java-cp/src/main/java/me/chanjar/weixin/cp/api/impl/WxCpDepartmentServiceImpl.java
+++ b/weixin-java-cp/src/main/java/me/chanjar/weixin/cp/api/impl/WxCpDepartmentServiceImpl.java
@@ -1,7 +1,5 @@
package me.chanjar.weixin.cp.api.impl;
-import java.util.List;
-
import com.google.gson.JsonElement;
import com.google.gson.JsonParser;
import com.google.gson.reflect.TypeToken;
@@ -12,6 +10,8 @@
import me.chanjar.weixin.cp.bean.WxCpDepart;
import me.chanjar.weixin.cp.util.json.WxCpGsonBuilder;
+import java.util.List;
+
/**
*
* 部门管理接口
@@ -29,25 +29,27 @@ public WxCpDepartmentServiceImpl(WxCpService mainService) {
@Override
public Long create(WxCpDepart depart) throws WxErrorException {
- String responseContent = this.mainService.post(WxCpDepartmentService.DEPARTMENT_CREATE, depart.toJson());
+ String url = this.mainService.getWxCpConfigStorage().getApiUrl(WxCpDepartmentService.DEPARTMENT_CREATE);
+ String responseContent = this.mainService.post(url, depart.toJson());
JsonElement tmpJsonElement = new JsonParser().parse(responseContent);
return GsonHelper.getAsLong(tmpJsonElement.getAsJsonObject().get("id"));
}
@Override
public void update(WxCpDepart group) throws WxErrorException {
- this.mainService.post(WxCpDepartmentService.DEPARTMENT_UPDATE, group.toJson());
+ String url = this.mainService.getWxCpConfigStorage().getApiUrl(WxCpDepartmentService.DEPARTMENT_UPDATE);
+ this.mainService.post(url, group.toJson());
}
@Override
public void delete(Long departId) throws WxErrorException {
- String url = String.format(WxCpDepartmentService.DEPARTMENT_DELETE, departId);
+ String url = String.format(this.mainService.getWxCpConfigStorage().getApiUrl(WxCpDepartmentService.DEPARTMENT_DELETE), departId);
this.mainService.get(url, null);
}
@Override
public List list(Long id) throws WxErrorException {
- String url = WxCpDepartmentService.DEPARTMENT_LIST;
+ String url = this.mainService.getWxCpConfigStorage().getApiUrl(WxCpDepartmentService.DEPARTMENT_LIST);
if (id != null) {
url += "?id=" + id;
}
diff --git a/weixin-java-cp/src/main/java/me/chanjar/weixin/cp/api/impl/WxCpMediaServiceImpl.java b/weixin-java-cp/src/main/java/me/chanjar/weixin/cp/api/impl/WxCpMediaServiceImpl.java
index 7a3dc444cf..fa31a033ba 100644
--- a/weixin-java-cp/src/main/java/me/chanjar/weixin/cp/api/impl/WxCpMediaServiceImpl.java
+++ b/weixin-java-cp/src/main/java/me/chanjar/weixin/cp/api/impl/WxCpMediaServiceImpl.java
@@ -1,18 +1,19 @@
package me.chanjar.weixin.cp.api.impl;
-import java.io.File;
-import java.io.IOException;
-import java.io.InputStream;
-import java.util.UUID;
-
import me.chanjar.weixin.common.bean.result.WxMediaUploadResult;
import me.chanjar.weixin.common.error.WxErrorException;
import me.chanjar.weixin.common.util.fs.FileUtils;
import me.chanjar.weixin.common.util.http.BaseMediaDownloadRequestExecutor;
import me.chanjar.weixin.common.util.http.MediaUploadRequestExecutor;
+import me.chanjar.weixin.cp.WxCpConsts;
import me.chanjar.weixin.cp.api.WxCpMediaService;
import me.chanjar.weixin.cp.api.WxCpService;
+import java.io.File;
+import java.io.IOException;
+import java.io.InputStream;
+import java.util.UUID;
+
/**
*
* 媒体管理接口.
@@ -37,7 +38,7 @@ public WxMediaUploadResult upload(String mediaType, String fileType, InputStream
@Override
public WxMediaUploadResult upload(String mediaType, File file) throws WxErrorException {
return this.mainService.execute(MediaUploadRequestExecutor.create(this.mainService.getRequestHttp()),
- MEDIA_UPLOAD_URL + mediaType, file);
+ this.mainService.getWxCpConfigStorage().getApiUrl(MEDIA_UPLOAD_URL + mediaType), file);
}
@Override
@@ -45,7 +46,7 @@ public File download(String mediaId) throws WxErrorException {
return this.mainService.execute(
BaseMediaDownloadRequestExecutor.create(this.mainService.getRequestHttp(),
this.mainService.getWxCpConfigStorage().getTmpDirFile()),
- MEDIA_GET_URL, "media_id=" + mediaId);
+ this.mainService.getWxCpConfigStorage().getApiUrl(MEDIA_GET_URL), "media_id=" + mediaId);
}
@Override
@@ -53,13 +54,13 @@ public File getJssdkFile(String mediaId) throws WxErrorException {
return this.mainService.execute(
BaseMediaDownloadRequestExecutor.create(this.mainService.getRequestHttp(),
this.mainService.getWxCpConfigStorage().getTmpDirFile()),
- JSSDK_MEDIA_GET_URL, "media_id=" + mediaId);
+ this.mainService.getWxCpConfigStorage().getApiUrl(JSSDK_MEDIA_GET_URL), "media_id=" + mediaId);
}
@Override
public String uploadImg(File file) throws WxErrorException {
final WxMediaUploadResult result = this.mainService
- .execute(MediaUploadRequestExecutor.create(this.mainService.getRequestHttp()), IMG_UPLOAD_URL, file);
+ .execute(MediaUploadRequestExecutor.create(this.mainService.getRequestHttp()), this.mainService.getWxCpConfigStorage().getApiUrl(IMG_UPLOAD_URL), file);
return result.getUrl();
}
}
diff --git a/weixin-java-cp/src/main/java/me/chanjar/weixin/cp/api/impl/WxCpMenuServiceImpl.java b/weixin-java-cp/src/main/java/me/chanjar/weixin/cp/api/impl/WxCpMenuServiceImpl.java
index d33ade0191..a03d600145 100644
--- a/weixin-java-cp/src/main/java/me/chanjar/weixin/cp/api/impl/WxCpMenuServiceImpl.java
+++ b/weixin-java-cp/src/main/java/me/chanjar/weixin/cp/api/impl/WxCpMenuServiceImpl.java
@@ -2,6 +2,7 @@
import me.chanjar.weixin.common.bean.menu.WxMenu;
import me.chanjar.weixin.common.error.WxErrorException;
+import me.chanjar.weixin.cp.WxCpConsts;
import me.chanjar.weixin.cp.api.WxCpMenuService;
import me.chanjar.weixin.cp.api.WxCpService;
import me.chanjar.weixin.cp.util.json.WxCpGsonBuilder;
@@ -28,7 +29,8 @@ public void create(WxMenu menu) throws WxErrorException {
@Override
public void create(Integer agentId, WxMenu menu) throws WxErrorException {
- this.mainService.post(String.format(WxCpMenuService.MENU_CREATE, agentId), menu.toJson());
+ String url = String.format(this.mainService.getWxCpConfigStorage().getApiUrl(WxCpMenuService.MENU_CREATE), agentId);
+ this.mainService.post(url, menu.toJson());
}
@Override
@@ -38,7 +40,7 @@ public void delete() throws WxErrorException {
@Override
public void delete(Integer agentId) throws WxErrorException {
- String url = String.format(WxCpMenuService.MENU_DELETE, agentId);
+ String url = String.format(this.mainService.getWxCpConfigStorage().getApiUrl(WxCpMenuService.MENU_DELETE), agentId);
this.mainService.get(url, null);
}
@@ -49,7 +51,7 @@ public WxMenu get() throws WxErrorException {
@Override
public WxMenu get(Integer agentId) throws WxErrorException {
- String url = String.format(WxCpMenuService.MENU_GET, agentId);
+ String url = String.format(this.mainService.getWxCpConfigStorage().getApiUrl(WxCpMenuService.MENU_GET), agentId);
try {
String resultContent = this.mainService.get(url, null);
return WxCpGsonBuilder.create().fromJson(resultContent, WxMenu.class);
diff --git a/weixin-java-cp/src/main/java/me/chanjar/weixin/cp/api/impl/WxCpOAuth2ServiceImpl.java b/weixin-java-cp/src/main/java/me/chanjar/weixin/cp/api/impl/WxCpOAuth2ServiceImpl.java
index 6ff91fc35a..aedc9ab24d 100644
--- a/weixin-java-cp/src/main/java/me/chanjar/weixin/cp/api/impl/WxCpOAuth2ServiceImpl.java
+++ b/weixin-java-cp/src/main/java/me/chanjar/weixin/cp/api/impl/WxCpOAuth2ServiceImpl.java
@@ -69,7 +69,7 @@ public WxCpOauth2UserInfo getUserInfo(String code) throws WxErrorException {
@Override
public WxCpOauth2UserInfo getUserInfo(Integer agentId, String code) throws WxErrorException {
- String responseText = this.mainService.get(String.format(WxCpOAuth2Service.URL_GET_USER_INFO, code, agentId), null);
+ String responseText = this.mainService.get(String.format(this.mainService.getWxCpConfigStorage().getApiUrl(WxCpOAuth2Service.URL_GET_USER_INFO), code, agentId), null);
JsonElement je = new JsonParser().parse(responseText);
JsonObject jo = je.getAsJsonObject();
@@ -86,7 +86,7 @@ public WxCpOauth2UserInfo getUserInfo(Integer agentId, String code) throws WxErr
public WxCpUserDetail getUserDetail(String userTicket) throws WxErrorException {
JsonObject param = new JsonObject();
param.addProperty("user_ticket", userTicket);
- String responseText = this.mainService.post(WxCpOAuth2Service.URL_GET_USER_DETAIL, param.toString());
+ String responseText = this.mainService.post(this.mainService.getWxCpConfigStorage().getApiUrl(WxCpOAuth2Service.URL_GET_USER_DETAIL), param.toString());
return WxCpGsonBuilder.create().fromJson(responseText, WxCpUserDetail.class);
}
}
diff --git a/weixin-java-cp/src/main/java/me/chanjar/weixin/cp/api/impl/WxCpOaServiceImpl.java b/weixin-java-cp/src/main/java/me/chanjar/weixin/cp/api/impl/WxCpOaServiceImpl.java
index cc3b274495..c0f6ddaab6 100644
--- a/weixin-java-cp/src/main/java/me/chanjar/weixin/cp/api/impl/WxCpOaServiceImpl.java
+++ b/weixin-java-cp/src/main/java/me/chanjar/weixin/cp/api/impl/WxCpOaServiceImpl.java
@@ -59,7 +59,7 @@ public List getCheckinData(Integer openCheckinDataType, Date st
jsonObject.add("useridlist", jsonArray);
- String responseContent = this.mainService.post(WxCpOaService.GET_CHECKIN_DATA, jsonObject.toString());
+ String responseContent = this.mainService.post(this.mainService.getWxCpConfigStorage().getApiUrl(WxCpOaService.GET_CHECKIN_DATA), jsonObject.toString());
JsonElement tmpJsonElement = new JsonParser().parse(responseContent);
return WxCpGsonBuilder.create()
.fromJson(
@@ -88,7 +88,7 @@ public List getCheckinOption(Date datetime, List user
jsonObject.addProperty("datetime", datetime.getTime() / 1000L);
jsonObject.add("useridlist", jsonArray);
- String responseContent = this.mainService.post(WxCpOaService.GET_CHECKIN_OPTION, jsonObject.toString());
+ String responseContent = this.mainService.post(this.mainService.getWxCpConfigStorage().getApiUrl(WxCpOaService.GET_CHECKIN_OPTION), jsonObject.toString());
JsonElement tmpJsonElement = new JsonParser().parse(responseContent);
return WxCpGsonBuilder.create()
@@ -108,7 +108,7 @@ public WxCpApprovalDataResult getApprovalData(Date startTime, Date endTime, Long
jsonObject.addProperty("next_spnum", nextSpnum);
}
- String responseContent = this.mainService.post(WxCpOaService.GET_APPROVAL_DATA, jsonObject.toString());
+ String responseContent = this.mainService.post(this.mainService.getWxCpConfigStorage().getApiUrl(WxCpOaService.GET_APPROVAL_DATA), jsonObject.toString());
return WxCpGsonBuilder.create().fromJson(responseContent, WxCpApprovalDataResult.class);
}
@@ -140,7 +140,7 @@ public List getDialRecord(Date startTime, Date endTime, Integer
jsonObject.addProperty("end_time", endtimestamp);
}
- String responseContent = this.mainService.post(WxCpOaService.GET_DIAL_RECORD, jsonObject.toString());
+ String responseContent = this.mainService.post(this.mainService.getWxCpConfigStorage().getApiUrl(WxCpOaService.GET_DIAL_RECORD), jsonObject.toString());
JsonElement tmpJsonElement = new JsonParser().parse(responseContent);
return WxCpGsonBuilder.create()
diff --git a/weixin-java-cp/src/main/java/me/chanjar/weixin/cp/api/impl/WxCpServiceApacheHttpClientImpl.java b/weixin-java-cp/src/main/java/me/chanjar/weixin/cp/api/impl/WxCpServiceApacheHttpClientImpl.java
index 11a183b507..6dea258b99 100644
--- a/weixin-java-cp/src/main/java/me/chanjar/weixin/cp/api/impl/WxCpServiceApacheHttpClientImpl.java
+++ b/weixin-java-cp/src/main/java/me/chanjar/weixin/cp/api/impl/WxCpServiceApacheHttpClientImpl.java
@@ -48,7 +48,8 @@ public String getAccessToken(boolean forceRefresh) throws WxErrorException {
}
synchronized (this.globalAccessTokenRefreshLock) {
- String url = String.format(WxCpService.GET_TOKEN, this.configStorage.getCorpId(), this.configStorage.getCorpSecret());
+ String url = String.format(this.configStorage.getApiUrl(WxCpService.GET_TOKEN), this.configStorage.getCorpId(), this.configStorage.getCorpSecret());
+
try {
HttpGet httpGet = new HttpGet(url);
if (this.httpProxy != null) {
diff --git a/weixin-java-cp/src/main/java/me/chanjar/weixin/cp/api/impl/WxCpServiceJoddHttpImpl.java b/weixin-java-cp/src/main/java/me/chanjar/weixin/cp/api/impl/WxCpServiceJoddHttpImpl.java
index d5d08900b9..f1c5c8419a 100644
--- a/weixin-java-cp/src/main/java/me/chanjar/weixin/cp/api/impl/WxCpServiceJoddHttpImpl.java
+++ b/weixin-java-cp/src/main/java/me/chanjar/weixin/cp/api/impl/WxCpServiceJoddHttpImpl.java
@@ -1,6 +1,10 @@
package me.chanjar.weixin.cp.api.impl;
-import jodd.http.*;
+import jodd.http.HttpConnectionProvider;
+import jodd.http.HttpRequest;
+import jodd.http.HttpResponse;
+import jodd.http.JoddHttp;
+import jodd.http.ProxyInfo;
import me.chanjar.weixin.common.WxType;
import me.chanjar.weixin.common.bean.WxAccessToken;
import me.chanjar.weixin.common.error.WxError;
@@ -39,7 +43,7 @@ public String getAccessToken(boolean forceRefresh) throws WxErrorException {
}
synchronized (this.globalAccessTokenRefreshLock) {
- HttpRequest request = HttpRequest.get(String.format(WxCpService.GET_TOKEN, this.configStorage.getCorpId(), this.configStorage.getCorpSecret()));
+ HttpRequest request = HttpRequest.get(String.format(this.configStorage.getApiUrl(WxCpService.GET_TOKEN), this.configStorage.getCorpId(), this.configStorage.getCorpSecret()));
if (this.httpProxy != null) {
httpClient.useProxy(this.httpProxy);
}
diff --git a/weixin-java-cp/src/main/java/me/chanjar/weixin/cp/api/impl/WxCpServiceOkHttpImpl.java b/weixin-java-cp/src/main/java/me/chanjar/weixin/cp/api/impl/WxCpServiceOkHttpImpl.java
index f4cc540f3c..4280174dc4 100644
--- a/weixin-java-cp/src/main/java/me/chanjar/weixin/cp/api/impl/WxCpServiceOkHttpImpl.java
+++ b/weixin-java-cp/src/main/java/me/chanjar/weixin/cp/api/impl/WxCpServiceOkHttpImpl.java
@@ -8,7 +8,12 @@
import me.chanjar.weixin.common.util.http.okhttp.OkHttpProxyInfo;
import me.chanjar.weixin.cp.api.WxCpService;
import me.chanjar.weixin.cp.config.WxCpConfigStorage;
-import okhttp3.*;
+import okhttp3.Authenticator;
+import okhttp3.Credentials;
+import okhttp3.OkHttpClient;
+import okhttp3.Request;
+import okhttp3.Response;
+import okhttp3.Route;
import java.io.IOException;
@@ -45,7 +50,7 @@ public String getAccessToken(boolean forceRefresh) throws WxErrorException {
OkHttpClient client = getRequestHttpClient();
//请求的request
Request request = new Request.Builder()
- .url(String.format(WxCpService.GET_TOKEN, this.configStorage.getCorpId(), this.configStorage.getCorpSecret()))
+ .url(String.format(this.configStorage.getApiUrl(WxCpService.GET_TOKEN), this.configStorage.getCorpId(), this.configStorage.getCorpSecret()))
.get()
.build();
String resultContent = null;
diff --git a/weixin-java-cp/src/main/java/me/chanjar/weixin/cp/api/impl/WxCpTagServiceImpl.java b/weixin-java-cp/src/main/java/me/chanjar/weixin/cp/api/impl/WxCpTagServiceImpl.java
index 413436207f..ee0db6da9f 100644
--- a/weixin-java-cp/src/main/java/me/chanjar/weixin/cp/api/impl/WxCpTagServiceImpl.java
+++ b/weixin-java-cp/src/main/java/me/chanjar/weixin/cp/api/impl/WxCpTagServiceImpl.java
@@ -1,6 +1,10 @@
package me.chanjar.weixin.cp.api.impl;
-import com.google.gson.*;
+import com.google.gson.JsonArray;
+import com.google.gson.JsonElement;
+import com.google.gson.JsonObject;
+import com.google.gson.JsonParser;
+import com.google.gson.JsonPrimitive;
import com.google.gson.reflect.TypeToken;
import me.chanjar.weixin.common.error.WxErrorException;
import me.chanjar.weixin.cp.api.WxCpService;
@@ -32,27 +36,31 @@ public WxCpTagServiceImpl(WxCpService mainService) {
public String create(String tagName) throws WxErrorException {
JsonObject o = new JsonObject();
o.addProperty("tagname", tagName);
- String responseContent = this.mainService.post(WxCpTagService.TAG_CREATE, o.toString());
+ String url = this.mainService.getWxCpConfigStorage().getApiUrl(WxCpTagService.TAG_CREATE);
+ String responseContent = this.mainService.post(url, o.toString());
JsonElement tmpJsonElement = new JsonParser().parse(responseContent);
return tmpJsonElement.getAsJsonObject().get("tagid").getAsString();
}
@Override
public void update(String tagId, String tagName) throws WxErrorException {
+ String url = this.mainService.getWxCpConfigStorage().getApiUrl(WxCpTagService.TAG_UPDATE);
JsonObject o = new JsonObject();
o.addProperty("tagid", tagId);
o.addProperty("tagname", tagName);
- this.mainService.post(WxCpTagService.TAG_UPDATE, o.toString());
+ this.mainService.post(url, o.toString());
}
@Override
public void delete(String tagId) throws WxErrorException {
- this.mainService.get(String.format(WxCpTagService.TAG_DELETE, tagId), null);
+ String url = String.format(this.mainService.getWxCpConfigStorage().getApiUrl(WxCpTagService.TAG_DELETE), tagId);
+ this.mainService.get(url, null);
}
@Override
public List listAll() throws WxErrorException {
- String responseContent = this.mainService.get(WxCpTagService.TAG_LIST, null);
+ String url = this.mainService.getWxCpConfigStorage().getApiUrl(WxCpTagService.TAG_LIST);
+ String responseContent = this.mainService.get(url, null);
JsonElement tmpJsonElement = new JsonParser().parse(responseContent);
return WxCpGsonBuilder.create()
.fromJson(
@@ -64,7 +72,8 @@ public List listAll() throws WxErrorException {
@Override
public List listUsersByTagId(String tagId) throws WxErrorException {
- String responseContent = this.mainService.get(String.format(WxCpTagService.TAG_GET, tagId), null);
+ String url = String.format(this.mainService.getWxCpConfigStorage().getApiUrl(WxCpTagService.TAG_GET), tagId);
+ String responseContent = this.mainService.get(url, null);
JsonElement tmpJsonElement = new JsonParser().parse(responseContent);
return WxCpGsonBuilder.create()
.fromJson(
@@ -76,20 +85,22 @@ public List listUsersByTagId(String tagId) throws WxErrorException {
@Override
public WxCpTagAddOrRemoveUsersResult addUsers2Tag(String tagId, List userIds, List partyIds) throws WxErrorException {
+ String url = this.mainService.getWxCpConfigStorage().getApiUrl(WxCpTagService.TAG_ADDTAGUSERS);
JsonObject jsonObject = new JsonObject();
jsonObject.addProperty("tagid", tagId);
this.addUserIdsAndPartyIdsToJson(userIds, partyIds, jsonObject);
- return WxCpTagAddOrRemoveUsersResult.fromJson(this.mainService.post(WxCpTagService.TAG_ADDTAGUSERS, jsonObject.toString()));
+ return WxCpTagAddOrRemoveUsersResult.fromJson(this.mainService.post(url, jsonObject.toString()));
}
@Override
public WxCpTagAddOrRemoveUsersResult removeUsersFromTag(String tagId, List userIds, List partyIds) throws WxErrorException {
+ String url = this.mainService.getWxCpConfigStorage().getApiUrl(WxCpTagService.TAG_DELTAGUSERS);
JsonObject jsonObject = new JsonObject();
jsonObject.addProperty("tagid", tagId);
this.addUserIdsAndPartyIdsToJson(userIds, partyIds, jsonObject);
- return WxCpTagAddOrRemoveUsersResult.fromJson(this.mainService.post(WxCpTagService.TAG_DELTAGUSERS, jsonObject.toString()));
+ return WxCpTagAddOrRemoveUsersResult.fromJson(this.mainService.post(url, jsonObject.toString()));
}
private void addUserIdsAndPartyIdsToJson(List userIds, List partyIds, JsonObject jsonObject) {
@@ -116,7 +127,8 @@ public WxCpTagGetResult get(String tagId) throws WxErrorException {
throw new IllegalArgumentException("缺少tagId参数");
}
- String responseContent = this.mainService.get(String.format(WxCpTagService.TAG_GET, tagId), null);
+ String url = String.format(this.mainService.getWxCpConfigStorage().getApiUrl(WxCpTagService.TAG_GET), tagId);
+ String responseContent = this.mainService.get(url, null);
return WxCpTagGetResult.fromJson(responseContent);
}
}
diff --git a/weixin-java-cp/src/main/java/me/chanjar/weixin/cp/api/impl/WxCpTaskCardServiceImpl.java b/weixin-java-cp/src/main/java/me/chanjar/weixin/cp/api/impl/WxCpTaskCardServiceImpl.java
index e70a7d376a..a2efabb497 100644
--- a/weixin-java-cp/src/main/java/me/chanjar/weixin/cp/api/impl/WxCpTaskCardServiceImpl.java
+++ b/weixin-java-cp/src/main/java/me/chanjar/weixin/cp/api/impl/WxCpTaskCardServiceImpl.java
@@ -3,6 +3,7 @@
import lombok.RequiredArgsConstructor;
import me.chanjar.weixin.common.error.WxErrorException;
import me.chanjar.weixin.common.util.json.WxGsonBuilder;
+import me.chanjar.weixin.cp.WxCpConsts;
import me.chanjar.weixin.cp.api.WxCpService;
import me.chanjar.weixin.cp.api.WxCpTaskCardService;
@@ -33,6 +34,7 @@ public void update(List userIds, String taskId, String clickedKey) throw
data.put("task_id", taskId);
data.put("clicked_key", clickedKey);
- this.mainService.post(MESSAGE_UPDATE_TASKCARD, WxGsonBuilder.create().toJson(data));
+ String url = this.mainService.getWxCpConfigStorage().getApiUrl(MESSAGE_UPDATE_TASKCARD);
+ this.mainService.post(url, WxGsonBuilder.create().toJson(data));
}
}
diff --git a/weixin-java-cp/src/main/java/me/chanjar/weixin/cp/api/impl/WxCpTpServiceApacheHttpClientImpl.java b/weixin-java-cp/src/main/java/me/chanjar/weixin/cp/api/impl/WxCpTpServiceApacheHttpClientImpl.java
index f673a622bd..04bc0a5f6e 100644
--- a/weixin-java-cp/src/main/java/me/chanjar/weixin/cp/api/impl/WxCpTpServiceApacheHttpClientImpl.java
+++ b/weixin-java-cp/src/main/java/me/chanjar/weixin/cp/api/impl/WxCpTpServiceApacheHttpClientImpl.java
@@ -52,7 +52,7 @@ public String getSuiteAccessToken(boolean forceRefresh) throws WxErrorException
synchronized (this.globalSuiteAccessTokenRefreshLock) {
try {
- HttpPost httpPost = new HttpPost(WxCpTpService.GET_SUITE_TOKEN);
+ HttpPost httpPost = new HttpPost(configStorage.getApiUrl(WxCpTpService.GET_SUITE_TOKEN));
if (this.httpProxy != null) {
RequestConfig config = RequestConfig.custom()
.setProxy(this.httpProxy).build();
diff --git a/weixin-java-cp/src/main/java/me/chanjar/weixin/cp/api/impl/WxCpUserServiceImpl.java b/weixin-java-cp/src/main/java/me/chanjar/weixin/cp/api/impl/WxCpUserServiceImpl.java
index b3b0909522..c38a680125 100644
--- a/weixin-java-cp/src/main/java/me/chanjar/weixin/cp/api/impl/WxCpUserServiceImpl.java
+++ b/weixin-java-cp/src/main/java/me/chanjar/weixin/cp/api/impl/WxCpUserServiceImpl.java
@@ -1,8 +1,5 @@
package me.chanjar.weixin.cp.api.impl;
-import java.util.List;
-import java.util.Map;
-
import com.google.common.collect.Maps;
import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
@@ -11,6 +8,7 @@
import com.google.gson.JsonPrimitive;
import com.google.gson.reflect.TypeToken;
import me.chanjar.weixin.common.error.WxErrorException;
+import me.chanjar.weixin.cp.WxCpConsts;
import me.chanjar.weixin.cp.api.WxCpService;
import me.chanjar.weixin.cp.api.WxCpUserService;
import me.chanjar.weixin.cp.bean.WxCpInviteResult;
@@ -18,6 +16,9 @@
import me.chanjar.weixin.cp.bean.WxCpUserExternalContactInfo;
import me.chanjar.weixin.cp.util.json.WxCpGsonBuilder;
+import java.util.List;
+import java.util.Map;
+
/**
*
* Created by BinaryWang on 2017/6/24.
@@ -34,23 +35,26 @@ public WxCpUserServiceImpl(WxCpService mainService) {
@Override
public void authenticate(String userId) throws WxErrorException {
- this.mainService.get(WxCpUserService.URL_AUTHENTICATE + userId, null);
+ this.mainService.get(this.mainService.getWxCpConfigStorage().getApiUrl(WxCpUserService.URL_AUTHENTICATE + userId), null);
}
@Override
public void create(WxCpUser user) throws WxErrorException {
- this.mainService.post(WxCpUserService.URL_USER_CREATE, user.toJson());
+ String url = this.mainService.getWxCpConfigStorage().getApiUrl(WxCpUserService.URL_USER_CREATE);
+ this.mainService.post(url, user.toJson());
}
@Override
public void update(WxCpUser user) throws WxErrorException {
- this.mainService.post(WxCpUserService.URL_USER_UPDATE, user.toJson());
+ String url = this.mainService.getWxCpConfigStorage().getApiUrl(WxCpUserService.URL_USER_UPDATE);
+ this.mainService.post(url, user.toJson());
}
@Override
public void delete(String... userIds) throws WxErrorException {
if (userIds.length == 1) {
- this.mainService.get(WxCpUserService.URL_USER_DELETE + userIds[0], null);
+ String url = this.mainService.getWxCpConfigStorage().getApiUrl(WxCpUserService.URL_USER_DELETE + userIds[0]);
+ this.mainService.get(url, null);
return;
}
@@ -66,7 +70,8 @@ public void delete(String... userIds) throws WxErrorException {
@Override
public WxCpUser getById(String userid) throws WxErrorException {
- String responseContent = this.mainService.get(WxCpUserService.URL_USER_GET + userid, null);
+ String url = this.mainService.getWxCpConfigStorage().getApiUrl(WxCpUserService.URL_USER_GET + userid);
+ String responseContent = this.mainService.get(url, null);
return WxCpUser.fromJson(responseContent);
}
@@ -82,7 +87,8 @@ public List listByDepartment(Long departId, Boolean fetchChild, Intege
params += "&status=0";
}
- String responseContent = this.mainService.get(WxCpUserService.URL_USER_LIST + departId, params);
+ String url = this.mainService.getWxCpConfigStorage().getApiUrl(WxCpUserService.URL_USER_LIST + departId);
+ String responseContent = this.mainService.get(url, params);
JsonElement tmpJsonElement = new JsonParser().parse(responseContent);
return WxCpGsonBuilder.create()
.fromJson(tmpJsonElement.getAsJsonObject().get("userlist"),
@@ -103,7 +109,8 @@ public List listSimpleByDepartment(Long departId, Boolean fetchChild,
params += "&status=0";
}
- String responseContent = this.mainService.get(WxCpUserService.URL_USER_SIMPLE_LIST + departId, params);
+ String url = this.mainService.getWxCpConfigStorage().getApiUrl(WxCpUserService.URL_USER_SIMPLE_LIST + departId);
+ String responseContent = this.mainService.get(url, params);
JsonElement tmpJsonElement = new JsonParser().parse(responseContent);
return WxCpGsonBuilder.create()
.fromJson(
@@ -140,18 +147,20 @@ public WxCpInviteResult invite(List userIds, List partyIds, List
jsonObject.add("tag", jsonArray);
}
- return WxCpInviteResult.fromJson(this.mainService.post(WxCpUserService.URL_BATCH_INVITE, jsonObject.toString()));
+ String url = this.mainService.getWxCpConfigStorage().getApiUrl(WxCpUserService.URL_BATCH_INVITE);
+ return WxCpInviteResult.fromJson(this.mainService.post(url, jsonObject.toString()));
}
@Override
public Map userId2Openid(String userId, Integer agentId) throws WxErrorException {
+ String url = this.mainService.getWxCpConfigStorage().getApiUrl(WxCpUserService.URL_CONVERT_TO_OPENID);
JsonObject jsonObject = new JsonObject();
jsonObject.addProperty("userid", userId);
if (agentId != null) {
jsonObject.addProperty("agentid", agentId);
}
- String responseContent = this.mainService.post(WxCpUserService.URL_CONVERT_TO_OPENID, jsonObject.toString());
+ String responseContent = this.mainService.post(url, jsonObject.toString());
JsonElement tmpJsonElement = new JsonParser().parse(responseContent);
Map result = Maps.newHashMap();
if (tmpJsonElement.getAsJsonObject().get("openid") != null) {
@@ -169,14 +178,16 @@ public Map userId2Openid(String userId, Integer agentId) throws
public String openid2UserId(String openid) throws WxErrorException {
JsonObject jsonObject = new JsonObject();
jsonObject.addProperty("openid", openid);
- String responseContent = this.mainService.post(WxCpUserService.URL_CONVERT_TO_USERID, jsonObject.toString());
+ String url = this.mainService.getWxCpConfigStorage().getApiUrl(WxCpUserService.URL_CONVERT_TO_USERID);
+ String responseContent = this.mainService.post(url, jsonObject.toString());
JsonElement tmpJsonElement = new JsonParser().parse(responseContent);
return tmpJsonElement.getAsJsonObject().get("userid").getAsString();
}
@Override
public WxCpUserExternalContactInfo getExternalContact(String userId) throws WxErrorException {
- String responseContent = this.mainService.get(WxCpUserService.URL_GET_EXTERNAL_CONTACT + userId, null);
+ String url = this.mainService.getWxCpConfigStorage().getApiUrl(WxCpUserService.URL_GET_EXTERNAL_CONTACT + userId);
+ String responseContent = this.mainService.get(url, null);
return WxCpUserExternalContactInfo.fromJson(responseContent);
}
}
diff --git a/weixin-java-cp/src/main/java/me/chanjar/weixin/cp/config/WxCpConfigStorage.java b/weixin-java-cp/src/main/java/me/chanjar/weixin/cp/config/WxCpConfigStorage.java
index e13738142f..a75ad1dfb1 100644
--- a/weixin-java-cp/src/main/java/me/chanjar/weixin/cp/config/WxCpConfigStorage.java
+++ b/weixin-java-cp/src/main/java/me/chanjar/weixin/cp/config/WxCpConfigStorage.java
@@ -12,6 +12,22 @@
*/
public interface WxCpConfigStorage {
+ /**
+ * 设置企业微信服务器 baseUrl.
+ *
+ * 默认值是 https://qyapi.weixin.qq.com , 如果使用默认值,则不需要调用 setBaseApiUrl
+ *
+ * @param baseUrl 企业微信服务器 Url
+ */
+ void setBaseApiUrl(String baseUrl);
+
+ /**
+ * 读取企业微信 API Url.
+ *
+ * 支持私有化企业微信服务器.
+ */
+ String getApiUrl(String path);
+
String getAccessToken();
boolean isAccessTokenExpired();
diff --git a/weixin-java-cp/src/main/java/me/chanjar/weixin/cp/config/WxCpInMemoryConfigStorage.java b/weixin-java-cp/src/main/java/me/chanjar/weixin/cp/config/WxCpInMemoryConfigStorage.java
index a501edeb6e..31e2a211d5 100644
--- a/weixin-java-cp/src/main/java/me/chanjar/weixin/cp/config/WxCpInMemoryConfigStorage.java
+++ b/weixin-java-cp/src/main/java/me/chanjar/weixin/cp/config/WxCpInMemoryConfigStorage.java
@@ -39,6 +39,21 @@ public class WxCpInMemoryConfigStorage implements WxCpConfigStorage {
private volatile ApacheHttpClientBuilder apacheHttpClientBuilder;
+ protected volatile String baseApiUrl;
+
+ @Override
+ public void setBaseApiUrl(String baseUrl) {
+ this.baseApiUrl = baseUrl;
+ }
+
+ @Override
+ public String getApiUrl(String path) {
+ if (baseApiUrl == null) {
+ baseApiUrl = "https://qyapi.weixin.qq.com";
+ }
+ return baseApiUrl + path;
+ }
+
@Override
public String getAccessToken() {
return this.accessToken;
diff --git a/weixin-java-cp/src/main/java/me/chanjar/weixin/cp/config/WxCpJedisConfigStorage.java b/weixin-java-cp/src/main/java/me/chanjar/weixin/cp/config/WxCpJedisConfigStorage.java
index 7c72de0e1b..b8deef1d51 100644
--- a/weixin-java-cp/src/main/java/me/chanjar/weixin/cp/config/WxCpJedisConfigStorage.java
+++ b/weixin-java-cp/src/main/java/me/chanjar/weixin/cp/config/WxCpJedisConfigStorage.java
@@ -38,6 +38,21 @@ public class WxCpJedisConfigStorage implements WxCpConfigStorage {
private volatile File tmpDirFile;
private volatile ApacheHttpClientBuilder apacheHttpClientBuilder;
+ protected volatile String baseApiUrl;
+
+ @Override
+ public void setBaseApiUrl(String baseUrl) {
+ this.baseApiUrl = baseUrl;
+ }
+
+ @Override
+ public String getApiUrl(String path) {
+ if (baseApiUrl == null) {
+ baseApiUrl = "https://qyapi.weixin.qq.com";
+ }
+ return baseApiUrl + path;
+ }
+
public WxCpJedisConfigStorage(JedisPool jedisPool) {
this.jedisPool = jedisPool;
}
diff --git a/weixin-java-cp/src/main/java/me/chanjar/weixin/cp/config/WxCpTpConfigStorage.java b/weixin-java-cp/src/main/java/me/chanjar/weixin/cp/config/WxCpTpConfigStorage.java
index a132dd1024..44ef4e17be 100644
--- a/weixin-java-cp/src/main/java/me/chanjar/weixin/cp/config/WxCpTpConfigStorage.java
+++ b/weixin-java-cp/src/main/java/me/chanjar/weixin/cp/config/WxCpTpConfigStorage.java
@@ -1,72 +1,88 @@
-package me.chanjar.weixin.cp.config;
-
-import me.chanjar.weixin.common.bean.WxAccessToken;
-import me.chanjar.weixin.common.util.http.apache.ApacheHttpClientBuilder;
-
-import java.io.File;
-
-/**
- * 微信客户端(第三方应用)配置存储
- *
- * @author zhenjun cai
- */
-public interface WxCpTpConfigStorage {
-
- String getSuiteAccessToken();
-
- boolean isSuiteAccessTokenExpired();
-
- /**
- * 强制将suite access token过期掉
- */
- void expireSuiteAccessToken();
-
- void updateSuiteAccessToken(WxAccessToken suiteAccessToken);
-
- void updateSuiteAccessToken(String suiteAccessToken, int expiresIn);
-
- String getSuiteTicket();
-
- boolean isSuiteTicketExpired();
-
- /**
- * 强制将suite ticket过期掉
- */
- void expireSuiteTicket();
-
- /**
- * 应该是线程安全的
- */
- void updateSuiteTicket(String suiteTicket, int expiresInSeconds);
-
- String getCorpId();
-
- String getCorpSecret();
-
- String getSuiteId();
-
- String getSuiteSecret();
-
- String getToken();
-
- String getAesKey();
-
- long getExpiresTime();
-
- String getHttpProxyHost();
-
- int getHttpProxyPort();
-
- String getHttpProxyUsername();
-
- String getHttpProxyPassword();
-
- File getTmpDirFile();
-
- /**
- * http client builder
- *
- * @return ApacheHttpClientBuilder
- */
- ApacheHttpClientBuilder getApacheHttpClientBuilder();
-}
+package me.chanjar.weixin.cp.config;
+
+import me.chanjar.weixin.common.bean.WxAccessToken;
+import me.chanjar.weixin.common.util.http.apache.ApacheHttpClientBuilder;
+
+import java.io.File;
+
+/**
+ * 微信客户端(第三方应用)配置存储
+ *
+ * @author zhenjun cai
+ */
+public interface WxCpTpConfigStorage {
+
+ /**
+ * 设置企业微信服务器 baseUrl.
+ *
+ * 默认值是 https://qyapi.weixin.qq.com , 如果使用默认值,则不需要调用 setBaseApiUrl
+ *
+ * @param baseUrl 企业微信服务器 Url
+ */
+ void setBaseApiUrl(String baseUrl);
+
+ /**
+ * 读取企业微信 API Url.
+ *
+ * 支持私有化企业微信服务器.
+ */
+ String getApiUrl(String path);
+
+ String getSuiteAccessToken();
+
+ boolean isSuiteAccessTokenExpired();
+
+ /**
+ * 强制将suite access token过期掉
+ */
+ void expireSuiteAccessToken();
+
+ void updateSuiteAccessToken(WxAccessToken suiteAccessToken);
+
+ void updateSuiteAccessToken(String suiteAccessToken, int expiresIn);
+
+ String getSuiteTicket();
+
+ boolean isSuiteTicketExpired();
+
+ /**
+ * 强制将suite ticket过期掉
+ */
+ void expireSuiteTicket();
+
+ /**
+ * 应该是线程安全的
+ */
+ void updateSuiteTicket(String suiteTicket, int expiresInSeconds);
+
+ String getCorpId();
+
+ String getCorpSecret();
+
+ String getSuiteId();
+
+ String getSuiteSecret();
+
+ String getToken();
+
+ String getAesKey();
+
+ long getExpiresTime();
+
+ String getHttpProxyHost();
+
+ int getHttpProxyPort();
+
+ String getHttpProxyUsername();
+
+ String getHttpProxyPassword();
+
+ File getTmpDirFile();
+
+ /**
+ * http client builder
+ *
+ * @return ApacheHttpClientBuilder
+ */
+ ApacheHttpClientBuilder getApacheHttpClientBuilder();
+}
diff --git a/weixin-java-cp/src/main/java/me/chanjar/weixin/cp/config/WxCpTpInMemoryConfigStorage.java b/weixin-java-cp/src/main/java/me/chanjar/weixin/cp/config/WxCpTpInMemoryConfigStorage.java
index c135c95b7f..6da47f5b72 100644
--- a/weixin-java-cp/src/main/java/me/chanjar/weixin/cp/config/WxCpTpInMemoryConfigStorage.java
+++ b/weixin-java-cp/src/main/java/me/chanjar/weixin/cp/config/WxCpTpInMemoryConfigStorage.java
@@ -1,230 +1,245 @@
-package me.chanjar.weixin.cp.config;
-
-import me.chanjar.weixin.common.bean.WxAccessToken;
-import me.chanjar.weixin.common.util.http.apache.ApacheHttpClientBuilder;
-import me.chanjar.weixin.cp.util.json.WxCpGsonBuilder;
-
-import java.io.File;
-
-/**
- * 基于内存的微信配置provider,在实际生产环境中应该将这些配置持久化
- *
- * @author Daniel Qian
- */
-public class WxCpTpInMemoryConfigStorage implements WxCpTpConfigStorage {
- protected volatile String corpId;
- protected volatile String corpSecret;
-
- protected volatile String suiteId;
- protected volatile String suiteSecret;
-
- protected volatile String token;
- protected volatile String suiteAccessToken;
- protected volatile String aesKey;
- protected volatile long expiresTime;
-
- protected volatile String oauth2redirectUri;
-
- protected volatile String httpProxyHost;
- protected volatile int httpProxyPort;
- protected volatile String httpProxyUsername;
- protected volatile String httpProxyPassword;
-
- protected volatile String suiteTicket;
- protected volatile long suiteTicketExpiresTime;
-
-
- protected volatile File tmpDirFile;
-
- private volatile ApacheHttpClientBuilder apacheHttpClientBuilder;
-
- @Override
- public String getSuiteAccessToken() {
- return this.suiteAccessToken;
- }
-
- public void setSuiteAccessToken(String suiteAccessToken) {
- this.suiteAccessToken = suiteAccessToken;
- }
-
- @Override
- public boolean isSuiteAccessTokenExpired() {
- return System.currentTimeMillis() > this.expiresTime;
- }
-
- @Override
- public void expireSuiteAccessToken() {
- this.expiresTime = 0;
- }
-
- @Override
- public synchronized void updateSuiteAccessToken(WxAccessToken suiteAccessToken) {
- updateSuiteAccessToken(suiteAccessToken.getAccessToken(), suiteAccessToken.getExpiresIn());
- }
-
- @Override
- public synchronized void updateSuiteAccessToken(String suiteAccessToken, int expiresInSeconds) {
- this.suiteAccessToken = suiteAccessToken;
- this.expiresTime = System.currentTimeMillis() + (expiresInSeconds - 200) * 1000L;
- }
-
- @Override
- public String getCorpId() {
- return this.corpId;
- }
-
- public void setCorpId(String corpId) {
- this.corpId = corpId;
- }
-
- @Override
- public String getCorpSecret() {
- return this.corpSecret;
- }
-
- public void setCorpSecret(String corpSecret) {
- this.corpSecret = corpSecret;
- }
-
- @Override
- public String getSuiteTicket() {
- return this.suiteTicket;
- }
-
- public void setSuiteTicket(String suiteTicket) {
- this.suiteTicket = suiteTicket;
- }
-
- public long getSuiteTicketExpiresTime() {
- return this.suiteTicketExpiresTime;
- }
-
- public void setSuiteTicketExpiresTime(long suiteTicketExpiresTime) {
- this.suiteTicketExpiresTime = suiteTicketExpiresTime;
- }
-
- @Override
- public boolean isSuiteTicketExpired() {
- return System.currentTimeMillis() > this.suiteTicketExpiresTime;
- }
-
- @Override
- public synchronized void updateSuiteTicket(String suiteTicket, int expiresInSeconds) {
- this.suiteTicket = suiteTicket;
- // 预留200秒的时间
- this.suiteTicketExpiresTime = System.currentTimeMillis() + (expiresInSeconds - 200) * 1000L;
- }
-
- @Override
- public void expireSuiteTicket() {
- this.suiteTicketExpiresTime = 0;
- }
-
- @Override
- public String getSuiteId() {
- return this.suiteId;
- }
-
- public void setSuiteId(String corpId) {
- this.suiteId = corpId;
- }
-
- @Override
- public String getSuiteSecret() {
- return this.suiteSecret;
- }
-
- public void setSuiteSecret(String corpSecret) {
- this.suiteSecret = corpSecret;
- }
-
- @Override
- public String getToken() {
- return this.token;
- }
-
- public void setToken(String token) {
- this.token = token;
- }
-
- @Override
- public long getExpiresTime() {
- return this.expiresTime;
- }
-
- public void setExpiresTime(long expiresTime) {
- this.expiresTime = expiresTime;
- }
-
- @Override
- public String getAesKey() {
- return this.aesKey;
- }
-
- public void setAesKey(String aesKey) {
- this.aesKey = aesKey;
- }
-
- public void setOauth2redirectUri(String oauth2redirectUri) {
- this.oauth2redirectUri = oauth2redirectUri;
- }
-
- @Override
- public String getHttpProxyHost() {
- return this.httpProxyHost;
- }
-
- public void setHttpProxyHost(String httpProxyHost) {
- this.httpProxyHost = httpProxyHost;
- }
-
- @Override
- public int getHttpProxyPort() {
- return this.httpProxyPort;
- }
-
- public void setHttpProxyPort(int httpProxyPort) {
- this.httpProxyPort = httpProxyPort;
- }
-
- @Override
- public String getHttpProxyUsername() {
- return this.httpProxyUsername;
- }
-
- public void setHttpProxyUsername(String httpProxyUsername) {
- this.httpProxyUsername = httpProxyUsername;
- }
-
- @Override
- public String getHttpProxyPassword() {
- return this.httpProxyPassword;
- }
-
- public void setHttpProxyPassword(String httpProxyPassword) {
- this.httpProxyPassword = httpProxyPassword;
- }
-
- @Override
- public String toString() {
- return WxCpGsonBuilder.create().toJson(this);
- }
-
- @Override
- public File getTmpDirFile() {
- return this.tmpDirFile;
- }
-
- public void setTmpDirFile(File tmpDirFile) {
- this.tmpDirFile = tmpDirFile;
- }
-
- @Override
- public ApacheHttpClientBuilder getApacheHttpClientBuilder() {
- return this.apacheHttpClientBuilder;
- }
-
- public void setApacheHttpClientBuilder(ApacheHttpClientBuilder apacheHttpClientBuilder) {
- this.apacheHttpClientBuilder = apacheHttpClientBuilder;
- }
-}
+package me.chanjar.weixin.cp.config;
+
+import me.chanjar.weixin.common.bean.WxAccessToken;
+import me.chanjar.weixin.common.util.http.apache.ApacheHttpClientBuilder;
+import me.chanjar.weixin.cp.util.json.WxCpGsonBuilder;
+
+import java.io.File;
+
+/**
+ * 基于内存的微信配置provider,在实际生产环境中应该将这些配置持久化
+ *
+ * @author Daniel Qian
+ */
+public class WxCpTpInMemoryConfigStorage implements WxCpTpConfigStorage {
+ protected volatile String corpId;
+ protected volatile String corpSecret;
+
+ protected volatile String suiteId;
+ protected volatile String suiteSecret;
+
+ protected volatile String token;
+ protected volatile String suiteAccessToken;
+ protected volatile String aesKey;
+ protected volatile long expiresTime;
+
+ protected volatile String oauth2redirectUri;
+
+ protected volatile String httpProxyHost;
+ protected volatile int httpProxyPort;
+ protected volatile String httpProxyUsername;
+ protected volatile String httpProxyPassword;
+
+ protected volatile String suiteTicket;
+ protected volatile long suiteTicketExpiresTime;
+
+
+ protected volatile File tmpDirFile;
+
+ private volatile ApacheHttpClientBuilder apacheHttpClientBuilder;
+
+ protected volatile String baseApiUrl;
+
+ @Override
+ public void setBaseApiUrl(String baseUrl) {
+ this.baseApiUrl = baseUrl;
+ }
+
+ @Override
+ public String getApiUrl(String path) {
+ if (baseApiUrl == null) {
+ baseApiUrl = "https://qyapi.weixin.qq.com";
+ }
+ return baseApiUrl + path;
+ }
+
+ @Override
+ public String getSuiteAccessToken() {
+ return this.suiteAccessToken;
+ }
+
+ public void setSuiteAccessToken(String suiteAccessToken) {
+ this.suiteAccessToken = suiteAccessToken;
+ }
+
+ @Override
+ public boolean isSuiteAccessTokenExpired() {
+ return System.currentTimeMillis() > this.expiresTime;
+ }
+
+ @Override
+ public void expireSuiteAccessToken() {
+ this.expiresTime = 0;
+ }
+
+ @Override
+ public synchronized void updateSuiteAccessToken(WxAccessToken suiteAccessToken) {
+ updateSuiteAccessToken(suiteAccessToken.getAccessToken(), suiteAccessToken.getExpiresIn());
+ }
+
+ @Override
+ public synchronized void updateSuiteAccessToken(String suiteAccessToken, int expiresInSeconds) {
+ this.suiteAccessToken = suiteAccessToken;
+ this.expiresTime = System.currentTimeMillis() + (expiresInSeconds - 200) * 1000L;
+ }
+
+ @Override
+ public String getCorpId() {
+ return this.corpId;
+ }
+
+ public void setCorpId(String corpId) {
+ this.corpId = corpId;
+ }
+
+ @Override
+ public String getCorpSecret() {
+ return this.corpSecret;
+ }
+
+ public void setCorpSecret(String corpSecret) {
+ this.corpSecret = corpSecret;
+ }
+
+ @Override
+ public String getSuiteTicket() {
+ return this.suiteTicket;
+ }
+
+ public void setSuiteTicket(String suiteTicket) {
+ this.suiteTicket = suiteTicket;
+ }
+
+ public long getSuiteTicketExpiresTime() {
+ return this.suiteTicketExpiresTime;
+ }
+
+ public void setSuiteTicketExpiresTime(long suiteTicketExpiresTime) {
+ this.suiteTicketExpiresTime = suiteTicketExpiresTime;
+ }
+
+ @Override
+ public boolean isSuiteTicketExpired() {
+ return System.currentTimeMillis() > this.suiteTicketExpiresTime;
+ }
+
+ @Override
+ public synchronized void updateSuiteTicket(String suiteTicket, int expiresInSeconds) {
+ this.suiteTicket = suiteTicket;
+ // 预留200秒的时间
+ this.suiteTicketExpiresTime = System.currentTimeMillis() + (expiresInSeconds - 200) * 1000L;
+ }
+
+ @Override
+ public void expireSuiteTicket() {
+ this.suiteTicketExpiresTime = 0;
+ }
+
+ @Override
+ public String getSuiteId() {
+ return this.suiteId;
+ }
+
+ public void setSuiteId(String corpId) {
+ this.suiteId = corpId;
+ }
+
+ @Override
+ public String getSuiteSecret() {
+ return this.suiteSecret;
+ }
+
+ public void setSuiteSecret(String corpSecret) {
+ this.suiteSecret = corpSecret;
+ }
+
+ @Override
+ public String getToken() {
+ return this.token;
+ }
+
+ public void setToken(String token) {
+ this.token = token;
+ }
+
+ @Override
+ public long getExpiresTime() {
+ return this.expiresTime;
+ }
+
+ public void setExpiresTime(long expiresTime) {
+ this.expiresTime = expiresTime;
+ }
+
+ @Override
+ public String getAesKey() {
+ return this.aesKey;
+ }
+
+ public void setAesKey(String aesKey) {
+ this.aesKey = aesKey;
+ }
+
+ public void setOauth2redirectUri(String oauth2redirectUri) {
+ this.oauth2redirectUri = oauth2redirectUri;
+ }
+
+ @Override
+ public String getHttpProxyHost() {
+ return this.httpProxyHost;
+ }
+
+ public void setHttpProxyHost(String httpProxyHost) {
+ this.httpProxyHost = httpProxyHost;
+ }
+
+ @Override
+ public int getHttpProxyPort() {
+ return this.httpProxyPort;
+ }
+
+ public void setHttpProxyPort(int httpProxyPort) {
+ this.httpProxyPort = httpProxyPort;
+ }
+
+ @Override
+ public String getHttpProxyUsername() {
+ return this.httpProxyUsername;
+ }
+
+ public void setHttpProxyUsername(String httpProxyUsername) {
+ this.httpProxyUsername = httpProxyUsername;
+ }
+
+ @Override
+ public String getHttpProxyPassword() {
+ return this.httpProxyPassword;
+ }
+
+ public void setHttpProxyPassword(String httpProxyPassword) {
+ this.httpProxyPassword = httpProxyPassword;
+ }
+
+ @Override
+ public String toString() {
+ return WxCpGsonBuilder.create().toJson(this);
+ }
+
+ @Override
+ public File getTmpDirFile() {
+ return this.tmpDirFile;
+ }
+
+ public void setTmpDirFile(File tmpDirFile) {
+ this.tmpDirFile = tmpDirFile;
+ }
+
+ @Override
+ public ApacheHttpClientBuilder getApacheHttpClientBuilder() {
+ return this.apacheHttpClientBuilder;
+ }
+
+ public void setApacheHttpClientBuilder(ApacheHttpClientBuilder apacheHttpClientBuilder) {
+ this.apacheHttpClientBuilder = apacheHttpClientBuilder;
+ }
+}
diff --git a/weixin-java-cp/src/test/java/me/chanjar/weixin/cp/api/impl/WxCpAgentServiceImplTest.java b/weixin-java-cp/src/test/java/me/chanjar/weixin/cp/api/impl/WxCpAgentServiceImplTest.java
index a2dc46e826..9fabe55068 100644
--- a/weixin-java-cp/src/test/java/me/chanjar/weixin/cp/api/impl/WxCpAgentServiceImplTest.java
+++ b/weixin-java-cp/src/test/java/me/chanjar/weixin/cp/api/impl/WxCpAgentServiceImplTest.java
@@ -2,6 +2,7 @@
import com.google.inject.Inject;
import me.chanjar.weixin.common.error.WxErrorException;
+import me.chanjar.weixin.cp.WxCpConsts;
import me.chanjar.weixin.cp.api.ApiTestModule;
import me.chanjar.weixin.cp.api.WxCpAgentService;
import me.chanjar.weixin.cp.api.WxCpService;
@@ -14,7 +15,6 @@
import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
-import static org.testng.Assert.assertEquals;
/**
@@ -70,7 +70,7 @@ public static class MockTest {
@Test
public void testGet() throws Exception {
String returnJson = "{\"errcode\": 0,\"errmsg\": \"ok\",\"agentid\": 9,\"name\": \"测试应用\",\"square_logo_url\": \"http://wx.qlogo.cn/mmhead/alksjf;lasdjf;lasjfuodiuj3rj2o34j/0\",\"description\": \"这是一个企业号应用\",\"allow_userinfos\": {\"user\": [{\"userid\": \"0009854\"}, {\"userid\": \"1723\"}, {\"userid\": \"5625\"}]},\"allow_partys\": {\"partyid\": [42762742]},\"allow_tags\": {\"tagid\": [23, 22, 35, 19, 32, 125, 133, 46, 150, 38, 183, 9, 7]},\"close\": 0,\"redirect_domain\": \"weixin.com.cn\",\"report_location_flag\": 0,\"isreportenter\": 0,\"home_url\": \"\"}";
- when(wxService.get(String.format(WxCpAgentService.GET_AGENT, 9), null)).thenReturn(returnJson);
+ when(wxService.get(String.format(wxService.getWxCpConfigStorage().getApiUrl(WxCpAgentService.GET_AGENT), 9), null)).thenReturn(returnJson);
when(wxService.getAgentService()).thenReturn(new WxCpAgentServiceImpl(wxService));
WxCpAgentService wxAgentService = this.wxService.getAgentService();
diff --git a/weixin-java-cp/src/test/java/me/chanjar/weixin/cp/api/impl/WxCpTagServiceImplTest.java b/weixin-java-cp/src/test/java/me/chanjar/weixin/cp/api/impl/WxCpTagServiceImplTest.java
index c4c8b3ccb5..858fde203c 100644
--- a/weixin-java-cp/src/test/java/me/chanjar/weixin/cp/api/impl/WxCpTagServiceImplTest.java
+++ b/weixin-java-cp/src/test/java/me/chanjar/weixin/cp/api/impl/WxCpTagServiceImplTest.java
@@ -17,7 +17,6 @@
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
-import static org.testng.Assert.assertEquals;
import static org.testng.Assert.assertNotEquals;
/**
@@ -83,7 +82,7 @@ public void testDelete() throws Exception {
public void testGet() throws WxErrorException {
String apiResultJson = "{\"errcode\": 0,\"errmsg\": \"ok\",\"userlist\": [{\"userid\": \"0124035\",\"name\": \"王五\"},{\"userid\": \"0114035\",\"name\": \"梦雪\"}],\"partylist\": [9576,9567,9566],\"tagname\": \"测试标签-001\"}";
WxCpService wxService = mock(WxCpService.class);
- when(wxService.get(String.format(WxCpTagService.TAG_GET, 150), null)).thenReturn(apiResultJson);
+ when(wxService.get(String.format(wxService.getWxCpConfigStorage().getApiUrl(WxCpTagService.TAG_GET), 150), null)).thenReturn(apiResultJson);
when(wxService.getTagService()).thenReturn(new WxCpTagServiceImpl(wxService));
WxCpTagService wxCpTagService = wxService.getTagService();