package com.zhiwei.weixin.accesstoken;

import com.zhiwei.conf.Conf;
import com.zhiwei.weixin.pojo.AccessToken;
import com.zhiwei.weixin.util.WeixinUtil;

/** 
 * @Package com.zhiwei.weixin.accesstoken
 * @ClassName: CreatAccessToken
 * @Description: TODO(创建微信公号token)
 * @author 志伟
 * @date 2015-3-28 下午9:20:33
 */
public class CreatAccessToken {
	
	 // 第三方用户唯一凭证  
//	 private final static String appId = Conf.appId;
	private static String appId;  
	  // 第三方用户唯一凭证密钥  
//	 private final static String appSecret = Conf.appSecret; 
	 private static String appSecret; 
	 // 调用接口获取access_token  
	 private static AccessToken at;
	 
	public CreatAccessToken() {
		appId = Conf.appId;
		appSecret = Conf.appSecret; 
		if(null==at)
		{
			at  = WeixinUtil.getAccessToken(appId, appSecret); 
		}else
		{
			long b =System.currentTimeMillis() - at.getExpiresIn();
			if((b/1000)>=3000)//3600
			{
				at = WeixinUtil.getAccessToken(appId, appSecret);
			}
		}
	}
	
	public String getAccessToken()
	{
		return at.getToken();
	}
	
	public void refreshToken()
	{
		at  = WeixinUtil.getAccessToken(appId, appSecret); 
	}

}
