mirror of
				https://gitee.com/binary/weixin-java-tools.git
				synced 2025-11-01 00:46:54 +08:00 
			
		
		
		
	🆕 #2222 【公众号】 新增短key托管和解析的接口
This commit is contained in:
		| @@ -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.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; | ||||
|  | ||||
| @@ -26,6 +27,31 @@ import java.util.Map; | ||||
|  * @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> | ||||
|    * 验证消息的确来自微信服务器. | ||||
| @@ -263,7 +289,7 @@ public interface WxMpService extends WxService { | ||||
|   /** | ||||
|    * 当本Service没有实现某个API的时候,可以用这个,针对所有微信API中的POST请求. | ||||
|    * | ||||
|    * @param url      请求接口地址 | ||||
|    * @param url 请求接口地址 | ||||
|    * @param obj 请求参数 | ||||
|    * @return 接口响应字符串 string | ||||
|    * @throws WxErrorException 异常 | ||||
| @@ -536,12 +562,14 @@ public interface WxMpService extends WxService { | ||||
|  | ||||
|   /** | ||||
|    * 返回电子发票报销方相关接口 | ||||
|    * | ||||
|    * @return WxMpReimburseInvoiceService | ||||
|    */ | ||||
|   WxMpReimburseInvoiceService getReimburseInvoiceService(); | ||||
|  | ||||
|   /** | ||||
|    * . | ||||
|    * | ||||
|    * @param reimburseInvoiceService . | ||||
|    */ | ||||
|   void setReimburseInvoiceService(WxMpReimburseInvoiceService reimburseInvoiceService); | ||||
|   | ||||
| @@ -32,6 +32,7 @@ import me.chanjar.weixin.mp.api.*; | ||||
| 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; | ||||
| @@ -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 { | ||||
|   | ||||
| @@ -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); | ||||
|   } | ||||
| } | ||||
| @@ -157,7 +157,17 @@ public interface 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; | ||||
|   | ||||
		Reference in New Issue
	
	Block a user
	 zsail
					zsail