1
0
mirror of synced 2025-12-22 00:48:00 +08:00

🆕 #2222 【公众号】 新增短key托管和解析的接口

This commit is contained in:
zsail
2021-07-29 22:24:19 +08:00
committed by GitHub
parent 00347dc50c
commit 6d2c0a3709
4 changed files with 98 additions and 2 deletions

View File

@@ -15,6 +15,7 @@ import me.chanjar.weixin.common.util.http.RequestHttp;
import me.chanjar.weixin.mp.bean.WxMpSemanticQuery; import me.chanjar.weixin.mp.bean.WxMpSemanticQuery;
import me.chanjar.weixin.mp.bean.result.WxMpCurrentAutoReplyInfo; import me.chanjar.weixin.mp.bean.result.WxMpCurrentAutoReplyInfo;
import me.chanjar.weixin.mp.bean.result.WxMpSemanticQueryResult; import me.chanjar.weixin.mp.bean.result.WxMpSemanticQueryResult;
import me.chanjar.weixin.mp.bean.result.WxMpShortKeyResult;
import me.chanjar.weixin.mp.config.WxMpConfigStorage; import me.chanjar.weixin.mp.config.WxMpConfigStorage;
import me.chanjar.weixin.mp.enums.WxMpApiUrl; import me.chanjar.weixin.mp.enums.WxMpApiUrl;
@@ -26,6 +27,31 @@ import java.util.Map;
* @author chanjarster * @author chanjarster
*/ */
public interface WxMpService extends WxService { public interface WxMpService extends WxService {
/**
* <pre>
* 短key托管 类似于短链API.
* 详情请见: https://developers.weixin.qq.com/doc/offiaccount/Account_Management/KEY_Shortener.html
* </pre>
*
* @param longData 需要转换的长信息不超过4KB
* @param expireSeconds 短key有效期(单位秒)最大值为2592000即30天默认为2592000(30天)
* @return shortKey 短key15字节base62编码(0-9/a-z/A-Z)
* @throws WxErrorException .
*/
String genShorten(String longData, Integer expireSeconds) throws WxErrorException;
/**
* <pre>
* 短key解析 将短key还原为长信息。
* 详情请见: https://developers.weixin.qq.com/doc/offiaccount/Account_Management/KEY_Shortener.html
* </pre>
*
* @param shortKey 短key
* @return WxMpShortKeyResult 解析结果
* @throws WxErrorException .
*/
WxMpShortKeyResult fetchShorten(String shortKey) throws WxErrorException;
/** /**
* <pre> * <pre>
* 验证消息的确来自微信服务器. * 验证消息的确来自微信服务器.
@@ -536,12 +562,14 @@ public interface WxMpService extends WxService {
/** /**
* 返回电子发票报销方相关接口 * 返回电子发票报销方相关接口
*
* @return WxMpReimburseInvoiceService * @return WxMpReimburseInvoiceService
*/ */
WxMpReimburseInvoiceService getReimburseInvoiceService(); WxMpReimburseInvoiceService getReimburseInvoiceService();
/** /**
* . * .
*
* @param reimburseInvoiceService . * @param reimburseInvoiceService .
*/ */
void setReimburseInvoiceService(WxMpReimburseInvoiceService reimburseInvoiceService); void setReimburseInvoiceService(WxMpReimburseInvoiceService reimburseInvoiceService);

View File

@@ -32,6 +32,7 @@ import me.chanjar.weixin.mp.api.*;
import me.chanjar.weixin.mp.bean.WxMpSemanticQuery; import me.chanjar.weixin.mp.bean.WxMpSemanticQuery;
import me.chanjar.weixin.mp.bean.result.WxMpCurrentAutoReplyInfo; import me.chanjar.weixin.mp.bean.result.WxMpCurrentAutoReplyInfo;
import me.chanjar.weixin.mp.bean.result.WxMpSemanticQueryResult; import me.chanjar.weixin.mp.bean.result.WxMpSemanticQueryResult;
import me.chanjar.weixin.mp.bean.result.WxMpShortKeyResult;
import me.chanjar.weixin.mp.config.WxMpConfigStorage; import me.chanjar.weixin.mp.config.WxMpConfigStorage;
import me.chanjar.weixin.mp.enums.WxMpApiUrl; import me.chanjar.weixin.mp.enums.WxMpApiUrl;
import me.chanjar.weixin.mp.util.WxMpConfigStorageHolder; import me.chanjar.weixin.mp.util.WxMpConfigStorageHolder;
@@ -150,6 +151,24 @@ public abstract class BaseWxMpServiceImpl<H, P> implements WxMpService, RequestH
private int retrySleepMillis = 1000; private int retrySleepMillis = 1000;
private int maxRetryTimes = 5; private int maxRetryTimes = 5;
@Override
public String genShorten(String longData, Integer expireSeconds) throws WxErrorException {
JsonObject param = new JsonObject();
param.addProperty("long_data", longData);
param.addProperty("expire_seconds", expireSeconds);
String responseContent = this.post(GEN_SHORTEN_URL, param.toString());
JsonObject tmpJsonObject = GsonParser.parse(responseContent);
return tmpJsonObject.get("short_key").getAsString();
}
@Override
public WxMpShortKeyResult fetchShorten(String shortKey) throws WxErrorException {
JsonObject param = new JsonObject();
param.addProperty("short_key", shortKey);
String responseContent = this.post(FETCH_SHORTEN_URL, param.toString());
return WxMpShortKeyResult.fromJson(responseContent);
}
@Override @Override
public boolean checkSignature(String timestamp, String nonce, String signature) { public boolean checkSignature(String timestamp, String nonce, String signature) {
try { try {

View File

@@ -0,0 +1,39 @@
package me.chanjar.weixin.mp.bean.result;
import lombok.Data;
import me.chanjar.weixin.mp.util.json.WxMpGsonBuilder;
import java.io.Serializable;
/**
* 短key解析结果
*
* @author zsa
*/
@Data
public class WxMpShortKeyResult implements Serializable {
private static final long serialVersionUID = 5770641374997158852L;
/**
* 创建的时间戳
*/
protected String longData;
/**
* 创建的时间戳
*/
protected long createTime;
/**
* 剩余的过期秒数
*/
protected long expireSeconds;
public static WxMpShortKeyResult fromJson(String json) {
return WxMpGsonBuilder.create().fromJson(json, WxMpShortKeyResult.class);
}
@Override
public String toString() {
return WxMpGsonBuilder.create().toJson(this);
}
}

View File

@@ -157,7 +157,17 @@ public interface WxMpApiUrl {
/** /**
* 公众号调用或第三方平台帮公众号调用对公众号的所有api调用包括第三方帮其调用次数进行清零. * 公众号调用或第三方平台帮公众号调用对公众号的所有api调用包括第三方帮其调用次数进行清零.
*/ */
CLEAR_QUOTA_URL(API_DEFAULT_HOST_URL, "/cgi-bin/clear_quota"); CLEAR_QUOTA_URL(API_DEFAULT_HOST_URL, "/cgi-bin/clear_quota"),
/**
* 短key托管(生成短key的url)
*/
GEN_SHORTEN_URL(API_DEFAULT_HOST_URL, "/cgi-bin/shorten/gen"),
/**
* 短key解析(解析短key的url)
*/
FETCH_SHORTEN_URL(API_DEFAULT_HOST_URL, "/cgi-bin/shorten/fetch");
private final String prefix; private final String prefix;
private final String path; private final String path;