mirror of
https://gitee.com/binary/weixin-java-tools.git
synced 2025-10-22 03:27:39 +08:00
🎨 重构代码,抽取公共方法到接口
This commit is contained in:
@@ -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的时候,可以用这个,
|
||||
|
@@ -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);
|
||||
|
@@ -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);
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user