Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Merged
merged 1 commit into from
Jul 29, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import me.chanjar.weixin.mp.bean.WxMpSemanticQuery;
import me.chanjar.weixin.mp.bean.result.WxMpCurrentAutoReplyInfo;
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.enums.WxMpApiUrl;

Expand All @@ -26,6 +27,31 @@
* @author chanjarster
*/
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 短key,15字节,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>
* 验证消息的确来自微信服务器.
Expand Down Expand Up @@ -263,7 +289,7 @@ public interface WxMpService extends WxService {
/**
* 当本Service没有实现某个API的时候,可以用这个,针对所有微信API中的POST请求.
*
* @param url 请求接口地址
* @param url 请求接口地址
* @param obj 请求参数
* @return 接口响应字符串 string
* @throws WxErrorException 异常
Expand Down Expand Up @@ -536,12 +562,14 @@ public interface WxMpService extends WxService {

/**
* 返回电子发票报销方相关接口
*
* @return WxMpReimburseInvoiceService
*/
WxMpReimburseInvoiceService getReimburseInvoiceService();

/**
* .
*
* @param reimburseInvoiceService .
*/
void setReimburseInvoiceService(WxMpReimburseInvoiceService reimburseInvoiceService);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
import me.chanjar.weixin.mp.bean.WxMpSemanticQuery;
import me.chanjar.weixin.mp.bean.result.WxMpCurrentAutoReplyInfo;
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.enums.WxMpApiUrl;
import me.chanjar.weixin.mp.util.WxMpConfigStorageHolder;
Expand Down Expand Up @@ -150,6 +151,24 @@ public abstract class BaseWxMpServiceImpl<H, P> implements WxMpService, RequestH
private int retrySleepMillis = 1000;
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
public boolean checkSignature(String timestamp, String nonce, String signature) {
try {
Expand Down
Original file line number Diff line number Diff line change
@@ -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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,17 @@ enum Other implements WxMpApiUrl {
/**
* 公众号调用或第三方平台帮公众号调用对公众号的所有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 path;
Expand Down