Skip to content

Commit

Permalink
Merge pull request #13 from chengn/master
Browse files Browse the repository at this point in the history
add js-sdk
  • Loading branch information
chengn committed Jan 30, 2015
2 parents f37c8c3 + 17dd978 commit 7f6efaa
Show file tree
Hide file tree
Showing 33 changed files with 767 additions and 244 deletions.
11 changes: 8 additions & 3 deletions META-INF/wechat4j.properties.sample
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,14 @@ wechat.encodingaeskey=
#wechat appid
wechat.appid=appid
#wechat app secret
wechat.appsecret=secret
wechat.appsecret=appsecret

#wechat access token server ,when you save in db,must implement you server class
#this class must extend org.sword.wechat4j.token.DbAccessTokenServer
#if no this property,then token server is default memery accesstoken server()
#this class must extend org.sword.wechat4j.token.server.CustomerServer
#if no this property,then token server is default memery accesstoken server
wechat.accessToken.server.class=

#jsapi_ticket customer server class name,
#this class must extend org.sword.wechat4j.token.server.CustomerServer
#if no this property,then ticket server is default memery ticket server
wechat.ticket.jsapi.server.class=
7 changes: 7 additions & 0 deletions src/org/sword/wechat4j/common/Config.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ public class Config {
private String appid;
private String appSecret;
private String accessTokenServer;
private String jsApiTicketServer;
private static Config config = new Config();

private Config(){
Expand All @@ -42,6 +43,7 @@ private Config(){
this.appid = p.getProperty("wechat.appid");
this.appSecret = p.getProperty("wechat.appsecret");
this.accessTokenServer = p.getProperty("wechat.accessToken.server.class");
this.jsApiTicketServer = p.getProperty("wechat.ticket.jsapi.server.class");
inStream.close();
} catch (IOException e) {
logger.error("load wechat4j.properties error,class根目录下找不到wechat4j.properties文件");
Expand Down Expand Up @@ -74,5 +76,10 @@ public String getEncodingAESKey() {
public String getAccessTokenServer(){
return accessTokenServer;
}

public String getJsApiTicketServer() {
return jsApiTicketServer;
}


}
4 changes: 2 additions & 2 deletions src/org/sword/wechat4j/message/CustomerMsg.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
import org.sword.wechat4j.response.MusicResponse;
import org.sword.wechat4j.response.VideoResponse;
import org.sword.wechat4j.token.AccessToken;
import org.sword.wechat4j.token.AccessTokenProxy;
import org.sword.wechat4j.token.TokenProxy;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
Expand Down Expand Up @@ -56,7 +56,7 @@ public CustomerMsg(String toUserOpenId){
* @param msgBody
*/
private void send(){
String accessToken = AccessTokenProxy.token();
String accessToken = TokenProxy.accessToken();
if(StringUtils.isBlank(this.toUserOpenId))
return;
//token不存在则重新刷新token
Expand Down
4 changes: 2 additions & 2 deletions src/org/sword/wechat4j/message/TemplateMsg.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
import org.sword.lang.HttpUtils;
import org.sword.wechat4j.message.template.TemplateMsgBody;
import org.sword.wechat4j.message.template.TemplateMsgData;
import org.sword.wechat4j.token.AccessTokenProxy;
import org.sword.wechat4j.token.TokenProxy;

import com.alibaba.fastjson.JSONObject;

Expand All @@ -31,7 +31,7 @@ public class TemplateMsg {
private String accessToken;

public TemplateMsg(){
this.accessToken = AccessTokenProxy.token();
this.accessToken = TokenProxy.accessToken();
}

/**
Expand Down
102 changes: 12 additions & 90 deletions src/org/sword/wechat4j/token/AccessToken.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,120 +3,42 @@
*/
package org.sword.wechat4j.token;

import java.util.Date;

import org.apache.commons.lang3.StringUtils;
import org.apache.log4j.Logger;
import org.sword.lang.HttpUtils;
import org.sword.wechat4j.common.Config;

import com.alibaba.fastjson.JSONObject;

/**
* Access token实体模型
* @author ChengNing
* @date 2014年12月12日
*/
public class AccessToken {
public class AccessToken extends Token {

private static Logger logger = Logger.getLogger(AccessToken.class);
private static final String ACCESS_TOKEN_URL = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential";

private String accessToken; //token
private long expires; //token有效时间
private long tokenTime; //token产生时间

private int redundance = 10*1000; //冗余时间,提前10秒就去请求新的token

/**
* 请求信的access token
* http请求方式: GET
https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=APPID&secret=APPSECRET
{"access_token":"ACCESS_TOKEN","expires_in":7200}
{"errcode":40013,"errmsg":"invalid appid"}
* @return
*/
public boolean request(){
String url = accessTokenUrl();
String result = HttpUtils.get(url);
if(StringUtils.isBlank(result))
return false;
JSONObject jsonObject = JSONObject.parseObject(result);
String accessToken = jsonObject.get("access_token").toString();
if(StringUtils.isBlank(accessToken)){
logger.error("access_token获取失败,获取结果" + result);
return false;
}
this.accessToken = accessToken;
this.tokenTime = (new Date()).getTime();
String expiresIn = jsonObject.get("expires_in").toString();
if(StringUtils.isBlank(expiresIn)){
logger.error("access_token获取失败,获取结果" + expiresIn);
return false;
}
else{
this.expires = Long.valueOf(expiresIn);
}
logger.info("access_token获取成功");
return true;
}

/**
* 得到access token
* @return
*/
public String getAccessToken(){
return this.accessToken;

@Override
protected String tokenName() {
return "access_token";
}

/**
* 得到有效时间
* @return
*/
public long getExpires() {
return expires;

@Override
protected String expiresInName() {
return "expires_in";
}

/**
* 组织accesstoken的请求utl
* @return
*/
private String accessTokenUrl(){
@Override
protected String accessTokenUrl() {
String appid = Config.instance().getAppid();
String appsecret = Config.instance().getAppSecret();
String url = ACCESS_TOKEN_URL + "&appid=" + appid + "&secret=" + appsecret;
logger.info("创建获取access_token url");
return url;

}

/**
* accessToken 是否有效
* @return true:有效,false: 无效
*/
public boolean isValid(){
//黑名单判定法
if(StringUtils.isBlank(this.accessToken))
return false;
if(this.expires <= 0)
return false;
//过期
if(isExpire())
return false;
return true;
}

/**
* 是否过期
* @return true 过期 false:有效
*/
private boolean isExpire(){
Date currentDate = new Date();
long currentTime = currentDate.getTime();
long expiresTime = expires * 1000 - redundance;
//判断是否过期
if((tokenTime + expiresTime) > currentTime)
return false;
return true;
}


Expand Down
31 changes: 0 additions & 31 deletions src/org/sword/wechat4j/token/AccessTokenProxy.java

This file was deleted.

46 changes: 0 additions & 46 deletions src/org/sword/wechat4j/token/AccessTokenServer.java

This file was deleted.

32 changes: 0 additions & 32 deletions src/org/sword/wechat4j/token/DbAccessTokenServer.java

This file was deleted.

19 changes: 0 additions & 19 deletions src/org/sword/wechat4j/token/IAccessTokenServer.java

This file was deleted.

Loading

0 comments on commit 7f6efaa

Please sign in to comment.