All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.foxinmy.weixin4j.mp.token.WeixinJSTicketCreator Maven / Gradle / Ivy

There is a newer version: 1.10.2
Show newest version
package com.foxinmy.weixin4j.mp.token;

import com.alibaba.fastjson.JSONObject;
import com.foxinmy.weixin4j.exception.WeixinException;
import com.foxinmy.weixin4j.http.weixin.WeixinHttpClient;
import com.foxinmy.weixin4j.http.weixin.WeixinResponse;
import com.foxinmy.weixin4j.model.Token;
import com.foxinmy.weixin4j.mp.type.URLConsts;
import com.foxinmy.weixin4j.token.TokenCreator;
import com.foxinmy.weixin4j.token.TokenHolder;

/**
 * 微信公众平台JSTICKET创建者
 * 
 * @className WeixinJSTicketCreator
 * @author jy
 * @date 2015年1月10日
 * @since JDK 1.7
 * @see JS
 *      TICKET
 */
public class WeixinJSTicketCreator implements TokenCreator {

	private final String appid;
	private final TokenHolder weixinTokenHolder;
	private final WeixinHttpClient httpClient;

	/**
	 * jssdk
	 * 
	 * @param appid
	 *            公众号的appid
	 * @param weixinTokenHolder
	 *            公众平台的access_token
	 */
	public WeixinJSTicketCreator(String appid, TokenHolder weixinTokenHolder) {
		this.appid = appid;
		this.weixinTokenHolder = weixinTokenHolder;
		this.httpClient = new WeixinHttpClient();
	}

	@Override
	public String getCacheKey() {
		return String.format("mp_jsticket_%s", appid);
	}

	@Override
	public Token createToken() throws WeixinException {
		WeixinResponse response = httpClient.get(String.format(
				URLConsts.JS_TICKET_URL, weixinTokenHolder.getToken()
						.getAccessToken()));
		JSONObject result = response.getAsJson();
		Token token = new Token(result.getString("ticket"));
		token.setExpiresIn(result.getIntValue("expires_in"));
		token.setTime(System.currentTimeMillis());
		return token;
	}
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy