Skip to content

Commit 6d2c0a3

Browse files
authored
🆕 #2222 【公众号】 新增短key托管和解析的接口
1 parent 00347dc commit 6d2c0a3

File tree

4 files changed

+98
-2
lines changed

4 files changed

+98
-2
lines changed

weixin-java-mp/src/main/java/me/chanjar/weixin/mp/api/WxMpService.java

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
import me.chanjar.weixin.mp.bean.WxMpSemanticQuery;
1616
import me.chanjar.weixin.mp.bean.result.WxMpCurrentAutoReplyInfo;
1717
import me.chanjar.weixin.mp.bean.result.WxMpSemanticQueryResult;
18+
import me.chanjar.weixin.mp.bean.result.WxMpShortKeyResult;
1819
import me.chanjar.weixin.mp.config.WxMpConfigStorage;
1920
import me.chanjar.weixin.mp.enums.WxMpApiUrl;
2021

@@ -26,6 +27,31 @@
2627
* @author chanjarster
2728
*/
2829
public interface WxMpService extends WxService {
30+
/**
31+
* <pre>
32+
* 短key托管 类似于短链API.
33+
* 详情请见: https://developers.weixin.qq.com/doc/offiaccount/Account_Management/KEY_Shortener.html
34+
* </pre>
35+
*
36+
* @param longData 需要转换的长信息,不超过4KB
37+
* @param expireSeconds 短key有效期(单位秒),最大值为2592000(即30天),默认为2592000(30天)
38+
* @return shortKey 短key,15字节,base62编码(0-9/a-z/A-Z)
39+
* @throws WxErrorException .
40+
*/
41+
String genShorten(String longData, Integer expireSeconds) throws WxErrorException;
42+
43+
/**
44+
* <pre>
45+
* 短key解析 将短key还原为长信息。
46+
* 详情请见: https://developers.weixin.qq.com/doc/offiaccount/Account_Management/KEY_Shortener.html
47+
* </pre>
48+
*
49+
* @param shortKey 短key
50+
* @return WxMpShortKeyResult 解析结果
51+
* @throws WxErrorException .
52+
*/
53+
WxMpShortKeyResult fetchShorten(String shortKey) throws WxErrorException;
54+
2955
/**
3056
* <pre>
3157
* 验证消息的确来自微信服务器.
@@ -263,7 +289,7 @@ public interface WxMpService extends WxService {
263289
/**
264290
* 当本Service没有实现某个API的时候,可以用这个,针对所有微信API中的POST请求.
265291
*
266-
* @param url 请求接口地址
292+
* @param url 请求接口地址
267293
* @param obj 请求参数
268294
* @return 接口响应字符串 string
269295
* @throws WxErrorException 异常
@@ -536,12 +562,14 @@ public interface WxMpService extends WxService {
536562

537563
/**
538564
* 返回电子发票报销方相关接口
565+
*
539566
* @return WxMpReimburseInvoiceService
540567
*/
541568
WxMpReimburseInvoiceService getReimburseInvoiceService();
542569

543570
/**
544571
* .
572+
*
545573
* @param reimburseInvoiceService .
546574
*/
547575
void setReimburseInvoiceService(WxMpReimburseInvoiceService reimburseInvoiceService);

weixin-java-mp/src/main/java/me/chanjar/weixin/mp/api/impl/BaseWxMpServiceImpl.java

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
import me.chanjar.weixin.mp.bean.WxMpSemanticQuery;
3333
import me.chanjar.weixin.mp.bean.result.WxMpCurrentAutoReplyInfo;
3434
import me.chanjar.weixin.mp.bean.result.WxMpSemanticQueryResult;
35+
import me.chanjar.weixin.mp.bean.result.WxMpShortKeyResult;
3536
import me.chanjar.weixin.mp.config.WxMpConfigStorage;
3637
import me.chanjar.weixin.mp.enums.WxMpApiUrl;
3738
import me.chanjar.weixin.mp.util.WxMpConfigStorageHolder;
@@ -150,6 +151,24 @@ public abstract class BaseWxMpServiceImpl<H, P> implements WxMpService, RequestH
150151
private int retrySleepMillis = 1000;
151152
private int maxRetryTimes = 5;
152153

154+
@Override
155+
public String genShorten(String longData, Integer expireSeconds) throws WxErrorException {
156+
JsonObject param = new JsonObject();
157+
param.addProperty("long_data", longData);
158+
param.addProperty("expire_seconds", expireSeconds);
159+
String responseContent = this.post(GEN_SHORTEN_URL, param.toString());
160+
JsonObject tmpJsonObject = GsonParser.parse(responseContent);
161+
return tmpJsonObject.get("short_key").getAsString();
162+
}
163+
164+
@Override
165+
public WxMpShortKeyResult fetchShorten(String shortKey) throws WxErrorException {
166+
JsonObject param = new JsonObject();
167+
param.addProperty("short_key", shortKey);
168+
String responseContent = this.post(FETCH_SHORTEN_URL, param.toString());
169+
return WxMpShortKeyResult.fromJson(responseContent);
170+
}
171+
153172
@Override
154173
public boolean checkSignature(String timestamp, String nonce, String signature) {
155174
try {
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
package me.chanjar.weixin.mp.bean.result;
2+
3+
import lombok.Data;
4+
import me.chanjar.weixin.mp.util.json.WxMpGsonBuilder;
5+
6+
import java.io.Serializable;
7+
8+
/**
9+
* 短key解析结果
10+
*
11+
* @author zsa
12+
*/
13+
@Data
14+
public class WxMpShortKeyResult implements Serializable {
15+
16+
private static final long serialVersionUID = 5770641374997158852L;
17+
18+
/**
19+
* 创建的时间戳
20+
*/
21+
protected String longData;
22+
/**
23+
* 创建的时间戳
24+
*/
25+
protected long createTime;
26+
/**
27+
* 剩余的过期秒数
28+
*/
29+
protected long expireSeconds;
30+
31+
public static WxMpShortKeyResult fromJson(String json) {
32+
return WxMpGsonBuilder.create().fromJson(json, WxMpShortKeyResult.class);
33+
}
34+
35+
@Override
36+
public String toString() {
37+
return WxMpGsonBuilder.create().toJson(this);
38+
}
39+
}

weixin-java-mp/src/main/java/me/chanjar/weixin/mp/enums/WxMpApiUrl.java

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -157,7 +157,17 @@ enum Other implements WxMpApiUrl {
157157
/**
158158
* 公众号调用或第三方平台帮公众号调用对公众号的所有api调用(包括第三方帮其调用)次数进行清零.
159159
*/
160-
CLEAR_QUOTA_URL(API_DEFAULT_HOST_URL, "/cgi-bin/clear_quota");
160+
CLEAR_QUOTA_URL(API_DEFAULT_HOST_URL, "/cgi-bin/clear_quota"),
161+
162+
/**
163+
* 短key托管(生成短key的url)
164+
*/
165+
GEN_SHORTEN_URL(API_DEFAULT_HOST_URL, "/cgi-bin/shorten/gen"),
166+
167+
/**
168+
* 短key解析(解析短key的url)
169+
*/
170+
FETCH_SHORTEN_URL(API_DEFAULT_HOST_URL, "/cgi-bin/shorten/fetch");
161171

162172
private final String prefix;
163173
private final String path;

0 commit comments

Comments
 (0)