Skip to content

Commit c66ecbf

Browse files
authored
🎨 #2461 【小程序】自定义交易组件上传接口支持图片链接
1 parent 31984c1 commit c66ecbf

File tree

7 files changed

+88
-21
lines changed

7 files changed

+88
-21
lines changed

Diff for: weixin-java-common/src/main/java/me/chanjar/weixin/common/util/http/MinishopUploadRequestCustomizeExecutor.java

+14-5
Original file line numberDiff line numberDiff line change
@@ -13,25 +13,34 @@
1313
public abstract class MinishopUploadRequestCustomizeExecutor<H, P> implements RequestExecutor<WxMinishopImageUploadCustomizeResult, File> {
1414
protected RequestHttp<H, P> requestHttp;
1515
protected String respType;
16+
protected String uploadType;
17+
protected String imgUrl;
1618

17-
public MinishopUploadRequestCustomizeExecutor(RequestHttp requestHttp, String respType) {
19+
public MinishopUploadRequestCustomizeExecutor(RequestHttp requestHttp, String respType, String imgUrl) {
1820
this.requestHttp = requestHttp;
1921
this.respType = respType;
22+
if (imgUrl == null || imgUrl.isEmpty()) {
23+
this.uploadType = "0";
24+
}
25+
else {
26+
this.uploadType = "1";
27+
this.imgUrl = imgUrl;
28+
}
2029
}
2130

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

27-
public static RequestExecutor<WxMinishopImageUploadCustomizeResult, File> create(RequestHttp requestHttp, String respType) {
36+
public static RequestExecutor<WxMinishopImageUploadCustomizeResult, File> create(RequestHttp requestHttp, String respType, String imgUrl) {
2837
switch (requestHttp.getRequestType()) {
2938
case APACHE_HTTP:
30-
return new ApacheMinishopMediaUploadRequestCustomizeExecutor(requestHttp, respType);
39+
return new ApacheMinishopMediaUploadRequestCustomizeExecutor(requestHttp, respType, imgUrl);
3140
case JODD_HTTP:
32-
return new JoddHttpMinishopMediaUploadRequestCustomizeExecutor(requestHttp, respType);
41+
return new JoddHttpMinishopMediaUploadRequestCustomizeExecutor(requestHttp, respType, imgUrl);
3342
case OK_HTTP:
34-
return new OkHttpMinishopMediaUploadRequestCustomizeExecutor(requestHttp, respType);
43+
return new OkHttpMinishopMediaUploadRequestCustomizeExecutor(requestHttp, respType, imgUrl);
3544
default:
3645
return null;
3746
}

Diff for: weixin-java-common/src/main/java/me/chanjar/weixin/common/util/http/apache/ApacheMinishopMediaUploadRequestCustomizeExecutor.java

+17-3
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,8 @@
2424
*/
2525
@Slf4j
2626
public class ApacheMinishopMediaUploadRequestCustomizeExecutor extends MinishopUploadRequestCustomizeExecutor<CloseableHttpClient, HttpHost> {
27-
public ApacheMinishopMediaUploadRequestCustomizeExecutor(RequestHttp requestHttp, String respType) {
28-
super(requestHttp, respType);
27+
public ApacheMinishopMediaUploadRequestCustomizeExecutor(RequestHttp requestHttp, String respType, String imgUrl) {
28+
super(requestHttp, respType, imgUrl);
2929
}
3030

3131
@Override
@@ -35,15 +35,29 @@ public WxMinishopImageUploadCustomizeResult execute(String uri, File file, WxTyp
3535
RequestConfig config = RequestConfig.custom().setProxy(requestHttp.getRequestHttpProxy()).build();
3636
httpPost.setConfig(config);
3737
}
38-
if (file != null) {
38+
if (this.uploadType.equals("0")) {
39+
if (file == null) {
40+
throw new WxErrorException("上传文件为空");
41+
}
3942
HttpEntity entity = MultipartEntityBuilder
4043
.create()
4144
.addBinaryBody("media", file)
4245
.addTextBody("resp_type", this.respType)
46+
.addTextBody("upload_type", this.uploadType)
4347
.setMode(HttpMultipartMode.RFC6532)
4448
.build();
4549
httpPost.setEntity(entity);
4650
}
51+
else {
52+
HttpEntity entity = MultipartEntityBuilder
53+
.create()
54+
.addTextBody("resp_type", this.respType)
55+
.addTextBody("upload_type", this.uploadType)
56+
.addTextBody("img_url", this.imgUrl)
57+
.setMode(HttpMultipartMode.RFC6532)
58+
.build();
59+
httpPost.setEntity(entity);
60+
}
4761
try (CloseableHttpResponse response = requestHttp.getRequestHttpClient().execute(httpPost)) {
4862
String responseContent = Utf8ResponseHandler.INSTANCE.handleResponse(response);
4963
WxError error = WxError.fromJson(responseContent, wxType);

Diff for: weixin-java-common/src/main/java/me/chanjar/weixin/common/util/http/jodd/JoddHttpMinishopMediaUploadRequestCustomizeExecutor.java

+12-3
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,8 @@
2222
*/
2323
@Slf4j
2424
public class JoddHttpMinishopMediaUploadRequestCustomizeExecutor extends MinishopUploadRequestCustomizeExecutor<HttpConnectionProvider, ProxyInfo> {
25-
public JoddHttpMinishopMediaUploadRequestCustomizeExecutor(RequestHttp requestHttp, String respType) {
26-
super(requestHttp, respType);
25+
public JoddHttpMinishopMediaUploadRequestCustomizeExecutor(RequestHttp requestHttp, String respType, String imgUrl) {
26+
super(requestHttp, respType, imgUrl);
2727
}
2828

2929
@Override
@@ -33,7 +33,16 @@ public WxMinishopImageUploadCustomizeResult execute(String uri, File file, WxTyp
3333
requestHttp.getRequestHttpClient().useProxy(requestHttp.getRequestHttpProxy());
3434
}
3535
request.withConnectionProvider(requestHttp.getRequestHttpClient());
36-
request.form("media", file);
36+
if (this.uploadType.equals("0")) {
37+
request.form("resp_type", this.respType,
38+
"upload_type", this.uploadType,
39+
"media", file);
40+
}
41+
else {
42+
request.form("resp_type", this.respType,
43+
"upload_type", this.uploadType,
44+
"img_url", this.imgUrl);
45+
}
3746
HttpResponse response = request.send();
3847
response.charset(StandardCharsets.UTF_8.name());
3948

Diff for: weixin-java-common/src/main/java/me/chanjar/weixin/common/util/http/okhttp/OkHttpMinishopMediaUploadRequestCustomizeExecutor.java

+19-8
Original file line numberDiff line numberDiff line change
@@ -18,19 +18,30 @@
1818
*/
1919
@Slf4j
2020
public class OkHttpMinishopMediaUploadRequestCustomizeExecutor extends MinishopUploadRequestCustomizeExecutor<OkHttpClient, OkHttpProxyInfo> {
21-
public OkHttpMinishopMediaUploadRequestCustomizeExecutor(RequestHttp requestHttp, String respType) {
22-
super(requestHttp, respType);
21+
public OkHttpMinishopMediaUploadRequestCustomizeExecutor(RequestHttp requestHttp, String respType, String imgUrl) {
22+
super(requestHttp, respType, imgUrl);
2323
}
2424

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

28-
RequestBody body = new MultipartBody.Builder()
29-
.setType(MediaType.parse("multipart/form-data"))
30-
.addFormDataPart("media",
31-
file.getName(),
32-
RequestBody.create(MediaType.parse("application/octet-stream"), file))
33-
.build();
28+
RequestBody body = null;
29+
if (this.uploadType.equals("0")) {
30+
body = new MultipartBody.Builder()
31+
.setType(MediaType.parse("multipart/form-data"))
32+
.addFormDataPart("resp_type", this.respType)
33+
.addFormDataPart("upload_type", this.uploadType)
34+
.addFormDataPart("media", file.getName(), RequestBody.create(MediaType.parse("application/octet-stream"), file))
35+
.build();
36+
}
37+
else {
38+
body = new MultipartBody.Builder()
39+
.setType(MediaType.parse("multipart/form-data"))
40+
.addFormDataPart("resp_type", this.respType)
41+
.addFormDataPart("upload_type", this.uploadType)
42+
.addFormDataPart("img_url", this.imgUrl)
43+
.build();
44+
}
3445
Request request = new Request.Builder().url(uri).post(body).build();
3546

3647
Response response = requestHttp.getRequestHttpClient().newCall(request).execute();

Diff for: weixin-java-miniapp/src/main/java/cn/binarywang/wx/miniapp/api/WxMaShopImgService.java

+10
Original file line numberDiff line numberDiff line change
@@ -29,4 +29,14 @@ public interface WxMaShopImgService {
2929
* @throws WxErrorException
3030
*/
3131
WxMinishopImageUploadCustomizeResult uploadImg(File file, String respType) throws WxErrorException;
32+
33+
/**
34+
* 上传图片链接,带respType参数
35+
*
36+
* @param imgUrl
37+
* @param respType
38+
* @return WxMinishopImageUploadCustomizeResult
39+
* @throws WxErrorException
40+
*/
41+
WxMinishopImageUploadCustomizeResult uploadImg(String imgUrl, String respType) throws WxErrorException;
3242
}

Diff for: weixin-java-miniapp/src/main/java/cn/binarywang/wx/miniapp/api/impl/WxMaShopImgServiceImpl.java

+9-2
Original file line numberDiff line numberDiff line change
@@ -23,14 +23,21 @@ public class WxMaShopImgServiceImpl implements WxMaShopImgService {
2323
@Override
2424
public WxMinishopImageUploadCustomizeResult uploadImg(File file) throws WxErrorException {
2525
WxMinishopImageUploadCustomizeResult result = this.service.execute(
26-
MinishopUploadRequestCustomizeExecutor.create(this.service.getRequestHttp(), "0"), IMG_UPLOAD, file);
26+
MinishopUploadRequestCustomizeExecutor.create(this.service.getRequestHttp(), "0", null), IMG_UPLOAD, file);
2727
return result;
2828
}
2929

3030
@Override
3131
public WxMinishopImageUploadCustomizeResult uploadImg(File file, String respType) throws WxErrorException {
3232
WxMinishopImageUploadCustomizeResult result = this.service.execute(
33-
MinishopUploadRequestCustomizeExecutor.create(this.service.getRequestHttp(), respType), IMG_UPLOAD, file);
33+
MinishopUploadRequestCustomizeExecutor.create(this.service.getRequestHttp(), respType, null), IMG_UPLOAD, file);
34+
return result;
35+
}
36+
37+
@Override
38+
public WxMinishopImageUploadCustomizeResult uploadImg(String imgUrl, String respType) throws WxErrorException {
39+
WxMinishopImageUploadCustomizeResult result = this.service.execute(
40+
MinishopUploadRequestCustomizeExecutor.create(this.service.getRequestHttp(), respType, imgUrl), IMG_UPLOAD, null);
3441
return result;
3542
}
3643
}

Diff for: weixin-java-miniapp/src/test/java/cn/binarywang/wx/miniapp/api/impl/WxMaShopImgServiceImplTest.java

+7
Original file line numberDiff line numberDiff line change
@@ -35,4 +35,11 @@ public void testUploadImg2() throws WxErrorException {
3535
WxMinishopImageUploadCustomizeResult result = wxService.getShopImgService().uploadImg(file, "1");
3636
assertThat(result).isNotNull();
3737
}
38+
39+
@Test
40+
public void testUploadImg3() throws WxErrorException {
41+
String imgUrl = "https://www.example.com/demo.jpg";
42+
WxMinishopImageUploadCustomizeResult result = wxService.getShopImgService().uploadImg(imgUrl, "1");
43+
assertThat(result).isNotNull();
44+
}
3845
}

0 commit comments

Comments
 (0)