🎨 重构代码,抽取公共方法到接口

This commit is contained in:
Binary Wang 2020-04-25 21:53:05 +08:00
parent 6da4b4fca1
commit ebe46822ef
6 changed files with 89 additions and 69 deletions

View File

@ -0,0 +1,41 @@
package me.chanjar.weixin.common.service;
import me.chanjar.weixin.common.error.WxErrorException;
/**
* 微信服务接口.
*
* @author <a href="https://github.com/binarywang">Binary Wang</a>
* @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;
}

View File

@ -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 <a href="https://github.com/binarywang">Binary Wang</a>
*/
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;
/**
* <pre>
* Service没有实现某个API的时候可以用这个

View File

@ -44,23 +44,23 @@ public class WxMaServiceImpl implements WxMaService, RequestHttp<CloseableHttpCl
private HttpHost httpProxy;
private WxMaConfig wxMaConfig;
private WxMaMsgService kefuService = new WxMaMsgServiceImpl(this);
private WxMaMediaService materialService = new WxMaMediaServiceImpl(this);
private WxMaUserService userService = new WxMaUserServiceImpl(this);
private WxMaQrcodeService qrCodeService = new WxMaQrcodeServiceImpl(this);
private WxMaTemplateService templateService = new WxMaTemplateServiceImpl(this);
private WxMaAnalysisService analysisService = new WxMaAnalysisServiceImpl(this);
private WxMaCodeService codeService = new WxMaCodeServiceImpl(this);
private WxMaSettingService settingService = new WxMaSettingServiceImpl(this);
private WxMaJsapiService jsapiService = new WxMaJsapiServiceImpl(this);
private WxMaShareService shareService = new WxMaShareServiceImpl(this);
private WxMaRunService runService = new WxMaRunServiceImpl(this);
private WxMaSecCheckService secCheckService = new WxMaSecCheckServiceImpl(this);
private WxMaPluginService pluginService = new WxMaPluginServiceImpl(this);
private WxMaExpressService expressService = new WxMaExpressServiceImpl(this);
private WxMaSubscribeService subscribeService = new WxMaSubscribeServiceImpl(this);
private WxMaCloudService cloudService = new WxMaCloudServiceImpl(this);
private WxMaLiveService liveService = new WxMaLiveServiceImpl(this);
private final WxMaMsgService kefuService = new WxMaMsgServiceImpl(this);
private final WxMaMediaService materialService = new WxMaMediaServiceImpl(this);
private final WxMaUserService userService = new WxMaUserServiceImpl(this);
private final WxMaQrcodeService qrCodeService = new WxMaQrcodeServiceImpl(this);
private final WxMaTemplateService templateService = new WxMaTemplateServiceImpl(this);
private final WxMaAnalysisService analysisService = new WxMaAnalysisServiceImpl(this);
private final WxMaCodeService codeService = new WxMaCodeServiceImpl(this);
private final WxMaSettingService settingService = new WxMaSettingServiceImpl(this);
private final WxMaJsapiService jsapiService = new WxMaJsapiServiceImpl(this);
private final WxMaShareService shareService = new WxMaShareServiceImpl(this);
private final WxMaRunService runService = new WxMaRunServiceImpl(this);
private final WxMaSecCheckService secCheckService = new WxMaSecCheckServiceImpl(this);
private final WxMaPluginService pluginService = new WxMaPluginServiceImpl(this);
private final WxMaExpressService expressService = new WxMaExpressServiceImpl(this);
private final WxMaSubscribeService subscribeService = new WxMaSubscribeServiceImpl(this);
private final WxMaCloudService cloudService = new WxMaCloudServiceImpl(this);
private final WxMaLiveService liveService = new WxMaLiveServiceImpl(this);
private int retrySleepMillis = 1000;
private int maxRetryTimes = 5;

View File

@ -3,6 +3,7 @@ package me.chanjar.weixin.mp.api;
import me.chanjar.weixin.common.bean.WxJsapiSignature;
import me.chanjar.weixin.common.bean.WxNetCheckResult;
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;
@ -22,7 +23,7 @@ import java.util.Map;
*
* @author chanjarster
*/
public interface WxMpService {
public interface WxMpService extends WxService {
/**
* <pre>
* 验证消息的确来自微信服务器.
@ -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;
/**
* <pre>
* Service没有实现某个API的时候可以用这个

View File

@ -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 BaseWxMpServiceImpl<H, P> implements 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<H, P> 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, E> T execute(RequestExecutor<T, E> executor, WxMpApiUrl url, E data) throws WxErrorException {
return this.execute(executor, url.getUrl(this.getWxMpConfigStorage()), data);

View File

@ -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);
}
}