mirror of
				https://gitee.com/binary/weixin-java-tools.git
				synced 2025-10-30 18:19:04 +08:00 
			
		
		
		
	🎨 #2461 【小程序】自定义交易组件上传接口支持图片链接
This commit is contained in:
		| @ -13,10 +13,19 @@ import java.io.IOException; | ||||
| 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 | ||||
| @ -24,14 +33,14 @@ public abstract class MinishopUploadRequestCustomizeExecutor<H, P> implements Re | ||||
|     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; | ||||
|     } | ||||
|  | ||||
| @ -24,8 +24,8 @@ import java.io.IOException; | ||||
|  */ | ||||
| @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 | ||||
| @ -35,15 +35,29 @@ public class ApacheMinishopMediaUploadRequestCustomizeExecutor extends MinishopU | ||||
|       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); | ||||
|  | ||||
| @ -22,8 +22,8 @@ import java.nio.charset.StandardCharsets; | ||||
|  */ | ||||
| @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 | ||||
| @ -33,7 +33,16 @@ public class JoddHttpMinishopMediaUploadRequestCustomizeExecutor extends Minisho | ||||
|       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()); | ||||
|  | ||||
|  | ||||
| @ -18,19 +18,30 @@ import java.io.IOException; | ||||
|  */ | ||||
| @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(); | ||||
|  | ||||
| @ -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; | ||||
| } | ||||
|  | ||||
| @ -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; | ||||
|   } | ||||
| } | ||||
|  | ||||
| @ -35,4 +35,11 @@ public class WxMaShopImgServiceImplTest { | ||||
|     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(); | ||||
|   } | ||||
| } | ||||
|  | ||||
		Reference in New Issue
	
	Block a user
	 linlinjava
					linlinjava