Skip to content

Commit e9c1cda

Browse files
authored
🆕 #2252 【小程序】增加自定义组件之图片上传接口
1 parent 24bde92 commit e9c1cda

File tree

12 files changed

+338
-0
lines changed

12 files changed

+338
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
package me.chanjar.weixin.common.bean.result;
2+
3+
import com.google.gson.JsonObject;
4+
import com.google.gson.JsonParser;
5+
import lombok.Data;
6+
import me.chanjar.weixin.common.util.json.WxGsonBuilder;
7+
8+
import java.io.Serializable;
9+
10+
@Data
11+
public class WxMinishopImageUploadCustomizeResult implements Serializable {
12+
private String errcode;
13+
private String errmsg;
14+
15+
private WxMinishopPicFileCustomizeResult imgInfo;
16+
17+
public static WxMinishopImageUploadCustomizeResult fromJson(String json) {
18+
JsonObject jsonObject = new JsonParser().parse(json).getAsJsonObject();
19+
WxMinishopImageUploadCustomizeResult result = new WxMinishopImageUploadCustomizeResult();
20+
result.setErrcode(jsonObject.get("errcode").getAsNumber().toString());
21+
if (result.getErrcode().equals("0")) {
22+
WxMinishopPicFileCustomizeResult picFileResult = new WxMinishopPicFileCustomizeResult();
23+
JsonObject picObject = jsonObject.get("img_info").getAsJsonObject();
24+
picFileResult.setMediaId(picObject.get("media_id").getAsString());
25+
if (picObject.has("temp_img_url")) {
26+
picFileResult.setTempImgUrl(picObject.get("temp_img_url").getAsString());
27+
}
28+
result.setImgInfo(picFileResult);
29+
30+
}
31+
return result;
32+
}
33+
34+
@Override
35+
public String toString() {
36+
return WxGsonBuilder.create().toJson(this);
37+
}
38+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package me.chanjar.weixin.common.bean.result;
2+
3+
import lombok.Data;
4+
5+
import java.io.Serializable;
6+
7+
@Data
8+
public class WxMinishopPicFileCustomizeResult implements Serializable {
9+
private String mediaId;
10+
private String tempImgUrl;
11+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package me.chanjar.weixin.common.util.http;
2+
3+
import me.chanjar.weixin.common.bean.result.WxMinishopImageUploadCustomizeResult;
4+
import me.chanjar.weixin.common.enums.WxType;
5+
import me.chanjar.weixin.common.error.WxErrorException;
6+
import me.chanjar.weixin.common.util.http.apache.ApacheMinishopMediaUploadRequestCustomizeExecutor;
7+
import me.chanjar.weixin.common.util.http.jodd.JoddHttpMinishopMediaUploadRequestCustomizeExecutor;
8+
import me.chanjar.weixin.common.util.http.okhttp.OkHttpMinishopMediaUploadRequestCustomizeExecutor;
9+
10+
import java.io.File;
11+
import java.io.IOException;
12+
13+
public abstract class MinishopUploadRequestCustomizeExecutor<H, P> implements RequestExecutor<WxMinishopImageUploadCustomizeResult, File> {
14+
protected RequestHttp<H, P> requestHttp;
15+
16+
public MinishopUploadRequestCustomizeExecutor(RequestHttp requestHttp) {
17+
this.requestHttp = requestHttp;
18+
}
19+
20+
@Override
21+
public void execute(String uri, File data, ResponseHandler<WxMinishopImageUploadCustomizeResult> handler, WxType wxType) throws WxErrorException, IOException {
22+
handler.handle(this.execute(uri, data, wxType));
23+
}
24+
25+
public static RequestExecutor<WxMinishopImageUploadCustomizeResult, File> create(RequestHttp requestHttp) {
26+
switch (requestHttp.getRequestType()) {
27+
case APACHE_HTTP:
28+
return new ApacheMinishopMediaUploadRequestCustomizeExecutor(requestHttp);
29+
case JODD_HTTP:
30+
return new JoddHttpMinishopMediaUploadRequestCustomizeExecutor(requestHttp);
31+
case OK_HTTP:
32+
return new OkHttpMinishopMediaUploadRequestCustomizeExecutor(requestHttp);
33+
default:
34+
return null;
35+
}
36+
}
37+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
package me.chanjar.weixin.common.util.http.apache;
2+
3+
import lombok.extern.slf4j.Slf4j;
4+
import me.chanjar.weixin.common.bean.result.WxMinishopImageUploadCustomizeResult;
5+
import me.chanjar.weixin.common.enums.WxType;
6+
import me.chanjar.weixin.common.error.WxError;
7+
import me.chanjar.weixin.common.error.WxErrorException;
8+
import me.chanjar.weixin.common.util.http.MinishopUploadRequestCustomizeExecutor;
9+
import me.chanjar.weixin.common.util.http.RequestHttp;
10+
import org.apache.http.HttpEntity;
11+
import org.apache.http.HttpHost;
12+
import org.apache.http.client.config.RequestConfig;
13+
import org.apache.http.client.methods.CloseableHttpResponse;
14+
import org.apache.http.client.methods.HttpPost;
15+
import org.apache.http.entity.mime.HttpMultipartMode;
16+
import org.apache.http.entity.mime.MultipartEntityBuilder;
17+
import org.apache.http.impl.client.CloseableHttpClient;
18+
19+
import java.io.File;
20+
import java.io.IOException;
21+
22+
/**
23+
* Created by liming1019 on 2021/8/10.
24+
*/
25+
@Slf4j
26+
public class ApacheMinishopMediaUploadRequestCustomizeExecutor extends MinishopUploadRequestCustomizeExecutor<CloseableHttpClient, HttpHost> {
27+
public ApacheMinishopMediaUploadRequestCustomizeExecutor(RequestHttp requestHttp) {
28+
super(requestHttp);
29+
}
30+
31+
@Override
32+
public WxMinishopImageUploadCustomizeResult execute(String uri, File file, WxType wxType) throws WxErrorException, IOException {
33+
HttpPost httpPost = new HttpPost(uri);
34+
if (requestHttp.getRequestHttpProxy() != null) {
35+
RequestConfig config = RequestConfig.custom().setProxy(requestHttp.getRequestHttpProxy()).build();
36+
httpPost.setConfig(config);
37+
}
38+
if (file != null) {
39+
HttpEntity entity = MultipartEntityBuilder
40+
.create()
41+
.addBinaryBody("media", file)
42+
.setMode(HttpMultipartMode.RFC6532)
43+
.build();
44+
httpPost.setEntity(entity);
45+
}
46+
try (CloseableHttpResponse response = requestHttp.getRequestHttpClient().execute(httpPost)) {
47+
String responseContent = Utf8ResponseHandler.INSTANCE.handleResponse(response);
48+
WxError error = WxError.fromJson(responseContent, wxType);
49+
if (error.getErrorCode() != 0) {
50+
throw new WxErrorException(error);
51+
}
52+
log.info("responseContent: " + responseContent);
53+
return WxMinishopImageUploadCustomizeResult.fromJson(responseContent);
54+
} finally {
55+
httpPost.releaseConnection();
56+
}
57+
}
58+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
package me.chanjar.weixin.common.util.http.jodd;
2+
3+
import jodd.http.HttpConnectionProvider;
4+
import jodd.http.HttpRequest;
5+
import jodd.http.HttpResponse;
6+
import jodd.http.ProxyInfo;
7+
import lombok.extern.slf4j.Slf4j;
8+
import me.chanjar.weixin.common.bean.result.WxMinishopImageUploadCustomizeResult;
9+
import me.chanjar.weixin.common.enums.WxType;
10+
import me.chanjar.weixin.common.error.WxError;
11+
import me.chanjar.weixin.common.error.WxErrorException;
12+
import me.chanjar.weixin.common.util.http.MinishopUploadRequestCustomizeExecutor;
13+
import me.chanjar.weixin.common.util.http.RequestHttp;
14+
15+
import java.io.File;
16+
import java.io.IOException;
17+
import java.nio.charset.StandardCharsets;
18+
19+
/**
20+
* @author liming1019
21+
* @date 2021/8/10
22+
*/
23+
@Slf4j
24+
public class JoddHttpMinishopMediaUploadRequestCustomizeExecutor extends MinishopUploadRequestCustomizeExecutor<HttpConnectionProvider, ProxyInfo> {
25+
public JoddHttpMinishopMediaUploadRequestCustomizeExecutor(RequestHttp requestHttp) {
26+
super(requestHttp);
27+
}
28+
29+
@Override
30+
public WxMinishopImageUploadCustomizeResult execute(String uri, File file, WxType wxType) throws WxErrorException, IOException {
31+
HttpRequest request = HttpRequest.post(uri);
32+
if (requestHttp.getRequestHttpProxy() != null) {
33+
requestHttp.getRequestHttpClient().useProxy(requestHttp.getRequestHttpProxy());
34+
}
35+
request.withConnectionProvider(requestHttp.getRequestHttpClient());
36+
request.form("media", file);
37+
HttpResponse response = request.send();
38+
response.charset(StandardCharsets.UTF_8.name());
39+
40+
String responseContent = response.bodyText();
41+
WxError error = WxError.fromJson(responseContent, wxType);
42+
if (error.getErrorCode() != 0) {
43+
throw new WxErrorException(error);
44+
}
45+
log.info("responseContent: " + responseContent);
46+
47+
return WxMinishopImageUploadCustomizeResult.fromJson(responseContent);
48+
}
49+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
package me.chanjar.weixin.common.util.http.okhttp;
2+
3+
import lombok.extern.slf4j.Slf4j;
4+
import me.chanjar.weixin.common.bean.result.WxMinishopImageUploadCustomizeResult;
5+
import me.chanjar.weixin.common.enums.WxType;
6+
import me.chanjar.weixin.common.error.WxError;
7+
import me.chanjar.weixin.common.error.WxErrorException;
8+
import me.chanjar.weixin.common.util.http.MinishopUploadRequestCustomizeExecutor;
9+
import me.chanjar.weixin.common.util.http.RequestHttp;
10+
import okhttp3.*;
11+
12+
import java.io.File;
13+
import java.io.IOException;
14+
15+
/**
16+
* @author liming1019
17+
* @date 2021/8/10
18+
*/
19+
@Slf4j
20+
public class OkHttpMinishopMediaUploadRequestCustomizeExecutor extends MinishopUploadRequestCustomizeExecutor<OkHttpClient, OkHttpProxyInfo> {
21+
public OkHttpMinishopMediaUploadRequestCustomizeExecutor(RequestHttp requestHttp) {
22+
super(requestHttp);
23+
}
24+
25+
@Override
26+
public WxMinishopImageUploadCustomizeResult execute(String uri, File file, WxType wxType) throws WxErrorException, IOException {
27+
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();
34+
Request request = new Request.Builder().url(uri).post(body).build();
35+
36+
Response response = requestHttp.getRequestHttpClient().newCall(request).execute();
37+
String responseContent = response.body().string();
38+
WxError error = WxError.fromJson(responseContent, wxType);
39+
if (error.getErrorCode() != 0) {
40+
throw new WxErrorException(error);
41+
}
42+
log.info("responseContent: " + responseContent);
43+
44+
return WxMinishopImageUploadCustomizeResult.fromJson(responseContent);
45+
}
46+
47+
}

weixin-java-miniapp/src/main/java/cn/binarywang/wx/miniapp/api/WxMaService.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -426,6 +426,13 @@ public interface WxMaService extends WxService {
426426
*/
427427
WxMaShopCatService getShopCatService();
428428

429+
/**
430+
* 小程序交易组件-接入商品前必需接口-上传图片
431+
*
432+
* @return
433+
*/
434+
WxMaShopImgService getShopImgService();
435+
429436
/**
430437
* 获取小程序 URL Link服务接口
431438
*
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package cn.binarywang.wx.miniapp.api;
2+
3+
import me.chanjar.weixin.common.bean.result.WxMinishopImageUploadCustomizeResult;
4+
import me.chanjar.weixin.common.error.WxErrorException;
5+
6+
import java.io.File;
7+
8+
/**
9+
* 小程序交易组件-接入商品前必需接口
10+
*
11+
* @author liming1019
12+
*/
13+
public interface WxMaShopImgService {
14+
/**
15+
* 上传图片
16+
*
17+
* @return WxMinishopImageUploadCustomizeResult
18+
* @throws WxErrorException
19+
*/
20+
WxMinishopImageUploadCustomizeResult uploadImg(File file) throws WxErrorException;
21+
}

weixin-java-miniapp/src/main/java/cn/binarywang/wx/miniapp/api/impl/BaseWxMaServiceImpl.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@ public abstract class BaseWxMaServiceImpl<H, P> implements WxMaService, RequestH
6868
private final WxMaShopRegisterService shopRegisterService = new WxMaShopRegisterServiceImpl(this);
6969
private final WxMaShopAccountService shopAccountService = new WxMaShopAccountServiceImpl(this);
7070
private final WxMaShopCatService shopCatService = new WxMaShopCatServiceImpl(this);
71+
private final WxMaShopImgService shopImgService = new WxMaShopImgServiceImpl(this);
7172
private final WxMaLinkService linkService = new WxMaLinkServiceImpl(this);
7273
private final WxMaReimburseInvoiceService reimburseInvoiceService = new WxMaReimburseInvoiceServiceImpl(this);
7374
private Map<String, WxMaConfig> configMap;
@@ -534,6 +535,11 @@ public WxMaShopCatService getShopCatService() {
534535
return this.shopCatService;
535536
}
536537

538+
@Override
539+
public WxMaShopImgService getShopImgService() {
540+
return this.shopImgService;
541+
}
542+
537543
@Override
538544
public WxMaLinkService getLinkService() {
539545
return this.linkService;
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package cn.binarywang.wx.miniapp.api.impl;
2+
3+
import cn.binarywang.wx.miniapp.api.WxMaService;
4+
import cn.binarywang.wx.miniapp.api.WxMaShopImgService;
5+
import lombok.RequiredArgsConstructor;
6+
import lombok.extern.slf4j.Slf4j;
7+
import me.chanjar.weixin.common.bean.result.WxMinishopImageUploadCustomizeResult;
8+
import me.chanjar.weixin.common.error.WxErrorException;
9+
import me.chanjar.weixin.common.util.http.MinishopUploadRequestCustomizeExecutor;
10+
11+
import java.io.File;
12+
13+
import static cn.binarywang.wx.miniapp.constant.WxMaApiUrlConstants.Shop.Img.IMG_UPLOAD;
14+
15+
/**
16+
* @author liming1019
17+
*/
18+
@RequiredArgsConstructor
19+
@Slf4j
20+
public class WxMaShopImgServiceImpl implements WxMaShopImgService {
21+
private final WxMaService service;
22+
23+
@Override
24+
public WxMinishopImageUploadCustomizeResult uploadImg(File file) throws WxErrorException {
25+
WxMinishopImageUploadCustomizeResult result = this.service.execute(
26+
MinishopUploadRequestCustomizeExecutor.create(this.service.getRequestHttp()), IMG_UPLOAD, file);
27+
return result;
28+
}
29+
}

weixin-java-miniapp/src/main/java/cn/binarywang/wx/miniapp/constant/WxMaApiUrlConstants.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -337,6 +337,10 @@ interface Account {
337337
interface Cat {
338338
String GET_CAT = "https://api.weixin.qq.com/shop/cat/get";
339339
}
340+
341+
interface Img {
342+
String IMG_UPLOAD = "https://api.weixin.qq.com/shop/img/upload";
343+
}
340344
}
341345

342346
/**
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package cn.binarywang.wx.miniapp.api.impl;
2+
3+
import cn.binarywang.wx.miniapp.api.WxMaService;
4+
import cn.binarywang.wx.miniapp.test.ApiTestModule;
5+
import com.google.inject.Inject;
6+
import me.chanjar.weixin.common.bean.result.WxMinishopImageUploadCustomizeResult;
7+
import me.chanjar.weixin.common.error.WxErrorException;
8+
import org.testng.annotations.Guice;
9+
import org.testng.annotations.Test;
10+
11+
import java.io.File;
12+
13+
import static org.assertj.core.api.Assertions.assertThat;
14+
15+
/**
16+
* @author liming1019
17+
*/
18+
@Test
19+
@Guice(modules = ApiTestModule.class)
20+
public class WxMaShopImgServiceImplTest {
21+
22+
@Inject
23+
private WxMaService wxService;
24+
25+
@Test
26+
public void testUploadImg() throws WxErrorException {
27+
File file = new File("/Users/liming/Desktop/test.jpeg");
28+
WxMinishopImageUploadCustomizeResult result = wxService.getShopImgService().uploadImg(file);
29+
assertThat(result).isNotNull();
30+
}
31+
}

0 commit comments

Comments
 (0)