新增第三方组件WeixinComponentProxy
This commit is contained in:
@@ -234,4 +234,8 @@
|
||||
|
||||
+ 初始化开放平台第三方组件TokenCreator
|
||||
|
||||
+ 新增第三方组件ComponentApi
|
||||
+ 新增第三方组件ComponentApi
|
||||
|
||||
* 2016-07-06
|
||||
|
||||
+ 新增第三方组件WeixinComponentProxy
|
||||
@@ -32,6 +32,8 @@ weixin4j-mp
|
||||
* TmplApi `模板消息API`
|
||||
|
||||
* UserApi `用户管理API`
|
||||
|
||||
* ComponentApi `第三方组件API`
|
||||
|
||||
[如何使用](https://github.com/foxinmy/weixin4j/wiki)
|
||||
---------
|
||||
|
||||
@@ -0,0 +1,189 @@
|
||||
package com.foxinmy.weixin4j.mp;
|
||||
|
||||
import java.io.UnsupportedEncodingException;
|
||||
import java.net.URLEncoder;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
import com.alibaba.fastjson.JSON;
|
||||
import com.foxinmy.weixin4j.exception.WeixinException;
|
||||
import com.foxinmy.weixin4j.model.Consts;
|
||||
import com.foxinmy.weixin4j.model.Token;
|
||||
import com.foxinmy.weixin4j.model.WeixinAccount;
|
||||
import com.foxinmy.weixin4j.mp.api.ComponentApi;
|
||||
import com.foxinmy.weixin4j.mp.model.WeixinMpAccount;
|
||||
import com.foxinmy.weixin4j.mp.type.URLConsts;
|
||||
import com.foxinmy.weixin4j.setting.Weixin4jSettings;
|
||||
import com.foxinmy.weixin4j.token.TicketManager;
|
||||
import com.foxinmy.weixin4j.util.StringUtil;
|
||||
import com.foxinmy.weixin4j.util.Weixin4jConfigUtil;
|
||||
|
||||
/**
|
||||
* 微信第三方应用接口实现
|
||||
*
|
||||
* @className WeixinComponentProxy
|
||||
* @author jinyu(foxinmy@gmail.com)
|
||||
* @date 2016年7月5日
|
||||
* @since JDK 1.6
|
||||
* @see com.foxinmy.weixin4j.mp.api.ComponentApi
|
||||
* @see <a href=
|
||||
* "https://open.weixin.qq.com/cgi-bin/showdocument?action=dir_list&t=resource/res_list&verify=1&id=open1419318292&token=&lang=zh_CN">
|
||||
* 公众号第三方应用</a>
|
||||
*/
|
||||
public class WeixinComponentProxy {
|
||||
|
||||
/**
|
||||
* 每个组件授权不一样 componentId - componentApi
|
||||
*/
|
||||
private Map<String, ComponentApi> componentMap;
|
||||
/**
|
||||
* 配置相关
|
||||
*/
|
||||
private final Weixin4jSettings<WeixinMpAccount> settings;
|
||||
|
||||
/**
|
||||
* 默认使用文件方式保存token、使用weixin4j.properties配置的账号信息
|
||||
*/
|
||||
public WeixinComponentProxy() {
|
||||
this(new Weixin4jSettings<WeixinMpAccount>(
|
||||
JSON.parseObject(Weixin4jConfigUtil.getValue("account"), WeixinMpAccount.class)));
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @param settings
|
||||
* 配置信息
|
||||
*/
|
||||
public WeixinComponentProxy(Weixin4jSettings<WeixinMpAccount> settings) {
|
||||
this.settings = settings;
|
||||
List<WeixinAccount> components = settings.getAccount().getComponents();
|
||||
this.componentMap = new HashMap<String, ComponentApi>(components.size());
|
||||
for (WeixinAccount component : components) {
|
||||
this.componentMap.put(component.getId(), new ComponentApi(
|
||||
new TicketManager(component.getId(), component.getSecret(), settings.getCacheStorager0())));
|
||||
}
|
||||
this.componentMap.put(null, componentMap.get(components.get(0).getId()));
|
||||
}
|
||||
|
||||
/**
|
||||
* 公众号信息
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
public WeixinMpAccount getWeixinAccount() {
|
||||
return this.settings.getAccount();
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取组接口对象(只关注第一个套件
|
||||
*
|
||||
* @see com.foxinmy.weixin4j.mp.api.ComponentApi
|
||||
* @return API实例
|
||||
*/
|
||||
public ComponentApi component() {
|
||||
return this.componentMap.get(null);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取套件接口对象(多个套件
|
||||
*
|
||||
* @see com.foxinmy.weixin4j.mp.api.ComponentApi
|
||||
* @param componentId
|
||||
* 组件ID
|
||||
* @return API实例
|
||||
*/
|
||||
public ComponentApi component(String componentId) {
|
||||
return this.componentMap.get(componentId);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取组件的预授权码 <font color="red">需先缓存ticket</font>
|
||||
*
|
||||
* @param componentId
|
||||
* 组件ID
|
||||
* @return 预授权码
|
||||
* @see com.foxinmy.weixin4j.mp.api.ComponentApi
|
||||
* @see com.foxinmy.weixin4j.mp.api.ComponentApi#getTicketManager()
|
||||
* @see com.foxinmy.weixin4j.mp.api.ComponentApi#getPreCodeManager()
|
||||
* @throws WeixinException
|
||||
*/
|
||||
public String getPreComponentTicket(String componentId) throws WeixinException {
|
||||
Token token = component(componentId).getTicketManager().getTicket();
|
||||
if (token == null || StringUtil.isBlank(token.getAccessToken())) {
|
||||
throw new WeixinException("maybe oauth first?");
|
||||
}
|
||||
return token.getAccessToken();
|
||||
}
|
||||
|
||||
/**
|
||||
* 缓存组件ticket
|
||||
*
|
||||
* @param componentId
|
||||
* 组件ID
|
||||
* @param componentTicket
|
||||
* 组件ticket内容
|
||||
* @throws WeixinException
|
||||
*/
|
||||
public void cacheTicket(String componentId, String componentTicket) throws WeixinException {
|
||||
component(componentId).getTicketManager().cachingTicket(componentTicket);
|
||||
}
|
||||
|
||||
/**
|
||||
* 应用组件授权 <font color="red">需先缓存ticket</font>
|
||||
*
|
||||
* @see {@link #getComponentAuthorizeURL(String, String,String)}
|
||||
* @param componentId
|
||||
* 组件ID
|
||||
* @see {@link #cacheTicket(String, String)}
|
||||
* @return 请求授权的URL
|
||||
* @throws WeixinException
|
||||
*/
|
||||
public String getComponentAuthorizeURL(String componentId) throws WeixinException {
|
||||
String redirectUri = Weixin4jConfigUtil.getValue("component.oauth.redirect.uri");
|
||||
return getComponentAuthorizeURL(componentId, redirectUri, "state");
|
||||
}
|
||||
|
||||
/**
|
||||
* 应用组件授权 <font color="red">需先缓存ticket</font>
|
||||
*
|
||||
* @param componentId
|
||||
* 组件ID
|
||||
* @param redirectUri
|
||||
* 授权后重定向url
|
||||
* @param state
|
||||
* 回调后原样返回
|
||||
* @see com.foxinmy.weixin4j.mp.api.ComponentApi
|
||||
* @see com.foxinmy.weixin4j.mp.api.ComponentApi#getTicketManager()
|
||||
* @see com.foxinmy.weixin4j.mp.api.ComponentApi#getPreCodeManager()
|
||||
* @return 请求授权的URL
|
||||
* @throws WeixinException
|
||||
*/
|
||||
public String getComponentAuthorizeURL(String componentId, String redirectUri, String state)
|
||||
throws WeixinException {
|
||||
try {
|
||||
return String.format(URLConsts.COMPONENT_OAUTH_URL, componentId, getPreComponentTicket(componentId),
|
||||
URLEncoder.encode(redirectUri, Consts.UTF_8.name()), state);
|
||||
} catch (UnsupportedEncodingException e) {
|
||||
;
|
||||
}
|
||||
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.0";
|
||||
}
|
||||
@@ -63,7 +63,7 @@ public class ComponentApi extends MpApi {
|
||||
/**
|
||||
* 应用组件token
|
||||
*
|
||||
* @return
|
||||
* @return 应用组件的token管理
|
||||
*/
|
||||
public TokenManager getTokenManager() {
|
||||
return this.tokenManager;
|
||||
@@ -72,7 +72,7 @@ public class ComponentApi extends MpApi {
|
||||
/**
|
||||
* 应用组件ticket
|
||||
*
|
||||
* @return
|
||||
* @return 应用组件的ticket管理
|
||||
*/
|
||||
public TicketManager getTicketManager() {
|
||||
return this.ticketManager;
|
||||
@@ -81,7 +81,7 @@ public class ComponentApi extends MpApi {
|
||||
/**
|
||||
* 应用组件预授权码
|
||||
*
|
||||
* @return
|
||||
* @return 应用组件的precode管理
|
||||
*/
|
||||
public TokenManager getPreCodeManager() {
|
||||
return this.preCodeManager;
|
||||
@@ -93,7 +93,7 @@ public class ComponentApi extends MpApi {
|
||||
*
|
||||
* @param authAppId
|
||||
* 授权方appid
|
||||
* @return
|
||||
* @return 应用组件的perticket管理
|
||||
*/
|
||||
public PerTicketManager getRefreshTokenManager(String authAppId) {
|
||||
return new PerTicketManager(authAppId, ticketManager.getThirdId(),
|
||||
|
||||
@@ -0,0 +1,54 @@
|
||||
package com.foxinmy.weixin4j.mp.model;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import com.alibaba.fastjson.annotation.JSONCreator;
|
||||
import com.alibaba.fastjson.annotation.JSONField;
|
||||
import com.foxinmy.weixin4j.model.WeixinAccount;
|
||||
|
||||
/**
|
||||
* 微信公众号信息
|
||||
*
|
||||
* @className WeixinMpAccount
|
||||
* @author jinyu
|
||||
* @date Jul 6, 2016
|
||||
* @since JDK 1.8
|
||||
* @see
|
||||
*/
|
||||
public class WeixinMpAccount extends WeixinAccount {
|
||||
|
||||
private static final long serialVersionUID = 3689999353867189585L;
|
||||
/**
|
||||
* 多个应用组件信息
|
||||
*/
|
||||
private List<WeixinAccount> components;
|
||||
|
||||
/**
|
||||
*
|
||||
* @param appId
|
||||
* 应用组件ID 必填
|
||||
* @param appSecret
|
||||
* 应用组件密钥 使用普通接口(WeixinProxy对象)必须填写
|
||||
* @param components
|
||||
* 应用组件集合 使用套件接口(WeixinSuiteProxy#SuiteApi)必须填写
|
||||
*/
|
||||
@JSONCreator
|
||||
public WeixinMpAccount(@JSONField(name = "id") String appId, @JSONField(name = "secret") String appSecret,
|
||||
@JSONField(name = "components") List<WeixinAccount> components) {
|
||||
super(appId, appSecret);
|
||||
this.components = components;
|
||||
}
|
||||
|
||||
public List<WeixinAccount> getComponents() {
|
||||
return components;
|
||||
}
|
||||
|
||||
public WeixinAccount[] componentsToArray() {
|
||||
return components != null ? components.toArray(new WeixinAccount[components.size()]) : null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "WeixinMpAccount [" + super.toString() + ", components=" + components + "]";
|
||||
}
|
||||
}
|
||||
@@ -14,18 +14,15 @@ public final class URLConsts {
|
||||
/**
|
||||
* 公众平台获取token的url
|
||||
*/
|
||||
public static final String ASSESS_TOKEN_URL = BASE_URL
|
||||
+ "/token?grant_type=client_credential&appid=%s&secret=%s";
|
||||
public static final String ASSESS_TOKEN_URL = BASE_URL + "/token?grant_type=client_credential&appid=%s&secret=%s";
|
||||
/**
|
||||
* 公众平台jssdk获取token的url
|
||||
*/
|
||||
public static final String JS_TICKET_URL = BASE_URL
|
||||
+ "/ticket/getticket?access_token=%s&type=%s";
|
||||
public static final String JS_TICKET_URL = BASE_URL + "/ticket/getticket?access_token=%s&type=%s";
|
||||
/**
|
||||
* 开放平台获取token的url
|
||||
*/
|
||||
public static final String COMPONENT_TOKEN_URL = BASE_URL
|
||||
+ "/component/api_component_token";
|
||||
public static final String COMPONENT_TOKEN_URL = BASE_URL + "/component/api_component_token";
|
||||
/**
|
||||
* 开放平台获取预授权码的url
|
||||
*/
|
||||
@@ -36,4 +33,9 @@ public final class URLConsts {
|
||||
*/
|
||||
public static final String TOKEN_COMPONENT_URL = BASE_URL
|
||||
+ "/component/api_authorizer_token?component_access_token=%s";
|
||||
/**
|
||||
* 开放平台oauth授权的url
|
||||
*/
|
||||
public static final String COMPONENT_OAUTH_URL = BASE_URL
|
||||
+ "/componentloginpage?component_appid=%s&pre_auth_code=%s&redirect_uri=%s";
|
||||
}
|
||||
|
||||
@@ -2,6 +2,7 @@
|
||||
|
||||
# \u516c\u4f17\u53f7\u4fe1\u606f \u8bf7\u6309\u9700\u586b\u5199
|
||||
weixin4j.account={"id":"wx4ab8f8de58159a57","secret":"1d4eb0f4bf556aaed539f30ed05ca795",\
|
||||
"components":[{"id":"\u5e94\u7528\u7ec4\u4ef6\u7684id","secret":"\u5e94\u7528\u7ec4\u4ef6\u7684secret"}],\
|
||||
"mchId":"\u5fae\u4fe1\u5546\u6237\u53f7 \u5fae\u4fe1\u652f\u4ed8\u65f6\u9700\u8981\u586b\u5165",\
|
||||
"certificateKey":"\u52a0\u8f7d\u652f\u4ed8\u8bc1\u4e66\u6587\u4ef6\u7684\u5bc6\u7801 \u5982\u679c\u4e0d\u586b\u5199\u5219\u9ed8\u8ba4\u83b7\u53d6mchId\u4f5c\u4e3a\u5bc6\u7801",\
|
||||
"paySignKey":"\u5fae\u4fe1\u652f\u4ed8\u4e2d\u8c03\u7528API\u7684\u5bc6\u94a5 \u5fae\u4fe1\u652f\u4ed8\u65f6\u9700\u8981\u586b\u5165"}
|
||||
@@ -17,4 +18,7 @@ weixin4j.tmpdir=
|
||||
weixin4j.certificate.file=/tmp/weixin4j/xxxxx.p12
|
||||
|
||||
# \u7528\u6237oauth\u6388\u6743\u540e\u91cd\u5b9a\u5411\u7684url(\u5728\u4f7f\u7528OauthApi\u65f6\u586b\u5199)
|
||||
weixin4j.user.oauth.redirect.uri=
|
||||
weixin4j.user.oauth.redirect.uri=
|
||||
|
||||
# \u7b2c\u4e09\u65b9\u7ec4\u4ef6\u6388\u6743\u540e\u91cd\u5b9a\u5411\u7684url(\u5728\u4f7f\u7528ComponentApi\u65f6\u586b\u5199)
|
||||
weixin4j.component.oauth.redirect.uri=
|
||||
Reference in New Issue
Block a user