diff --git a/weixin-java-common/src/main/java/me/chanjar/weixin/common/service/WxService.java b/weixin-java-common/src/main/java/me/chanjar/weixin/common/service/WxService.java new file mode 100644 index 00000000..fc49bfd9 --- /dev/null +++ b/weixin-java-common/src/main/java/me/chanjar/weixin/common/service/WxService.java @@ -0,0 +1,41 @@ +package me.chanjar.weixin.common.service; + +import me.chanjar.weixin.common.error.WxErrorException; + +/** + * 微信服务接口. + * + * @author Binary Wang + * @date 2020-04-25 + */ +public interface WxService { + /** + * 当本Service没有实现某个API的时候,可以用这个,针对所有微信API中的GET请求. + * + * @param queryParam 参数 + * @param url 请求接口地址 + * @return 接口响应字符串 + * @throws WxErrorException 异常 + */ + String get(String url, String queryParam) throws WxErrorException; + + /** + * 当本Service没有实现某个API的时候,可以用这个,针对所有微信API中的POST请求. + * + * @param postData 请求参数json值 + * @param url 请求接口地址 + * @return 接口响应字符串 + * @throws WxErrorException 异常 + */ + String post(String url, String postData) throws WxErrorException; + + /** + * 当本Service没有实现某个API的时候,可以用这个,针对所有微信API中的POST请求. + * + * @param url 请求接口地址 + * @param obj 请求对象 + * @return 接口响应字符串 + * @throws WxErrorException 异常 + */ + String post(String url, Object obj) throws WxErrorException; +} diff --git a/weixin-java-miniapp/src/main/java/cn/binarywang/wx/miniapp/api/WxMaService.java b/weixin-java-miniapp/src/main/java/cn/binarywang/wx/miniapp/api/WxMaService.java index 6b4a7156..875d9430 100644 --- a/weixin-java-miniapp/src/main/java/cn/binarywang/wx/miniapp/api/WxMaService.java +++ b/weixin-java-miniapp/src/main/java/cn/binarywang/wx/miniapp/api/WxMaService.java @@ -3,6 +3,7 @@ package cn.binarywang.wx.miniapp.api; import cn.binarywang.wx.miniapp.bean.WxMaJscode2SessionResult; import cn.binarywang.wx.miniapp.config.WxMaConfig; import me.chanjar.weixin.common.error.WxErrorException; +import me.chanjar.weixin.common.service.WxService; import me.chanjar.weixin.common.util.http.MediaUploadRequestExecutor; import me.chanjar.weixin.common.util.http.RequestExecutor; import me.chanjar.weixin.common.util.http.RequestHttp; @@ -10,7 +11,7 @@ import me.chanjar.weixin.common.util.http.RequestHttp; /** * @author Binary Wang */ -public interface WxMaService { +public interface WxMaService extends WxService { /** * 获取access_token. */ @@ -99,21 +100,6 @@ public interface WxMaService { */ String getPaidUnionId(String openid, String transactionId, String mchId, String outTradeNo) throws WxErrorException; - /** - * 当本Service没有实现某个API的时候,可以用这个,针对所有微信API中的GET请求. - */ - String get(String url, String queryParam) throws WxErrorException; - - /** - * 当本Service没有实现某个API的时候,可以用这个,针对所有微信API中的POST请求. - */ - String post(String url, String postData) throws WxErrorException; - - /** - * 当本Service没有实现某个API的时候,可以用这个,针对所有微信API中的POST请求. - */ - String post(String url, Object obj) throws WxErrorException; - /** *
* Service没有实现某个API的时候,可以用这个, diff --git a/weixin-java-miniapp/src/main/java/cn/binarywang/wx/miniapp/api/impl/WxMaServiceImpl.java b/weixin-java-miniapp/src/main/java/cn/binarywang/wx/miniapp/api/impl/WxMaServiceImpl.java index 88984c35..e7608eef 100644 --- a/weixin-java-miniapp/src/main/java/cn/binarywang/wx/miniapp/api/impl/WxMaServiceImpl.java +++ b/weixin-java-miniapp/src/main/java/cn/binarywang/wx/miniapp/api/impl/WxMaServiceImpl.java @@ -44,23 +44,23 @@ public class WxMaServiceImpl implements WxMaService, RequestHttp* 验证消息的确来自微信服务器. @@ -276,26 +277,6 @@ public interface WxMpService { */ void clearQuota(String appid) throws WxErrorException; - /** - * 当本Service没有实现某个API的时候,可以用这个,针对所有微信API中的GET请求. - * - * @param queryParam 参数 - * @param url 请求接口地址 - * @return 接口响应字符串 - * @throws WxErrorException 异常 - */ - String get(String url, String queryParam) throws WxErrorException; - - /** - * 当本Service没有实现某个API的时候,可以用这个,针对所有微信API中的POST请求. - * - * @param postData 请求参数json值 - * @param url 请求接口地址 - * @return 接口响应字符串 - * @throws WxErrorException 异常 - */ - String post(String url, String postData) throws WxErrorException; - /** * * Service没有实现某个API的时候,可以用这个, diff --git a/weixin-java-mp/src/main/java/me/chanjar/weixin/mp/api/impl/BaseWxMpServiceImpl.java b/weixin-java-mp/src/main/java/me/chanjar/weixin/mp/api/impl/BaseWxMpServiceImpl.java index 09275cd6..58ba8446 100644 --- a/weixin-java-mp/src/main/java/me/chanjar/weixin/mp/api/impl/BaseWxMpServiceImpl.java +++ b/weixin-java-mp/src/main/java/me/chanjar/weixin/mp/api/impl/BaseWxMpServiceImpl.java @@ -18,6 +18,7 @@ import me.chanjar.weixin.common.util.DataUtils; import me.chanjar.weixin.common.util.RandomUtils; import me.chanjar.weixin.common.util.crypto.SHA1; import me.chanjar.weixin.common.util.http.*; +import me.chanjar.weixin.common.util.json.WxGsonBuilder; import me.chanjar.weixin.mp.api.*; import me.chanjar.weixin.mp.bean.WxMpSemanticQuery; import me.chanjar.weixin.mp.bean.result.WxMpCurrentAutoReplyInfo; @@ -57,13 +58,13 @@ public abstract class BaseWxMpServiceImplimplements WxMpService, RequestH private WxMpDataCubeService dataCubeService = new WxMpDataCubeServiceImpl(this); private WxMpUserBlacklistService blackListService = new WxMpUserBlacklistServiceImpl(this); private WxMpTemplateMsgService templateMsgService = new WxMpTemplateMsgServiceImpl(this); - private WxMpSubscribeMsgService subscribeMsgService = new WxMpSubscribeMsgServiceImpl(this); + private final WxMpSubscribeMsgService subscribeMsgService = new WxMpSubscribeMsgServiceImpl(this); private WxMpDeviceService deviceService = new WxMpDeviceServiceImpl(this); private WxMpShakeService shakeService = new WxMpShakeServiceImpl(this); private WxMpMemberCardService memberCardService = new WxMpMemberCardServiceImpl(this); private WxMpMassMessageService massMessageService = new WxMpMassMessageServiceImpl(this); private WxMpAiOpenService aiOpenService = new WxMpAiOpenServiceImpl(this); - private WxMpWifiService wifiService = new WxMpWifiServiceImpl(this); + private final WxMpWifiService wifiService = new WxMpWifiServiceImpl(this); private WxMpMarketingService marketingService = new WxMpMarketingServiceImpl(this); private WxMpCommentService commentService = new WxMpCommentServiceImpl(this); private WxMpOcrService ocrService = new WxMpOcrServiceImpl(this); @@ -286,6 +287,11 @@ public abstract class BaseWxMpServiceImpl implements WxMpService, RequestH return this.post(url.getUrl(this.getWxMpConfigStorage()), postData); } + @Override + public String post(String url, Object obj) throws WxErrorException { + return this.execute(SimplePostRequestExecutor.create(this), url, WxGsonBuilder.create().toJson(obj)); + } + @Override public T execute(RequestExecutor executor, WxMpApiUrl url, E data) throws WxErrorException { return this.execute(executor, url.getUrl(this.getWxMpConfigStorage()), data); diff --git a/weixin-java-mp/src/main/java/me/chanjar/weixin/mp/api/impl/WxMpOcrServiceImpl.java b/weixin-java-mp/src/main/java/me/chanjar/weixin/mp/api/impl/WxMpOcrServiceImpl.java index 118a02c9..dbf3dd03 100644 --- a/weixin-java-mp/src/main/java/me/chanjar/weixin/mp/api/impl/WxMpOcrServiceImpl.java +++ b/weixin-java-mp/src/main/java/me/chanjar/weixin/mp/api/impl/WxMpOcrServiceImpl.java @@ -38,7 +38,7 @@ import static me.chanjar.weixin.mp.enums.WxMpApiUrl.Ocr.IDCARD; */ @RequiredArgsConstructor public class WxMpOcrServiceImpl implements WxMpOcrService { - private final WxMpService wxMpService; + private final WxMpService mainService; @Override public WxMpOcrIdCardResult idCard(String imgUrl) throws WxErrorException { @@ -48,14 +48,15 @@ public class WxMpOcrServiceImpl implements WxMpOcrService { // ignore cannot happen } - final String result = this.wxMpService.get(String.format(IDCARD.getUrl(this.wxMpService.getWxMpConfigStorage()), + final String result = this.mainService.get(String.format(IDCARD.getUrl(this.mainService.getWxMpConfigStorage()), imgUrl), null); return WxMpOcrIdCardResult.fromJson(result); } @Override public WxMpOcrIdCardResult idCard(File imgFile) throws WxErrorException { - String result = this.wxMpService.execute(OcrDiscernRequestExecutor.create(this.wxMpService.getRequestHttp()), FILEIDCARD.getUrl(this.wxMpService.getWxMpConfigStorage()), imgFile); + String result = this.mainService.execute(OcrDiscernRequestExecutor.create(this.mainService.getRequestHttp()), + FILEIDCARD.getUrl(this.mainService.getWxMpConfigStorage()), imgFile); return WxMpOcrIdCardResult.fromJson(result); } @@ -67,14 +68,15 @@ public class WxMpOcrServiceImpl implements WxMpOcrService { // ignore cannot happen } - final String result = this.wxMpService.get(String.format(BANK_CARD.getUrl(this.wxMpService.getWxMpConfigStorage()), + final String result = this.mainService.get(String.format(BANK_CARD.getUrl(this.mainService.getWxMpConfigStorage()), imgUrl), null); return WxMpOcrBankCardResult.fromJson(result); } @Override public WxMpOcrBankCardResult bankCard(File imgFile) throws WxErrorException { - String result = this.wxMpService.execute(OcrDiscernRequestExecutor.create(this.wxMpService.getRequestHttp()), FILE_BANK_CARD.getUrl(this.wxMpService.getWxMpConfigStorage()), imgFile); + String result = this.mainService.execute(OcrDiscernRequestExecutor.create(this.mainService.getRequestHttp()), + FILE_BANK_CARD.getUrl(this.mainService.getWxMpConfigStorage()), imgFile); return WxMpOcrBankCardResult.fromJson(result); } @@ -86,14 +88,15 @@ public class WxMpOcrServiceImpl implements WxMpOcrService { // ignore cannot happen } - final String result = this.wxMpService.get(String.format(DRIVING.getUrl(this.wxMpService.getWxMpConfigStorage()), + final String result = this.mainService.get(String.format(DRIVING.getUrl(this.mainService.getWxMpConfigStorage()), imgUrl), null); return WxMpOcrDrivingResult.fromJson(result); } @Override public WxMpOcrDrivingResult driving(File imgFile) throws WxErrorException { - String result = this.wxMpService.execute(OcrDiscernRequestExecutor.create(this.wxMpService.getRequestHttp()), FILE_DRIVING.getUrl(this.wxMpService.getWxMpConfigStorage()), imgFile); + String result = this.mainService.execute(OcrDiscernRequestExecutor.create(this.mainService.getRequestHttp()), + FILE_DRIVING.getUrl(this.mainService.getWxMpConfigStorage()), imgFile); return WxMpOcrDrivingResult.fromJson(result); } @@ -105,14 +108,15 @@ public class WxMpOcrServiceImpl implements WxMpOcrService { // ignore cannot happen } - final String result = this.wxMpService.get(String.format(DRIVING_LICENSE.getUrl(this.wxMpService.getWxMpConfigStorage()), + final String result = this.mainService.get(String.format(DRIVING_LICENSE.getUrl(this.mainService.getWxMpConfigStorage()), imgUrl), null); return WxMpOcrDrivingLicenseResult.fromJson(result); } @Override public WxMpOcrDrivingLicenseResult drivingLicense(File imgFile) throws WxErrorException { - String result = this.wxMpService.execute(OcrDiscernRequestExecutor.create(this.wxMpService.getRequestHttp()), FILE_DRIVING_LICENSE.getUrl(this.wxMpService.getWxMpConfigStorage()), imgFile); + String result = this.mainService.execute(OcrDiscernRequestExecutor.create(this.mainService.getRequestHttp()), + FILE_DRIVING_LICENSE.getUrl(this.mainService.getWxMpConfigStorage()), imgFile); return WxMpOcrDrivingLicenseResult.fromJson(result); } @@ -124,14 +128,15 @@ public class WxMpOcrServiceImpl implements WxMpOcrService { // ignore cannot happen } - final String result = this.wxMpService.get(String.format(BIZ_LICENSE.getUrl(this.wxMpService.getWxMpConfigStorage()), + final String result = this.mainService.get(String.format(BIZ_LICENSE.getUrl(this.mainService.getWxMpConfigStorage()), imgUrl), null); return WxMpOcrBizLicenseResult.fromJson(result); } @Override public WxMpOcrBizLicenseResult bizLicense(File imgFile) throws WxErrorException { - String result = this.wxMpService.execute(OcrDiscernRequestExecutor.create(this.wxMpService.getRequestHttp()), FILE_BIZ_LICENSE.getUrl(this.wxMpService.getWxMpConfigStorage()), imgFile); + String result = this.mainService.execute(OcrDiscernRequestExecutor.create(this.mainService.getRequestHttp()), + FILE_BIZ_LICENSE.getUrl(this.mainService.getWxMpConfigStorage()), imgFile); return WxMpOcrBizLicenseResult.fromJson(result); } @@ -143,14 +148,15 @@ public class WxMpOcrServiceImpl implements WxMpOcrService { // ignore cannot happen } - final String result = this.wxMpService.get(String.format(COMM.getUrl(this.wxMpService.getWxMpConfigStorage()), + final String result = this.mainService.get(String.format(COMM.getUrl(this.mainService.getWxMpConfigStorage()), imgUrl), null); return WxMpOcrCommResult.fromJson(result); } @Override public WxMpOcrCommResult comm(File imgFile) throws WxErrorException { - String result = this.wxMpService.execute(OcrDiscernRequestExecutor.create(this.wxMpService.getRequestHttp()), FILE_COMM.getUrl(this.wxMpService.getWxMpConfigStorage()), imgFile); + String result = this.mainService.execute(OcrDiscernRequestExecutor.create(this.mainService.getRequestHttp()), + FILE_COMM.getUrl(this.mainService.getWxMpConfigStorage()), imgFile); return WxMpOcrCommResult.fromJson(result); } }