初始化开放平台第三方组件
This commit is contained in:
+13
-1
@@ -220,4 +220,16 @@
|
||||
|
||||
* 2016-05-07
|
||||
|
||||
+ version upgrade to 1.6.9
|
||||
+ version upgrade to 1.6.9
|
||||
|
||||
* 2016-05-30
|
||||
|
||||
+ 新增接口调用次数清零接口
|
||||
|
||||
* 2016-06-20
|
||||
|
||||
+ version upgrade to 1.7.0
|
||||
|
||||
* 2016-07-05
|
||||
|
||||
+ 初始化开放平台第三方组件TokenCreator
|
||||
@@ -0,0 +1,71 @@
|
||||
package com.foxinmy.weixin4j.mp.api;
|
||||
|
||||
import com.foxinmy.weixin4j.mp.component.WeixinComponentPreCodeCreator;
|
||||
import com.foxinmy.weixin4j.mp.component.WeixinComponentTokenCreator;
|
||||
import com.foxinmy.weixin4j.token.TicketManager;
|
||||
import com.foxinmy.weixin4j.token.TokenManager;
|
||||
|
||||
/**
|
||||
* 第三方应用组件
|
||||
*
|
||||
* @className ComponentApi
|
||||
* @author jinyu(foxinmy@gmail.com)
|
||||
* @date 2015年6月17日
|
||||
* @since JDK 1.6
|
||||
* @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>
|
||||
*/
|
||||
public class ComponentApi extends MpApi {
|
||||
/**
|
||||
* 应用套件token
|
||||
*/
|
||||
private final TokenManager tokenManager;
|
||||
/**
|
||||
* 应用套件ticket
|
||||
*/
|
||||
private final TicketManager ticketManager;
|
||||
/**
|
||||
* 应用套件pre_code
|
||||
*/
|
||||
private final TokenManager preCodeManager;
|
||||
|
||||
/**
|
||||
*
|
||||
* @param ticketManager
|
||||
* 组件ticket存取
|
||||
*/
|
||||
public ComponentApi(TicketManager ticketManager) {
|
||||
this.ticketManager = ticketManager;
|
||||
this.tokenManager = new TokenManager(new WeixinComponentTokenCreator(ticketManager),
|
||||
ticketManager.getCacheStorager());
|
||||
this.preCodeManager = new TokenManager(new WeixinComponentPreCodeCreator(tokenManager, ticketManager.getId()),
|
||||
ticketManager.getCacheStorager());
|
||||
}
|
||||
|
||||
/**
|
||||
* 应用组件token
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
public TokenManager getTokenManager() {
|
||||
return this.tokenManager;
|
||||
}
|
||||
|
||||
/**
|
||||
* 应用组件ticket
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
public TicketManager getTicketManager() {
|
||||
return this.ticketManager;
|
||||
}
|
||||
|
||||
/**
|
||||
* 应用组件预授权码
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
public TokenManager getPreCodeManager() {
|
||||
return this.preCodeManager;
|
||||
}
|
||||
}
|
||||
+49
@@ -0,0 +1,49 @@
|
||||
package com.foxinmy.weixin4j.mp.component;
|
||||
|
||||
import com.alibaba.fastjson.JSONObject;
|
||||
import com.foxinmy.weixin4j.exception.WeixinException;
|
||||
import com.foxinmy.weixin4j.http.weixin.WeixinResponse;
|
||||
import com.foxinmy.weixin4j.model.Token;
|
||||
import com.foxinmy.weixin4j.mp.type.URLConsts;
|
||||
import com.foxinmy.weixin4j.token.TokenCreator;
|
||||
import com.foxinmy.weixin4j.token.TokenManager;
|
||||
|
||||
/**
|
||||
* 微信企业号应用套件预授权码创建
|
||||
*
|
||||
* @className WeixinComponentPreCodeCreator
|
||||
* @author jinyu(foxinmy@gmail.com)
|
||||
* @date 2016年7月5日
|
||||
* @since JDK 1.6
|
||||
*/
|
||||
public class WeixinComponentPreCodeCreator extends TokenCreator {
|
||||
|
||||
private final TokenManager componentTokenManager;
|
||||
private final String componentId;
|
||||
|
||||
/**
|
||||
*
|
||||
* @param componentTokenManager
|
||||
* 应用套件的token
|
||||
* @param componentId
|
||||
* 应用组件ID
|
||||
*/
|
||||
public WeixinComponentPreCodeCreator(TokenManager componentTokenManager, String componentId) {
|
||||
this.componentTokenManager = componentTokenManager;
|
||||
this.componentId = componentId;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String key0() {
|
||||
return String.format("mp_component_precode_%s", componentId);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Token create() throws WeixinException {
|
||||
WeixinResponse response = weixinExecutor.post(
|
||||
String.format(URLConsts.COMPONENET_PRE_CODE_URL, componentTokenManager.getAccessToken()),
|
||||
String.format("{\"component_appid\":\"%s\"}", componentId));
|
||||
JSONObject result = response.getAsJson();
|
||||
return new Token(result.getString("pre_auth_code"), result.getLongValue("expires_in") * 1000l);
|
||||
}
|
||||
}
|
||||
+48
@@ -0,0 +1,48 @@
|
||||
package com.foxinmy.weixin4j.mp.component;
|
||||
|
||||
import com.alibaba.fastjson.JSONObject;
|
||||
import com.foxinmy.weixin4j.exception.WeixinException;
|
||||
import com.foxinmy.weixin4j.http.weixin.WeixinResponse;
|
||||
import com.foxinmy.weixin4j.model.Token;
|
||||
import com.foxinmy.weixin4j.mp.type.URLConsts;
|
||||
import com.foxinmy.weixin4j.token.TicketManager;
|
||||
import com.foxinmy.weixin4j.token.TokenCreator;
|
||||
|
||||
/**
|
||||
* 微信公众号应用组件凭证创建
|
||||
*
|
||||
* @className WeixinComponentTokenCreator
|
||||
* @author jinyu(foxinmy@gmail.com)
|
||||
* @date 2016年7月5日
|
||||
* @since JDK 1.6
|
||||
*/
|
||||
public class WeixinComponentTokenCreator extends TokenCreator {
|
||||
private final TicketManager ticketManager;
|
||||
|
||||
/**
|
||||
*
|
||||
* @param ticketManager
|
||||
* 组件ticket存取
|
||||
*/
|
||||
public WeixinComponentTokenCreator(TicketManager ticketManager) {
|
||||
this.ticketManager = ticketManager;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String key0() {
|
||||
return String.format("mp_component_token_%s", ticketManager.getId());
|
||||
}
|
||||
|
||||
@Override
|
||||
public Token create() throws WeixinException {
|
||||
JSONObject obj = new JSONObject();
|
||||
obj.put("component_appid", ticketManager.getId());
|
||||
obj.put("component_appsecret", ticketManager.getSecret());
|
||||
obj.put("component_verify_ticket", ticketManager.getTicket());
|
||||
WeixinResponse response = weixinExecutor.post(
|
||||
URLConsts.COMPONENT_TOKEN_URL, obj.toJSONString());
|
||||
obj = response.getAsJson();
|
||||
return new Token(obj.getString("suite_access_token"),
|
||||
obj.getLongValue("expires_in") * 1000l);
|
||||
}
|
||||
}
|
||||
@@ -14,11 +14,18 @@ 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";
|
||||
/**
|
||||
* 开发平台获取预授权码的url
|
||||
*/
|
||||
public static final String COMPONENET_PRE_CODE_URL = BASE_URL
|
||||
+ "/component/api_create_preauthcode?component_access_token=%s";
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user