Skip to content

Commit f7f2121

Browse files
authored
🐛 #2301 【小程序】修复生成小程序码的okhttp与Jodd实现类当微信后端报错时不会抛异常的问题
1 parent 8f49939 commit f7f2121

File tree

4 files changed

+67
-9
lines changed

4 files changed

+67
-9
lines changed

weixin-java-miniapp/src/main/java/cn/binarywang/wx/miniapp/executor/JoddHttpQrcodeFileRequestExecutor.java

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -57,12 +57,15 @@ public File execute(String uri, AbstractWxMaQrcodeWrapper qrcodeWrapper, WxType
5757
HttpResponse response = request.send();
5858
response.charset(StandardCharsets.UTF_8.name());
5959
String contentTypeHeader = response.header("Content-Type");
60-
if (MimeTypes.MIME_TEXT_PLAIN.equals(contentTypeHeader)) {
60+
if (MimeTypes.MIME_APPLICATION_JSON.equals(contentTypeHeader)) {
6161
String responseContent = response.bodyText();
62-
throw new WxErrorException(WxError.fromJson(responseContent, WxType.MiniApp));
62+
throw new WxErrorException(WxError.fromJson(responseContent, wxType));
6363
}
6464
try (InputStream inputStream = new ByteArrayInputStream(response.bodyBytes())) {
65-
return FileUtils.createTmpFile(inputStream, UUID.randomUUID().toString(), "jpg");
65+
if (StringUtils.isBlank(filePath)) {
66+
return FileUtils.createTmpFile(inputStream, UUID.randomUUID().toString(), "jpg");
67+
}
68+
return FileUtils.createTmpFile(inputStream, UUID.randomUUID().toString(), "jpg", Paths.get(filePath).toFile());
6669
}
6770
}
6871
}
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
package cn.binarywang.wx.miniapp.executor;
2+
3+
import java.io.IOException;
4+
import java.nio.charset.StandardCharsets;
5+
6+
import cn.binarywang.wx.miniapp.bean.AbstractWxMaQrcodeWrapper;
7+
import jodd.http.HttpConnectionProvider;
8+
import jodd.http.HttpRequest;
9+
import jodd.http.HttpResponse;
10+
import jodd.http.ProxyInfo;
11+
import jodd.net.MimeTypes;
12+
import me.chanjar.weixin.common.enums.WxType;
13+
import me.chanjar.weixin.common.error.WxError;
14+
import me.chanjar.weixin.common.error.WxErrorException;
15+
import me.chanjar.weixin.common.util.http.RequestHttp;
16+
17+
/**
18+
* @author vania
19+
* @since 2021/09/06
20+
*/
21+
public class JoddQrcodeBytesRequestExecutor extends QrcodeBytesRequestExecutor<HttpConnectionProvider, ProxyInfo> {
22+
23+
24+
public JoddQrcodeBytesRequestExecutor(RequestHttp<HttpConnectionProvider, ProxyInfo> requestHttp) {
25+
super(requestHttp);
26+
}
27+
28+
/**
29+
* 执行http请求.
30+
*
31+
* @param uri uri
32+
* @param qrcodeWrapper 数据
33+
* @param wxType 微信模块类型
34+
* @return 响应结果
35+
* @throws WxErrorException 自定义异常
36+
* @throws IOException io异常
37+
*/
38+
@Override
39+
public byte[] execute(String uri, AbstractWxMaQrcodeWrapper qrcodeWrapper, WxType wxType) throws WxErrorException, IOException {
40+
HttpRequest request = HttpRequest.get(uri);
41+
if (requestHttp.getRequestHttpProxy() != null) {
42+
requestHttp.getRequestHttpClient().useProxy(requestHttp.getRequestHttpProxy());
43+
}
44+
request.withConnectionProvider(requestHttp.getRequestHttpClient());
45+
46+
HttpResponse response = request.send();
47+
response.charset(StandardCharsets.UTF_8.name());
48+
String contentTypeHeader = response.header("Content-Type");
49+
if (MimeTypes.MIME_APPLICATION_JSON.equals(contentTypeHeader)) {
50+
String responseContent = response.bodyText();
51+
throw new WxErrorException(WxError.fromJson(responseContent, wxType));
52+
}
53+
return response.bodyBytes();
54+
}
55+
}

weixin-java-miniapp/src/main/java/cn/binarywang/wx/miniapp/executor/OkHttpQrcodeBytesRequestExecutor.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,13 +35,13 @@ public OkHttpQrcodeBytesRequestExecutor(RequestHttp<OkHttpClient, OkHttpProxyInf
3535
*/
3636
@Override
3737
public byte[] execute(String uri, AbstractWxMaQrcodeWrapper qrcodeWrapper, WxType wxType) throws WxErrorException, IOException {
38-
RequestBody body = RequestBody.Companion.create(qrcodeWrapper.toJson(), MediaType.parse("text/plain; charset=utf-8"));
38+
RequestBody body = RequestBody.Companion.create(qrcodeWrapper.toJson(), MediaType.parse("application/json; charset=utf-8"));
3939
Request request = new Request.Builder().url(uri).post(body).build();
4040
Response response = requestHttp.getRequestHttpClient().newCall(request).execute();
4141
String contentTypeHeader = response.header("Content-Type");
42-
if ("text/plain".equals(contentTypeHeader)) {
42+
if (null != contentTypeHeader && contentTypeHeader.startsWith("application/json")) {
4343
String responseContent = response.body().string();
44-
throw new WxErrorException(WxError.fromJson(responseContent, WxType.MP));
44+
throw new WxErrorException(WxError.fromJson(responseContent, wxType));
4545
}
4646

4747
try (InputStream inputStream = response.body().byteStream()) {

weixin-java-miniapp/src/main/java/cn/binarywang/wx/miniapp/executor/OkHttpQrcodeFileRequestExecutor.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -41,13 +41,13 @@ public OkHttpQrcodeFileRequestExecutor(RequestHttp<OkHttpClient, OkHttpProxyInfo
4141
*/
4242
@Override
4343
public File execute(String uri, AbstractWxMaQrcodeWrapper qrcodeWrapper, WxType wxType) throws WxErrorException, IOException {
44-
RequestBody body = RequestBody.Companion.create(qrcodeWrapper.toJson(), MediaType.parse("text/plain; charset=utf-8"));
44+
RequestBody body = RequestBody.Companion.create(qrcodeWrapper.toJson(), MediaType.parse("application/json; charset=utf-8"));
4545
Request request = new Request.Builder().url(uri).post(body).build();
4646
Response response = requestHttp.getRequestHttpClient().newCall(request).execute();
4747
String contentTypeHeader = response.header("Content-Type");
48-
if ("text/plain".equals(contentTypeHeader)) {
48+
if (null != contentTypeHeader && contentTypeHeader.startsWith("application/json")) {
4949
String responseContent = response.body().string();
50-
throw new WxErrorException(WxError.fromJson(responseContent, WxType.MP));
50+
throw new WxErrorException(WxError.fromJson(responseContent, wxType));
5151
}
5252

5353
try (InputStream inputStream = response.body().byteStream()) {

0 commit comments

Comments
 (0)