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

com.foxinmy.weixin4j.mp.token.WeixinTicketCreator 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.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.TokenManager;
import com.foxinmy.weixin4j.type.TicketType;

/**
 * 微信公众平台TICKET创建(包括jsticket、其它JSSDK所需的ticket的创建
 *
 * @className WeixinTicketCreator
 * @author jinyu([email protected])
 * @date 2015年1月10日
 * @since JDK 1.6
 * @see 
 *      JS TICKET
 */
public class WeixinTicketCreator extends TokenCreator {

    private final TicketType ticketType;
    private final TokenManager weixinTokenManager;

    /**
     * jssdk
     *
     * @param ticketType
     *            票据类型
     * @param weixinTokenManager
     *            公众平台的access_token
     */
    public WeixinTicketCreator(TicketType ticketType, TokenManager weixinTokenManager) {
        this.ticketType = ticketType;
        this.weixinTokenManager = weixinTokenManager;
    }

    @Override
    public String name() {
        return String.format("mp_ticket_%s", ticketType.name());
    }

    @Override
    public String uniqueid() {
        return weixinTokenManager.getWeixinId();
    }

    @Override
    public Token create() throws WeixinException {
        WeixinResponse response = weixinExecutor
                .get(String.format(URLConsts.JS_TICKET_URL, weixinTokenManager.getAccessToken(), ticketType.name()));
        JSONObject result = response.getAsJson();
        return new Token(result.getString("ticket"), result.getLongValue("expires_in") * 1000l);
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy