JSSDKHelper rename to JSSDKConfigurator & 重构了token类
This commit is contained in:
parent
ab0619ac38
commit
8da3fab8b8
@ -5,7 +5,7 @@
|
||||
<parent>
|
||||
<groupId>com.foxinmy</groupId>
|
||||
<artifactId>weixin4j</artifactId>
|
||||
<version>1.6.5</version>
|
||||
<version>1.6.6</version>
|
||||
</parent>
|
||||
<artifactId>weixin4j-base</artifactId>
|
||||
<name>weixin4j-base</name>
|
||||
|
||||
@ -1,4 +1,4 @@
|
||||
package com.foxinmy.weixin4j.util;
|
||||
package com.foxinmy.weixin4j.jssdk;
|
||||
|
||||
/**
|
||||
* JSSDK接口列表
|
||||
@ -243,7 +243,17 @@ public enum JSSDKAPI {
|
||||
* "http://mp.weixin.qq.com/wiki/11/74ad127cc054f6b80759c40f77ec03db.html#.E5.8F.91.E8.B5.B7.E4.B8.80.E4.B8.AA.E5.BE.AE.E4.BF.A1.E6.94.AF.E4.BB.98.E8.AF.B7.E6.B1.82"
|
||||
* >微信支付-发起一个微信支付请求</a>
|
||||
*/
|
||||
chooseWXPay;
|
||||
chooseWXPay,
|
||||
/**
|
||||
* <a href=
|
||||
* "http://qydev.weixin.qq.com/wiki/index.php?title=%E5%BE%AE%E4%BF%A1JS-SDK%E6%8E%A5%E5%8F%A3#.E5.88.9B.E5.BB.BA.E4.BC.81.E4.B8.9A.E5.8F.B7.E4.BC.9A.E8.AF.9D"
|
||||
* >企业号会话-创建企业号会话</a>
|
||||
*/
|
||||
openEnterpriseChat,
|
||||
/**
|
||||
* 企业号-选取联系人(内测)
|
||||
*/
|
||||
openEnterpriseContact;
|
||||
|
||||
/**
|
||||
* 分享接口集合
|
||||
@ -302,4 +312,8 @@ public enum JSSDKAPI {
|
||||
* 微信支付接口集合
|
||||
*/
|
||||
public final static JSSDKAPI[] PAY_APIS = { chooseWXPay };
|
||||
/**
|
||||
* 企业号会话接口集合
|
||||
*/
|
||||
public final static JSSDKAPI[] CHAT_APIS = { openEnterpriseChat };
|
||||
}
|
||||
@ -1,4 +1,4 @@
|
||||
package com.foxinmy.weixin4j.util;
|
||||
package com.foxinmy.weixin4j.jssdk;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.HashSet;
|
||||
@ -8,17 +8,23 @@ import java.util.Set;
|
||||
import com.alibaba.fastjson.JSONObject;
|
||||
import com.foxinmy.weixin4j.exception.WeixinException;
|
||||
import com.foxinmy.weixin4j.token.TokenHolder;
|
||||
import com.foxinmy.weixin4j.util.DateUtil;
|
||||
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帮助类
|
||||
*
|
||||
* @className JSSDKHelper
|
||||
* @className JSSDKConfigurator
|
||||
* @author jy
|
||||
* @date 2015年12月23日
|
||||
* @since JDK 1.6
|
||||
* @see
|
||||
*/
|
||||
public class JSSDKHelper {
|
||||
public class JSSDKConfigurator {
|
||||
private final TokenHolder ticketTokenHolder;
|
||||
private JSONObject config;
|
||||
private Set<JSSDKAPI> apis;
|
||||
@ -28,7 +34,7 @@ public class JSSDKHelper {
|
||||
*
|
||||
* @param ticketTokenHolder
|
||||
*/
|
||||
public JSSDKHelper(TokenHolder ticketTokenHolder) {
|
||||
public JSSDKConfigurator(TokenHolder ticketTokenHolder) {
|
||||
this.ticketTokenHolder = ticketTokenHolder;
|
||||
this.config = new JSONObject();
|
||||
this.apis = new HashSet<JSSDKAPI>();
|
||||
@ -40,7 +46,7 @@ public class JSSDKHelper {
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
public JSSDKHelper debugMode() {
|
||||
public JSSDKConfigurator debugMode() {
|
||||
config.put("debug", true);
|
||||
return this;
|
||||
}
|
||||
@ -51,7 +57,7 @@ public class JSSDKHelper {
|
||||
* @param appId
|
||||
* @return
|
||||
*/
|
||||
public JSSDKHelper appId(String appId) {
|
||||
public JSSDKConfigurator appId(String appId) {
|
||||
config.put("appId", appId);
|
||||
return this;
|
||||
}
|
||||
@ -63,7 +69,7 @@ public class JSSDKHelper {
|
||||
* @param apis
|
||||
* @return
|
||||
*/
|
||||
public JSSDKHelper apis(JSSDKAPI... apis) {
|
||||
public JSSDKConfigurator apis(JSSDKAPI... apis) {
|
||||
for (JSSDKAPI api : apis) {
|
||||
this.apis.add(api);
|
||||
}
|
||||
@ -77,7 +83,7 @@ public class JSSDKHelper {
|
||||
* @param apis
|
||||
* @return
|
||||
*/
|
||||
public JSSDKHelper apis(JSSDKAPI[]... apis) {
|
||||
public JSSDKConfigurator apis(JSSDKAPI[]... apis) {
|
||||
for (JSSDKAPI[] api : apis) {
|
||||
for (JSSDKAPI apii : api) {
|
||||
this.apis.add(apii);
|
||||
@ -32,9 +32,13 @@ public class Token implements Serializable {
|
||||
@JSONField(name = "expires_in")
|
||||
private int expiresIn;
|
||||
/**
|
||||
* token创建的时间 只在FileTokenStorager模式下有效
|
||||
* token创建的时间
|
||||
*/
|
||||
private long time;
|
||||
private long createTime;
|
||||
/**
|
||||
* 请求返回的原始结果
|
||||
*/
|
||||
private String originalResult;
|
||||
|
||||
public Token() {
|
||||
|
||||
@ -60,17 +64,25 @@ public class Token implements Serializable {
|
||||
this.expiresIn = expiresIn;
|
||||
}
|
||||
|
||||
public long getTime() {
|
||||
return time;
|
||||
public long getCreateTime() {
|
||||
return createTime;
|
||||
}
|
||||
|
||||
public void setTime(long time) {
|
||||
this.time = time;
|
||||
public void setCreateTime(long createTime) {
|
||||
this.createTime = createTime;
|
||||
}
|
||||
|
||||
public String getOriginalResult() {
|
||||
return originalResult;
|
||||
}
|
||||
|
||||
public void setOriginalResult(String originalResult) {
|
||||
this.originalResult = originalResult;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "Token [accessToken=" + accessToken + ", expiresIn=" + expiresIn
|
||||
+ ", time=" + time + "]";
|
||||
+ ", createTime=" + createTime + "]";
|
||||
}
|
||||
}
|
||||
|
||||
@ -33,10 +33,10 @@ public class FileTokenStorager implements TokenStorager {
|
||||
if (token_file.exists()) {
|
||||
Token token = XmlStream.fromXML(
|
||||
new FileInputStream(token_file), Token.class);
|
||||
if (token.getTime() < 0) {
|
||||
if (token.getCreateTime() < 0) {
|
||||
return token;
|
||||
}
|
||||
if ((token.getTime() + (token.getExpiresIn() * 1000l) - 2) > System
|
||||
if ((token.getCreateTime() + (token.getExpiresIn() * 1000l) - 2) > System
|
||||
.currentTimeMillis()) {
|
||||
return token;
|
||||
}
|
||||
|
||||
@ -1,12 +1,14 @@
|
||||
package com.foxinmy.weixin4j.token;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
import redis.clients.jedis.Jedis;
|
||||
import redis.clients.jedis.JedisPool;
|
||||
import redis.clients.jedis.JedisPoolConfig;
|
||||
|
||||
import com.foxinmy.weixin4j.exception.WeixinException;
|
||||
import com.foxinmy.weixin4j.model.Token;
|
||||
import com.foxinmy.weixin4j.util.StringUtil;
|
||||
|
||||
/**
|
||||
* 用REDIS保存TOKEN
|
||||
@ -54,9 +56,9 @@ public class RedisTokenStorager implements TokenStorager {
|
||||
Jedis jedis = null;
|
||||
try {
|
||||
jedis = jedisPool.getResource();
|
||||
String accessToken = jedis.get(cacheKey);
|
||||
if (!StringUtil.isBlank(accessToken)) {
|
||||
return new Token(accessToken);
|
||||
Map<String, String> map = jedis.hgetAll(cacheKey);
|
||||
if (map != null && !map.isEmpty()) {
|
||||
return map2token(map);
|
||||
}
|
||||
} finally {
|
||||
if (jedis != null) {
|
||||
@ -71,11 +73,9 @@ public class RedisTokenStorager implements TokenStorager {
|
||||
Jedis jedis = null;
|
||||
try {
|
||||
jedis = jedisPool.getResource();
|
||||
jedis.hmset(cacheKey, token2map(token));
|
||||
if (token.getExpiresIn() > 0) {
|
||||
jedis.setex(cacheKey, (int) token.getExpiresIn(),
|
||||
token.getAccessToken());
|
||||
} else {
|
||||
jedis.set(cacheKey, token.getAccessToken());
|
||||
jedis.expire(cacheKey, token.getExpiresIn());
|
||||
}
|
||||
} finally {
|
||||
if (jedis != null) {
|
||||
@ -83,4 +83,21 @@ public class RedisTokenStorager implements TokenStorager {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
protected Map<String, String> token2map(Token token) {
|
||||
Map<String, String> map = new HashMap<String, String>();
|
||||
map.put("accessToken", token.getAccessToken());
|
||||
map.put("originalResult", token.getOriginalResult());
|
||||
map.put("createTime", Long.toString(token.getCreateTime()));
|
||||
map.put("expiresIn", Integer.toString(token.getExpiresIn()));
|
||||
return map;
|
||||
}
|
||||
|
||||
protected Token map2token(Map<String, String> map) {
|
||||
Token token = new Token(map.get("accessToken"));
|
||||
token.setCreateTime(Long.parseLong(map.get("createTime")));
|
||||
token.setExpiresIn(Integer.parseInt(map.get("expiresIn")));
|
||||
token.setOriginalResult(map.get("originalResult"));
|
||||
return token;
|
||||
}
|
||||
}
|
||||
@ -72,8 +72,8 @@ public class OauthToken extends Token {
|
||||
public String toString() {
|
||||
return "OauthToken [openId=" + openId + ", unionId=" + unionId
|
||||
+ ", refreshToken=" + refreshToken + ", scope=" + scope
|
||||
+ ", getAccessToken()=" + getAccessToken()
|
||||
+ ", getExpiresIn()=" + getExpiresIn() + ", getTime()="
|
||||
+ getTime() + "]";
|
||||
+ ", accessToken=" + getAccessToken()
|
||||
+ ", expiresIn=" + getExpiresIn() + ", createTime="
|
||||
+ getCreateTime() + "]";
|
||||
}
|
||||
}
|
||||
|
||||
@ -53,7 +53,8 @@ public class WeixinJSTicketCreator implements TokenCreator {
|
||||
JSONObject result = response.getAsJson();
|
||||
Token token = new Token(result.getString("ticket"));
|
||||
token.setExpiresIn(result.getIntValue("expires_in"));
|
||||
token.setTime(System.currentTimeMillis());
|
||||
token.setCreateTime(System.currentTimeMillis());
|
||||
token.setOriginalResult(response.getAsString());
|
||||
return token;
|
||||
}
|
||||
}
|
||||
|
||||
@ -50,7 +50,8 @@ public class WeixinTokenCreator implements TokenCreator {
|
||||
WeixinResponse response = weixinExecutor.get(tokenUrl);
|
||||
Token token = response.getAsObject(new TypeReference<Token>() {
|
||||
});
|
||||
token.setTime(System.currentTimeMillis());
|
||||
token.setCreateTime(System.currentTimeMillis());
|
||||
token.setOriginalResult(response.getAsString());
|
||||
return token;
|
||||
}
|
||||
}
|
||||
|
||||
@ -17,7 +17,7 @@ public class XmlstreamTest {
|
||||
Token token = new Token();
|
||||
token.setAccessToken("accessToken");
|
||||
token.setExpiresIn(12);
|
||||
token.setTime(13l);
|
||||
token.setCreateTime(13l);
|
||||
String content = XmlStream.toXML(token);
|
||||
System.err.println(content);
|
||||
}
|
||||
|
||||
@ -184,7 +184,7 @@ public class SuiteApi extends QyApi {
|
||||
suitePerCodeHolder, suiteTokenHolder);
|
||||
Token token = new Token(obj.getString("access_token"));
|
||||
token.setExpiresIn(obj.getIntValue("expires_in"));
|
||||
token.setTime(System.currentTimeMillis());
|
||||
token.setCreateTime(System.currentTimeMillis());
|
||||
suiteTicketHolder.getTokenStorager().caching(
|
||||
tokenCreator.getCacheKey(), token);
|
||||
// 缓存微信企业号永久授权码
|
||||
|
||||
@ -54,7 +54,8 @@ public class WeixinSuitePreCodeCreator implements TokenCreator {
|
||||
JSONObject result = response.getAsJson();
|
||||
Token token = new Token(result.getString("pre_auth_code"));
|
||||
token.setExpiresIn(result.getIntValue("expires_in"));
|
||||
token.setTime(System.currentTimeMillis());
|
||||
token.setCreateTime(System.currentTimeMillis());
|
||||
token.setOriginalResult(response.getAsString());
|
||||
return token;
|
||||
}
|
||||
}
|
||||
|
||||
@ -50,7 +50,8 @@ public class WeixinSuiteTokenCreator implements TokenCreator {
|
||||
obj = response.getAsJson();
|
||||
Token token = new Token(obj.getString("suite_access_token"));
|
||||
token.setExpiresIn(obj.getIntValue("expires_in"));
|
||||
token.setTime(System.currentTimeMillis());
|
||||
token.setCreateTime(System.currentTimeMillis());
|
||||
token.setOriginalResult(response.getAsString());
|
||||
return token;
|
||||
}
|
||||
}
|
||||
|
||||
@ -60,7 +60,8 @@ public class WeixinTokenSuiteCreator implements TokenCreator {
|
||||
obj = response.getAsJson();
|
||||
Token token = new Token(obj.getString("access_token"));
|
||||
token.setExpiresIn(obj.getIntValue("expires_in"));
|
||||
token.setTime(System.currentTimeMillis());
|
||||
token.setCreateTime(System.currentTimeMillis());
|
||||
token.setOriginalResult(response.getAsString());
|
||||
return token;
|
||||
}
|
||||
|
||||
|
||||
@ -52,7 +52,8 @@ public class WeixinJSTicketCreator implements TokenCreator {
|
||||
JSONObject result = response.getAsJson();
|
||||
Token token = new Token(result.getString("ticket"));
|
||||
token.setExpiresIn(result.getIntValue("expires_in"));
|
||||
token.setTime(System.currentTimeMillis());
|
||||
token.setCreateTime(System.currentTimeMillis());
|
||||
token.setOriginalResult(response.getAsString());
|
||||
return token;
|
||||
}
|
||||
}
|
||||
|
||||
@ -53,7 +53,8 @@ public class WeixinProviderTokenCreator implements TokenCreator {
|
||||
obj = response.getAsJson();
|
||||
Token token = new Token(obj.getString("provider_access_token"));
|
||||
token.setExpiresIn(obj.getIntValue("expires_in"));
|
||||
token.setTime(System.currentTimeMillis());
|
||||
token.setCreateTime(System.currentTimeMillis());
|
||||
token.setOriginalResult(response.getAsString());
|
||||
return token;
|
||||
}
|
||||
}
|
||||
|
||||
@ -50,7 +50,8 @@ public class WeixinTokenCreator implements TokenCreator {
|
||||
WeixinResponse response = weixinExecutor.get(tokenUrl);
|
||||
Token token = response.getAsObject(new TypeReference<Token>() {
|
||||
});
|
||||
token.setTime(System.currentTimeMillis());
|
||||
token.setCreateTime(System.currentTimeMillis());
|
||||
token.setOriginalResult(response.getAsString());
|
||||
return token;
|
||||
}
|
||||
}
|
||||
|
||||
@ -29,6 +29,11 @@ public final class URLConsts {
|
||||
*/
|
||||
public static final String JS_TICKET_URL = BASE_URL
|
||||
+ "/get_jsapi_ticket?access_token=%s";
|
||||
/**
|
||||
* 企业号获取ticket的url
|
||||
*/
|
||||
public static final String TICKET_URL = BASE_URL
|
||||
+ "/ticket/get?access_token=%s&type=%s";
|
||||
/**
|
||||
* 企业号第三方应用套件获取token的url
|
||||
*/
|
||||
|
||||
@ -12,7 +12,7 @@ import java.security.NoSuchAlgorithmException;
|
||||
* @since JDK 1.6
|
||||
* @see
|
||||
*/
|
||||
public final class DigestUtil {
|
||||
public final class DigestUtils {
|
||||
|
||||
private static MessageDigest getDigest(final String algorithm) {
|
||||
try {
|
||||
@ -35,7 +35,7 @@ public final class MessageUtil {
|
||||
for (String str : para) {
|
||||
sb.append(str);
|
||||
}
|
||||
return DigestUtil.SHA1(sb.toString());
|
||||
return DigestUtils.SHA1(sb.toString());
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user