统一第三方接口方法名

This commit is contained in:
jinyu 2016-07-07 18:05:57 +08:00
parent f47bba49d1
commit 17344ec20a
4 changed files with 55 additions and 78 deletions

View File

@ -21,8 +21,7 @@ public class Weixin4jConfigUtil {
private final static String CLASSPATH_VALUE;
private static ResourceBundle weixinBundle;
static {
CLASSPATH_VALUE = Thread.currentThread().getContextClassLoader()
.getResource("").getPath();
CLASSPATH_VALUE = Thread.currentThread().getContextClassLoader().getResource("").getPath();
try {
weixinBundle = ResourceBundle.getBundle(Consts.WEIXIN4J);
} catch (MissingResourceException e) {
@ -89,21 +88,22 @@ public class Weixin4jConfigUtil {
* @return
*/
public static String getClassPathValue(String key, String defaultValue) {
return getValue(key, defaultValue).replaceFirst(CLASSPATH_PREFIX,
CLASSPATH_VALUE);
return getValue(key, defaultValue).replaceFirst(CLASSPATH_PREFIX, CLASSPATH_VALUE);
}
/**
* 获取微信账号信息
*
* @return 微信账号信息
*/
public static WeixinAccount getWeixinAccount() {
WeixinAccount account = null;
try {
account = JSON
.parseObject(getValue("account"), WeixinAccount.class);
account = JSON.parseObject(getValue("account"), WeixinAccount.class);
} catch (NullPointerException e) {
System.err
.println("'weixin4j.account' key not found in weixin4j.properties.");
System.err.println("'weixin4j.account' key not found in weixin4j.properties.");
} catch (MissingResourceException e) {
System.err
.println("'weixin4j.account' key not found in weixin4j.properties.");
System.err.println("'weixin4j.account' key not found in weixin4j.properties.");
}
return account;
}

View File

@ -114,7 +114,7 @@ public class ComponentApi extends MpApi {
* @see com.foxinmy.weixin4j.mp.model.ComponentAuthInfo
* @throws WeixinException
*/
public ComponentAuthInfo exchangeComponentAuthInfo(String authCode)
public ComponentAuthInfo exchangeAuthInfo(String authCode)
throws WeixinException {
String component_exchange_authorizer_uri = getRequestUri("component_exchange_authorizer_uri");
JSONObject obj = new JSONObject();
@ -143,7 +143,7 @@ public class ComponentApi extends MpApi {
Token token = new Token(authObj.getString("authorizer_access_token"),
authObj.getLongValue("expires_in") * 1000l);
ticketManager.getCacheStorager().caching(tokenCreator.key(), token);
// 缓存微信企业号的永久授权码
// 缓存微信公众号的永久授权码(refresh_token)
perTicketManager.cachingTicket(authObj
.getString("authorizer_refresh_token"));
return info;
@ -160,7 +160,7 @@ public class ComponentApi extends MpApi {
* @see com.foxinmy.weixin4j.mp.model.ComponentAuthInfo
* @throws WeixinException
*/
public ComponentAuthInfo getComponentAuthInfo(String authAppId)
public ComponentAuthInfo getAuthInfo(String authAppId)
throws WeixinException {
String component_get_authorizer_uri = getRequestUri("component_get_authorizer_uri");
JSONObject obj = new JSONObject();
@ -222,7 +222,7 @@ public class ComponentApi extends MpApi {
* @see com.foxinmy.weixin4j.mp.model.AuthorizerOption
* @throws WeixinException
*/
public JsonResult getAuthorizerOption(String authAppId,
public JsonResult setAuthorizerOption(String authAppId,
AuthorizerOption option) throws WeixinException {
String component_set_authorizer_option_uri = getRequestUri("component_set_authorizer_option_uri");
JSONObject obj = new JSONObject();

View File

@ -25,8 +25,8 @@ import com.foxinmy.weixin4j.token.TokenManager;
* @author jinyu(foxinmy@gmail.com)
* @date 2015年6月17日
* @since JDK 1.6
* @see <a
* href="http://qydev.weixin.qq.com/wiki/index.php?title=%E7%AC%AC%E4%B8%89%E6%96%B9%E5%BA%94%E7%94%A8%E6%8E%88%E6%9D%83">第三方应用授权</a>
* @see <a href=
* "http://qydev.weixin.qq.com/wiki/index.php?title=%E7%AC%AC%E4%B8%89%E6%96%B9%E5%BA%94%E7%94%A8%E6%8E%88%E6%9D%83">第三方应用授权</a>
*/
public class SuiteApi extends QyApi {
/**
@ -49,10 +49,9 @@ public class SuiteApi extends QyApi {
*/
public SuiteApi(TicketManager ticketManager) {
this.ticketManager = ticketManager;
this.tokenManager = new TokenManager(new WeixinSuiteTokenCreator(
ticketManager), ticketManager.getCacheStorager());
this.preCodeManager = new TokenManager(new WeixinSuitePreCodeCreator(
tokenManager, ticketManager.getThirdId()),
this.tokenManager = new TokenManager(new WeixinSuiteTokenCreator(ticketManager),
ticketManager.getCacheStorager());
this.preCodeManager = new TokenManager(new WeixinSuitePreCodeCreator(tokenManager, ticketManager.getThirdId()),
ticketManager.getCacheStorager());
}
@ -91,8 +90,7 @@ public class SuiteApi extends QyApi {
* @return 应用套件的preticket管理
*/
public PerTicketManager getPerTicketManager(String authCorpId) {
return new PerTicketManager(authCorpId, ticketManager.getThirdId(),
ticketManager.getThirdSecret(),
return new PerTicketManager(authCorpId, ticketManager.getThirdId(), ticketManager.getThirdSecret(),
ticketManager.getCacheStorager());
}
@ -104,8 +102,7 @@ public class SuiteApi extends QyApi {
* @return 企业号token
*/
public TokenManager getPerTokenManager(String authCorpId) {
return new TokenManager(new WeixinTokenSuiteCreator(
getPerTicketManager(authCorpId), tokenManager),
return new TokenManager(new WeixinTokenSuiteCreator(getPerTicketManager(authCorpId), tokenManager),
ticketManager.getCacheStorager());
}
@ -127,14 +124,13 @@ public class SuiteApi extends QyApi {
JSONObject appid = new JSONObject();
appid.put("appid", appids);
para.put("session_info", appid);
WeixinResponse response = weixinExecutor.post(
String.format(suite_set_session_uri,
tokenManager.getAccessToken()), para.toJSONString());
WeixinResponse response = weixinExecutor
.post(String.format(suite_set_session_uri, tokenManager.getAccessToken()), para.toJSONString());
return response.getAsJsonResult();
}
/**
* 获取企业号的永久授权码
* 临时授权码换取授权方的永久授权码并换取授权信息企业access_token
*
* @param authCode
* 临时授权码会在授权成功时附加在redirect_uri中跳转回应用提供商网站
@ -145,27 +141,22 @@ public class SuiteApi extends QyApi {
* "http://qydev.weixin.qq.com/wiki/index.php?title=%E7%AC%AC%E4%B8%89%E6%96%B9%E5%BA%94%E7%94%A8%E6%8E%A5%E5%8F%A3%E8%AF%B4%E6%98%8E#.E8.8E.B7.E5.8F.96.E4.BC.81.E4.B8.9A.E5.8F.B7.E7.9A.84.E6.B0.B8.E4.B9.85.E6.8E.88.E6.9D.83.E7.A0.81"
* >获取企业号的永久授权码</a>
*/
public OUserInfo exchangePermanentCode(String authCode)
throws WeixinException {
public OUserInfo exchangeAuthInfo(String authCode) throws WeixinException {
String suite_get_permanent_uri = getRequestUri("suite_get_permanent_uri");
JSONObject obj = new JSONObject();
obj.put("suite_id", ticketManager.getThirdId());
obj.put("auth_code", authCode);
WeixinResponse response = weixinExecutor.post(
String.format(suite_get_permanent_uri,
tokenManager.getAccessToken()), obj.toJSONString());
WeixinResponse response = weixinExecutor
.post(String.format(suite_get_permanent_uri, tokenManager.getAccessToken()), obj.toJSONString());
obj = response.getAsJson();
obj.put("corp_info", obj.remove("auth_corp_info"));
obj.put("user_info", obj.remove("auth_user_info"));
OUserInfo oInfo = JSON.toJavaObject(obj, OUserInfo.class);
// 微信授权企业号的永久授权码
PerTicketManager perTicketManager = getPerTicketManager(oInfo
.getCorpInfo().getCorpId());
PerTicketManager perTicketManager = getPerTicketManager(oInfo.getCorpInfo().getCorpId());
// 缓存微信企业号的access_token
TokenCreator tokenCreator = new WeixinTokenSuiteCreator(
perTicketManager, tokenManager);
Token token = new Token(obj.getString("access_token"),
obj.getLongValue("expires_in") * 1000l);
TokenCreator tokenCreator = new WeixinTokenSuiteCreator(perTicketManager, tokenManager);
Token token = new Token(obj.getString("access_token"), obj.getLongValue("expires_in") * 1000l);
ticketManager.getCacheStorager().caching(tokenCreator.key(), token);
// 缓存微信企业号的永久授权码
perTicketManager.cachingTicket(obj.getString("permanent_code"));
@ -180,18 +171,17 @@ public class SuiteApi extends QyApi {
* @return 授权方信息
* @throws WeixinException
* @see com.foxinmy.weixin4j.qy.model.OUserInfo
* @see <a
* href="http://qydev.weixin.qq.com/wiki/index.php?title=%E7%AC%AC%E4%B8%89%E6%96%B9%E5%BA%94%E7%94%A8%E6%8E%A5%E5%8F%A3%E8%AF%B4%E6%98%8E#.E8.8E.B7.E5.8F.96.E4.BC.81.E4.B8.9A.E5.8F.B7.E7.9A.84.E6.8E.88.E6.9D.83.E4.BF.A1.E6.81.AF">获取企业号的授权信息</a>
* @see <a href=
* "http://qydev.weixin.qq.com/wiki/index.php?title=%E7%AC%AC%E4%B8%89%E6%96%B9%E5%BA%94%E7%94%A8%E6%8E%A5%E5%8F%A3%E8%AF%B4%E6%98%8E#.E8.8E.B7.E5.8F.96.E4.BC.81.E4.B8.9A.E5.8F.B7.E7.9A.84.E6.8E.88.E6.9D.83.E4.BF.A1.E6.81.AF">获取企业号的授权信息</a>
*/
public OUserInfo getOAuthInfo(String authCorpId) throws WeixinException {
public OUserInfo getAuthInfo(String authCorpId) throws WeixinException {
String suite_get_authinfo_uri = getRequestUri("suite_get_authinfo_uri");
JSONObject obj = new JSONObject();
obj.put("suite_id", ticketManager.getThirdId());
obj.put("auth_corpid", authCorpId);
obj.put("permanent_code", getPerTicketManager(authCorpId).getAccessTicket());
WeixinResponse response = weixinExecutor.post(
String.format(suite_get_authinfo_uri,
tokenManager.getAccessToken()), obj.toJSONString());
WeixinResponse response = weixinExecutor
.post(String.format(suite_get_authinfo_uri, tokenManager.getAccessToken()), obj.toJSONString());
obj = response.getAsJson();
obj.put("corp_info", obj.remove("auth_corp_info"));
obj.put("user_info", obj.remove("auth_user_info"));
@ -207,31 +197,25 @@ public class SuiteApi extends QyApi {
* 授权方应用id
* @return 应用信息
* @see com.foxinmy.weixin4j.qy.model.AgentInfo
* @see <a
* href="http://qydev.weixin.qq.com/wiki/index.php?title=%E7%AC%AC%E4%B8%89%E6%96%B9%E5%BA%94%E7%94%A8%E6%8E%A5%E5%8F%A3%E8%AF%B4%E6%98%8E#.E8.8E.B7.E5.8F.96.E4.BC.81.E4.B8.9A.E5.8F.B7.E5.BA.94.E7.94.A8">获取企业号应用</a>
* @see <a href=
* "http://qydev.weixin.qq.com/wiki/index.php?title=%E7%AC%AC%E4%B8%89%E6%96%B9%E5%BA%94%E7%94%A8%E6%8E%A5%E5%8F%A3%E8%AF%B4%E6%98%8E#.E8.8E.B7.E5.8F.96.E4.BC.81.E4.B8.9A.E5.8F.B7.E5.BA.94.E7.94.A8">获取企业号应用</a>
* @throws WeixinException
*/
public AgentInfo getAgent(String authCorpId, int agentid)
throws WeixinException {
public AgentInfo getAgent(String authCorpId, int agentid) throws WeixinException {
String suite_get_agent_uri = getRequestUri("suite_get_agent_uri");
JSONObject obj = new JSONObject();
obj.put("suite_id", ticketManager.getThirdId());
obj.put("auth_corpid", authCorpId);
obj.put("permanent_code", getPerTicketManager(authCorpId).getAccessTicket());
obj.put("agentid", agentid);
WeixinResponse response = weixinExecutor.post(
String.format(suite_get_agent_uri,
tokenManager.getAccessToken()), obj.toJSONString());
WeixinResponse response = weixinExecutor.post(String.format(suite_get_agent_uri, tokenManager.getAccessToken()),
obj.toJSONString());
JSONObject jsonObj = response.getAsJson();
AgentInfo agent = JSON.toJavaObject(jsonObj, AgentInfo.class);
agent.setAllowUsers(JSON.parseArray(
jsonObj.getJSONObject("allow_userinfos").getString("user"),
User.class));
agent.setAllowPartys(JSON.parseArray(
jsonObj.getJSONObject("allow_partys").getString("partyid"),
Integer.class));
agent.setAllowTags(JSON.parseArray(jsonObj.getJSONObject("allow_tags")
.getString("tagid"), Integer.class));
agent.setAllowUsers(JSON.parseArray(jsonObj.getJSONObject("allow_userinfos").getString("user"), User.class));
agent.setAllowPartys(
JSON.parseArray(jsonObj.getJSONObject("allow_partys").getString("partyid"), Integer.class));
agent.setAllowTags(JSON.parseArray(jsonObj.getJSONObject("allow_tags").getString("tagid"), Integer.class));
return agent;
}
@ -243,22 +227,19 @@ public class SuiteApi extends QyApi {
* @param agentSet
* 设置信息
* @see com.foxinmy.weixin4j.qy.model.AgentSetter
* @see <a
* href="http://qydev.weixin.qq.com/wiki/index.php?title=%E8%AE%BE%E7%BD%AE%E4%BC%81%E4%B8%9A%E5%8F%B7%E5%BA%94%E7%94%A8">设置企业号信息</a>
* @see <a href=
* "http://qydev.weixin.qq.com/wiki/index.php?title=%E8%AE%BE%E7%BD%AE%E4%BC%81%E4%B8%9A%E5%8F%B7%E5%BA%94%E7%94%A8">设置企业号信息</a>
* @return 处理结果
* @throws WeixinException
*/
public JsonResult setAgent(String authCorpId, AgentSetter agentSet)
throws WeixinException {
public JsonResult setAgent(String authCorpId, AgentSetter agentSet) throws WeixinException {
String suite_set_agent_uri = getRequestUri("suite_set_agent_uri");
JSONObject obj = new JSONObject();
obj.put("suite_id", ticketManager.getThirdId());
obj.put("auth_corpid", authCorpId);
obj.put("permanent_code", getPerTicketManager(authCorpId).getAccessTicket());
obj.put("agent", agentSet);
WeixinResponse response = weixinExecutor.post(
String.format(suite_set_agent_uri,
tokenManager.getAccessToken()),
WeixinResponse response = weixinExecutor.post(String.format(suite_set_agent_uri, tokenManager.getAccessToken()),
JSON.toJSONString(obj, AgentApi.typeFilter));
return response.getAsJsonResult();
}

View File

@ -16,28 +16,25 @@ import com.foxinmy.weixin4j.request.WeixinRequest;
* @since JDK 1.6
* @see
*/
public abstract class MultipleMessageHandlerAdapter implements
WeixinMessageHandler {
public abstract class MultipleMessageHandlerAdapter implements WeixinMessageHandler {
private final Set<Class<? extends WeixinMessage>> messageClasses;
public MultipleMessageHandlerAdapter(
Class<? extends WeixinMessage>... messageClasses) {
public MultipleMessageHandlerAdapter(Class<? extends WeixinMessage>... messageClasses) {
if (messageClasses == null) {
throw new IllegalArgumentException("messageClasses not be empty");
}
this.messageClasses = new HashSet<Class<? extends WeixinMessage>>(
(int) Math.ceil(messageClasses.length * 0.75));
Math.max((int) (messageClasses.length / .75f) + 1, 16));
for (Class<? extends WeixinMessage> clazz : messageClasses) {
this.messageClasses.add(clazz);
}
}
@Override
public boolean canHandle(WeixinRequest request, WeixinMessage message,
Set<String> nodeNames) throws WeixinException {
return message != null && messageClasses.contains(message.getClass())
&& canHandle0(request, message);
public boolean canHandle(WeixinRequest request, WeixinMessage message, Set<String> nodeNames)
throws WeixinException {
return message != null && messageClasses.contains(message.getClass()) && canHandle0(request, message);
}
/**
@ -50,8 +47,7 @@ public abstract class MultipleMessageHandlerAdapter implements
* @return true则执行doHandler
* @throws WeixinException
*/
public boolean canHandle0(WeixinRequest request, WeixinMessage message)
throws WeixinException {
public boolean canHandle0(WeixinRequest request, WeixinMessage message) throws WeixinException {
return true;
}