|
| 1 | +package cn.binarywang.wx.miniapp.api.impl; |
| 2 | + |
| 3 | +import cn.binarywang.wx.miniapp.api.WxMaOpenApiService; |
| 4 | +import cn.binarywang.wx.miniapp.api.WxMaService; |
| 5 | +import cn.binarywang.wx.miniapp.bean.openapi.WxMiniGetApiQuotaResult; |
| 6 | +import cn.binarywang.wx.miniapp.bean.openapi.WxMiniGetRidInfoResult; |
| 7 | +import cn.binarywang.wx.miniapp.constant.WxMaApiUrlConstants; |
| 8 | +import cn.binarywang.wx.miniapp.json.WxMaGsonBuilder; |
| 9 | +import com.google.gson.JsonObject; |
| 10 | +import lombok.RequiredArgsConstructor; |
| 11 | +import me.chanjar.weixin.common.enums.WxType; |
| 12 | +import me.chanjar.weixin.common.error.WxError; |
| 13 | +import me.chanjar.weixin.common.error.WxErrorException; |
| 14 | +import me.chanjar.weixin.common.util.json.GsonParser; |
| 15 | + |
| 16 | +import static me.chanjar.weixin.common.api.WxConsts.ERR_CODE; |
| 17 | + |
| 18 | +/** |
| 19 | + * @author shuiyihan12 |
| 20 | + * @since 2023/7/7 17:08 |
| 21 | + */ |
| 22 | +@RequiredArgsConstructor |
| 23 | +public class WxMaOpenApiServiceImpl implements WxMaOpenApiService { |
| 24 | + private final WxMaService wxMaService; |
| 25 | + |
| 26 | + private static final String QUOTA = "quota"; |
| 27 | + private static final String REQUEST = "request"; |
| 28 | + |
| 29 | + |
| 30 | + @Override |
| 31 | + public boolean clearQuota() throws WxErrorException { |
| 32 | + JsonObject params = new JsonObject(); |
| 33 | + params.addProperty("appid", this.wxMaService.getWxMaConfig().getAppid()); |
| 34 | + String responseContent = this.wxMaService.post(WxMaApiUrlConstants.OpenApi.CLEAR_QUOTA, params.toString()); |
| 35 | + parseErrorResponse(responseContent); |
| 36 | + return true; |
| 37 | + } |
| 38 | + |
| 39 | + @Override |
| 40 | + public WxMiniGetApiQuotaResult getApiQuota(String cgiPath) throws WxErrorException { |
| 41 | + JsonObject params = new JsonObject(); |
| 42 | + params.addProperty("cgi_path", cgiPath); |
| 43 | + String responseContent = this.wxMaService.post(WxMaApiUrlConstants.OpenApi.GET_API_QUOTA, params.toString()); |
| 44 | + parseErrorResponse(responseContent); |
| 45 | + JsonObject response = GsonParser.parse(responseContent); |
| 46 | + if (response.has(QUOTA)) { |
| 47 | + return WxMaGsonBuilder.create().fromJson(response.getAsJsonObject(QUOTA), WxMiniGetApiQuotaResult.class); |
| 48 | + } |
| 49 | + return null; |
| 50 | + } |
| 51 | + |
| 52 | + |
| 53 | + @Override |
| 54 | + public WxMiniGetRidInfoResult getRidInfo(String rid) throws WxErrorException { |
| 55 | + JsonObject params = new JsonObject(); |
| 56 | + params.addProperty("rid", rid); |
| 57 | + String responseContent = this.wxMaService.post(WxMaApiUrlConstants.OpenApi.GET_RID_INFO, params.toString()); |
| 58 | + parseErrorResponse(responseContent); |
| 59 | + JsonObject response = GsonParser.parse(responseContent); |
| 60 | + if (response.has(REQUEST)) { |
| 61 | + return WxMaGsonBuilder.create().fromJson(response.getAsJsonObject(QUOTA), WxMiniGetRidInfoResult.class); |
| 62 | + } |
| 63 | + return null; |
| 64 | + } |
| 65 | + |
| 66 | + @Override |
| 67 | + public boolean clearQuotaByAppSecret() throws WxErrorException { |
| 68 | + String url = String.format(WxMaApiUrlConstants.OpenApi.CLEAR_QUOTA_BY_APP_SECRET, this.wxMaService.getWxMaConfig().getAppid(), this.wxMaService.getWxMaConfig().getSecret()); |
| 69 | + String responseContent = this.wxMaService.post(url, ""); |
| 70 | + parseErrorResponse(responseContent); |
| 71 | + return true; |
| 72 | + } |
| 73 | + |
| 74 | + |
| 75 | + private void parseErrorResponse(String response) throws WxErrorException { |
| 76 | + JsonObject jsonObject = GsonParser.parse(response); |
| 77 | + if (jsonObject.get(ERR_CODE).getAsInt() != 0) { |
| 78 | + throw new WxErrorException(WxError.fromJson(response, WxType.MiniApp)); |
| 79 | + } |
| 80 | + } |
| 81 | +} |
0 commit comments