TokenCreator新增uniqueid方法
This commit is contained in:
@@ -281,7 +281,7 @@ public class WeixinProxy {
|
||||
* @return
|
||||
*/
|
||||
public TokenManager getTicketManager(TicketType ticketType) {
|
||||
return new TokenManager(new WeixinTicketCreator(weixinAccount.getId(), ticketType, this.tokenManager),
|
||||
return new TokenManager(new WeixinTicketCreator(ticketType, this.tokenManager),
|
||||
this.cacheStorager);
|
||||
}
|
||||
|
||||
|
||||
+31
-25
@@ -18,32 +18,38 @@ import com.foxinmy.weixin4j.token.TokenManager;
|
||||
*/
|
||||
public class WeixinComponentPreCodeCreator extends TokenCreator {
|
||||
|
||||
private final TokenManager componentTokenManager;
|
||||
private final String componentId;
|
||||
private final TokenManager componentTokenManager;
|
||||
private final String componentId;
|
||||
|
||||
/**
|
||||
*
|
||||
* @param componentTokenManager
|
||||
* 应用套件的token
|
||||
* @param componentId
|
||||
* 应用组件ID
|
||||
*/
|
||||
public WeixinComponentPreCodeCreator(TokenManager componentTokenManager, String componentId) {
|
||||
this.componentTokenManager = componentTokenManager;
|
||||
this.componentId = componentId;
|
||||
}
|
||||
/**
|
||||
*
|
||||
* @param componentTokenManager
|
||||
* 应用套件的token
|
||||
* @param componentId
|
||||
* 应用组件ID
|
||||
*/
|
||||
public WeixinComponentPreCodeCreator(TokenManager componentTokenManager, String componentId) {
|
||||
this.componentTokenManager = componentTokenManager;
|
||||
this.componentId = componentId;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String key0() {
|
||||
return String.format("mp_component_precode_%s", componentId);
|
||||
}
|
||||
@Override
|
||||
public String name() {
|
||||
return "mp_component_precode";
|
||||
}
|
||||
|
||||
@Override
|
||||
public String uniqueid() {
|
||||
return componentId;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Token create() throws WeixinException {
|
||||
WeixinResponse response = weixinExecutor.post(
|
||||
String.format(URLConsts.COMPONENET_PRE_CODE_URL, componentTokenManager.getAccessToken()),
|
||||
String.format("{\"component_appid\":\"%s\"}", componentId));
|
||||
JSONObject result = response.getAsJson();
|
||||
return new Token(result.getString("pre_auth_code"), result.getLongValue("expires_in") * 1000l);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Token create() throws WeixinException {
|
||||
WeixinResponse response = weixinExecutor.post(
|
||||
String.format(URLConsts.COMPONENET_PRE_CODE_URL, componentTokenManager.getAccessToken()),
|
||||
String.format("{\"component_appid\":\"%s\"}", componentId));
|
||||
JSONObject result = response.getAsJson();
|
||||
return new Token(result.getString("pre_auth_code"), result.getLongValue("expires_in") * 1000l);
|
||||
}
|
||||
}
|
||||
|
||||
+28
-25
@@ -17,32 +17,35 @@ import com.foxinmy.weixin4j.token.TokenCreator;
|
||||
* @since JDK 1.6
|
||||
*/
|
||||
public class WeixinComponentTokenCreator extends TokenCreator {
|
||||
private final TicketManager ticketManager;
|
||||
private final TicketManager ticketManager;
|
||||
|
||||
/**
|
||||
*
|
||||
* @param ticketManager
|
||||
* 组件ticket存取
|
||||
*/
|
||||
public WeixinComponentTokenCreator(TicketManager ticketManager) {
|
||||
this.ticketManager = ticketManager;
|
||||
}
|
||||
/**
|
||||
*
|
||||
* @param ticketManager
|
||||
* 组件ticket存取
|
||||
*/
|
||||
public WeixinComponentTokenCreator(TicketManager ticketManager) {
|
||||
this.ticketManager = ticketManager;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String key0() {
|
||||
return String.format("mp_component_token_%s", ticketManager.getThirdId());
|
||||
}
|
||||
@Override
|
||||
public String name() {
|
||||
return "mp_component_token";
|
||||
}
|
||||
|
||||
@Override
|
||||
public Token create() throws WeixinException {
|
||||
JSONObject obj = new JSONObject();
|
||||
obj.put("component_appid", ticketManager.getThirdId());
|
||||
obj.put("component_appsecret", ticketManager.getThirdSecret());
|
||||
obj.put("component_verify_ticket", ticketManager.getAccessTicket());
|
||||
WeixinResponse response = weixinExecutor.post(
|
||||
URLConsts.COMPONENT_TOKEN_URL, obj.toJSONString());
|
||||
obj = response.getAsJson();
|
||||
return new Token(obj.getString("component_access_token"),
|
||||
obj.getLongValue("expires_in") * 1000l);
|
||||
}
|
||||
@Override
|
||||
public String uniqueid() {
|
||||
return ticketManager.getThirdId();
|
||||
}
|
||||
|
||||
@Override
|
||||
public Token create() throws WeixinException {
|
||||
JSONObject obj = new JSONObject();
|
||||
obj.put("component_appid", ticketManager.getThirdId());
|
||||
obj.put("component_appsecret", ticketManager.getThirdSecret());
|
||||
obj.put("component_verify_ticket", ticketManager.getAccessTicket());
|
||||
WeixinResponse response = weixinExecutor.post(URLConsts.COMPONENT_TOKEN_URL, obj.toJSONString());
|
||||
obj = response.getAsJson();
|
||||
return new Token(obj.getString("component_access_token"), obj.getLongValue("expires_in") * 1000l);
|
||||
}
|
||||
}
|
||||
|
||||
+36
-32
@@ -19,39 +19,43 @@ import com.foxinmy.weixin4j.token.TokenManager;
|
||||
*/
|
||||
public class WeixinTokenComponentCreator extends TokenCreator {
|
||||
|
||||
private final PerTicketManager perTicketManager;
|
||||
private final TokenManager componentTokenManager;
|
||||
private final PerTicketManager perTicketManager;
|
||||
private final TokenManager componentTokenManager;
|
||||
|
||||
/**
|
||||
*
|
||||
* @param perTicketManager
|
||||
* 第三方套件永久授权码
|
||||
* @param componentTokenManager
|
||||
* 第三方套件凭证token
|
||||
*/
|
||||
public WeixinTokenComponentCreator(PerTicketManager perTicketManager,
|
||||
TokenManager componentTokenManager) {
|
||||
this.perTicketManager = perTicketManager;
|
||||
this.componentTokenManager = componentTokenManager;
|
||||
}
|
||||
/**
|
||||
*
|
||||
* @param perTicketManager
|
||||
* 第三方套件永久授权码
|
||||
* @param componentTokenManager
|
||||
* 第三方套件凭证token
|
||||
*/
|
||||
public WeixinTokenComponentCreator(PerTicketManager perTicketManager, TokenManager componentTokenManager) {
|
||||
this.perTicketManager = perTicketManager;
|
||||
this.componentTokenManager = componentTokenManager;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String key0() {
|
||||
return String.format("mp_token_component_%s_%s",
|
||||
perTicketManager.getThirdId(), perTicketManager.getAuthAppId());
|
||||
}
|
||||
@Override
|
||||
public String name() {
|
||||
return String.format("mp_token_component_%s_%s", perTicketManager.getThirdId(),
|
||||
perTicketManager.getAuthAppId());
|
||||
}
|
||||
|
||||
@Override
|
||||
public String uniqueid() {
|
||||
throw new UnsupportedOperationException();
|
||||
}
|
||||
|
||||
@Override
|
||||
public Token create() throws WeixinException {
|
||||
JSONObject obj = new JSONObject();
|
||||
obj.put("component_appid", perTicketManager.getThirdId());
|
||||
obj.put("authorizer_appid", perTicketManager.getAuthAppId());
|
||||
obj.put("authorizer_refresh_token", perTicketManager.getAccessTicket());
|
||||
WeixinResponse response = weixinExecutor.post(
|
||||
String.format(URLConsts.TOKEN_COMPONENT_URL, componentTokenManager.getAccessToken()),
|
||||
obj.toJSONString());
|
||||
obj = response.getAsJson();
|
||||
return new Token(obj.getString("access_token"), obj.getLongValue("expires_in") * 1000l);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Token create() throws WeixinException {
|
||||
JSONObject obj = new JSONObject();
|
||||
obj.put("component_appid", perTicketManager.getThirdId());
|
||||
obj.put("authorizer_appid", perTicketManager.getAuthAppId());
|
||||
obj.put("authorizer_refresh_token", perTicketManager.getAccessTicket());
|
||||
WeixinResponse response = weixinExecutor.post(
|
||||
String.format(URLConsts.TOKEN_COMPONENT_URL,
|
||||
componentTokenManager.getAccessToken()), obj.toJSONString());
|
||||
obj = response.getAsJson();
|
||||
return new Token(obj.getString("access_token"),
|
||||
obj.getLongValue("expires_in") * 1000l);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -15,175 +15,211 @@ import com.foxinmy.weixin4j.util.NameValue;
|
||||
* @author jinyu(foxinmy@gmail.com)
|
||||
* @date 2014年9月29日
|
||||
* @since JDK 1.6
|
||||
* @see <a
|
||||
* href="https://mp.weixin.qq.com/wiki?t=resource/res_main&id=mp1433751277&token=&lang=zh_CN">模板消息</a>
|
||||
* @see <a href=
|
||||
* "https://mp.weixin.qq.com/wiki?t=resource/res_main&id=mp1433751277&token=&lang=zh_CN">模板消息</a>
|
||||
*/
|
||||
public class TemplateMessage implements Serializable {
|
||||
|
||||
private static final long serialVersionUID = 7950608393821661436L;
|
||||
private static final long serialVersionUID = 7950608393821661436L;
|
||||
|
||||
/**
|
||||
* 用户的openid
|
||||
*/
|
||||
@JSONField(name = "touser")
|
||||
private String toUser;
|
||||
/**
|
||||
* 模板ID
|
||||
*/
|
||||
@JSONField(name = "template_id")
|
||||
private String templateId;
|
||||
/**
|
||||
* 点击消息跳转的url
|
||||
*/
|
||||
private String url;
|
||||
/**
|
||||
* 头部信息(first第一行)
|
||||
*/
|
||||
@JSONField(serialize = false)
|
||||
private NameValue head;
|
||||
/**
|
||||
* 尾部信息(remark最后行)
|
||||
*/
|
||||
@JSONField(serialize = false)
|
||||
private NameValue tail;
|
||||
/**
|
||||
* 数据项
|
||||
*/
|
||||
@JSONField(name = "data")
|
||||
private Map<String, NameValue> content;
|
||||
/**
|
||||
* 用户的openid
|
||||
*/
|
||||
@JSONField(name = "touser")
|
||||
private String toUser;
|
||||
/**
|
||||
* 模板ID
|
||||
*/
|
||||
@JSONField(name = "template_id")
|
||||
private String templateId;
|
||||
/**
|
||||
* 点击消息跳转的url
|
||||
*/
|
||||
private String url;
|
||||
/**
|
||||
* 头部信息(first第一行)
|
||||
*/
|
||||
@JSONField(serialize = false)
|
||||
private NameValue head;
|
||||
/**
|
||||
* 尾部信息(remark最后行)
|
||||
*/
|
||||
@JSONField(serialize = false)
|
||||
private NameValue tail;
|
||||
|
||||
private final static String HEAD_KEY = "first";
|
||||
private final static String TAIL_KEY = "remark";
|
||||
private final static String DEFAULT_COLOR = "#173177";
|
||||
/**
|
||||
* 跳小程序所需数据,不需跳小程序可不用传该数据
|
||||
*/
|
||||
private String miniprogram;
|
||||
/**
|
||||
* 所需跳转到的小程序appid(该小程序appid必须与发模板消息的公众号是绑定关联关系)
|
||||
*/
|
||||
private String appid;
|
||||
/**
|
||||
* 所需跳转到小程序的具体页面路径,支持带参数,(示例index?foo=bar)
|
||||
*/
|
||||
private String pagepath;
|
||||
|
||||
@JSONCreator
|
||||
public TemplateMessage(@JSONField(name = "toUser") String toUser,
|
||||
@JSONField(name = "templateId") String templateId,
|
||||
@JSONField(name = "url") String url) {
|
||||
this.toUser = toUser;
|
||||
this.templateId = templateId;
|
||||
this.url = url;
|
||||
this.content = new HashMap<String, NameValue>();
|
||||
}
|
||||
/**
|
||||
* 数据项
|
||||
*/
|
||||
@JSONField(name = "data")
|
||||
private Map<String, NameValue> content;
|
||||
|
||||
public String getToUser() {
|
||||
return toUser;
|
||||
}
|
||||
private final static String HEAD_KEY = "first";
|
||||
private final static String TAIL_KEY = "remark";
|
||||
private final static String DEFAULT_COLOR = "#173177";
|
||||
|
||||
public String getTemplateId() {
|
||||
return templateId;
|
||||
}
|
||||
@JSONCreator
|
||||
public TemplateMessage(@JSONField(name = "toUser") String toUser, @JSONField(name = "templateId") String templateId,
|
||||
@JSONField(name = "url") String url) {
|
||||
this.toUser = toUser;
|
||||
this.templateId = templateId;
|
||||
this.url = url;
|
||||
this.content = new HashMap<String, NameValue>();
|
||||
}
|
||||
|
||||
public String getUrl() {
|
||||
return url;
|
||||
}
|
||||
public String getToUser() {
|
||||
return toUser;
|
||||
}
|
||||
|
||||
public NameValue getHead() {
|
||||
return head == null ? content.get(HEAD_KEY) : head;
|
||||
}
|
||||
public String getTemplateId() {
|
||||
return templateId;
|
||||
}
|
||||
|
||||
public NameValue getTail() {
|
||||
return tail == null ? content.get(TAIL_KEY) : tail;
|
||||
}
|
||||
public String getUrl() {
|
||||
return url;
|
||||
}
|
||||
|
||||
public Map<String, NameValue> getContent() {
|
||||
return content;
|
||||
}
|
||||
public NameValue getHead() {
|
||||
return head == null ? content.get(HEAD_KEY) : head;
|
||||
}
|
||||
|
||||
/**
|
||||
* 新增头部字段(默认颜色为#FF0000)
|
||||
*
|
||||
* @param text
|
||||
* 字段文本
|
||||
* @return
|
||||
*/
|
||||
public TemplateMessage pushHead(String text) {
|
||||
return pushHead("#FF0000", text);
|
||||
}
|
||||
public NameValue getTail() {
|
||||
return tail == null ? content.get(TAIL_KEY) : tail;
|
||||
}
|
||||
|
||||
/**
|
||||
* 新增头部字段
|
||||
*
|
||||
* @param color
|
||||
* 文字颜色
|
||||
* @param text
|
||||
* 字段文本
|
||||
* @return
|
||||
*/
|
||||
public TemplateMessage pushHead(String color, String text) {
|
||||
head = new NameValue(color, text);
|
||||
content.put(HEAD_KEY, head);
|
||||
return this;
|
||||
}
|
||||
public Map<String, NameValue> getContent() {
|
||||
return content;
|
||||
}
|
||||
|
||||
/**
|
||||
* 新增尾部字段(默认颜色为#173177)
|
||||
*
|
||||
* @param text
|
||||
* 字段文本
|
||||
* @return
|
||||
*/
|
||||
public TemplateMessage pushTail(String text) {
|
||||
return pushTail(DEFAULT_COLOR, text);
|
||||
}
|
||||
/**
|
||||
* 新增头部字段(默认颜色为#FF0000)
|
||||
*
|
||||
* @param text
|
||||
* 字段文本
|
||||
* @return
|
||||
*/
|
||||
public TemplateMessage pushHead(String text) {
|
||||
return pushHead("#FF0000", text);
|
||||
}
|
||||
|
||||
/**
|
||||
* 新增尾部字段
|
||||
*
|
||||
* @param color
|
||||
* 文字颜色
|
||||
* @param text
|
||||
* 字段文本
|
||||
* @return
|
||||
*/
|
||||
public TemplateMessage pushTail(String color, String text) {
|
||||
tail = new NameValue(color, text);
|
||||
content.put(TAIL_KEY, tail);
|
||||
return this;
|
||||
}
|
||||
/**
|
||||
* 新增头部字段
|
||||
*
|
||||
* @param color
|
||||
* 文字颜色
|
||||
* @param text
|
||||
* 字段文本
|
||||
* @return
|
||||
*/
|
||||
public TemplateMessage pushHead(String color, String text) {
|
||||
head = new NameValue(color, text);
|
||||
content.put(HEAD_KEY, head);
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* 新增字段项(默认颜色为#173177)
|
||||
*
|
||||
* @param key
|
||||
* 预留的字段名
|
||||
* @param text
|
||||
* 字段文本
|
||||
* @return
|
||||
*/
|
||||
public TemplateMessage pushItem(String key, String text) {
|
||||
return pushItem(key, DEFAULT_COLOR, text);
|
||||
}
|
||||
/**
|
||||
* 新增尾部字段(默认颜色为#173177)
|
||||
*
|
||||
* @param text
|
||||
* 字段文本
|
||||
* @return
|
||||
*/
|
||||
public TemplateMessage pushTail(String text) {
|
||||
return pushTail(DEFAULT_COLOR, text);
|
||||
}
|
||||
|
||||
/**
|
||||
* 新增字段项
|
||||
*
|
||||
* @param key
|
||||
* 预留的字段名
|
||||
* @param color
|
||||
* 文字颜色
|
||||
* @param text
|
||||
* 字段文本
|
||||
* @return
|
||||
*/
|
||||
public TemplateMessage pushItem(String key, String color, String text) {
|
||||
content.put(key, new NameValue(color, text));
|
||||
return this;
|
||||
}
|
||||
/**
|
||||
* 新增尾部字段
|
||||
*
|
||||
* @param color
|
||||
* 文字颜色
|
||||
* @param text
|
||||
* 字段文本
|
||||
* @return
|
||||
*/
|
||||
public TemplateMessage pushTail(String color, String text) {
|
||||
tail = new NameValue(color, text);
|
||||
content.put(TAIL_KEY, tail);
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* 设置所有字段项
|
||||
*
|
||||
* @param items
|
||||
*/
|
||||
public void setItems(Map<String, NameValue> items) {
|
||||
this.content = items;
|
||||
}
|
||||
/**
|
||||
* 新增字段项(默认颜色为#173177)
|
||||
*
|
||||
* @param key
|
||||
* 预留的字段名
|
||||
* @param text
|
||||
* 字段文本
|
||||
* @return
|
||||
*/
|
||||
public TemplateMessage pushItem(String key, String text) {
|
||||
return pushItem(key, DEFAULT_COLOR, text);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "TemplateMessage [toUser=" + toUser + ", templateId="
|
||||
+ templateId + ", url=" + url + ", head=" + getHead()
|
||||
+ ", tail=" + getTail() + ", content=" + content + "]";
|
||||
}
|
||||
/**
|
||||
* 新增字段项
|
||||
*
|
||||
* @param key
|
||||
* 预留的字段名
|
||||
* @param color
|
||||
* 文字颜色
|
||||
* @param text
|
||||
* 字段文本
|
||||
* @return
|
||||
*/
|
||||
public TemplateMessage pushItem(String key, String color, String text) {
|
||||
content.put(key, new NameValue(color, text));
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* 设置所有字段项
|
||||
*
|
||||
* @param items
|
||||
*/
|
||||
public void setItems(Map<String, NameValue> items) {
|
||||
this.content = items;
|
||||
}
|
||||
|
||||
public String getMiniprogram() {
|
||||
return miniprogram;
|
||||
}
|
||||
|
||||
public void setMiniprogram(String miniprogram) {
|
||||
this.miniprogram = miniprogram;
|
||||
}
|
||||
|
||||
public String getAppid() {
|
||||
return appid;
|
||||
}
|
||||
|
||||
public void setAppid(String appid) {
|
||||
this.appid = appid;
|
||||
}
|
||||
|
||||
public String getPagepath() {
|
||||
return pagepath;
|
||||
}
|
||||
|
||||
public void setPagepath(String pagepath) {
|
||||
this.pagepath = pagepath;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "TemplateMessage [toUser=" + toUser + ", templateId=" + templateId + ", url=" + url + ", head="
|
||||
+ getHead() + ", tail=" + getTail() + ", content=" + content + "]";
|
||||
}
|
||||
}
|
||||
|
||||
@@ -22,39 +22,37 @@ import com.foxinmy.weixin4j.type.TicketType;
|
||||
*/
|
||||
public class WeixinTicketCreator extends TokenCreator {
|
||||
|
||||
private final String appid;
|
||||
private final TicketType ticketType;
|
||||
private final TokenManager weixinTokenManager;
|
||||
private final TicketType ticketType;
|
||||
private final TokenManager weixinTokenManager;
|
||||
|
||||
/**
|
||||
* jssdk
|
||||
*
|
||||
* @param appid
|
||||
* 公众号的appid
|
||||
* @param ticketType
|
||||
* 票据类型
|
||||
* @param weixinTokenManager
|
||||
* <font color="red">公众平台的access_token</font>
|
||||
*/
|
||||
public WeixinTicketCreator(String appid, TicketType ticketType,
|
||||
TokenManager weixinTokenManager) {
|
||||
this.appid = appid;
|
||||
this.ticketType = ticketType;
|
||||
this.weixinTokenManager = weixinTokenManager;
|
||||
}
|
||||
/**
|
||||
* jssdk
|
||||
*
|
||||
* @param ticketType
|
||||
* 票据类型
|
||||
* @param weixinTokenManager
|
||||
* <font color="red">公众平台的access_token</font>
|
||||
*/
|
||||
public WeixinTicketCreator(TicketType ticketType, TokenManager weixinTokenManager) {
|
||||
this.ticketType = ticketType;
|
||||
this.weixinTokenManager = weixinTokenManager;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String key0() {
|
||||
return String.format("mp_ticket_%s_%s", ticketType.name(), appid);
|
||||
}
|
||||
@Override
|
||||
public String name() {
|
||||
return String.format("mp_ticket_%s", ticketType.name());
|
||||
}
|
||||
|
||||
@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);
|
||||
}
|
||||
@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);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -14,39 +14,42 @@ import com.foxinmy.weixin4j.token.TokenCreator;
|
||||
* @author jinyu(foxinmy@gmail.com)
|
||||
* @date 2015年1月10日
|
||||
* @since JDK 1.6
|
||||
* @see <a
|
||||
* href="https://mp.weixin.qq.com/wiki?t=resource/res_main&id=mp1421140183&token=&lang=zh_CN">微信公众平台获取token说明</a>
|
||||
* @see <a href=
|
||||
* "https://mp.weixin.qq.com/wiki?t=resource/res_main&id=mp1421140183&token=&lang=zh_CN">微信公众平台获取token说明</a>
|
||||
* @see com.foxinmy.weixin4j.model.Token
|
||||
*/
|
||||
public class WeixinTokenCreator extends TokenCreator {
|
||||
|
||||
private final String appid;
|
||||
private final String secret;
|
||||
private final String appid;
|
||||
private final String secret;
|
||||
|
||||
/**
|
||||
*
|
||||
* @param appid
|
||||
* 公众号ID
|
||||
* @param secret
|
||||
* 公众号secret
|
||||
*/
|
||||
public WeixinTokenCreator(String appid, String secret) {
|
||||
this.appid = appid;
|
||||
this.secret = secret;
|
||||
}
|
||||
/**
|
||||
*
|
||||
* @param appid
|
||||
* 公众号ID
|
||||
* @param secret
|
||||
* 公众号secret
|
||||
*/
|
||||
public WeixinTokenCreator(String appid, String secret) {
|
||||
this.appid = appid;
|
||||
this.secret = secret;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String key0() {
|
||||
return String.format("mp_token_%s", appid);
|
||||
}
|
||||
@Override
|
||||
public String name() {
|
||||
return "mp_token";
|
||||
}
|
||||
|
||||
@Override
|
||||
public Token create() throws WeixinException {
|
||||
String tokenUrl = String.format(URLConsts.ASSESS_TOKEN_URL, appid,
|
||||
secret);
|
||||
WeixinResponse response = weixinExecutor.get(tokenUrl);
|
||||
JSONObject result = response.getAsJson();
|
||||
return new Token(result.getString("access_token"),
|
||||
result.getLongValue("expires_in") * 1000l);
|
||||
}
|
||||
@Override
|
||||
public String uniqueid() {
|
||||
return appid;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Token create() throws WeixinException {
|
||||
String tokenUrl = String.format(URLConsts.ASSESS_TOKEN_URL, appid, secret);
|
||||
WeixinResponse response = weixinExecutor.get(tokenUrl);
|
||||
JSONObject result = response.getAsJson();
|
||||
return new Token(result.getString("access_token"), result.getLongValue("expires_in") * 1000l);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user