统一第三方接口方法名

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

View File

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

View File

@ -25,8 +25,8 @@ import com.foxinmy.weixin4j.token.TokenManager;
* @author jinyu(foxinmy@gmail.com) * @author jinyu(foxinmy@gmail.com)
* @date 2015年6月17日 * @date 2015年6月17日
* @since JDK 1.6 * @since JDK 1.6
* @see <a * @see <a href=
* 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> * "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 { public class SuiteApi extends QyApi {
/** /**
@ -49,10 +49,9 @@ public class SuiteApi extends QyApi {
*/ */
public SuiteApi(TicketManager ticketManager) { public SuiteApi(TicketManager ticketManager) {
this.ticketManager = ticketManager; this.ticketManager = ticketManager;
this.tokenManager = new TokenManager(new WeixinSuiteTokenCreator( this.tokenManager = new TokenManager(new WeixinSuiteTokenCreator(ticketManager),
ticketManager), ticketManager.getCacheStorager()); ticketManager.getCacheStorager());
this.preCodeManager = new TokenManager(new WeixinSuitePreCodeCreator( this.preCodeManager = new TokenManager(new WeixinSuitePreCodeCreator(tokenManager, ticketManager.getThirdId()),
tokenManager, ticketManager.getThirdId()),
ticketManager.getCacheStorager()); ticketManager.getCacheStorager());
} }
@ -91,8 +90,7 @@ public class SuiteApi extends QyApi {
* @return 应用套件的preticket管理 * @return 应用套件的preticket管理
*/ */
public PerTicketManager getPerTicketManager(String authCorpId) { public PerTicketManager getPerTicketManager(String authCorpId) {
return new PerTicketManager(authCorpId, ticketManager.getThirdId(), return new PerTicketManager(authCorpId, ticketManager.getThirdId(), ticketManager.getThirdSecret(),
ticketManager.getThirdSecret(),
ticketManager.getCacheStorager()); ticketManager.getCacheStorager());
} }
@ -104,8 +102,7 @@ public class SuiteApi extends QyApi {
* @return 企业号token * @return 企业号token
*/ */
public TokenManager getPerTokenManager(String authCorpId) { public TokenManager getPerTokenManager(String authCorpId) {
return new TokenManager(new WeixinTokenSuiteCreator( return new TokenManager(new WeixinTokenSuiteCreator(getPerTicketManager(authCorpId), tokenManager),
getPerTicketManager(authCorpId), tokenManager),
ticketManager.getCacheStorager()); ticketManager.getCacheStorager());
} }
@ -127,14 +124,13 @@ public class SuiteApi extends QyApi {
JSONObject appid = new JSONObject(); JSONObject appid = new JSONObject();
appid.put("appid", appids); appid.put("appid", appids);
para.put("session_info", appid); para.put("session_info", appid);
WeixinResponse response = weixinExecutor.post( WeixinResponse response = weixinExecutor
String.format(suite_set_session_uri, .post(String.format(suite_set_session_uri, tokenManager.getAccessToken()), para.toJSONString());
tokenManager.getAccessToken()), para.toJSONString());
return response.getAsJsonResult(); return response.getAsJsonResult();
} }
/** /**
* 获取企业号的永久授权码 * 临时授权码换取授权方的永久授权码并换取授权信息企业access_token
* *
* @param authCode * @param authCode
* 临时授权码会在授权成功时附加在redirect_uri中跳转回应用提供商网站 * 临时授权码会在授权成功时附加在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" * "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> * >获取企业号的永久授权码</a>
*/ */
public OUserInfo exchangePermanentCode(String authCode) public OUserInfo exchangeAuthInfo(String authCode) throws WeixinException {
throws WeixinException {
String suite_get_permanent_uri = getRequestUri("suite_get_permanent_uri"); String suite_get_permanent_uri = getRequestUri("suite_get_permanent_uri");
JSONObject obj = new JSONObject(); JSONObject obj = new JSONObject();
obj.put("suite_id", ticketManager.getThirdId()); obj.put("suite_id", ticketManager.getThirdId());
obj.put("auth_code", authCode); obj.put("auth_code", authCode);
WeixinResponse response = weixinExecutor.post( WeixinResponse response = weixinExecutor
String.format(suite_get_permanent_uri, .post(String.format(suite_get_permanent_uri, tokenManager.getAccessToken()), obj.toJSONString());
tokenManager.getAccessToken()), obj.toJSONString());
obj = response.getAsJson(); obj = response.getAsJson();
obj.put("corp_info", obj.remove("auth_corp_info")); obj.put("corp_info", obj.remove("auth_corp_info"));
obj.put("user_info", obj.remove("auth_user_info")); obj.put("user_info", obj.remove("auth_user_info"));
OUserInfo oInfo = JSON.toJavaObject(obj, OUserInfo.class); OUserInfo oInfo = JSON.toJavaObject(obj, OUserInfo.class);
// 微信授权企业号的永久授权码 // 微信授权企业号的永久授权码
PerTicketManager perTicketManager = getPerTicketManager(oInfo PerTicketManager perTicketManager = getPerTicketManager(oInfo.getCorpInfo().getCorpId());
.getCorpInfo().getCorpId());
// 缓存微信企业号的access_token // 缓存微信企业号的access_token
TokenCreator tokenCreator = new WeixinTokenSuiteCreator( TokenCreator tokenCreator = new WeixinTokenSuiteCreator(perTicketManager, tokenManager);
perTicketManager, tokenManager); Token token = new Token(obj.getString("access_token"), obj.getLongValue("expires_in") * 1000l);
Token token = new Token(obj.getString("access_token"),
obj.getLongValue("expires_in") * 1000l);
ticketManager.getCacheStorager().caching(tokenCreator.key(), token); ticketManager.getCacheStorager().caching(tokenCreator.key(), token);
// 缓存微信企业号的永久授权码 // 缓存微信企业号的永久授权码
perTicketManager.cachingTicket(obj.getString("permanent_code")); perTicketManager.cachingTicket(obj.getString("permanent_code"));
@ -180,18 +171,17 @@ public class SuiteApi extends QyApi {
* @return 授权方信息 * @return 授权方信息
* @throws WeixinException * @throws WeixinException
* @see com.foxinmy.weixin4j.qy.model.OUserInfo * @see com.foxinmy.weixin4j.qy.model.OUserInfo
* @see <a * @see <a href=
* 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> * "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"); String suite_get_authinfo_uri = getRequestUri("suite_get_authinfo_uri");
JSONObject obj = new JSONObject(); JSONObject obj = new JSONObject();
obj.put("suite_id", ticketManager.getThirdId()); obj.put("suite_id", ticketManager.getThirdId());
obj.put("auth_corpid", authCorpId); obj.put("auth_corpid", authCorpId);
obj.put("permanent_code", getPerTicketManager(authCorpId).getAccessTicket()); obj.put("permanent_code", getPerTicketManager(authCorpId).getAccessTicket());
WeixinResponse response = weixinExecutor.post( WeixinResponse response = weixinExecutor
String.format(suite_get_authinfo_uri, .post(String.format(suite_get_authinfo_uri, tokenManager.getAccessToken()), obj.toJSONString());
tokenManager.getAccessToken()), obj.toJSONString());
obj = response.getAsJson(); obj = response.getAsJson();
obj.put("corp_info", obj.remove("auth_corp_info")); obj.put("corp_info", obj.remove("auth_corp_info"));
obj.put("user_info", obj.remove("auth_user_info")); obj.put("user_info", obj.remove("auth_user_info"));
@ -207,31 +197,25 @@ public class SuiteApi extends QyApi {
* 授权方应用id * 授权方应用id
* @return 应用信息 * @return 应用信息
* @see com.foxinmy.weixin4j.qy.model.AgentInfo * @see com.foxinmy.weixin4j.qy.model.AgentInfo
* @see <a * @see <a href=
* 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> * "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 * @throws WeixinException
*/ */
public AgentInfo getAgent(String authCorpId, int agentid) public AgentInfo getAgent(String authCorpId, int agentid) throws WeixinException {
throws WeixinException {
String suite_get_agent_uri = getRequestUri("suite_get_agent_uri"); String suite_get_agent_uri = getRequestUri("suite_get_agent_uri");
JSONObject obj = new JSONObject(); JSONObject obj = new JSONObject();
obj.put("suite_id", ticketManager.getThirdId()); obj.put("suite_id", ticketManager.getThirdId());
obj.put("auth_corpid", authCorpId); obj.put("auth_corpid", authCorpId);
obj.put("permanent_code", getPerTicketManager(authCorpId).getAccessTicket()); obj.put("permanent_code", getPerTicketManager(authCorpId).getAccessTicket());
obj.put("agentid", agentid); obj.put("agentid", agentid);
WeixinResponse response = weixinExecutor.post( WeixinResponse response = weixinExecutor.post(String.format(suite_get_agent_uri, tokenManager.getAccessToken()),
String.format(suite_get_agent_uri, obj.toJSONString());
tokenManager.getAccessToken()), obj.toJSONString());
JSONObject jsonObj = response.getAsJson(); JSONObject jsonObj = response.getAsJson();
AgentInfo agent = JSON.toJavaObject(jsonObj, AgentInfo.class); AgentInfo agent = JSON.toJavaObject(jsonObj, AgentInfo.class);
agent.setAllowUsers(JSON.parseArray( agent.setAllowUsers(JSON.parseArray(jsonObj.getJSONObject("allow_userinfos").getString("user"), User.class));
jsonObj.getJSONObject("allow_userinfos").getString("user"), agent.setAllowPartys(
User.class)); JSON.parseArray(jsonObj.getJSONObject("allow_partys").getString("partyid"), Integer.class));
agent.setAllowPartys(JSON.parseArray( agent.setAllowTags(JSON.parseArray(jsonObj.getJSONObject("allow_tags").getString("tagid"), Integer.class));
jsonObj.getJSONObject("allow_partys").getString("partyid"),
Integer.class));
agent.setAllowTags(JSON.parseArray(jsonObj.getJSONObject("allow_tags")
.getString("tagid"), Integer.class));
return agent; return agent;
} }
@ -243,22 +227,19 @@ public class SuiteApi extends QyApi {
* @param agentSet * @param agentSet
* 设置信息 * 设置信息
* @see com.foxinmy.weixin4j.qy.model.AgentSetter * @see com.foxinmy.weixin4j.qy.model.AgentSetter
* @see <a * @see <a href=
* 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> * "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 处理结果 * @return 处理结果
* @throws WeixinException * @throws WeixinException
*/ */
public JsonResult setAgent(String authCorpId, AgentSetter agentSet) public JsonResult setAgent(String authCorpId, AgentSetter agentSet) throws WeixinException {
throws WeixinException {
String suite_set_agent_uri = getRequestUri("suite_set_agent_uri"); String suite_set_agent_uri = getRequestUri("suite_set_agent_uri");
JSONObject obj = new JSONObject(); JSONObject obj = new JSONObject();
obj.put("suite_id", ticketManager.getThirdId()); obj.put("suite_id", ticketManager.getThirdId());
obj.put("auth_corpid", authCorpId); obj.put("auth_corpid", authCorpId);
obj.put("permanent_code", getPerTicketManager(authCorpId).getAccessTicket()); obj.put("permanent_code", getPerTicketManager(authCorpId).getAccessTicket());
obj.put("agent", agentSet); obj.put("agent", agentSet);
WeixinResponse response = weixinExecutor.post( WeixinResponse response = weixinExecutor.post(String.format(suite_set_agent_uri, tokenManager.getAccessToken()),
String.format(suite_set_agent_uri,
tokenManager.getAccessToken()),
JSON.toJSONString(obj, AgentApi.typeFilter)); JSON.toJSONString(obj, AgentApi.typeFilter));
return response.getAsJsonResult(); return response.getAsJsonResult();
} }

View File

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