-
-
Notifications
You must be signed in to change notification settings - Fork 8.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
242 additions
and
0 deletions.
There are no files selected for viewing
32 changes: 32 additions & 0 deletions
32
...ava-miniapp/src/main/java/cn/binarywang/wx/miniapp/bean/WxMaUploadAuthMaterialResult.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
package cn.binarywang.wx.miniapp.bean; | ||
|
||
import com.google.gson.annotations.SerializedName; | ||
import lombok.Data; | ||
import me.chanjar.weixin.common.util.json.WxGsonBuilder; | ||
|
||
import java.io.Serializable; | ||
|
||
/** | ||
* 小程序认证上传补充材料 | ||
* | ||
* @author penhuozhu | ||
* @since 2024/01/07 | ||
*/ | ||
@Data | ||
public class WxMaUploadAuthMaterialResult implements Serializable { | ||
private static final long serialVersionUID = 1L; | ||
|
||
private String type; | ||
|
||
@SerializedName("mediaid") | ||
private String mediaId; | ||
|
||
public static WxMaUploadAuthMaterialResult fromJson(String json) { | ||
return WxGsonBuilder.create().fromJson(json, WxMaUploadAuthMaterialResult.class); | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
return WxGsonBuilder.create().toJson(this); | ||
} | ||
} |
57 changes: 57 additions & 0 deletions
57
.../main/java/cn/binarywang/wx/miniapp/executor/ApacheUploadAuthMaterialRequestExecutor.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
package cn.binarywang.wx.miniapp.executor; | ||
|
||
import cn.binarywang.wx.miniapp.bean.WxMaUploadAuthMaterialResult; | ||
import me.chanjar.weixin.common.enums.WxType; | ||
import me.chanjar.weixin.common.error.WxError; | ||
import me.chanjar.weixin.common.error.WxErrorException; | ||
import me.chanjar.weixin.common.util.http.RequestHttp; | ||
import me.chanjar.weixin.common.util.http.apache.Utf8ResponseHandler; | ||
import org.apache.http.HttpEntity; | ||
import org.apache.http.HttpHost; | ||
import org.apache.http.client.config.RequestConfig; | ||
import org.apache.http.client.methods.CloseableHttpResponse; | ||
import org.apache.http.client.methods.HttpPost; | ||
import org.apache.http.entity.mime.HttpMultipartMode; | ||
import org.apache.http.entity.mime.MultipartEntityBuilder; | ||
import org.apache.http.impl.client.CloseableHttpClient; | ||
|
||
import java.io.File; | ||
import java.io.IOException; | ||
|
||
/** | ||
* @author penhuozhu | ||
* @since 2024/01/07 | ||
*/ | ||
public class ApacheUploadAuthMaterialRequestExecutor extends UploadAuthMaterialRequestExecutor<CloseableHttpClient, HttpHost> { | ||
|
||
public ApacheUploadAuthMaterialRequestExecutor(RequestHttp requestHttp) { | ||
super(requestHttp); | ||
} | ||
|
||
@Override | ||
public WxMaUploadAuthMaterialResult execute(String uri, File file, WxType wxType) throws WxErrorException, IOException { | ||
HttpPost httpPost = new HttpPost(uri); | ||
if (requestHttp.getRequestHttpProxy() != null) { | ||
RequestConfig config = RequestConfig.custom().setProxy(requestHttp.getRequestHttpProxy()).build(); | ||
httpPost.setConfig(config); | ||
} | ||
if (file != null) { | ||
HttpEntity entity = MultipartEntityBuilder | ||
.create() | ||
.addBinaryBody("media", file) | ||
.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); | ||
if (error.getErrorCode() != 0) { | ||
throw new WxErrorException(error); | ||
} | ||
return WxMaUploadAuthMaterialResult.fromJson(responseContent); | ||
} finally { | ||
httpPost.releaseConnection(); | ||
} | ||
} | ||
} |
45 changes: 45 additions & 0 deletions
45
...ain/java/cn/binarywang/wx/miniapp/executor/JoddHttpUploadAuthMaterialRequestExecutor.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
package cn.binarywang.wx.miniapp.executor; | ||
|
||
import cn.binarywang.wx.miniapp.bean.WxMaUploadAuthMaterialResult; | ||
import jodd.http.HttpConnectionProvider; | ||
import jodd.http.HttpRequest; | ||
import jodd.http.HttpResponse; | ||
import jodd.http.ProxyInfo; | ||
import me.chanjar.weixin.common.enums.WxType; | ||
import me.chanjar.weixin.common.error.WxError; | ||
import me.chanjar.weixin.common.error.WxErrorException; | ||
import me.chanjar.weixin.common.util.http.RequestHttp; | ||
|
||
import java.io.File; | ||
import java.io.IOException; | ||
import java.nio.charset.StandardCharsets; | ||
|
||
/** | ||
* @author penhuozhu | ||
* @since 2024/01/07 | ||
*/ | ||
public class JoddHttpUploadAuthMaterialRequestExecutor extends UploadAuthMaterialRequestExecutor<HttpConnectionProvider, ProxyInfo> { | ||
|
||
public JoddHttpUploadAuthMaterialRequestExecutor(RequestHttp requestHttp) { | ||
super(requestHttp); | ||
} | ||
|
||
@Override | ||
public WxMaUploadAuthMaterialResult execute(String uri, File file, WxType wxType) throws WxErrorException, IOException { | ||
HttpRequest request = HttpRequest.post(uri); | ||
if (requestHttp.getRequestHttpProxy() != null) { | ||
requestHttp.getRequestHttpClient().useProxy(requestHttp.getRequestHttpProxy()); | ||
} | ||
request.withConnectionProvider(requestHttp.getRequestHttpClient()); | ||
request.form("media", file); | ||
HttpResponse response = request.send(); | ||
response.charset(StandardCharsets.UTF_8.name()); | ||
|
||
String responseContent = response.bodyText(); | ||
WxError error = WxError.fromJson(responseContent, wxType); | ||
if (error.getErrorCode() != 0) { | ||
throw new WxErrorException(error); | ||
} | ||
return WxMaUploadAuthMaterialResult.fromJson(responseContent); | ||
} | ||
} |
43 changes: 43 additions & 0 deletions
43
.../main/java/cn/binarywang/wx/miniapp/executor/OkHttpUploadAuthMaterialRequestExecutor.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
package cn.binarywang.wx.miniapp.executor; | ||
|
||
import cn.binarywang.wx.miniapp.bean.WxMaUploadAuthMaterialResult; | ||
import me.chanjar.weixin.common.enums.WxType; | ||
import me.chanjar.weixin.common.error.WxError; | ||
import me.chanjar.weixin.common.error.WxErrorException; | ||
import me.chanjar.weixin.common.util.http.RequestHttp; | ||
import me.chanjar.weixin.common.util.http.okhttp.OkHttpProxyInfo; | ||
import okhttp3.*; | ||
|
||
import java.io.File; | ||
import java.io.IOException; | ||
|
||
/** | ||
* @author penhuozhu | ||
* @since 2024/01/07 | ||
*/ | ||
public class OkHttpUploadAuthMaterialRequestExecutor extends UploadAuthMaterialRequestExecutor<OkHttpClient, OkHttpProxyInfo> { | ||
|
||
public OkHttpUploadAuthMaterialRequestExecutor(RequestHttp requestHttp) { | ||
super(requestHttp); | ||
} | ||
|
||
@Override | ||
public WxMaUploadAuthMaterialResult 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(); | ||
Request request = new Request.Builder().url(uri).post(body).build(); | ||
|
||
Response response = requestHttp.getRequestHttpClient().newCall(request).execute(); | ||
String responseContent = response.body().string(); | ||
WxError error = WxError.fromJson(responseContent, wxType); | ||
if (error.getErrorCode() != 0) { | ||
throw new WxErrorException(error); | ||
} | ||
return WxMaUploadAuthMaterialResult.fromJson(responseContent); | ||
} | ||
} |
45 changes: 45 additions & 0 deletions
45
...pp/src/main/java/cn/binarywang/wx/miniapp/executor/UploadAuthMaterialRequestExecutor.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
package cn.binarywang.wx.miniapp.executor; | ||
|
||
import cn.binarywang.wx.miniapp.bean.WxMaUploadAuthMaterialResult; | ||
import me.chanjar.weixin.common.enums.WxType; | ||
import me.chanjar.weixin.common.error.WxErrorException; | ||
import me.chanjar.weixin.common.util.http.RequestExecutor; | ||
import me.chanjar.weixin.common.util.http.RequestHttp; | ||
import me.chanjar.weixin.common.util.http.ResponseHandler; | ||
|
||
import java.io.File; | ||
import java.io.IOException; | ||
|
||
/** | ||
* 小程序认证上传补充材料 | ||
* 上传媒体文件请求执行器. | ||
* 请求的参数是File, 返回的结果是String | ||
* | ||
* @author penhuozhu | ||
* @since 2024/01/07 | ||
*/ | ||
public abstract class UploadAuthMaterialRequestExecutor<H, P> implements RequestExecutor<WxMaUploadAuthMaterialResult, File> { | ||
protected RequestHttp<H, P> requestHttp; | ||
|
||
public UploadAuthMaterialRequestExecutor(RequestHttp requestHttp) { | ||
this.requestHttp = requestHttp; | ||
} | ||
|
||
@Override | ||
public void execute(String uri, File data, ResponseHandler<WxMaUploadAuthMaterialResult> handler, WxType wxType) throws WxErrorException, IOException { | ||
handler.handle(this.execute(uri, data, wxType)); | ||
} | ||
|
||
public static RequestExecutor<WxMaUploadAuthMaterialResult, File> create(RequestHttp requestHttp) { | ||
switch (requestHttp.getRequestType()) { | ||
case APACHE_HTTP: | ||
return new ApacheUploadAuthMaterialRequestExecutor(requestHttp); | ||
case JODD_HTTP: | ||
return new JoddHttpUploadAuthMaterialRequestExecutor(requestHttp); | ||
case OK_HTTP: | ||
return new OkHttpUploadAuthMaterialRequestExecutor(requestHttp); | ||
default: | ||
return null; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters