component

This commit is contained in:
胡金宇 2017-08-14 13:28:04 +08:00
parent af879a9d32
commit 17949abed2
4 changed files with 2247 additions and 2254 deletions

View File

@ -57,8 +57,7 @@ public class WeixinComponentProxy {
* token管理 * token管理
*/ */
public WeixinComponentProxy(CacheStorager<Token> cacheStorager) { public WeixinComponentProxy(CacheStorager<Token> cacheStorager) {
this(JSON.parseObject(Weixin4jConfigUtil.getValue("account"), this(JSON.parseObject(Weixin4jConfigUtil.getValue("account"), WeixinMpAccount.class), cacheStorager);
WeixinMpAccount.class), cacheStorager);
} }
/** /**
@ -69,26 +68,20 @@ public class WeixinComponentProxy {
* @param cacheStorager * @param cacheStorager
* token管理 * token管理
*/ */
public WeixinComponentProxy(WeixinMpAccount weixinMpAccount, public WeixinComponentProxy(WeixinMpAccount weixinMpAccount, CacheStorager<Token> cacheStorager) {
CacheStorager<Token> cacheStorager) {
if (weixinMpAccount == null) { if (weixinMpAccount == null) {
throw new IllegalArgumentException( throw new IllegalArgumentException("weixinMpAccount must not be empty");
"weixinMpAccount must not be empty");
} }
if (cacheStorager == null) { if (cacheStorager == null) {
throw new IllegalArgumentException( throw new IllegalArgumentException("cacheStorager must not be empty");
"cacheStorager must not be empty");
} }
this.weixinMpAccount = weixinMpAccount; this.weixinMpAccount = weixinMpAccount;
this.componentMap = new HashMap<String, ComponentApi>(weixinMpAccount this.componentMap = new HashMap<String, ComponentApi>(weixinMpAccount.getComponents().size());
.getComponents().size());
for (WeixinAccount component : weixinMpAccount.getComponents()) { for (WeixinAccount component : weixinMpAccount.getComponents()) {
this.componentMap.put(component.getId(), new ComponentApi( this.componentMap.put(component.getId(),
new TicketManager(component.getId(), component.getSecret(), new ComponentApi(new TicketManager(component.getId(), component.getSecret(), cacheStorager)));
cacheStorager)));
} }
this.componentMap.put(null, componentMap.get(weixinMpAccount this.componentMap.put(null, componentMap.get(weixinMpAccount.getComponents().get(0).getId()));
.getComponents().get(0).getId()));
} }
/** /**
@ -134,8 +127,7 @@ public class WeixinComponentProxy {
* @see com.foxinmy.weixin4j.mp.api.ComponentApi#getPreCodeManager() * @see com.foxinmy.weixin4j.mp.api.ComponentApi#getPreCodeManager()
* @throws WeixinException * @throws WeixinException
*/ */
public String getPreComponentTicket(String componentId) public String getPreComponentTicket(String componentId) throws WeixinException {
throws WeixinException {
ComponentApi component = component(componentId); ComponentApi component = component(componentId);
Token token = component.getTicketManager().getTicket(); Token token = component.getTicketManager().getTicket();
if (token == null || StringUtil.isBlank(token.getAccessToken())) { if (token == null || StringUtil.isBlank(token.getAccessToken())) {
@ -153,16 +145,14 @@ public class WeixinComponentProxy {
* 组件ticket内容 * 组件ticket内容
* @throws WeixinException * @throws WeixinException
*/ */
public void cacheComponentTicket(String componentId, String componentTicket) public void cacheComponentTicket(String componentId, String componentTicket) throws WeixinException {
throws WeixinException { component(componentId).getTicketManager().cachingTicket(componentTicket);
component(componentId).getTicketManager()
.cachingTicket(componentTicket);
} }
/** /**
* 应用组件授权 <font color="red">需先缓存ticket</font> <li> * 应用组件授权 <font color="red">需先缓存ticket</font>
* redirectUri默认填写weixin4j.properties#component.oauth.redirect.uri <li> * <li>redirectUri默认填写weixin4j.properties#component.oauth.redirect.uri
* state默认填写state * <li>state默认填写state
* *
* @param componentId * @param componentId
* 组件ID * 组件ID
@ -170,17 +160,14 @@ public class WeixinComponentProxy {
* @return 请求授权的URL * @return 请求授权的URL
* @throws WeixinException * @throws WeixinException
*/ */
public String getComponentAuthorizationURL(String componentId) public String getComponentAuthorizationURL(String componentId) throws WeixinException {
throws WeixinException { String redirectUri = Weixin4jConfigUtil.getValue("component.oauth.redirect.uri");
String redirectUri = Weixin4jConfigUtil
.getValue("component.oauth.redirect.uri");
return getComponentAuthorizationURL(componentId, redirectUri, "state"); return getComponentAuthorizationURL(componentId, redirectUri, "state");
} }
/** /**
* 应用组件授权 <font * 应用组件授权 <font color="red">需先缓存ticket在授权完成之后需要调用ComponentApi#
* color="red">需先缓存ticket在授权完成之后需要调用ComponentApi#exchangeAuthInfo方法 * exchangeAuthorizerToken方法 ,否则无法缓存token相关导致后续的组件接口调用失败</font>
* ,否则无法缓存token相关导致后续的组件接口调用失败</font>
* *
* @param componentId * @param componentId
* 组件ID * 组件ID
@ -192,17 +179,17 @@ public class WeixinComponentProxy {
* @see com.foxinmy.weixin4j.mp.api.ComponentApi * @see com.foxinmy.weixin4j.mp.api.ComponentApi
* @see com.foxinmy.weixin4j.mp.api.ComponentApi#getTicketManager() * @see com.foxinmy.weixin4j.mp.api.ComponentApi#getTicketManager()
* @see com.foxinmy.weixin4j.mp.api.ComponentApi#getPreCodeManager() * @see com.foxinmy.weixin4j.mp.api.ComponentApi#getPreCodeManager()
* @see com.foxinmy.weixin4j.mp.api.ComponentApi#exchangeAuthInfo(String) * @see com.foxinmy.weixin4j.mp.api.ComponentApi#exchangeAuthorizerToken(String)
* @see <a * @see <a href=
* href="https://open.weixin.qq.com/cgi-bin/showdocument?action=dir_list&t=resource/res_list&verify=1&id=open1453779503&token=&lang=zh_CN">应用组件授权</a> * "https://open.weixin.qq.com/cgi-bin/showdocument?action=dir_list&t=resource/res_list&verify=1&id=open1453779503&token=&lang=zh_CN">
* 应用组件授权</a>
* @return 请求授权的URL * @return 请求授权的URL
* @throws WeixinException * @throws WeixinException
*/ */
public String getComponentAuthorizationURL(String componentId, public String getComponentAuthorizationURL(String componentId, String redirectUri, String state)
String redirectUri, String state) throws WeixinException { throws WeixinException {
try { try {
return String.format(URLConsts.COMPONENT_OAUTH_URL, componentId, return String.format(URLConsts.COMPONENT_OAUTH_URL, componentId, getPreComponentTicket(componentId),
getPreComponentTicket(componentId),
URLEncoder.encode(redirectUri, Consts.UTF_8.name()), state); URLEncoder.encode(redirectUri, Consts.UTF_8.name()), state);
} catch (UnsupportedEncodingException e) { } catch (UnsupportedEncodingException e) {
; ;
@ -210,5 +197,20 @@ public class WeixinComponentProxy {
return ""; return "";
} }
/**
* 创建WeixinProxy对象
*
* @param componentId
* 组件ID
* @param authAppId
* 已授权的appid
* @see com.foxinmy.weixin4j.mp.WeixinProxy
* @return
*/
public WeixinProxy getWeixinProxy(String componentId, String authAppId) {
return new WeixinProxy(component(componentId).getRefreshTokenManager(authAppId),
component(componentId).getTokenManager());
}
public final static String VERSION = "1.7.7"; public final static String VERSION = "1.7.7";
} }

View File

@ -13,6 +13,7 @@ import com.foxinmy.weixin4j.http.weixin.ApiResult;
import com.foxinmy.weixin4j.http.weixin.WeixinResponse; import com.foxinmy.weixin4j.http.weixin.WeixinResponse;
import com.foxinmy.weixin4j.mp.component.WeixinComponentPreCodeCreator; import com.foxinmy.weixin4j.mp.component.WeixinComponentPreCodeCreator;
import com.foxinmy.weixin4j.mp.component.WeixinComponentTokenCreator; import com.foxinmy.weixin4j.mp.component.WeixinComponentTokenCreator;
import com.foxinmy.weixin4j.mp.component.WeixinTokenComponentCreator;
import com.foxinmy.weixin4j.mp.model.AuthorizerOption; import com.foxinmy.weixin4j.mp.model.AuthorizerOption;
import com.foxinmy.weixin4j.mp.model.AuthorizerOption.AuthorizerOptionName; import com.foxinmy.weixin4j.mp.model.AuthorizerOption.AuthorizerOptionName;
import com.foxinmy.weixin4j.mp.model.ComponentAuthorizer; import com.foxinmy.weixin4j.mp.model.ComponentAuthorizer;
@ -20,6 +21,7 @@ import com.foxinmy.weixin4j.mp.model.ComponentAuthorizerToken;
import com.foxinmy.weixin4j.mp.model.OauthToken; import com.foxinmy.weixin4j.mp.model.OauthToken;
import com.foxinmy.weixin4j.token.PerTicketManager; import com.foxinmy.weixin4j.token.PerTicketManager;
import com.foxinmy.weixin4j.token.TicketManager; import com.foxinmy.weixin4j.token.TicketManager;
import com.foxinmy.weixin4j.token.TokenCreator;
import com.foxinmy.weixin4j.token.TokenManager; import com.foxinmy.weixin4j.token.TokenManager;
import com.foxinmy.weixin4j.util.Consts; import com.foxinmy.weixin4j.util.Consts;
import com.foxinmy.weixin4j.util.Weixin4jConfigUtil; import com.foxinmy.weixin4j.util.Weixin4jConfigUtil;
@ -32,7 +34,8 @@ import com.foxinmy.weixin4j.util.Weixin4jConfigUtil;
* @date 2015年6月17日 * @date 2015年6月17日
* @since JDK 1.6 * @since JDK 1.6
* @see <a href= * @see <a href=
* "https://open.weixin.qq.com/cgi-bin/showdocument?action=dir_list&t=resource/res_list&verify=1&id=open1453779503&token=&lang=zh_CN">第三方应用组件概述</a> * "https://open.weixin.qq.com/cgi-bin/showdocument?action=dir_list&t=resource/res_list&verify=1&id=open1453779503&token=&lang=zh_CN">
* 第三方应用组件概述</a>
*/ */
public class ComponentApi extends MpApi { public class ComponentApi extends MpApi {
/** /**
@ -55,11 +58,10 @@ public class ComponentApi extends MpApi {
*/ */
public ComponentApi(TicketManager ticketManager) { public ComponentApi(TicketManager ticketManager) {
this.ticketManager = ticketManager; this.ticketManager = ticketManager;
this.tokenManager = new TokenManager(new WeixinComponentTokenCreator( this.tokenManager = new TokenManager(new WeixinComponentTokenCreator(ticketManager),
ticketManager), ticketManager.getCacheStorager()); ticketManager.getCacheStorager());
this.preCodeManager = new TokenManager( this.preCodeManager = new TokenManager(
new WeixinComponentPreCodeCreator(tokenManager, new WeixinComponentPreCodeCreator(tokenManager, ticketManager.getThirdId()),
ticketManager.getThirdId()),
ticketManager.getCacheStorager()); ticketManager.getCacheStorager());
} }
@ -99,28 +101,26 @@ public class ComponentApi extends MpApi {
* @return 应用组件的perticket管理 * @return 应用组件的perticket管理
*/ */
public PerTicketManager getRefreshTokenManager(String authAppId) { public PerTicketManager getRefreshTokenManager(String authAppId) {
return new PerTicketManager(authAppId, ticketManager.getThirdId(), return new PerTicketManager(authAppId, ticketManager.getThirdId(), ticketManager.getThirdSecret(),
ticketManager.getThirdSecret(),
ticketManager.getCacheStorager()); ticketManager.getCacheStorager());
} }
/** /**
* 第三方组件代替授权公众号发起网页授权获取code <li> * 第三方组件代替授权公众号发起网页授权获取code
* redirectUri默认填写weixin4j.properties#component.user.oauth.redirect.uri <li> * <li>redirectUri默认填写weixin4j.properties#component.user.oauth.redirect.uri
* scope默认填写snsapi_base <li> * <li>scope默认填写snsapi_base
* state默认填写state * <li>state默认填写state
* *
* @param authAppId * @param authAppId
* 公众号的appid * 公众号的appid
* @see #getAuthorizationURL(String, String, String, String) * @see #getAuthorizationURL(String, String, String, String)
* @see <a * @see <a href=
* href="https://open.weixin.qq.com/cgi-bin/showdocument?action=dir_list&t=resource/res_list&verify=1&id=open1419318590&token=&lang=zh_CN">第三方组件代替授权公众号发起网页授权</a> * "https://open.weixin.qq.com/cgi-bin/showdocument?action=dir_list&t=resource/res_list&verify=1&id=open1419318590&token=&lang=zh_CN">
* 第三方组件代替授权公众号发起网页授权</a>
*/ */
public String getUserAuthorizationURL(String authAppId) { public String getUserAuthorizationURL(String authAppId) {
String redirectUri = Weixin4jConfigUtil String redirectUri = Weixin4jConfigUtil.getValue("component.user.oauth.redirect.uri");
.getValue("component.user.oauth.redirect.uri"); return getUserAuthorizationURL(authAppId, redirectUri, "snsapi_base", "state");
return getUserAuthorizationURL(authAppId, redirectUri, "snsapi_base",
"state");
} }
/** /**
@ -135,16 +135,15 @@ public class ComponentApi extends MpApi {
* @param state * @param state
* 重定向后会带上state参数开发者可以填写任意参数值最多128字节 * 重定向后会带上state参数开发者可以填写任意参数值最多128字节
* @return oauth授权URL * @return oauth授权URL
* @see <a * @see <a href=
* href="https://open.weixin.qq.com/cgi-bin/showdocument?action=dir_list&t=resource/res_list&verify=1&id=open1419318590&token=&lang=zh_CN">第三方组件代替授权公众号发起网页授权</a> * "https://open.weixin.qq.com/cgi-bin/showdocument?action=dir_list&t=resource/res_list&verify=1&id=open1419318590&token=&lang=zh_CN">
* 第三方组件代替授权公众号发起网页授权</a>
*/ */
public String getUserAuthorizationURL(String authAppId, String redirectUri, public String getUserAuthorizationURL(String authAppId, String redirectUri, String scope, String state) {
String scope, String state) {
String sns_component_user_auth_uri = getRequestUri("sns_component_user_auth_uri"); String sns_component_user_auth_uri = getRequestUri("sns_component_user_auth_uri");
try { try {
return String.format(sns_component_user_auth_uri, authAppId, return String.format(sns_component_user_auth_uri, authAppId,
URLEncoder.encode(redirectUri, Consts.UTF_8.name()), scope, URLEncoder.encode(redirectUri, Consts.UTF_8.name()), scope, state, this.ticketManager.getThirdId());
state, this.ticketManager.getThirdId());
} catch (UnsupportedEncodingException e) { } catch (UnsupportedEncodingException e) {
; ;
} }
@ -163,16 +162,13 @@ public class ComponentApi extends MpApi {
* @see com.foxinmy.weixin4j.mp.model.OauthToken * @see com.foxinmy.weixin4j.mp.model.OauthToken
* @throws WeixinException * @throws WeixinException
*/ */
public OauthToken getAuthorizationToken(String authAppId, String code) public OauthToken getAuthorizationToken(String authAppId, String code) throws WeixinException {
throws WeixinException {
String sns_component_user_token_uri = getRequestUri("sns_component_user_token_uri"); String sns_component_user_token_uri = getRequestUri("sns_component_user_token_uri");
String accessToken = tokenManager.getAccessToken(); String accessToken = tokenManager.getAccessToken();
WeixinResponse response = weixinExecutor.get(String.format( WeixinResponse response = weixinExecutor.get(
sns_component_user_token_uri, authAppId, code, String.format(sns_component_user_token_uri, authAppId, code, ticketManager.getThirdId(), accessToken));
ticketManager.getThirdId(), accessToken));
JSONObject result = response.getAsJson(); JSONObject result = response.getAsJson();
OauthToken token = new OauthToken(result.getString("access_token"), OauthToken token = new OauthToken(result.getString("access_token"), result.getLongValue("expires_in") * 1000l);
result.getLongValue("expires_in") * 1000l);
token.setOpenId(result.getString("openid")); token.setOpenId(result.getString("openid"));
token.setScope(result.getString("scope")); token.setScope(result.getString("scope"));
token.setRefreshToken(result.getString("refresh_token")); token.setRefreshToken(result.getString("refresh_token"));
@ -192,16 +188,13 @@ public class ComponentApi extends MpApi {
* @see com.foxinmy.weixin4j.mp.model.OauthToken * @see com.foxinmy.weixin4j.mp.model.OauthToken
* @throws WeixinException * @throws WeixinException
*/ */
public OauthToken refreshAuthorizationToken(String authAppId, public OauthToken refreshAuthorizationToken(String authAppId, String refreshToken) throws WeixinException {
String refreshToken) throws WeixinException {
String sns_component_token_refresh_uri = getRequestUri("sns_component_token_refresh_uri"); String sns_component_token_refresh_uri = getRequestUri("sns_component_token_refresh_uri");
String accessToken = tokenManager.getAccessToken(); String accessToken = tokenManager.getAccessToken();
WeixinResponse response = weixinExecutor.get(String.format( WeixinResponse response = weixinExecutor.get(String.format(sns_component_token_refresh_uri, authAppId,
sns_component_token_refresh_uri, authAppId,
ticketManager.getThirdId(), accessToken, refreshToken)); ticketManager.getThirdId(), accessToken, refreshToken));
JSONObject result = response.getAsJson(); JSONObject result = response.getAsJson();
OauthToken token = new OauthToken(result.getString("access_token"), OauthToken token = new OauthToken(result.getString("access_token"), result.getLongValue("expires_in") * 1000l);
result.getLongValue("expires_in") * 1000l);
token.setOpenId(result.getString("openid")); token.setOpenId(result.getString("openid"));
token.setScope(result.getString("scope")); token.setScope(result.getString("scope"));
token.setRefreshToken(result.getString("refresh_token")); token.setRefreshToken(result.getString("refresh_token"));
@ -222,61 +215,31 @@ public class ComponentApi extends MpApi {
* @see com.foxinmy.weixin4j.mp.model.ComponentAuthorizerToken * @see com.foxinmy.weixin4j.mp.model.ComponentAuthorizerToken
* @throws WeixinException * @throws WeixinException
*/ */
public ComponentAuthorizerToken exchangeAuthorizerToken(String authCode) public ComponentAuthorizerToken exchangeAuthorizerToken(String authCode) throws WeixinException {
throws WeixinException {
String component_exchange_authorizer_uri = getRequestUri("component_query_authorization_uri"); String component_exchange_authorizer_uri = getRequestUri("component_query_authorization_uri");
JSONObject obj = new JSONObject(); JSONObject obj = new JSONObject();
obj.put("component_appid", ticketManager.getThirdId()); obj.put("component_appid", ticketManager.getThirdId());
obj.put("authorization_code", authCode); obj.put("authorization_code", authCode);
WeixinResponse response = weixinExecutor.post( WeixinResponse response = weixinExecutor.post(
String.format(component_exchange_authorizer_uri, String.format(component_exchange_authorizer_uri, tokenManager.getAccessToken()), obj.toJSONString());
tokenManager.getAccessToken()), obj.toJSONString()); JSONObject authObj = response.getAsJson().getJSONObject("authorization_info");
obj = response.getAsJson();
JSONObject authObj = obj.getJSONObject("authorization_info");
JSONArray privilegesObj = authObj.getJSONArray("func_info"); JSONArray privilegesObj = authObj.getJSONArray("func_info");
List<Integer> privileges = new ArrayList<Integer>(privilegesObj.size()); List<Integer> privileges = new ArrayList<Integer>(privilegesObj.size());
for (int i = 0; i < privilegesObj.size(); i++) { for (int i = 0; i < privilegesObj.size(); i++) {
privileges.add(privilegesObj.getJSONObject(i) privileges.add(privilegesObj.getJSONObject(i).getJSONObject("funcscope_category").getInteger("id"));
.getJSONObject("funcscope_category").getInteger("id"));
} }
ComponentAuthorizerToken token = new ComponentAuthorizerToken( ComponentAuthorizerToken token = new ComponentAuthorizerToken(authObj.getString("authorizer_access_token"),
authObj.getString("authorizer_access_token"), authObj.getLongValue("expires_in") * 1000l);
authObj.getLongValue("expires_in"));
token.setRefreshToken(authObj.getString("authorizer_refresh_token")); token.setRefreshToken(authObj.getString("authorizer_refresh_token"));
token.setPrivileges(privileges); token.setPrivileges(privileges);
token.setAppId(authObj.getString("authorizer_appid")); token.setAppId(authObj.getString("authorizer_appid"));
return token; // 微信授权公众号的永久刷新令牌
} PerTicketManager perTicketManager = getRefreshTokenManager(token.getAppId());
// 缓存微信公众号的access_token
/** TokenCreator tokenCreator = new WeixinTokenComponentCreator(perTicketManager, tokenManager);
* 获取刷新授权公众号或小程序的接口调用凭据令牌 ticketManager.getCacheStorager().caching(tokenCreator.key(), token);
* // 缓存微信公众号的永久授权码(refresh_token)
* @param authAppId perTicketManager.cachingTicket(token.getRefreshToken());
* 授权方appid
* @param authRefreshToken
* 授权方的刷新令牌刷新令牌主要用于第三方平台获取和刷新已授权用户的access_token只会在授权时刻提供请妥善保存
* 一旦丢失只能让用户重新授权才能再次拿到新的刷新令牌
* @return 第三方组件授权信息
* @see {@link exchangeAuthInfo(String)}
* @see com.foxinmy.weixin4j.mp.model.ComponentAuthorizerToken
* @throws WeixinException
*/
public ComponentAuthorizerToken refreshAuthorizerToken(String authAppId,
String authRefreshToken) throws WeixinException {
String component_refresh_authorizer_token_uri = getRequestUri("component_refresh_authorizer_token_uri");
JSONObject obj = new JSONObject();
obj.put("component_appid", ticketManager.getThirdId());
obj.put("authorizer_appid", authAppId);
obj.put("authorizer_refresh_token", authRefreshToken);
WeixinResponse response = weixinExecutor.post(String.format(
component_refresh_authorizer_token_uri,
tokenManager.getAccessToken()), obj.toJSONString());
obj = response.getAsJson();
ComponentAuthorizerToken token = new ComponentAuthorizerToken(
obj.getString("authorizer_access_token"),
obj.getLongValue("expires_in"));
token.setRefreshToken(obj.getString("authorizer_refresh_token"));
token.setAppId(authAppId);
return token; return token;
} }
@ -291,29 +254,23 @@ public class ComponentApi extends MpApi {
* @see com.foxinmy.weixin4j.mp.model.ComponentAuthorizer * @see com.foxinmy.weixin4j.mp.model.ComponentAuthorizer
* @throws WeixinException * @throws WeixinException
*/ */
public ComponentAuthorizer getAuthorizerInfo(String authAppId) public ComponentAuthorizer getAuthorizerInfo(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();
obj.put("component_appid", ticketManager.getThirdId()); obj.put("component_appid", ticketManager.getThirdId());
obj.put("authorizer_appid", authAppId); obj.put("authorizer_appid", authAppId);
WeixinResponse response = weixinExecutor.post( WeixinResponse response = weixinExecutor
String.format(component_get_authorizer_uri, .post(String.format(component_get_authorizer_uri, tokenManager.getAccessToken()), obj.toJSONString());
tokenManager.getAccessToken()), obj.toJSONString());
obj = response.getAsJson(); obj = response.getAsJson();
JSONObject auth = obj.getJSONObject("authorizer_info"); JSONObject auth = obj.getJSONObject("authorizer_info");
ComponentAuthorizer authorizer = JSON.toJavaObject(auth, ComponentAuthorizer authorizer = JSON.toJavaObject(auth, ComponentAuthorizer.class);
ComponentAuthorizer.class); authorizer.setServiceType(auth.getJSONObject("service_type_info").getIntValue("id"));
authorizer.setServiceType(auth.getJSONObject("service_type_info") authorizer.setVerifyType(auth.getJSONObject("verify_type_info").getIntValue("id"));
.getIntValue("id"));
authorizer.setVerifyType(auth.getJSONObject("verify_type_info")
.getIntValue("id"));
auth = obj.getJSONObject("authorization_info"); auth = obj.getJSONObject("authorization_info");
JSONArray privilegesObj = auth.getJSONArray("func_info"); JSONArray privilegesObj = auth.getJSONArray("func_info");
List<Integer> privileges = new ArrayList<Integer>(privilegesObj.size()); List<Integer> privileges = new ArrayList<Integer>(privilegesObj.size());
for (int i = 0; i < privilegesObj.size(); i++) { for (int i = 0; i < privilegesObj.size(); i++) {
privileges.add(privilegesObj.getJSONObject(i) privileges.add(privilegesObj.getJSONObject(i).getJSONObject("funcscope_category").getInteger("id"));
.getJSONObject("funcscope_category").getInteger("id"));
} }
authorizer.setPrivileges(privileges); authorizer.setPrivileges(privileges);
authorizer.setAppId(auth.getString("appid")); authorizer.setAppId(auth.getString("appid"));
@ -331,16 +288,15 @@ public class ComponentApi extends MpApi {
* @see com.foxinmy.weixin4j.mp.model.AuthorizerOption * @see com.foxinmy.weixin4j.mp.model.AuthorizerOption
* @throws WeixinException * @throws WeixinException
*/ */
public AuthorizerOption getAuthorizerOption(String authAppId, public AuthorizerOption getAuthorizerOption(String authAppId, AuthorizerOptionName optionName)
AuthorizerOptionName optionName) throws WeixinException { throws WeixinException {
String component_get_authorizer_option_uri = getRequestUri("component_get_authorizer_option_uri"); String component_get_authorizer_option_uri = getRequestUri("component_get_authorizer_option_uri");
JSONObject obj = new JSONObject(); JSONObject obj = new JSONObject();
obj.put("component_appid", ticketManager.getThirdId()); obj.put("component_appid", ticketManager.getThirdId());
obj.put("authorizer_appid", authAppId); obj.put("authorizer_appid", authAppId);
obj.put("option_name", optionName.name()); obj.put("option_name", optionName.name());
WeixinResponse response = weixinExecutor.post( WeixinResponse response = weixinExecutor.post(
String.format(component_get_authorizer_option_uri, String.format(component_get_authorizer_option_uri, tokenManager.getAccessToken()), obj.toJSONString());
tokenManager.getAccessToken()), obj.toJSONString());
int optionValue = response.getAsJson().getIntValue("option_value"); int optionValue = response.getAsJson().getIntValue("option_value");
return AuthorizerOption.parse(optionName, optionValue); return AuthorizerOption.parse(optionName, optionValue);
} }
@ -354,8 +310,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 ApiResult setAuthorizerOption(String authAppId, public ApiResult 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();
obj.put("component_appid", ticketManager.getThirdId()); obj.put("component_appid", ticketManager.getThirdId());
@ -363,8 +318,7 @@ public class ComponentApi extends MpApi {
obj.put("option_name", option.getName()); obj.put("option_name", option.getName());
obj.put("option_value", option.getValue()); obj.put("option_value", option.getValue());
WeixinResponse response = weixinExecutor.post( WeixinResponse response = weixinExecutor.post(
String.format(component_set_authorizer_option_uri, String.format(component_set_authorizer_option_uri, tokenManager.getAccessToken()), obj.toJSONString());
tokenManager.getAccessToken()), obj.toJSONString());
return response.getAsResult(); return response.getAsResult();
} }
} }

View File

@ -19,43 +19,43 @@ import com.foxinmy.weixin4j.token.TokenManager;
*/ */
public class WeixinTokenComponentCreator extends TokenCreator { public class WeixinTokenComponentCreator extends TokenCreator {
private final PerTicketManager perTicketManager; private final PerTicketManager perTicketManager;
private final TokenManager componentTokenManager; private final TokenManager componentTokenManager;
/** /**
* *
* @param perTicketManager * @param perTicketManager
* 第三方套件永久授权码 * 第三方套件永久授权码
* @param componentTokenManager * @param componentTokenManager
* 第三方套件凭证token * 第三方套件凭证token
*/ */
public WeixinTokenComponentCreator(PerTicketManager perTicketManager, TokenManager componentTokenManager) { public WeixinTokenComponentCreator(PerTicketManager perTicketManager, TokenManager componentTokenManager) {
this.perTicketManager = perTicketManager; this.perTicketManager = perTicketManager;
this.componentTokenManager = componentTokenManager; this.componentTokenManager = componentTokenManager;
} }
@Override @Override
public String name() { public String name() {
return String.format("mp_token_component_%s_%s", perTicketManager.getThirdId(), return String.format("mp_token_component_%s_%s", perTicketManager.getThirdId(),
perTicketManager.getAuthAppId()); perTicketManager.getAuthAppId());
} }
@Override @Override
public String uniqueid() { public String uniqueid() {
throw new UnsupportedOperationException(); throw new UnsupportedOperationException();
} }
@Override
public Token create() throws WeixinException {
JSONObject obj = new JSONObject();
obj.put("component_appid", perTicketManager.getThirdId());
obj.put("authorizer_appid", perTicketManager.getAuthAppId());
obj.put("authorizer_refresh_token", perTicketManager.getAccessTicket());
WeixinResponse response = weixinExecutor.post(
String.format(URLConsts.TOKEN_COMPONENT_URL, componentTokenManager.getAccessToken()),
obj.toJSONString());
obj = response.getAsJson();
return new Token(obj.getString("access_token"), obj.getLongValue("expires_in") * 1000l);
}
@Override
public Token create() throws WeixinException {
JSONObject obj = new JSONObject();
obj.put("component_appid", perTicketManager.getThirdId());
obj.put("authorizer_appid", perTicketManager.getAuthAppId());
obj.put("authorizer_refresh_token", perTicketManager.getAccessTicket());
WeixinResponse response = weixinExecutor.post(
String.format(URLConsts.TOKEN_COMPONENT_URL, componentTokenManager.getAccessToken()),
obj.toJSONString());
obj = response.getAsJson();
perTicketManager.cachingTicket(obj.getString("authorizer_refresh_token"));
return new Token(obj.getString("authorizer_access_token"), obj.getLongValue("expires_in") * 1000l);
}
} }