TokenCreator新增uniqueid方法
This commit is contained in:
parent
4ccc11d8cf
commit
c8726ddc3f
@ -15,70 +15,69 @@ import com.foxinmy.weixin4j.exception.WeixinException;
|
||||
* @see
|
||||
*/
|
||||
public class CacheManager<T extends Cacheable> {
|
||||
protected final CacheCreator<T> cacheCreator;
|
||||
protected final CacheStorager<T> cacheStorager;
|
||||
private final ReentrantLock lock = new ReentrantLock();
|
||||
protected final CacheCreator<T> cacheCreator;
|
||||
protected final CacheStorager<T> cacheStorager;
|
||||
private final ReentrantLock lock = new ReentrantLock();
|
||||
|
||||
public CacheManager(CacheCreator<T> cacheCreator,
|
||||
CacheStorager<T> cacheStorager) {
|
||||
this.cacheCreator = cacheCreator;
|
||||
this.cacheStorager = cacheStorager;
|
||||
}
|
||||
public CacheManager(CacheCreator<T> cacheCreator, CacheStorager<T> cacheStorager) {
|
||||
this.cacheCreator = cacheCreator;
|
||||
this.cacheStorager = cacheStorager;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取缓存对象
|
||||
*
|
||||
* @return 缓存对象
|
||||
* @throws WeixinException
|
||||
*/
|
||||
public T getCache() throws WeixinException {
|
||||
String cacheKey = cacheCreator.key();
|
||||
T cache = cacheStorager.lookup(cacheKey);
|
||||
try {
|
||||
if (cache == null && lock.tryLock(3, TimeUnit.SECONDS)) {
|
||||
try {
|
||||
cache = cacheStorager.lookup(cacheKey);
|
||||
if (cache == null) {
|
||||
cache = cacheCreator.create();
|
||||
cacheStorager.caching(cacheKey, cache);
|
||||
}
|
||||
} finally {
|
||||
lock.unlock();
|
||||
}
|
||||
}
|
||||
} catch (InterruptedException e) {
|
||||
throw new WeixinException("get cache error on lock", e);
|
||||
}
|
||||
return cache;
|
||||
}
|
||||
/**
|
||||
* 获取缓存对象
|
||||
*
|
||||
* @return 缓存对象
|
||||
* @throws WeixinException
|
||||
*/
|
||||
public T getCache() throws WeixinException {
|
||||
String cacheKey = cacheCreator.key();
|
||||
T cache = cacheStorager.lookup(cacheKey);
|
||||
try {
|
||||
if (cache == null && lock.tryLock(3, TimeUnit.SECONDS)) {
|
||||
try {
|
||||
cache = cacheStorager.lookup(cacheKey);
|
||||
if (cache == null) {
|
||||
cache = cacheCreator.create();
|
||||
cacheStorager.caching(cacheKey, cache);
|
||||
}
|
||||
} finally {
|
||||
lock.unlock();
|
||||
}
|
||||
}
|
||||
} catch (InterruptedException e) {
|
||||
throw new WeixinException("get cache error on lock", e);
|
||||
}
|
||||
return cache;
|
||||
}
|
||||
|
||||
/**
|
||||
* 刷新缓存对象
|
||||
*
|
||||
* @return 缓存对象
|
||||
* @throws WeixinException
|
||||
*/
|
||||
public T refreshCache() throws WeixinException {
|
||||
String cacheKey = cacheCreator.key();
|
||||
T cache = cacheCreator.create();
|
||||
cacheStorager.caching(cacheKey, cache);
|
||||
return cache;
|
||||
}
|
||||
/**
|
||||
* 刷新缓存对象
|
||||
*
|
||||
* @return 缓存对象
|
||||
* @throws WeixinException
|
||||
*/
|
||||
public T refreshCache() throws WeixinException {
|
||||
String cacheKey = cacheCreator.key();
|
||||
T cache = cacheCreator.create();
|
||||
cacheStorager.caching(cacheKey, cache);
|
||||
return cache;
|
||||
}
|
||||
|
||||
/**
|
||||
* 移除缓存
|
||||
*
|
||||
* @return 被移除的缓存对象
|
||||
*/
|
||||
public T evictCache() {
|
||||
String cacheKey = cacheCreator.key();
|
||||
return cacheStorager.evict(cacheKey);
|
||||
}
|
||||
/**
|
||||
* 移除缓存
|
||||
*
|
||||
* @return 被移除的缓存对象
|
||||
*/
|
||||
public T evictCache() {
|
||||
String cacheKey = cacheCreator.key();
|
||||
return cacheStorager.evict(cacheKey);
|
||||
}
|
||||
|
||||
/**
|
||||
* 清除所有的缓存(<font color="red">请慎重</font>)
|
||||
*/
|
||||
public void clearCache() {
|
||||
cacheStorager.clear();
|
||||
}
|
||||
/**
|
||||
* 清除所有的缓存(<font color="red">请慎重</font>)
|
||||
*/
|
||||
public void clearCache() {
|
||||
cacheStorager.clear();
|
||||
}
|
||||
}
|
||||
|
||||
@ -13,7 +13,6 @@ import com.foxinmy.weixin4j.util.DigestUtil;
|
||||
import com.foxinmy.weixin4j.util.MapUtil;
|
||||
import com.foxinmy.weixin4j.util.RandomUtil;
|
||||
import com.foxinmy.weixin4j.util.StringUtil;
|
||||
import com.foxinmy.weixin4j.util.Weixin4jConfigUtil;
|
||||
|
||||
/**
|
||||
* JSSDK配置类
|
||||
@ -25,106 +24,92 @@ import com.foxinmy.weixin4j.util.Weixin4jConfigUtil;
|
||||
* @see
|
||||
*/
|
||||
public class JSSDKConfigurator {
|
||||
private final TokenManager ticketTokenManager;
|
||||
private JSONObject config;
|
||||
private Set<JSSDKAPI> apis;
|
||||
private final TokenManager ticketTokenManager;
|
||||
private JSONObject config;
|
||||
private Set<JSSDKAPI> apis;
|
||||
|
||||
/**
|
||||
* ticket保存类 可调用WeixinProxy#getTicketManager获取
|
||||
*
|
||||
* @param ticketTokenManager
|
||||
*/
|
||||
public JSSDKConfigurator(TokenManager ticketTokenManager) {
|
||||
this.ticketTokenManager = ticketTokenManager;
|
||||
this.config = new JSONObject();
|
||||
this.apis = new HashSet<JSSDKAPI>();
|
||||
}
|
||||
/**
|
||||
* ticket保存类 可调用WeixinProxy#getTicketManager获取
|
||||
*
|
||||
* @param ticketTokenManager
|
||||
*/
|
||||
public JSSDKConfigurator(TokenManager ticketTokenManager) {
|
||||
this.ticketTokenManager = ticketTokenManager;
|
||||
this.config = new JSONObject();
|
||||
this.apis = new HashSet<JSSDKAPI>();
|
||||
}
|
||||
|
||||
/**
|
||||
* 开启调试模式,调用的所有api的返回值会在客户端alert出来,若要查看传入的参数,可以在pc端打开,参数信息会通过log打出,
|
||||
* 仅在pc端时才会打印。
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
public JSSDKConfigurator debugMode() {
|
||||
config.put("debug", true);
|
||||
return this;
|
||||
}
|
||||
/**
|
||||
* 开启调试模式,调用的所有api的返回值会在客户端alert出来,若要查看传入的参数,可以在pc端打开,参数信息会通过log打出,
|
||||
* 仅在pc端时才会打印。
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
public JSSDKConfigurator debugMode() {
|
||||
config.put("debug", true);
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* 公众号的唯一标识 不填则获取weixin4j.properties#account中的id
|
||||
*
|
||||
* @param appId
|
||||
* @return
|
||||
*/
|
||||
public JSSDKConfigurator appId(String appId) {
|
||||
config.put("appId", appId);
|
||||
return this;
|
||||
}
|
||||
/**
|
||||
* 需要使用的JS接口列表
|
||||
*
|
||||
* @see JSSDKAPI
|
||||
* @param apis
|
||||
* @return
|
||||
*/
|
||||
public JSSDKConfigurator apis(JSSDKAPI... apis) {
|
||||
for (JSSDKAPI api : apis) {
|
||||
this.apis.add(api);
|
||||
}
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* 需要使用的JS接口列表
|
||||
*
|
||||
* @see JSSDKAPI
|
||||
* @param apis
|
||||
* @return
|
||||
*/
|
||||
public JSSDKConfigurator apis(JSSDKAPI... apis) {
|
||||
for (JSSDKAPI api : apis) {
|
||||
this.apis.add(api);
|
||||
}
|
||||
return this;
|
||||
}
|
||||
/**
|
||||
* 需要使用的JS接口列表
|
||||
*
|
||||
* @see JSSDKAPI
|
||||
* @param apis
|
||||
* @return
|
||||
*/
|
||||
public JSSDKConfigurator apis(JSSDKAPI[]... apis) {
|
||||
for (JSSDKAPI[] api : apis) {
|
||||
apis(api);
|
||||
}
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* 需要使用的JS接口列表
|
||||
*
|
||||
* @see JSSDKAPI
|
||||
* @param apis
|
||||
* @return
|
||||
*/
|
||||
public JSSDKConfigurator apis(JSSDKAPI[]... apis) {
|
||||
for (JSSDKAPI[] api : apis) {
|
||||
apis(api);
|
||||
}
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* 生成config配置JSON串
|
||||
*
|
||||
* @param url
|
||||
* 当前网页的URL,不包含#及其后面部分
|
||||
* @return jssdk配置JSON字符串
|
||||
* @see <a
|
||||
* href="https://mp.weixin.qq.com/wiki?t=resource/res_main&id=mp1421141115&token=&lang=zh_CN">公众号JSSDK</a>
|
||||
* @see <a
|
||||
* href="http://qydev.weixin.qq.com/wiki/index.php?title=%E5%BE%AE%E4%BF%A1JS-SDK%E6%8E%A5%E5%8F%A3">企业号JSSDK</a>
|
||||
* @throws WeixinException
|
||||
*/
|
||||
public String toJSONConfig(String url) throws WeixinException {
|
||||
if (apis.isEmpty()) {
|
||||
throw new WeixinException("jsapilist not be empty");
|
||||
}
|
||||
Map<String, String> signMap = new HashMap<String, String>();
|
||||
String timestamp = DateUtil.timestamp2string();
|
||||
String noncestr = RandomUtil.generateString(24);
|
||||
signMap.put("timestamp", timestamp);
|
||||
signMap.put("noncestr", noncestr);
|
||||
signMap.put("jsapi_ticket", this.ticketTokenManager.getAccessToken());
|
||||
signMap.put("url", url);
|
||||
String sign = DigestUtil.SHA1(MapUtil.toJoinString(signMap, false,
|
||||
false));
|
||||
if (StringUtil.isBlank(config.getString("appId"))) {
|
||||
config.put("appId", Weixin4jConfigUtil.getWeixinAccount().getId());
|
||||
}
|
||||
if (StringUtil.isBlank(config.getString("debug"))) {
|
||||
config.put("debug", false);
|
||||
}
|
||||
config.put("timestamp", timestamp);
|
||||
config.put("nonceStr", noncestr);
|
||||
config.put("signature", sign);
|
||||
config.put("jsApiList", apis.toArray());
|
||||
return config.toJSONString();
|
||||
}
|
||||
/**
|
||||
* 生成config配置JSON串
|
||||
*
|
||||
* @param url
|
||||
* 当前网页的URL,不包含#及其后面部分
|
||||
* @return jssdk配置JSON字符串
|
||||
* @see <a href=
|
||||
* "https://mp.weixin.qq.com/wiki?t=resource/res_main&id=mp1421141115&token=&lang=zh_CN">公众号JSSDK</a>
|
||||
* @see <a href=
|
||||
* "http://qydev.weixin.qq.com/wiki/index.php?title=%E5%BE%AE%E4%BF%A1JS-SDK%E6%8E%A5%E5%8F%A3">企业号JSSDK</a>
|
||||
* @throws WeixinException
|
||||
*/
|
||||
public String toJSONConfig(String url) throws WeixinException {
|
||||
if (apis.isEmpty()) {
|
||||
throw new WeixinException("jsapilist not be empty");
|
||||
}
|
||||
Map<String, String> signMap = new HashMap<String, String>();
|
||||
String timestamp = DateUtil.timestamp2string();
|
||||
String noncestr = RandomUtil.generateString(24);
|
||||
signMap.put("timestamp", timestamp);
|
||||
signMap.put("noncestr", noncestr);
|
||||
signMap.put("jsapi_ticket", this.ticketTokenManager.getAccessToken());
|
||||
signMap.put("url", url);
|
||||
String sign = DigestUtil.SHA1(MapUtil.toJoinString(signMap, false, false));
|
||||
config.put("appId", ticketTokenManager.getWeixinId());
|
||||
if (StringUtil.isBlank(config.getString("debug"))) {
|
||||
config.put("debug", false);
|
||||
}
|
||||
config.put("timestamp", timestamp);
|
||||
config.put("nonceStr", noncestr);
|
||||
config.put("signature", sign);
|
||||
config.put("jsApiList", apis.toArray());
|
||||
return config.toJSONString();
|
||||
}
|
||||
}
|
||||
|
||||
@ -15,31 +15,38 @@ import com.foxinmy.weixin4j.model.Token;
|
||||
*/
|
||||
public abstract class TokenCreator implements CacheCreator<Token> {
|
||||
|
||||
/**
|
||||
* 缓存KEY前缀
|
||||
*/
|
||||
public final static String CACHEKEY_PREFIX = "weixin4j_";
|
||||
/**
|
||||
* 缓存KEY前缀
|
||||
*/
|
||||
public final static String CACHEKEY_PREFIX = "weixin4j_";
|
||||
|
||||
protected final WeixinRequestExecutor weixinExecutor;
|
||||
protected final WeixinRequestExecutor weixinExecutor;
|
||||
|
||||
public TokenCreator() {
|
||||
this.weixinExecutor = new WeixinRequestExecutor();
|
||||
}
|
||||
public TokenCreator() {
|
||||
this.weixinExecutor = new WeixinRequestExecutor();
|
||||
}
|
||||
|
||||
/**
|
||||
* 缓存key:附加key前缀
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
@Override
|
||||
public String key() {
|
||||
return String.format("%s%s", CACHEKEY_PREFIX, key0());
|
||||
}
|
||||
/**
|
||||
* 缓存key:附加key前缀
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
@Override
|
||||
public String key() {
|
||||
return String.format("%s%s_%s", CACHEKEY_PREFIX, name(), uniqueid());
|
||||
}
|
||||
|
||||
/**
|
||||
* 返回缓存KEY的名称:建议接口类型命名 如 mp_token_{appid}
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
public abstract String key0();
|
||||
/**
|
||||
* 返回缓存类型命名,如mp_token
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
public abstract String name();
|
||||
|
||||
/**
|
||||
* 返回缓存唯一标识,如appid
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
public abstract String uniqueid();
|
||||
}
|
||||
|
||||
@ -16,25 +16,33 @@ import com.foxinmy.weixin4j.model.Token;
|
||||
* @see CacheStorager
|
||||
*/
|
||||
public class TokenManager extends CacheManager<Token> {
|
||||
/**
|
||||
*
|
||||
* @param tokenCreator
|
||||
* 负责微信各种token的创建
|
||||
* @param cacheStorager
|
||||
* 负责token的存储
|
||||
*/
|
||||
public TokenManager(TokenCreator tokenCreator,
|
||||
CacheStorager<Token> cacheStorager) {
|
||||
super(tokenCreator, cacheStorager);
|
||||
}
|
||||
/**
|
||||
*
|
||||
* @param tokenCreator
|
||||
* 负责微信各种token的创建
|
||||
* @param cacheStorager
|
||||
* 负责token的存储
|
||||
*/
|
||||
public TokenManager(TokenCreator tokenCreator, CacheStorager<Token> cacheStorager) {
|
||||
super(tokenCreator, cacheStorager);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取token字符串
|
||||
*
|
||||
* @return token字符串
|
||||
* @throws WeixinException
|
||||
*/
|
||||
public String getAccessToken() throws WeixinException {
|
||||
return super.getCache().getAccessToken();
|
||||
}
|
||||
/**
|
||||
* 获取token字符串
|
||||
*
|
||||
* @return token字符串
|
||||
* @throws WeixinException
|
||||
*/
|
||||
public String getAccessToken() throws WeixinException {
|
||||
return super.getCache().getAccessToken();
|
||||
}
|
||||
|
||||
/**
|
||||
* 返回唯一标识ID
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
public String getWeixinId() {
|
||||
return ((TokenCreator) cacheCreator).uniqueid();
|
||||
}
|
||||
}
|
||||
|
||||
@ -281,7 +281,7 @@ public class WeixinProxy {
|
||||
* @return
|
||||
*/
|
||||
public TokenManager getTicketManager(TicketType ticketType) {
|
||||
return new TokenManager(new WeixinTicketCreator(weixinAccount.getId(), ticketType, this.tokenManager),
|
||||
return new TokenManager(new WeixinTicketCreator(ticketType, this.tokenManager),
|
||||
this.cacheStorager);
|
||||
}
|
||||
|
||||
|
||||
@ -18,32 +18,38 @@ import com.foxinmy.weixin4j.token.TokenManager;
|
||||
*/
|
||||
public class WeixinComponentPreCodeCreator extends TokenCreator {
|
||||
|
||||
private final TokenManager componentTokenManager;
|
||||
private final String componentId;
|
||||
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;
|
||||
}
|
||||
/**
|
||||
*
|
||||
* @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 String name() {
|
||||
return "mp_component_precode";
|
||||
}
|
||||
|
||||
@Override
|
||||
public String uniqueid() {
|
||||
return 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);
|
||||
}
|
||||
|
||||
@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);
|
||||
}
|
||||
}
|
||||
|
||||
@ -17,32 +17,35 @@ import com.foxinmy.weixin4j.token.TokenCreator;
|
||||
* @since JDK 1.6
|
||||
*/
|
||||
public class WeixinComponentTokenCreator extends TokenCreator {
|
||||
private final TicketManager ticketManager;
|
||||
private final TicketManager ticketManager;
|
||||
|
||||
/**
|
||||
*
|
||||
* @param ticketManager
|
||||
* 组件ticket存取
|
||||
*/
|
||||
public WeixinComponentTokenCreator(TicketManager ticketManager) {
|
||||
this.ticketManager = ticketManager;
|
||||
}
|
||||
/**
|
||||
*
|
||||
* @param ticketManager
|
||||
* 组件ticket存取
|
||||
*/
|
||||
public WeixinComponentTokenCreator(TicketManager ticketManager) {
|
||||
this.ticketManager = ticketManager;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String key0() {
|
||||
return String.format("mp_component_token_%s", ticketManager.getThirdId());
|
||||
}
|
||||
@Override
|
||||
public String name() {
|
||||
return "mp_component_token";
|
||||
}
|
||||
|
||||
@Override
|
||||
public Token create() throws WeixinException {
|
||||
JSONObject obj = new JSONObject();
|
||||
obj.put("component_appid", ticketManager.getThirdId());
|
||||
obj.put("component_appsecret", ticketManager.getThirdSecret());
|
||||
obj.put("component_verify_ticket", ticketManager.getAccessTicket());
|
||||
WeixinResponse response = weixinExecutor.post(
|
||||
URLConsts.COMPONENT_TOKEN_URL, obj.toJSONString());
|
||||
obj = response.getAsJson();
|
||||
return new Token(obj.getString("component_access_token"),
|
||||
obj.getLongValue("expires_in") * 1000l);
|
||||
}
|
||||
@Override
|
||||
public String uniqueid() {
|
||||
return ticketManager.getThirdId();
|
||||
}
|
||||
|
||||
@Override
|
||||
public Token create() throws WeixinException {
|
||||
JSONObject obj = new JSONObject();
|
||||
obj.put("component_appid", ticketManager.getThirdId());
|
||||
obj.put("component_appsecret", ticketManager.getThirdSecret());
|
||||
obj.put("component_verify_ticket", ticketManager.getAccessTicket());
|
||||
WeixinResponse response = weixinExecutor.post(URLConsts.COMPONENT_TOKEN_URL, obj.toJSONString());
|
||||
obj = response.getAsJson();
|
||||
return new Token(obj.getString("component_access_token"), obj.getLongValue("expires_in") * 1000l);
|
||||
}
|
||||
}
|
||||
|
||||
@ -19,39 +19,43 @@ import com.foxinmy.weixin4j.token.TokenManager;
|
||||
*/
|
||||
public class WeixinTokenComponentCreator extends TokenCreator {
|
||||
|
||||
private final PerTicketManager perTicketManager;
|
||||
private final TokenManager componentTokenManager;
|
||||
private final PerTicketManager perTicketManager;
|
||||
private final TokenManager componentTokenManager;
|
||||
|
||||
/**
|
||||
*
|
||||
* @param perTicketManager
|
||||
* 第三方套件永久授权码
|
||||
* @param componentTokenManager
|
||||
* 第三方套件凭证token
|
||||
*/
|
||||
public WeixinTokenComponentCreator(PerTicketManager perTicketManager,
|
||||
TokenManager componentTokenManager) {
|
||||
this.perTicketManager = perTicketManager;
|
||||
this.componentTokenManager = componentTokenManager;
|
||||
}
|
||||
/**
|
||||
*
|
||||
* @param perTicketManager
|
||||
* 第三方套件永久授权码
|
||||
* @param componentTokenManager
|
||||
* 第三方套件凭证token
|
||||
*/
|
||||
public WeixinTokenComponentCreator(PerTicketManager perTicketManager, TokenManager componentTokenManager) {
|
||||
this.perTicketManager = perTicketManager;
|
||||
this.componentTokenManager = componentTokenManager;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String key0() {
|
||||
return String.format("mp_token_component_%s_%s",
|
||||
perTicketManager.getThirdId(), perTicketManager.getAuthAppId());
|
||||
}
|
||||
@Override
|
||||
public String name() {
|
||||
return String.format("mp_token_component_%s_%s", perTicketManager.getThirdId(),
|
||||
perTicketManager.getAuthAppId());
|
||||
}
|
||||
|
||||
@Override
|
||||
public String uniqueid() {
|
||||
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();
|
||||
return new Token(obj.getString("access_token"),
|
||||
obj.getLongValue("expires_in") * 1000l);
|
||||
}
|
||||
}
|
||||
|
||||
@ -15,175 +15,211 @@ import com.foxinmy.weixin4j.util.NameValue;
|
||||
* @author jinyu(foxinmy@gmail.com)
|
||||
* @date 2014年9月29日
|
||||
* @since JDK 1.6
|
||||
* @see <a
|
||||
* href="https://mp.weixin.qq.com/wiki?t=resource/res_main&id=mp1433751277&token=&lang=zh_CN">模板消息</a>
|
||||
* @see <a href=
|
||||
* "https://mp.weixin.qq.com/wiki?t=resource/res_main&id=mp1433751277&token=&lang=zh_CN">模板消息</a>
|
||||
*/
|
||||
public class TemplateMessage implements Serializable {
|
||||
|
||||
private static final long serialVersionUID = 7950608393821661436L;
|
||||
private static final long serialVersionUID = 7950608393821661436L;
|
||||
|
||||
/**
|
||||
* 用户的openid
|
||||
*/
|
||||
@JSONField(name = "touser")
|
||||
private String toUser;
|
||||
/**
|
||||
* 模板ID
|
||||
*/
|
||||
@JSONField(name = "template_id")
|
||||
private String templateId;
|
||||
/**
|
||||
* 点击消息跳转的url
|
||||
*/
|
||||
private String url;
|
||||
/**
|
||||
* 头部信息(first第一行)
|
||||
*/
|
||||
@JSONField(serialize = false)
|
||||
private NameValue head;
|
||||
/**
|
||||
* 尾部信息(remark最后行)
|
||||
*/
|
||||
@JSONField(serialize = false)
|
||||
private NameValue tail;
|
||||
/**
|
||||
* 数据项
|
||||
*/
|
||||
@JSONField(name = "data")
|
||||
private Map<String, NameValue> content;
|
||||
/**
|
||||
* 用户的openid
|
||||
*/
|
||||
@JSONField(name = "touser")
|
||||
private String toUser;
|
||||
/**
|
||||
* 模板ID
|
||||
*/
|
||||
@JSONField(name = "template_id")
|
||||
private String templateId;
|
||||
/**
|
||||
* 点击消息跳转的url
|
||||
*/
|
||||
private String url;
|
||||
/**
|
||||
* 头部信息(first第一行)
|
||||
*/
|
||||
@JSONField(serialize = false)
|
||||
private NameValue head;
|
||||
/**
|
||||
* 尾部信息(remark最后行)
|
||||
*/
|
||||
@JSONField(serialize = false)
|
||||
private NameValue tail;
|
||||
|
||||
private final static String HEAD_KEY = "first";
|
||||
private final static String TAIL_KEY = "remark";
|
||||
private final static String DEFAULT_COLOR = "#173177";
|
||||
/**
|
||||
* 跳小程序所需数据,不需跳小程序可不用传该数据
|
||||
*/
|
||||
private String miniprogram;
|
||||
/**
|
||||
* 所需跳转到的小程序appid(该小程序appid必须与发模板消息的公众号是绑定关联关系)
|
||||
*/
|
||||
private String appid;
|
||||
/**
|
||||
* 所需跳转到小程序的具体页面路径,支持带参数,(示例index?foo=bar)
|
||||
*/
|
||||
private String pagepath;
|
||||
|
||||
@JSONCreator
|
||||
public TemplateMessage(@JSONField(name = "toUser") String toUser,
|
||||
@JSONField(name = "templateId") String templateId,
|
||||
@JSONField(name = "url") String url) {
|
||||
this.toUser = toUser;
|
||||
this.templateId = templateId;
|
||||
this.url = url;
|
||||
this.content = new HashMap<String, NameValue>();
|
||||
}
|
||||
/**
|
||||
* 数据项
|
||||
*/
|
||||
@JSONField(name = "data")
|
||||
private Map<String, NameValue> content;
|
||||
|
||||
public String getToUser() {
|
||||
return toUser;
|
||||
}
|
||||
private final static String HEAD_KEY = "first";
|
||||
private final static String TAIL_KEY = "remark";
|
||||
private final static String DEFAULT_COLOR = "#173177";
|
||||
|
||||
public String getTemplateId() {
|
||||
return templateId;
|
||||
}
|
||||
@JSONCreator
|
||||
public TemplateMessage(@JSONField(name = "toUser") String toUser, @JSONField(name = "templateId") String templateId,
|
||||
@JSONField(name = "url") String url) {
|
||||
this.toUser = toUser;
|
||||
this.templateId = templateId;
|
||||
this.url = url;
|
||||
this.content = new HashMap<String, NameValue>();
|
||||
}
|
||||
|
||||
public String getUrl() {
|
||||
return url;
|
||||
}
|
||||
public String getToUser() {
|
||||
return toUser;
|
||||
}
|
||||
|
||||
public NameValue getHead() {
|
||||
return head == null ? content.get(HEAD_KEY) : head;
|
||||
}
|
||||
public String getTemplateId() {
|
||||
return templateId;
|
||||
}
|
||||
|
||||
public NameValue getTail() {
|
||||
return tail == null ? content.get(TAIL_KEY) : tail;
|
||||
}
|
||||
public String getUrl() {
|
||||
return url;
|
||||
}
|
||||
|
||||
public Map<String, NameValue> getContent() {
|
||||
return content;
|
||||
}
|
||||
public NameValue getHead() {
|
||||
return head == null ? content.get(HEAD_KEY) : head;
|
||||
}
|
||||
|
||||
/**
|
||||
* 新增头部字段(默认颜色为#FF0000)
|
||||
*
|
||||
* @param text
|
||||
* 字段文本
|
||||
* @return
|
||||
*/
|
||||
public TemplateMessage pushHead(String text) {
|
||||
return pushHead("#FF0000", text);
|
||||
}
|
||||
public NameValue getTail() {
|
||||
return tail == null ? content.get(TAIL_KEY) : tail;
|
||||
}
|
||||
|
||||
/**
|
||||
* 新增头部字段
|
||||
*
|
||||
* @param color
|
||||
* 文字颜色
|
||||
* @param text
|
||||
* 字段文本
|
||||
* @return
|
||||
*/
|
||||
public TemplateMessage pushHead(String color, String text) {
|
||||
head = new NameValue(color, text);
|
||||
content.put(HEAD_KEY, head);
|
||||
return this;
|
||||
}
|
||||
public Map<String, NameValue> getContent() {
|
||||
return content;
|
||||
}
|
||||
|
||||
/**
|
||||
* 新增尾部字段(默认颜色为#173177)
|
||||
*
|
||||
* @param text
|
||||
* 字段文本
|
||||
* @return
|
||||
*/
|
||||
public TemplateMessage pushTail(String text) {
|
||||
return pushTail(DEFAULT_COLOR, text);
|
||||
}
|
||||
/**
|
||||
* 新增头部字段(默认颜色为#FF0000)
|
||||
*
|
||||
* @param text
|
||||
* 字段文本
|
||||
* @return
|
||||
*/
|
||||
public TemplateMessage pushHead(String text) {
|
||||
return pushHead("#FF0000", text);
|
||||
}
|
||||
|
||||
/**
|
||||
* 新增尾部字段
|
||||
*
|
||||
* @param color
|
||||
* 文字颜色
|
||||
* @param text
|
||||
* 字段文本
|
||||
* @return
|
||||
*/
|
||||
public TemplateMessage pushTail(String color, String text) {
|
||||
tail = new NameValue(color, text);
|
||||
content.put(TAIL_KEY, tail);
|
||||
return this;
|
||||
}
|
||||
/**
|
||||
* 新增头部字段
|
||||
*
|
||||
* @param color
|
||||
* 文字颜色
|
||||
* @param text
|
||||
* 字段文本
|
||||
* @return
|
||||
*/
|
||||
public TemplateMessage pushHead(String color, String text) {
|
||||
head = new NameValue(color, text);
|
||||
content.put(HEAD_KEY, head);
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* 新增字段项(默认颜色为#173177)
|
||||
*
|
||||
* @param key
|
||||
* 预留的字段名
|
||||
* @param text
|
||||
* 字段文本
|
||||
* @return
|
||||
*/
|
||||
public TemplateMessage pushItem(String key, String text) {
|
||||
return pushItem(key, DEFAULT_COLOR, text);
|
||||
}
|
||||
/**
|
||||
* 新增尾部字段(默认颜色为#173177)
|
||||
*
|
||||
* @param text
|
||||
* 字段文本
|
||||
* @return
|
||||
*/
|
||||
public TemplateMessage pushTail(String text) {
|
||||
return pushTail(DEFAULT_COLOR, text);
|
||||
}
|
||||
|
||||
/**
|
||||
* 新增字段项
|
||||
*
|
||||
* @param key
|
||||
* 预留的字段名
|
||||
* @param color
|
||||
* 文字颜色
|
||||
* @param text
|
||||
* 字段文本
|
||||
* @return
|
||||
*/
|
||||
public TemplateMessage pushItem(String key, String color, String text) {
|
||||
content.put(key, new NameValue(color, text));
|
||||
return this;
|
||||
}
|
||||
/**
|
||||
* 新增尾部字段
|
||||
*
|
||||
* @param color
|
||||
* 文字颜色
|
||||
* @param text
|
||||
* 字段文本
|
||||
* @return
|
||||
*/
|
||||
public TemplateMessage pushTail(String color, String text) {
|
||||
tail = new NameValue(color, text);
|
||||
content.put(TAIL_KEY, tail);
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* 设置所有字段项
|
||||
*
|
||||
* @param items
|
||||
*/
|
||||
public void setItems(Map<String, NameValue> items) {
|
||||
this.content = items;
|
||||
}
|
||||
/**
|
||||
* 新增字段项(默认颜色为#173177)
|
||||
*
|
||||
* @param key
|
||||
* 预留的字段名
|
||||
* @param text
|
||||
* 字段文本
|
||||
* @return
|
||||
*/
|
||||
public TemplateMessage pushItem(String key, String text) {
|
||||
return pushItem(key, DEFAULT_COLOR, text);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "TemplateMessage [toUser=" + toUser + ", templateId="
|
||||
+ templateId + ", url=" + url + ", head=" + getHead()
|
||||
+ ", tail=" + getTail() + ", content=" + content + "]";
|
||||
}
|
||||
/**
|
||||
* 新增字段项
|
||||
*
|
||||
* @param key
|
||||
* 预留的字段名
|
||||
* @param color
|
||||
* 文字颜色
|
||||
* @param text
|
||||
* 字段文本
|
||||
* @return
|
||||
*/
|
||||
public TemplateMessage pushItem(String key, String color, String text) {
|
||||
content.put(key, new NameValue(color, text));
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* 设置所有字段项
|
||||
*
|
||||
* @param items
|
||||
*/
|
||||
public void setItems(Map<String, NameValue> items) {
|
||||
this.content = items;
|
||||
}
|
||||
|
||||
public String getMiniprogram() {
|
||||
return miniprogram;
|
||||
}
|
||||
|
||||
public void setMiniprogram(String miniprogram) {
|
||||
this.miniprogram = miniprogram;
|
||||
}
|
||||
|
||||
public String getAppid() {
|
||||
return appid;
|
||||
}
|
||||
|
||||
public void setAppid(String appid) {
|
||||
this.appid = appid;
|
||||
}
|
||||
|
||||
public String getPagepath() {
|
||||
return pagepath;
|
||||
}
|
||||
|
||||
public void setPagepath(String pagepath) {
|
||||
this.pagepath = pagepath;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "TemplateMessage [toUser=" + toUser + ", templateId=" + templateId + ", url=" + url + ", head="
|
||||
+ getHead() + ", tail=" + getTail() + ", content=" + content + "]";
|
||||
}
|
||||
}
|
||||
|
||||
@ -22,39 +22,37 @@ import com.foxinmy.weixin4j.type.TicketType;
|
||||
*/
|
||||
public class WeixinTicketCreator extends TokenCreator {
|
||||
|
||||
private final String appid;
|
||||
private final TicketType ticketType;
|
||||
private final TokenManager weixinTokenManager;
|
||||
private final TicketType ticketType;
|
||||
private final TokenManager weixinTokenManager;
|
||||
|
||||
/**
|
||||
* jssdk
|
||||
*
|
||||
* @param appid
|
||||
* 公众号的appid
|
||||
* @param ticketType
|
||||
* 票据类型
|
||||
* @param weixinTokenManager
|
||||
* <font color="red">公众平台的access_token</font>
|
||||
*/
|
||||
public WeixinTicketCreator(String appid, TicketType ticketType,
|
||||
TokenManager weixinTokenManager) {
|
||||
this.appid = appid;
|
||||
this.ticketType = ticketType;
|
||||
this.weixinTokenManager = weixinTokenManager;
|
||||
}
|
||||
/**
|
||||
* jssdk
|
||||
*
|
||||
* @param ticketType
|
||||
* 票据类型
|
||||
* @param weixinTokenManager
|
||||
* <font color="red">公众平台的access_token</font>
|
||||
*/
|
||||
public WeixinTicketCreator(TicketType ticketType, TokenManager weixinTokenManager) {
|
||||
this.ticketType = ticketType;
|
||||
this.weixinTokenManager = weixinTokenManager;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String key0() {
|
||||
return String.format("mp_ticket_%s_%s", ticketType.name(), appid);
|
||||
}
|
||||
@Override
|
||||
public String name() {
|
||||
return String.format("mp_ticket_%s", ticketType.name());
|
||||
}
|
||||
|
||||
@Override
|
||||
public Token create() throws WeixinException {
|
||||
WeixinResponse response = weixinExecutor.get(String.format(
|
||||
URLConsts.JS_TICKET_URL, weixinTokenManager.getAccessToken(),
|
||||
ticketType.name()));
|
||||
JSONObject result = response.getAsJson();
|
||||
return new Token(result.getString("ticket"),
|
||||
result.getLongValue("expires_in") * 1000l);
|
||||
}
|
||||
@Override
|
||||
public String uniqueid() {
|
||||
return weixinTokenManager.getWeixinId();
|
||||
}
|
||||
|
||||
@Override
|
||||
public Token create() throws WeixinException {
|
||||
WeixinResponse response = weixinExecutor
|
||||
.get(String.format(URLConsts.JS_TICKET_URL, weixinTokenManager.getAccessToken(), ticketType.name()));
|
||||
JSONObject result = response.getAsJson();
|
||||
return new Token(result.getString("ticket"), result.getLongValue("expires_in") * 1000l);
|
||||
}
|
||||
}
|
||||
|
||||
@ -14,39 +14,42 @@ import com.foxinmy.weixin4j.token.TokenCreator;
|
||||
* @author jinyu(foxinmy@gmail.com)
|
||||
* @date 2015年1月10日
|
||||
* @since JDK 1.6
|
||||
* @see <a
|
||||
* href="https://mp.weixin.qq.com/wiki?t=resource/res_main&id=mp1421140183&token=&lang=zh_CN">微信公众平台获取token说明</a>
|
||||
* @see <a href=
|
||||
* "https://mp.weixin.qq.com/wiki?t=resource/res_main&id=mp1421140183&token=&lang=zh_CN">微信公众平台获取token说明</a>
|
||||
* @see com.foxinmy.weixin4j.model.Token
|
||||
*/
|
||||
public class WeixinTokenCreator extends TokenCreator {
|
||||
|
||||
private final String appid;
|
||||
private final String secret;
|
||||
private final String appid;
|
||||
private final String secret;
|
||||
|
||||
/**
|
||||
*
|
||||
* @param appid
|
||||
* 公众号ID
|
||||
* @param secret
|
||||
* 公众号secret
|
||||
*/
|
||||
public WeixinTokenCreator(String appid, String secret) {
|
||||
this.appid = appid;
|
||||
this.secret = secret;
|
||||
}
|
||||
/**
|
||||
*
|
||||
* @param appid
|
||||
* 公众号ID
|
||||
* @param secret
|
||||
* 公众号secret
|
||||
*/
|
||||
public WeixinTokenCreator(String appid, String secret) {
|
||||
this.appid = appid;
|
||||
this.secret = secret;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String key0() {
|
||||
return String.format("mp_token_%s", appid);
|
||||
}
|
||||
@Override
|
||||
public String name() {
|
||||
return "mp_token";
|
||||
}
|
||||
|
||||
@Override
|
||||
public Token create() throws WeixinException {
|
||||
String tokenUrl = String.format(URLConsts.ASSESS_TOKEN_URL, appid,
|
||||
secret);
|
||||
WeixinResponse response = weixinExecutor.get(tokenUrl);
|
||||
JSONObject result = response.getAsJson();
|
||||
return new Token(result.getString("access_token"),
|
||||
result.getLongValue("expires_in") * 1000l);
|
||||
}
|
||||
@Override
|
||||
public String uniqueid() {
|
||||
return appid;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Token create() throws WeixinException {
|
||||
String tokenUrl = String.format(URLConsts.ASSESS_TOKEN_URL, appid, secret);
|
||||
WeixinResponse response = weixinExecutor.get(tokenUrl);
|
||||
JSONObject result = response.getAsJson();
|
||||
return new Token(result.getString("access_token"), result.getLongValue("expires_in") * 1000l);
|
||||
}
|
||||
}
|
||||
|
||||
File diff suppressed because it is too large
Load Diff
@ -98,7 +98,7 @@ public class NotifyApi extends QyApi {
|
||||
String.format(message_send_uri, token.getAccessToken()),
|
||||
obj.toJSONString());
|
||||
obj = response.getAsJson();
|
||||
IdParameter idParameter = new IdParameter();
|
||||
IdParameter idParameter = IdParameter.get();
|
||||
if (obj.containsKey("invaliduser")) {
|
||||
idParameter.setUserIds(Arrays.asList(obj.getString("invaliduser")
|
||||
.split(IdParameter.SEPARATORS)));
|
||||
|
||||
@ -200,7 +200,7 @@ public class TagApi extends QyApi {
|
||||
WeixinResponse response = weixinExecutor.post(
|
||||
String.format(uri, token.getAccessToken()), obj.toJSONString());
|
||||
obj = response.getAsJson();
|
||||
IdParameter idParameter = new IdParameter();
|
||||
IdParameter idParameter = IdParameter.get();
|
||||
if (obj.containsKey("invalidlist")) {
|
||||
idParameter.setUserIds(Arrays.asList(obj.getString("invalidlist")
|
||||
.split(IdParameter.SEPARATORS)));
|
||||
|
||||
@ -8,7 +8,7 @@ import com.foxinmy.weixin4j.tuple.NotifyTuple;
|
||||
|
||||
/**
|
||||
* 消息提醒对象
|
||||
*
|
||||
*
|
||||
* @className NotifyMessage
|
||||
* @author jinyu(foxinmy@gmail.com)
|
||||
* @date 2014年11月22日
|
||||
@ -48,7 +48,7 @@ public class NotifyMessage implements Serializable {
|
||||
private IdParameter target;
|
||||
|
||||
public NotifyMessage(int agentid, NotifyTuple tuple) {
|
||||
this(agentid, tuple, new IdParameter(), false);
|
||||
this(agentid, tuple, IdParameter.get(), false);
|
||||
}
|
||||
|
||||
public NotifyMessage(int agentId, NotifyTuple tuple, IdParameter target, boolean isSafe) {
|
||||
|
||||
@ -35,12 +35,16 @@ public class IdParameter implements Serializable {
|
||||
@JSONField(name = "tag")
|
||||
private List<Integer> tagIds;
|
||||
|
||||
public IdParameter() {
|
||||
protected IdParameter() {
|
||||
this.userIds = new ArrayList<String>();
|
||||
this.partyIds = new ArrayList<Integer>();
|
||||
this.tagIds = new ArrayList<Integer>();
|
||||
}
|
||||
|
||||
public static IdParameter get(){
|
||||
return new IdParameter();
|
||||
}
|
||||
|
||||
/**
|
||||
* 增加成员ID列表,最多支持1000个
|
||||
*
|
||||
|
||||
@ -22,35 +22,37 @@ import com.foxinmy.weixin4j.token.TokenManager;
|
||||
*/
|
||||
public class WeixinSuitePreCodeCreator extends TokenCreator {
|
||||
|
||||
private final TokenManager suiteTokenManager;
|
||||
private final String suiteId;
|
||||
private final TokenManager suiteTokenManager;
|
||||
private final String suiteId;
|
||||
|
||||
/**
|
||||
*
|
||||
* @param suiteTokenManager
|
||||
* 应用套件的token
|
||||
* @param suiteId
|
||||
* 应用套件ID
|
||||
*/
|
||||
public WeixinSuitePreCodeCreator(TokenManager suiteTokenManager,
|
||||
String suiteId) {
|
||||
this.suiteTokenManager = suiteTokenManager;
|
||||
this.suiteId = suiteId;
|
||||
}
|
||||
/**
|
||||
*
|
||||
* @param suiteTokenManager
|
||||
* 应用套件的token
|
||||
* @param suiteId
|
||||
* 应用套件ID
|
||||
*/
|
||||
public WeixinSuitePreCodeCreator(TokenManager suiteTokenManager, String suiteId) {
|
||||
this.suiteTokenManager = suiteTokenManager;
|
||||
this.suiteId = suiteId;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String key0() {
|
||||
return String.format("qy_suite_precode_%s", suiteId);
|
||||
}
|
||||
@Override
|
||||
public String name() {
|
||||
return "qy_suite_precode";
|
||||
}
|
||||
|
||||
@Override
|
||||
public Token create() throws WeixinException {
|
||||
WeixinResponse response = weixinExecutor.post(
|
||||
String.format(URLConsts.SUITE_PRE_CODE_URL,
|
||||
suiteTokenManager.getAccessToken()),
|
||||
String.format("{\"suite_id\":\"%s\"}", suiteId));
|
||||
JSONObject result = response.getAsJson();
|
||||
return new Token(result.getString("pre_auth_code"),
|
||||
result.getLongValue("expires_in") * 1000l);
|
||||
}
|
||||
@Override
|
||||
public String uniqueid() {
|
||||
return suiteId;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Token create() throws WeixinException {
|
||||
WeixinResponse response = weixinExecutor.post(
|
||||
String.format(URLConsts.SUITE_PRE_CODE_URL, suiteTokenManager.getAccessToken()),
|
||||
String.format("{\"suite_id\":\"%s\"}", suiteId));
|
||||
JSONObject result = response.getAsJson();
|
||||
return new Token(result.getString("pre_auth_code"), result.getLongValue("expires_in") * 1000l);
|
||||
}
|
||||
}
|
||||
|
||||
@ -15,38 +15,41 @@ import com.foxinmy.weixin4j.token.TokenCreator;
|
||||
* @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%A5%E5%8F%A3%E8%AF%B4%E6%98%8E#.E8.8E.B7.E5.8F.96.E5.BA.94.E7.94.A8.E5.A5.97.E4.BB.B6.E4.BB.A4.E7.89.8C">获取应用套件凭证</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.E5.BA.94.E7.94.A8.E5.A5.97.E4.BB.B6.E4.BB.A4.E7.89.8C">获取应用套件凭证</a>
|
||||
* @see com.foxinmy.weixin4j.model.Token
|
||||
*/
|
||||
public class WeixinSuiteTokenCreator extends TokenCreator {
|
||||
|
||||
private final TicketManager ticketManager;
|
||||
private final TicketManager ticketManager;
|
||||
|
||||
/**
|
||||
*
|
||||
* @param ticketManager
|
||||
* 套件ticket存取
|
||||
*/
|
||||
public WeixinSuiteTokenCreator(TicketManager ticketManager) {
|
||||
this.ticketManager = ticketManager;
|
||||
}
|
||||
/**
|
||||
*
|
||||
* @param ticketManager
|
||||
* 套件ticket存取
|
||||
*/
|
||||
public WeixinSuiteTokenCreator(TicketManager ticketManager) {
|
||||
this.ticketManager = ticketManager;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String key0() {
|
||||
return String.format("qy_suite_token_%s", ticketManager.getThirdId());
|
||||
}
|
||||
@Override
|
||||
public String name() {
|
||||
return "qy_suite_token";
|
||||
}
|
||||
|
||||
@Override
|
||||
public Token create() throws WeixinException {
|
||||
JSONObject obj = new JSONObject();
|
||||
obj.put("suite_id", ticketManager.getThirdId());
|
||||
obj.put("suite_secret", ticketManager.getThirdSecret());
|
||||
obj.put("suite_ticket", ticketManager.getAccessTicket());
|
||||
WeixinResponse response = weixinExecutor.post(
|
||||
URLConsts.SUITE_TOKEN_URL, obj.toJSONString());
|
||||
obj = response.getAsJson();
|
||||
return new Token(obj.getString("suite_access_token"),
|
||||
obj.getLongValue("expires_in") * 1000l);
|
||||
}
|
||||
@Override
|
||||
public String uniqueid() {
|
||||
return ticketManager.getThirdId();
|
||||
}
|
||||
|
||||
@Override
|
||||
public Token create() throws WeixinException {
|
||||
JSONObject obj = new JSONObject();
|
||||
obj.put("suite_id", ticketManager.getThirdId());
|
||||
obj.put("suite_secret", ticketManager.getThirdSecret());
|
||||
obj.put("suite_ticket", ticketManager.getAccessTicket());
|
||||
WeixinResponse response = weixinExecutor.post(URLConsts.SUITE_TOKEN_URL, obj.toJSONString());
|
||||
obj = response.getAsJson();
|
||||
return new Token(obj.getString("suite_access_token"), obj.getLongValue("expires_in") * 1000l);
|
||||
}
|
||||
}
|
||||
|
||||
@ -23,39 +23,40 @@ import com.foxinmy.weixin4j.token.TokenManager;
|
||||
*/
|
||||
public class WeixinTokenSuiteCreator extends TokenCreator {
|
||||
|
||||
private final PerTicketManager perTicketManager;
|
||||
private final TokenManager suiteTokenManager;
|
||||
private final PerTicketManager perTicketManager;
|
||||
private final TokenManager suiteTokenManager;
|
||||
|
||||
/**
|
||||
*
|
||||
* @param perTicketManager
|
||||
* 第三方套件永久授权码
|
||||
* @param suiteTokenManager
|
||||
* 第三方套件凭证token
|
||||
*/
|
||||
public WeixinTokenSuiteCreator(PerTicketManager perTicketManager,
|
||||
TokenManager suiteTokenManager) {
|
||||
this.perTicketManager = perTicketManager;
|
||||
this.suiteTokenManager = suiteTokenManager;
|
||||
}
|
||||
/**
|
||||
*
|
||||
* @param perTicketManager
|
||||
* 第三方套件永久授权码
|
||||
* @param suiteTokenManager
|
||||
* 第三方套件凭证token
|
||||
*/
|
||||
public WeixinTokenSuiteCreator(PerTicketManager perTicketManager, TokenManager suiteTokenManager) {
|
||||
this.perTicketManager = perTicketManager;
|
||||
this.suiteTokenManager = suiteTokenManager;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String key0() {
|
||||
return String.format("qy_token_suite_%s_%s",
|
||||
perTicketManager.getThirdId(), perTicketManager.getAuthAppId());
|
||||
}
|
||||
@Override
|
||||
public String name() {
|
||||
return String.format("qy_token_suite_%s_%s", perTicketManager.getThirdId(), perTicketManager.getAuthAppId());
|
||||
}
|
||||
|
||||
@Override
|
||||
public Token create() throws WeixinException {
|
||||
JSONObject obj = new JSONObject();
|
||||
obj.put("suite_id", perTicketManager.getThirdId());
|
||||
obj.put("auth_corpid", perTicketManager.getAuthAppId());
|
||||
obj.put("permanent_code", perTicketManager.getAccessTicket());
|
||||
WeixinResponse response = weixinExecutor.post(
|
||||
String.format(URLConsts.TOKEN_SUITE_URL,
|
||||
suiteTokenManager.getAccessToken()), obj.toJSONString());
|
||||
obj = response.getAsJson();
|
||||
return new Token(obj.getString("access_token"),
|
||||
obj.getLongValue("expires_in") * 1000l);
|
||||
}
|
||||
@Override
|
||||
public String uniqueid() {
|
||||
throw new UnsupportedOperationException();
|
||||
}
|
||||
|
||||
@Override
|
||||
public Token create() throws WeixinException {
|
||||
JSONObject obj = new JSONObject();
|
||||
obj.put("suite_id", perTicketManager.getThirdId());
|
||||
obj.put("auth_corpid", perTicketManager.getAuthAppId());
|
||||
obj.put("permanent_code", perTicketManager.getAccessTicket());
|
||||
WeixinResponse response = weixinExecutor
|
||||
.post(String.format(URLConsts.TOKEN_SUITE_URL, suiteTokenManager.getAccessToken()), obj.toJSONString());
|
||||
obj = response.getAsJson();
|
||||
return new Token(obj.getString("access_token"), obj.getLongValue("expires_in") * 1000l);
|
||||
}
|
||||
}
|
||||
|
||||
@ -21,35 +21,38 @@ import com.foxinmy.weixin4j.token.TokenCreator;
|
||||
*/
|
||||
public class WeixinProviderTokenCreator extends TokenCreator {
|
||||
|
||||
private final String corpid;
|
||||
private final String providersecret;
|
||||
private final String corpid;
|
||||
private final String providersecret;
|
||||
|
||||
/**
|
||||
*
|
||||
* @param corpid
|
||||
* 企业号ID
|
||||
* @param providersecret
|
||||
* 企业号提供商的secret
|
||||
*/
|
||||
public WeixinProviderTokenCreator(String corpid, String providersecret) {
|
||||
this.corpid = corpid;
|
||||
this.providersecret = providersecret;
|
||||
}
|
||||
/**
|
||||
*
|
||||
* @param corpid
|
||||
* 企业号ID
|
||||
* @param providersecret
|
||||
* 企业号提供商的secret
|
||||
*/
|
||||
public WeixinProviderTokenCreator(String corpid, String providersecret) {
|
||||
this.corpid = corpid;
|
||||
this.providersecret = providersecret;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String key0() {
|
||||
return String.format("qy_provider_token_%s", corpid);
|
||||
}
|
||||
@Override
|
||||
public String name() {
|
||||
return "qy_provider_token";
|
||||
}
|
||||
|
||||
@Override
|
||||
public Token create() throws WeixinException {
|
||||
JSONObject obj = new JSONObject();
|
||||
obj.put("corpid", corpid);
|
||||
obj.put("provider_secret", providersecret);
|
||||
WeixinResponse response = weixinExecutor.post(
|
||||
URLConsts.PROVIDER_TOKEN_URL, obj.toJSONString());
|
||||
obj = response.getAsJson();
|
||||
return new Token(obj.getString("provider_access_token"),
|
||||
obj.getLongValue("expires_in") * 1000l);
|
||||
}
|
||||
@Override
|
||||
public String uniqueid() {
|
||||
return corpid;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Token create() throws WeixinException {
|
||||
JSONObject obj = new JSONObject();
|
||||
obj.put("corpid", corpid);
|
||||
obj.put("provider_secret", providersecret);
|
||||
WeixinResponse response = weixinExecutor.post(URLConsts.PROVIDER_TOKEN_URL, obj.toJSONString());
|
||||
obj = response.getAsJson();
|
||||
return new Token(obj.getString("provider_access_token"), obj.getLongValue("expires_in") * 1000l);
|
||||
}
|
||||
}
|
||||
|
||||
@ -22,45 +22,42 @@ import com.foxinmy.weixin4j.type.TicketType;
|
||||
*/
|
||||
public class WeixinTicketCreator extends TokenCreator {
|
||||
|
||||
private final String corpid;
|
||||
private final TicketType ticketType;
|
||||
private final TokenManager weixinTokenManager;
|
||||
private final TicketType ticketType;
|
||||
private final TokenManager weixinTokenManager;
|
||||
|
||||
/**
|
||||
* @param corpid
|
||||
* 企业号ID
|
||||
* @param ticketType
|
||||
* 票据类型
|
||||
* @param weixinTokenManager
|
||||
* <font color="red">企业号的access_token</font>
|
||||
*/
|
||||
public WeixinTicketCreator(String corpid, TicketType ticketType,
|
||||
TokenManager weixinTokenManager) {
|
||||
this.corpid = corpid;
|
||||
this.ticketType = ticketType;
|
||||
this.weixinTokenManager = weixinTokenManager;
|
||||
}
|
||||
/**
|
||||
* @param ticketType
|
||||
* 票据类型
|
||||
* @param weixinTokenManager
|
||||
* <font color="red">企业号的access_token</font>
|
||||
*/
|
||||
public WeixinTicketCreator(TicketType ticketType, TokenManager weixinTokenManager) {
|
||||
this.ticketType = ticketType;
|
||||
this.weixinTokenManager = weixinTokenManager;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String key0() {
|
||||
return String.format("qy_ticket_%s_%s", ticketType.name(), corpid);
|
||||
}
|
||||
@Override
|
||||
public String name() {
|
||||
return String.format("qy_ticket_%s", ticketType.name());
|
||||
}
|
||||
|
||||
@Override
|
||||
public Token create() throws WeixinException {
|
||||
WeixinResponse response = null;
|
||||
if (ticketType == TicketType.jsapi) {
|
||||
response = weixinExecutor.get(String.format(
|
||||
URLConsts.JS_TICKET_URL, weixinTokenManager.getCache()
|
||||
.getAccessToken()));
|
||||
} else {
|
||||
response = weixinExecutor.get(String.format(
|
||||
URLConsts.SUITE_TICKET_URL, weixinTokenManager.getCache()
|
||||
.getAccessToken(), ticketType.name()));
|
||||
}
|
||||
JSONObject result = response.getAsJson();
|
||||
return new Token(result.getString("ticket"),
|
||||
result.getLong("expires_in") * 1000l).pushExtra("group_id",
|
||||
result.getString("group_id"));
|
||||
}
|
||||
@Override
|
||||
public String uniqueid() {
|
||||
return weixinTokenManager.getWeixinId();
|
||||
}
|
||||
|
||||
@Override
|
||||
public Token create() throws WeixinException {
|
||||
WeixinResponse response = null;
|
||||
if (ticketType == TicketType.jsapi) {
|
||||
response = weixinExecutor
|
||||
.get(String.format(URLConsts.JS_TICKET_URL, weixinTokenManager.getCache().getAccessToken()));
|
||||
} else {
|
||||
response = weixinExecutor.get(String.format(URLConsts.SUITE_TICKET_URL,
|
||||
weixinTokenManager.getCache().getAccessToken(), ticketType.name()));
|
||||
}
|
||||
JSONObject result = response.getAsJson();
|
||||
return new Token(result.getString("ticket"), result.getLong("expires_in") * 1000l).pushExtra("group_id",
|
||||
result.getString("group_id"));
|
||||
}
|
||||
}
|
||||
|
||||
@ -21,33 +21,36 @@ import com.foxinmy.weixin4j.token.TokenCreator;
|
||||
*/
|
||||
public class WeixinTokenCreator extends TokenCreator {
|
||||
|
||||
private final String corpid;
|
||||
private final String corpsecret;
|
||||
private final String corpid;
|
||||
private final String corpsecret;
|
||||
|
||||
/**
|
||||
*
|
||||
* @param corpid
|
||||
* 企业号ID
|
||||
* @param corpsecret
|
||||
* 企业号secret
|
||||
*/
|
||||
public WeixinTokenCreator(String corpid, String corpsecret) {
|
||||
this.corpid = corpid;
|
||||
this.corpsecret = corpsecret;
|
||||
}
|
||||
/**
|
||||
*
|
||||
* @param corpid
|
||||
* 企业号ID
|
||||
* @param corpsecret
|
||||
* 企业号secret
|
||||
*/
|
||||
public WeixinTokenCreator(String corpid, String corpsecret) {
|
||||
this.corpid = corpid;
|
||||
this.corpsecret = corpsecret;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String key0() {
|
||||
return String.format("qy_token_%s", corpid);
|
||||
}
|
||||
@Override
|
||||
public String name() {
|
||||
return "qy_token";
|
||||
}
|
||||
|
||||
@Override
|
||||
public Token create() throws WeixinException {
|
||||
String tokenUrl = String.format(URLConsts.ASSESS_TOKEN_URL, corpid,
|
||||
corpsecret);
|
||||
WeixinResponse response = weixinExecutor.get(tokenUrl);
|
||||
JSONObject result = response.getAsJson();
|
||||
return new Token(result.getString("access_token"),
|
||||
result.getLongValue("expires_in") * 1000l);
|
||||
}
|
||||
@Override
|
||||
public String uniqueid() {
|
||||
return corpid;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Token create() throws WeixinException {
|
||||
String tokenUrl = String.format(URLConsts.ASSESS_TOKEN_URL, corpid, corpsecret);
|
||||
WeixinResponse response = weixinExecutor.get(tokenUrl);
|
||||
JSONObject result = response.getAsJson();
|
||||
return new Token(result.getString("access_token"), result.getLongValue("expires_in") * 1000l);
|
||||
}
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user