TokenCreator新增uniqueid方法
This commit is contained in:
@@ -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();
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user