From 8a63f022ceda29612de2cb0c8b2d971a71aa3618 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=B0=B4=E4=BE=9D=E5=AF=92?= Date: Tue, 26 Dec 2023 10:44:34 +0800 Subject: [PATCH] =?UTF-8?q?:art:=20#3194=20=E3=80=90=E5=B0=8F=E7=A8=8B?= =?UTF-8?q?=E5=BA=8F=E3=80=91=E4=BC=98=E5=8C=96openApi=E9=83=A8=E5=88=86?= =?UTF-8?q?=E6=8E=A5=E5=8F=A3=EF=BC=88getApiQuota=20=E5=92=8C=20getRidInfo?= =?UTF-8?q?=20=EF=BC=89=E5=93=8D=E5=BA=94=E7=B1=BB=E7=9A=84=E9=83=A8?= =?UTF-8?q?=E5=88=86=E5=AD=97=E6=AE=B5=E4=BF=A1=E6=81=AF?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../wx/miniapp/api/WxMaOpenApiService.java | 8 +-- .../api/impl/WxMaOpenApiServiceImpl.java | 9 +-- .../bean/openapi/WxMiniGetApiQuotaResult.java | 70 +++++++++++++++++-- .../api/impl/WxMaOpenApiServiceImplTest.java | 10 +++ 4 files changed, 78 insertions(+), 19 deletions(-) diff --git a/weixin-java-miniapp/src/main/java/cn/binarywang/wx/miniapp/api/WxMaOpenApiService.java b/weixin-java-miniapp/src/main/java/cn/binarywang/wx/miniapp/api/WxMaOpenApiService.java index 438c43c6b8..8ef7e2b24b 100644 --- a/weixin-java-miniapp/src/main/java/cn/binarywang/wx/miniapp/api/WxMaOpenApiService.java +++ b/weixin-java-miniapp/src/main/java/cn/binarywang/wx/miniapp/api/WxMaOpenApiService.java @@ -15,7 +15,6 @@ public interface WxMaOpenApiService { /** * 本接口用于清空公众号/小程序/第三方平台等接口的每日调用接口次数 - * HTTP调用:https://api.weixin.qq.com/cgi-bin/clear_quota?access_token=ACCESS_TOKEN * * @return 是否成功 * @throws WxErrorException the wx error exception @@ -28,18 +27,18 @@ public interface WxMaOpenApiService { /** * 查询API调用额度 - * HTTP调用:https://api.weixin.qq.com/cgi-bin/openapi/quota/get?access_token=ACCESS_TOKEN * - * @param cgiPath api的请求地址,例如"/cgi-bin/message/custom/send";不要前缀“https://api.weixin.qq.com” ,也不要漏了"/",否则都会76003的报错 + * @param cgiPath api的请求地址, + * 例如"/cgi-bin/message/custom/send";不要前缀“https://api.weixin.qq.com” ,也不要漏了"/",否则都会76003的报错; * @return 额度详情 * @throws WxErrorException 微信异常 + * @apiNote "/xxx/sns/xxx" 这类接口不支持使用该接口,会出现76022报错。 * @see 注意事项参考微信文档 */ WxMiniGetApiQuotaResult getApiQuota(String cgiPath) throws WxErrorException; /** * 查询rid信息 - * HTTP调用:https://api.weixin.qq.com/cgi-bin/openapi/rid/get?access_token=ACCESS_TOKEN * * @param rid 调用接口报错返回的rid * @return 该rid对应的请求详情 @@ -51,7 +50,6 @@ public interface WxMaOpenApiService { /** * 使用AppSecret重置 API 调用次数 - * HTTP调用:https://api.weixin.qq.com/cgi-bin/clear_quota/v2 * * @return 是否成功 * @throws WxErrorException 微信异常 diff --git a/weixin-java-miniapp/src/main/java/cn/binarywang/wx/miniapp/api/impl/WxMaOpenApiServiceImpl.java b/weixin-java-miniapp/src/main/java/cn/binarywang/wx/miniapp/api/impl/WxMaOpenApiServiceImpl.java index b74b9bfb27..863722a534 100644 --- a/weixin-java-miniapp/src/main/java/cn/binarywang/wx/miniapp/api/impl/WxMaOpenApiServiceImpl.java +++ b/weixin-java-miniapp/src/main/java/cn/binarywang/wx/miniapp/api/impl/WxMaOpenApiServiceImpl.java @@ -23,7 +23,6 @@ public class WxMaOpenApiServiceImpl implements WxMaOpenApiService { private final WxMaService wxMaService; - private static final String QUOTA = "quota"; private static final String REQUEST = "request"; @@ -42,11 +41,7 @@ public WxMiniGetApiQuotaResult getApiQuota(String cgiPath) throws WxErrorExcepti params.addProperty("cgi_path", cgiPath); String responseContent = this.wxMaService.post(WxMaApiUrlConstants.OpenApi.GET_API_QUOTA, params.toString()); parseErrorResponse(responseContent); - JsonObject response = GsonParser.parse(responseContent); - if (response.has(QUOTA)) { - return WxMaGsonBuilder.create().fromJson(response.getAsJsonObject(QUOTA), WxMiniGetApiQuotaResult.class); - } - return null; + return WxMaGsonBuilder.create().fromJson(GsonParser.parse(responseContent), WxMiniGetApiQuotaResult.class); } @@ -58,7 +53,7 @@ public WxMiniGetRidInfoResult getRidInfo(String rid) throws WxErrorException { parseErrorResponse(responseContent); JsonObject response = GsonParser.parse(responseContent); if (response.has(REQUEST)) { - return WxMaGsonBuilder.create().fromJson(response.getAsJsonObject(QUOTA), WxMiniGetRidInfoResult.class); + return WxMaGsonBuilder.create().fromJson(response.getAsJsonObject(REQUEST), WxMiniGetRidInfoResult.class); } return null; } diff --git a/weixin-java-miniapp/src/main/java/cn/binarywang/wx/miniapp/bean/openapi/WxMiniGetApiQuotaResult.java b/weixin-java-miniapp/src/main/java/cn/binarywang/wx/miniapp/bean/openapi/WxMiniGetApiQuotaResult.java index be02f68395..83359f92ba 100644 --- a/weixin-java-miniapp/src/main/java/cn/binarywang/wx/miniapp/bean/openapi/WxMiniGetApiQuotaResult.java +++ b/weixin-java-miniapp/src/main/java/cn/binarywang/wx/miniapp/bean/openapi/WxMiniGetApiQuotaResult.java @@ -12,17 +12,73 @@ @Data public class WxMiniGetApiQuotaResult { + + /** + * quota详情 + */ + private WxMiniGetApiQuotaDetail quota; + /** + * 普通调用频率限制 + */ + private WxMiniGetApiQuotaRateLimit rateLimit; + /** + * 代调用频率限制 + */ + private WxMiniGetApiQuotaComponentRateLimit componentRateLimit; + + /** - * 当天该账号可调用该接口的次数 + * quota详情 */ - @SerializedName("daily_limit") - private Integer dailyLimit; + @Data + private static class WxMiniGetApiQuotaDetail { + /** + * 当天该账号可调用该接口的次数 + */ + @SerializedName("daily_limit") + private Long dailyLimit; + /** + * 当天已经调用的次数 + */ + private Long used; + /** + * 当天剩余调用次数 + */ + private Long remain; + } + /** - * 当天已经调用的次数 + * 普通调用频率限制 */ - private Integer used; + @Data + private static class WxMiniGetApiQuotaRateLimit { + /** + * 周期内可调用数量,单位 次 + */ + @SerializedName("call_count") + private Long callCount; + /** + * 更新周期,单位 秒 + */ + @SerializedName("refresh_second") + private Long refreshSecond; + } + /** - * 当天剩余调用次数 + * 代调用频率限制 */ - private Integer remain; + @Data + private static class WxMiniGetApiQuotaComponentRateLimit { + /** + * 周期内可调用数量,单位 次 + */ + @SerializedName("call_count") + private Long callCount; + /** + * 更新周期,单位 秒 + */ + @SerializedName("refresh_second") + private Long refreshSecond; + } + } diff --git a/weixin-java-miniapp/src/test/java/cn/binarywang/wx/miniapp/api/impl/WxMaOpenApiServiceImplTest.java b/weixin-java-miniapp/src/test/java/cn/binarywang/wx/miniapp/api/impl/WxMaOpenApiServiceImplTest.java index 409e63e717..e7a1d04679 100644 --- a/weixin-java-miniapp/src/test/java/cn/binarywang/wx/miniapp/api/impl/WxMaOpenApiServiceImplTest.java +++ b/weixin-java-miniapp/src/test/java/cn/binarywang/wx/miniapp/api/impl/WxMaOpenApiServiceImplTest.java @@ -2,6 +2,7 @@ import cn.binarywang.wx.miniapp.api.WxMaService; import cn.binarywang.wx.miniapp.bean.openapi.WxMiniGetApiQuotaResult; +import cn.binarywang.wx.miniapp.bean.openapi.WxMiniGetRidInfoResult; import cn.binarywang.wx.miniapp.test.ApiTestModule; import com.google.gson.Gson; import com.google.inject.Inject; @@ -39,6 +40,15 @@ public void getApiQuota() throws WxErrorException { assertNotNull(apiQuota); System.out.println(new Gson().toJson(apiQuota)); } + + @Test + public void getApiQuotaInfo() throws WxErrorException { + String rid = "658723fa-2d3a0086-64bc7215"; + final WxMiniGetRidInfoResult ridInfo = wxMaService.getWxMaOpenApiService().getRidInfo(rid); + assertNotNull(ridInfo); + System.out.println(new Gson().toJson(ridInfo)); + } + @Test public void clearQuotaByAppSecret() throws WxErrorException { final boolean result = wxMaService.getWxMaOpenApiService().clearQuotaByAppSecret();