Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

小程序自定义版交易组件上传接口支持图片链接 #2461

Merged
merged 3 commits into from
Dec 27, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -13,25 +13,34 @@
public abstract class MinishopUploadRequestCustomizeExecutor<H, P> implements RequestExecutor<WxMinishopImageUploadCustomizeResult, File> {
protected RequestHttp<H, P> requestHttp;
protected String respType;
protected String uploadType;
protected String imgUrl;

public MinishopUploadRequestCustomizeExecutor(RequestHttp requestHttp, String respType) {
public MinishopUploadRequestCustomizeExecutor(RequestHttp requestHttp, String respType, String imgUrl) {
this.requestHttp = requestHttp;
this.respType = respType;
if (imgUrl == null || imgUrl.isEmpty()) {
this.uploadType = "0";
}
else {
this.uploadType = "1";
this.imgUrl = imgUrl;
}
}

@Override
public void execute(String uri, File data, ResponseHandler<WxMinishopImageUploadCustomizeResult> handler, WxType wxType) throws WxErrorException, IOException {
handler.handle(this.execute(uri, data, wxType));
}

public static RequestExecutor<WxMinishopImageUploadCustomizeResult, File> create(RequestHttp requestHttp, String respType) {
public static RequestExecutor<WxMinishopImageUploadCustomizeResult, File> create(RequestHttp requestHttp, String respType, String imgUrl) {
switch (requestHttp.getRequestType()) {
case APACHE_HTTP:
return new ApacheMinishopMediaUploadRequestCustomizeExecutor(requestHttp, respType);
return new ApacheMinishopMediaUploadRequestCustomizeExecutor(requestHttp, respType, imgUrl);
case JODD_HTTP:
return new JoddHttpMinishopMediaUploadRequestCustomizeExecutor(requestHttp, respType);
return new JoddHttpMinishopMediaUploadRequestCustomizeExecutor(requestHttp, respType, imgUrl);
case OK_HTTP:
return new OkHttpMinishopMediaUploadRequestCustomizeExecutor(requestHttp, respType);
return new OkHttpMinishopMediaUploadRequestCustomizeExecutor(requestHttp, respType, imgUrl);
default:
return null;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@
*/
@Slf4j
public class ApacheMinishopMediaUploadRequestCustomizeExecutor extends MinishopUploadRequestCustomizeExecutor<CloseableHttpClient, HttpHost> {
public ApacheMinishopMediaUploadRequestCustomizeExecutor(RequestHttp requestHttp, String respType) {
super(requestHttp, respType);
public ApacheMinishopMediaUploadRequestCustomizeExecutor(RequestHttp requestHttp, String respType, String imgUrl) {
super(requestHttp, respType, imgUrl);
}

@Override
Expand All @@ -35,15 +35,29 @@ public WxMinishopImageUploadCustomizeResult execute(String uri, File file, WxTyp
RequestConfig config = RequestConfig.custom().setProxy(requestHttp.getRequestHttpProxy()).build();
httpPost.setConfig(config);
}
if (file != null) {
if (this.uploadType.equals("0")) {
if (file == null) {
throw new WxErrorException("上传文件为空");
}
HttpEntity entity = MultipartEntityBuilder
.create()
.addBinaryBody("media", file)
.addTextBody("resp_type", this.respType)
.addTextBody("upload_type", this.uploadType)
.setMode(HttpMultipartMode.RFC6532)
.build();
httpPost.setEntity(entity);
}
else {
HttpEntity entity = MultipartEntityBuilder
.create()
.addTextBody("resp_type", this.respType)
.addTextBody("upload_type", this.uploadType)
.addTextBody("img_url", this.imgUrl)
.setMode(HttpMultipartMode.RFC6532)
.build();
httpPost.setEntity(entity);
}
try (CloseableHttpResponse response = requestHttp.getRequestHttpClient().execute(httpPost)) {
String responseContent = Utf8ResponseHandler.INSTANCE.handleResponse(response);
WxError error = WxError.fromJson(responseContent, wxType);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@
*/
@Slf4j
public class JoddHttpMinishopMediaUploadRequestCustomizeExecutor extends MinishopUploadRequestCustomizeExecutor<HttpConnectionProvider, ProxyInfo> {
public JoddHttpMinishopMediaUploadRequestCustomizeExecutor(RequestHttp requestHttp, String respType) {
super(requestHttp, respType);
public JoddHttpMinishopMediaUploadRequestCustomizeExecutor(RequestHttp requestHttp, String respType, String imgUrl) {
super(requestHttp, respType, imgUrl);
}

@Override
Expand All @@ -33,7 +33,16 @@ public WxMinishopImageUploadCustomizeResult execute(String uri, File file, WxTyp
requestHttp.getRequestHttpClient().useProxy(requestHttp.getRequestHttpProxy());
}
request.withConnectionProvider(requestHttp.getRequestHttpClient());
request.form("media", file);
if (this.uploadType.equals("0")) {
request.form("resp_type", this.respType,
"upload_type", this.uploadType,
"media", file);
}
else {
request.form("resp_type", this.respType,
"upload_type", this.uploadType,
"img_url", this.imgUrl);
}
HttpResponse response = request.send();
response.charset(StandardCharsets.UTF_8.name());

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,19 +18,30 @@
*/
@Slf4j
public class OkHttpMinishopMediaUploadRequestCustomizeExecutor extends MinishopUploadRequestCustomizeExecutor<OkHttpClient, OkHttpProxyInfo> {
public OkHttpMinishopMediaUploadRequestCustomizeExecutor(RequestHttp requestHttp, String respType) {
super(requestHttp, respType);
public OkHttpMinishopMediaUploadRequestCustomizeExecutor(RequestHttp requestHttp, String respType, String imgUrl) {
super(requestHttp, respType, imgUrl);
}

@Override
public WxMinishopImageUploadCustomizeResult execute(String uri, File file, WxType wxType) throws WxErrorException, IOException {

RequestBody body = new MultipartBody.Builder()
.setType(MediaType.parse("multipart/form-data"))
.addFormDataPart("media",
file.getName(),
RequestBody.create(MediaType.parse("application/octet-stream"), file))
.build();
RequestBody body = null;
if (this.uploadType.equals("0")) {
body = new MultipartBody.Builder()
.setType(MediaType.parse("multipart/form-data"))
.addFormDataPart("resp_type", this.respType)
.addFormDataPart("upload_type", this.uploadType)
.addFormDataPart("media", file.getName(), RequestBody.create(MediaType.parse("application/octet-stream"), file))
.build();
}
else {
body = new MultipartBody.Builder()
.setType(MediaType.parse("multipart/form-data"))
.addFormDataPart("resp_type", this.respType)
.addFormDataPart("upload_type", this.uploadType)
.addFormDataPart("img_url", this.imgUrl)
.build();
}
Request request = new Request.Builder().url(uri).post(body).build();

Response response = requestHttp.getRequestHttpClient().newCall(request).execute();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,4 +29,14 @@ public interface WxMaShopImgService {
* @throws WxErrorException
*/
WxMinishopImageUploadCustomizeResult uploadImg(File file, String respType) throws WxErrorException;

/**
* 上传图片链接,带respType参数
*
* @param imgUrl
* @param respType
* @return WxMinishopImageUploadCustomizeResult
* @throws WxErrorException
*/
WxMinishopImageUploadCustomizeResult uploadImg(String imgUrl, String respType) throws WxErrorException;
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,21 @@ public class WxMaShopImgServiceImpl implements WxMaShopImgService {
@Override
public WxMinishopImageUploadCustomizeResult uploadImg(File file) throws WxErrorException {
WxMinishopImageUploadCustomizeResult result = this.service.execute(
MinishopUploadRequestCustomizeExecutor.create(this.service.getRequestHttp(), "0"), IMG_UPLOAD, file);
MinishopUploadRequestCustomizeExecutor.create(this.service.getRequestHttp(), "0", null), IMG_UPLOAD, file);
return result;
}

@Override
public WxMinishopImageUploadCustomizeResult uploadImg(File file, String respType) throws WxErrorException {
WxMinishopImageUploadCustomizeResult result = this.service.execute(
MinishopUploadRequestCustomizeExecutor.create(this.service.getRequestHttp(), respType), IMG_UPLOAD, file);
MinishopUploadRequestCustomizeExecutor.create(this.service.getRequestHttp(), respType, null), IMG_UPLOAD, file);
return result;
}

@Override
public WxMinishopImageUploadCustomizeResult uploadImg(String imgUrl, String respType) throws WxErrorException {
WxMinishopImageUploadCustomizeResult result = this.service.execute(
MinishopUploadRequestCustomizeExecutor.create(this.service.getRequestHttp(), respType, imgUrl), IMG_UPLOAD, null);
return result;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -35,4 +35,11 @@ public void testUploadImg2() throws WxErrorException {
WxMinishopImageUploadCustomizeResult result = wxService.getShopImgService().uploadImg(file, "1");
assertThat(result).isNotNull();
}

@Test
public void testUploadImg3() throws WxErrorException {
String imgUrl = "https://www.example.com/demo.jpg";
WxMinishopImageUploadCustomizeResult result = wxService.getShopImgService().uploadImg(imgUrl, "1");
assertThat(result).isNotNull();
}
}