AbstractToken & TemplApi rename TmplApi
This commit is contained in:
parent
40621e78cf
commit
9d9e97f767
@ -10,7 +10,7 @@ import com.foxinmy.weixin4j.api.MediaApi;
|
||||
import com.foxinmy.weixin4j.api.MenuApi;
|
||||
import com.foxinmy.weixin4j.api.NotifyApi;
|
||||
import com.foxinmy.weixin4j.api.QrApi;
|
||||
import com.foxinmy.weixin4j.api.TemplApi;
|
||||
import com.foxinmy.weixin4j.api.TmplApi;
|
||||
import com.foxinmy.weixin4j.api.UserApi;
|
||||
import com.foxinmy.weixin4j.api.token.FileTokenApi;
|
||||
import com.foxinmy.weixin4j.api.token.TokenApi;
|
||||
@ -47,7 +47,7 @@ public class WeixinProxy {
|
||||
private final GroupApi groupApi;
|
||||
private final MenuApi menuApi;
|
||||
private final QrApi qrApi;
|
||||
private final TemplApi templApi;
|
||||
private final TmplApi templApi;
|
||||
|
||||
/**
|
||||
* 默认采用文件存放Token跟配置文件中的appi信息
|
||||
@ -74,7 +74,7 @@ public class WeixinProxy {
|
||||
this.groupApi = new GroupApi(tokenApi);
|
||||
this.menuApi = new MenuApi(tokenApi);
|
||||
this.qrApi = new QrApi(tokenApi);
|
||||
this.templApi = new TemplApi(tokenApi);
|
||||
this.templApi = new TmplApi(tokenApi);
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@ -17,11 +17,11 @@ import com.foxinmy.weixin4j.util.ConfigUtil;
|
||||
* @since JDK 1.7
|
||||
* @see
|
||||
*/
|
||||
public class TemplApi extends BaseApi {
|
||||
public class TmplApi extends BaseApi {
|
||||
|
||||
private final TokenApi tokenApi;
|
||||
|
||||
public TemplApi(TokenApi tokenApi) {
|
||||
public TmplApi(TokenApi tokenApi) {
|
||||
this.tokenApi = tokenApi;
|
||||
}
|
||||
|
||||
@ -0,0 +1,23 @@
|
||||
package com.foxinmy.weixin4j.api.token;
|
||||
|
||||
import com.foxinmy.weixin4j.util.ConfigUtil;
|
||||
|
||||
/**
|
||||
* 获取config.properties中的appid&appsecret信息
|
||||
*
|
||||
* @className AbstractTokenApi
|
||||
* @author jy
|
||||
* @date 2014年10月6日
|
||||
* @since JDK 1.7
|
||||
* @see
|
||||
*/
|
||||
public abstract class AbstractTokenApi implements TokenApi {
|
||||
|
||||
protected String getAppid() {
|
||||
return ConfigUtil.getValue("app_id");
|
||||
}
|
||||
|
||||
protected String getAppsecret() {
|
||||
return ConfigUtil.getValue("app_secret");
|
||||
}
|
||||
}
|
||||
@ -25,21 +25,23 @@ import com.foxinmy.weixin4j.xml.XStream;
|
||||
* href="http://mp.weixin.qq.com/wiki/index.php?title=%E8%8E%B7%E5%8F%96access_token">获取token说明</a>
|
||||
* @see com.foxinmy.weixin4j.model.Token
|
||||
*/
|
||||
public class FileTokenApi implements TokenApi {
|
||||
public class FileTokenApi extends AbstractTokenApi {
|
||||
|
||||
private final HttpRequest request = new HttpRequest();
|
||||
|
||||
private final String appid;
|
||||
private final String appsecret;
|
||||
|
||||
public FileTokenApi() {
|
||||
this(ConfigUtil.getValue("app_id"), ConfigUtil.getValue("app_secret"));
|
||||
this.appid = getAppid();
|
||||
this.appsecret = getAppsecret();
|
||||
}
|
||||
|
||||
public FileTokenApi(String appid, String appsecret) {
|
||||
this.appid = appid;
|
||||
this.appsecret = appsecret;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 获取token
|
||||
* <p>
|
||||
@ -55,22 +57,26 @@ public class FileTokenApi implements TokenApi {
|
||||
@Override
|
||||
public Token getToken() throws WeixinException {
|
||||
if (StringUtil.isBlank(appid) || StringUtil.isBlank(appsecret)) {
|
||||
throw new IllegalArgumentException("appid or appsecret not be null!");
|
||||
throw new IllegalArgumentException(
|
||||
"appid or appsecret not be null!");
|
||||
}
|
||||
XStream xstream = new XStream();
|
||||
xstream.autodetectAnnotations(true);
|
||||
xstream.processAnnotations(Token.class);
|
||||
File token_file = new File(String.format("%s/token_%s.xml", ConfigUtil.getValue("token_path"), appid));
|
||||
File token_file = new File(String.format("%s/token_%s.xml",
|
||||
ConfigUtil.getValue("token_path"), appid));
|
||||
Token token = null;
|
||||
Calendar ca = Calendar.getInstance();
|
||||
long now_time = ca.getTimeInMillis();
|
||||
try {
|
||||
String api_token_uri = String.format(ConfigUtil.getValue("api_token_uri"), appid, appsecret);
|
||||
String api_token_uri = String.format(
|
||||
ConfigUtil.getValue("api_token_uri"), appid, appsecret);
|
||||
Response response = null;
|
||||
if (token_file.exists()) {
|
||||
token = (Token) xstream.fromXML(token_file);
|
||||
|
||||
long expise_time = token.getTime() + (token.getExpiresIn() * 1000) - 3;
|
||||
long expise_time = token.getTime()
|
||||
+ (token.getExpiresIn() * 1000) - 3;
|
||||
if (expise_time > now_time) {
|
||||
return token;
|
||||
}
|
||||
|
||||
@ -22,7 +22,7 @@ import com.foxinmy.weixin4j.util.ConfigUtil;
|
||||
* href="http://mp.weixin.qq.com/wiki/index.php?title=%E8%8E%B7%E5%8F%96access_token">获取token说明</a>
|
||||
* @see com.foxinmy.weixin4j.model.Token
|
||||
*/
|
||||
public class RedisTokenApi implements TokenApi {
|
||||
public class RedisTokenApi extends AbstractTokenApi {
|
||||
|
||||
private final HttpRequest request = new HttpRequest();
|
||||
|
||||
@ -31,7 +31,8 @@ public class RedisTokenApi implements TokenApi {
|
||||
private JedisPool jedisPool;
|
||||
|
||||
public RedisTokenApi() {
|
||||
this(ConfigUtil.getValue("app_id"), ConfigUtil.getValue("app_secret"));
|
||||
this.appid = getAppid();
|
||||
this.appsecret = getAppsecret();
|
||||
}
|
||||
|
||||
public RedisTokenApi(String appid, String appsecret) {
|
||||
@ -53,7 +54,8 @@ public class RedisTokenApi implements TokenApi {
|
||||
@Override
|
||||
public Token getToken() throws WeixinException {
|
||||
if (StringUtil.isBlank(appid) || StringUtil.isBlank(appsecret)) {
|
||||
throw new IllegalArgumentException("appid or appsecret not be null!");
|
||||
throw new IllegalArgumentException(
|
||||
"appid or appsecret not be null!");
|
||||
}
|
||||
Token token = null;
|
||||
Jedis jedis = null;
|
||||
@ -62,9 +64,11 @@ public class RedisTokenApi implements TokenApi {
|
||||
String key = String.format("token:%s", appid);
|
||||
String accessToken = jedis.get(key);
|
||||
if (StringUtil.isBlank(accessToken)) {
|
||||
String api_token_uri = String.format(ConfigUtil.getValue("api_token_uri"), appid, appsecret);
|
||||
String api_token_uri = String.format(
|
||||
ConfigUtil.getValue("api_token_uri"), appid, appsecret);
|
||||
token = request.get(api_token_uri).getAsObject(Token.class);
|
||||
jedis.setex(key, token.getExpiresIn() - 3, token.getAccessToken());
|
||||
jedis.setex(key, token.getExpiresIn() - 3,
|
||||
token.getAccessToken());
|
||||
} else {
|
||||
token = new Token();
|
||||
token.setAccessToken(accessToken);
|
||||
|
||||
@ -11,6 +11,7 @@ import com.foxinmy.weixin4j.model.Token;
|
||||
* @date 2014年9月27日
|
||||
* @since JDK 1.7
|
||||
* @see com.foxinmy.weixin4j.model.Token
|
||||
* @see com.foxinmy.weixin4j.api.token.AbstractTokenApi
|
||||
* @see com.foxinmy.weixin4j.api.token.FileTokenApi
|
||||
* @see com.foxinmy.weixin4j.api.token.RedisTokenApi
|
||||
*/
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user