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