Skip to content

Commit 604098b

Browse files
authored
🆕 binarywang#1942 【小程序】增加生成小程序码的okhttp实现
1 parent 02d3c16 commit 604098b

8 files changed

+303
-101
lines changed

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

+9-16
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
import cn.binarywang.wx.miniapp.bean.WxaCode;
88
import cn.binarywang.wx.miniapp.bean.WxaCodeUnlimit;
99
import cn.binarywang.wx.miniapp.executor.QrcodeBytesRequestExecutor;
10-
import cn.binarywang.wx.miniapp.executor.QrcodeFileRequestExecutor;
1110
import cn.binarywang.wx.miniapp.executor.QrcodeRequestExecutor;
1211
import lombok.AllArgsConstructor;
1312
import me.chanjar.weixin.common.error.WxErrorException;
@@ -23,14 +22,12 @@ public class WxMaQrcodeServiceImpl implements WxMaQrcodeService {
2322

2423
@Override
2524
public byte[] createQrcodeBytes(String path, int width) throws WxErrorException {
26-
final QrcodeBytesRequestExecutor executor = new QrcodeBytesRequestExecutor(this.wxMaService.getRequestHttp());
27-
return this.wxMaService.execute(executor, CREATE_QRCODE_URL, new WxMaQrcode(path, width));
25+
return this.wxMaService.execute(QrcodeBytesRequestExecutor.create(this.wxMaService.getRequestHttp()), CREATE_QRCODE_URL, new WxMaQrcode(path, width));
2826
}
2927

3028
@Override
3129
public File createQrcode(String path, int width) throws WxErrorException {
32-
final QrcodeRequestExecutor executor = new QrcodeRequestExecutor(this.wxMaService.getRequestHttp());
33-
return this.wxMaService.execute(executor, CREATE_QRCODE_URL, new WxMaQrcode(path, width));
30+
return this.wxMaService.execute(QrcodeRequestExecutor.create(this.wxMaService.getRequestHttp()), CREATE_QRCODE_URL, new WxMaQrcode(path, width));
3431
}
3532

3633
@Override
@@ -41,8 +38,7 @@ public File createQrcode(String path) throws WxErrorException {
4138
@Override
4239
public byte[] createWxaCodeBytes(String path, int width, boolean autoColor, WxMaCodeLineColor lineColor, boolean isHyaline)
4340
throws WxErrorException {
44-
final QrcodeBytesRequestExecutor executor = new QrcodeBytesRequestExecutor(this.wxMaService.getRequestHttp());
45-
return this.wxMaService.execute(executor, GET_WXACODE_URL, WxaCode.builder()
41+
return this.wxMaService.execute(QrcodeBytesRequestExecutor.create(this.wxMaService.getRequestHttp()), GET_WXACODE_URL, WxaCode.builder()
4642
.path(path)
4743
.width(width)
4844
.autoColor(autoColor)
@@ -54,8 +50,7 @@ public byte[] createWxaCodeBytes(String path, int width, boolean autoColor, WxMa
5450
@Override
5551
public File createWxaCode(String path, int width, boolean autoColor, WxMaCodeLineColor lineColor, boolean isHyaline)
5652
throws WxErrorException {
57-
final QrcodeRequestExecutor executor = new QrcodeRequestExecutor(this.wxMaService.getRequestHttp());
58-
return this.wxMaService.execute(executor, GET_WXACODE_URL, WxaCode.builder()
53+
return this.wxMaService.execute(QrcodeRequestExecutor.create(this.wxMaService.getRequestHttp()), GET_WXACODE_URL, WxaCode.builder()
5954
.path(path)
6055
.width(width)
6156
.autoColor(autoColor)
@@ -77,15 +72,15 @@ public File createWxaCode(String path) throws WxErrorException {
7772
@Override
7873
public byte[] createWxaCodeUnlimitBytes(String scene, String page, int width, boolean autoColor,
7974
WxMaCodeLineColor lineColor, boolean isHyaline) throws WxErrorException {
80-
return this.wxMaService.execute(new QrcodeBytesRequestExecutor(this.wxMaService.getRequestHttp()),
75+
return this.wxMaService.execute(QrcodeBytesRequestExecutor.create(this.wxMaService.getRequestHttp()),
8176
GET_WXACODE_UNLIMIT_URL,
8277
this.buildWxaCodeUnlimit(scene, page, width, autoColor, lineColor, isHyaline));
8378
}
8479

8580
@Override
8681
public File createWxaCodeUnlimit(String scene, String page, int width, boolean autoColor,
8782
WxMaCodeLineColor lineColor, boolean isHyaline) throws WxErrorException {
88-
return this.wxMaService.execute(new QrcodeRequestExecutor(this.wxMaService.getRequestHttp()),
83+
return this.wxMaService.execute(QrcodeRequestExecutor.create(this.wxMaService.getRequestHttp()),
8984
GET_WXACODE_UNLIMIT_URL,
9085
this.buildWxaCodeUnlimit(scene, page, width, autoColor, lineColor, isHyaline));
9186
}
@@ -110,8 +105,7 @@ public File createWxaCodeUnlimit(String scene, String page) throws WxErrorExcept
110105

111106
@Override
112107
public File createQrcode(String path, int width, String filePath) throws WxErrorException {
113-
final QrcodeFileRequestExecutor executor = new QrcodeFileRequestExecutor(this.wxMaService.getRequestHttp(), filePath);
114-
return this.wxMaService.execute(executor, CREATE_QRCODE_URL, new WxMaQrcode(path, width));
108+
return this.wxMaService.execute(QrcodeRequestExecutor.create(this.wxMaService.getRequestHttp(), filePath), CREATE_QRCODE_URL, new WxMaQrcode(path, width));
115109
}
116110

117111
@Override
@@ -122,8 +116,7 @@ public File createQrcode(String path, String filePath) throws WxErrorException {
122116
@Override
123117
public File createWxaCode(String path, int width, String filePath, boolean autoColor, WxMaCodeLineColor lineColor, boolean isHyaline)
124118
throws WxErrorException {
125-
final QrcodeFileRequestExecutor executor = new QrcodeFileRequestExecutor(this.wxMaService.getRequestHttp(), filePath);
126-
return this.wxMaService.execute(executor, GET_WXACODE_URL, WxaCode.builder()
119+
return this.wxMaService.execute(QrcodeRequestExecutor.create(this.wxMaService.getRequestHttp(), filePath), GET_WXACODE_URL, WxaCode.builder()
127120
.path(path)
128121
.width(width)
129122
.autoColor(autoColor)
@@ -145,7 +138,7 @@ public File createWxaCode(String path, String filePath) throws WxErrorException
145138
@Override
146139
public File createWxaCodeUnlimit(String scene, String page, String filePath, int width, boolean autoColor,
147140
WxMaCodeLineColor lineColor, boolean isHyaline) throws WxErrorException {
148-
return this.wxMaService.execute(new QrcodeFileRequestExecutor(this.wxMaService.getRequestHttp(), filePath),
141+
return this.wxMaService.execute(QrcodeRequestExecutor.create(this.wxMaService.getRequestHttp(), filePath),
149142
GET_WXACODE_UNLIMIT_URL,
150143
this.buildWxaCodeUnlimit(scene, page, width, autoColor, lineColor, isHyaline));
151144
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
package cn.binarywang.wx.miniapp.executor;
2+
3+
import cn.binarywang.wx.miniapp.bean.AbstractWxMaQrcodeWrapper;
4+
import me.chanjar.weixin.common.enums.WxType;
5+
import me.chanjar.weixin.common.error.WxError;
6+
import me.chanjar.weixin.common.error.WxErrorException;
7+
import me.chanjar.weixin.common.util.http.RequestHttp;
8+
import me.chanjar.weixin.common.util.http.apache.InputStreamResponseHandler;
9+
import me.chanjar.weixin.common.util.http.apache.Utf8ResponseHandler;
10+
import org.apache.commons.io.IOUtils;
11+
import org.apache.http.Header;
12+
import org.apache.http.HttpHost;
13+
import org.apache.http.client.config.RequestConfig;
14+
import org.apache.http.client.methods.CloseableHttpResponse;
15+
import org.apache.http.client.methods.HttpPost;
16+
import org.apache.http.entity.ContentType;
17+
import org.apache.http.entity.StringEntity;
18+
import org.apache.http.impl.client.CloseableHttpClient;
19+
20+
import java.io.IOException;
21+
import java.io.InputStream;
22+
23+
/**
24+
* @author wenqiang
25+
* @since 2020/12/25
26+
*/
27+
public class ApacheQrcodeBytesRequestExecutor extends QrcodeBytesRequestExecutor<CloseableHttpClient, HttpHost> {
28+
29+
30+
public ApacheQrcodeBytesRequestExecutor(RequestHttp<CloseableHttpClient, HttpHost> requestHttp) {
31+
super(requestHttp);
32+
}
33+
34+
/**
35+
* 执行http请求.
36+
*
37+
* @param uri uri
38+
* @param qrcodeWrapper 数据
39+
* @param wxType 微信模块类型
40+
* @return 响应结果
41+
* @throws WxErrorException 自定义异常
42+
* @throws IOException io异常
43+
*/
44+
@Override
45+
public byte[] execute(String uri, AbstractWxMaQrcodeWrapper qrcodeWrapper, WxType wxType) throws WxErrorException, IOException {
46+
HttpPost httpPost = new HttpPost(uri);
47+
if (requestHttp.getRequestHttpProxy() != null) {
48+
httpPost.setConfig(
49+
RequestConfig.custom().setProxy(requestHttp.getRequestHttpProxy()).build()
50+
);
51+
}
52+
53+
httpPost.setEntity(new StringEntity(qrcodeWrapper.toJson()));
54+
55+
try (final CloseableHttpResponse response = requestHttp.getRequestHttpClient().execute(httpPost);
56+
final InputStream inputStream = InputStreamResponseHandler.INSTANCE.handleResponse(response)) {
57+
Header[] contentTypeHeader = response.getHeaders("Content-Type");
58+
if (contentTypeHeader != null && contentTypeHeader.length > 0
59+
&& ContentType.APPLICATION_JSON.getMimeType()
60+
.equals(ContentType.parse(contentTypeHeader[0].getValue()).getMimeType())) {
61+
String responseContent = Utf8ResponseHandler.INSTANCE.handleResponse(response);
62+
throw new WxErrorException(WxError.fromJson(responseContent, wxType));
63+
}
64+
65+
return IOUtils.toByteArray(inputStream);
66+
} finally {
67+
httpPost.releaseConnection();
68+
}
69+
}
70+
}

weixin-java-miniapp/src/main/java/cn/binarywang/wx/miniapp/executor/QrcodeFileRequestExecutor.java renamed to weixin-java-miniapp/src/main/java/cn/binarywang/wx/miniapp/executor/ApacheQrcodeFileRequestExecutor.java

+7-7
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,13 @@
1010
import me.chanjar.weixin.common.util.http.apache.Utf8ResponseHandler;
1111
import org.apache.commons.lang3.StringUtils;
1212
import org.apache.http.Header;
13+
import org.apache.http.HttpHost;
1314
import org.apache.http.client.config.RequestConfig;
1415
import org.apache.http.client.methods.CloseableHttpResponse;
1516
import org.apache.http.client.methods.HttpPost;
1617
import org.apache.http.entity.ContentType;
1718
import org.apache.http.entity.StringEntity;
19+
import org.apache.http.impl.client.CloseableHttpClient;
1820

1921
import java.io.File;
2022
import java.io.IOException;
@@ -23,15 +25,14 @@
2325
import java.util.UUID;
2426

2527
/**
26-
* @author <a href="https://github.com/gentryhuang">gentryhuang</a>
28+
* @author wenqiang
29+
* @since 2020/12/25
2730
*/
28-
public class QrcodeFileRequestExecutor extends QrcodeRequestExecutor {
29-
/**
30-
* 二维码生成的文件路径,例如: /var/temp
31-
*/
31+
public class ApacheQrcodeFileRequestExecutor extends QrcodeRequestExecutor<CloseableHttpClient, HttpHost> {
32+
3233
private final String filePath;
3334

34-
public QrcodeFileRequestExecutor(RequestHttp requestHttp, String filePath) {
35+
public ApacheQrcodeFileRequestExecutor(RequestHttp<CloseableHttpClient, HttpHost> requestHttp, String filePath) {
3536
super(requestHttp);
3637
this.filePath = filePath;
3738
}
@@ -69,7 +70,6 @@ public File execute(String uri, AbstractWxMaQrcodeWrapper qrcodeWrapper, WxType
6970
if (StringUtils.isBlank(filePath)) {
7071
return FileUtils.createTmpFile(inputStream, UUID.randomUUID().toString(), "jpg");
7172
}
72-
7373
return FileUtils.createTmpFile(inputStream, UUID.randomUUID().toString(), "jpg", Paths.get(filePath).toFile());
7474
} finally {
7575
httpPost.releaseConnection();
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
package cn.binarywang.wx.miniapp.executor;
2+
3+
import cn.binarywang.wx.miniapp.bean.AbstractWxMaQrcodeWrapper;
4+
import jodd.http.HttpConnectionProvider;
5+
import jodd.http.HttpRequest;
6+
import jodd.http.HttpResponse;
7+
import jodd.http.ProxyInfo;
8+
import jodd.net.MimeTypes;
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.fs.FileUtils;
13+
import me.chanjar.weixin.common.util.http.RequestHttp;
14+
import me.chanjar.weixin.common.util.http.okhttp.OkHttpProxyInfo;
15+
import okhttp3.*;
16+
import org.apache.commons.lang3.StringUtils;
17+
18+
import java.io.ByteArrayInputStream;
19+
import java.io.File;
20+
import java.io.IOException;
21+
import java.io.InputStream;
22+
import java.nio.charset.StandardCharsets;
23+
import java.nio.file.Paths;
24+
import java.util.UUID;
25+
26+
/**
27+
* @author wenqiang
28+
* @since 2020/12/25
29+
*/
30+
public class JoddHttpQrcodeFileRequestExecutor extends QrcodeRequestExecutor<HttpConnectionProvider, ProxyInfo> {
31+
32+
private final String filePath;
33+
34+
public JoddHttpQrcodeFileRequestExecutor(RequestHttp<HttpConnectionProvider, ProxyInfo> requestHttp, String filePath) {
35+
super(requestHttp);
36+
this.filePath = filePath;
37+
}
38+
39+
/**
40+
* 执行http请求.
41+
*
42+
* @param uri uri
43+
* @param qrcodeWrapper 数据
44+
* @param wxType 微信模块类型
45+
* @return 响应结果
46+
* @throws WxErrorException 自定义异常
47+
* @throws IOException io异常
48+
*/
49+
@Override
50+
public File execute(String uri, AbstractWxMaQrcodeWrapper qrcodeWrapper, WxType wxType) throws WxErrorException, IOException {
51+
HttpRequest request = HttpRequest.get(uri);
52+
if (requestHttp.getRequestHttpProxy() != null) {
53+
requestHttp.getRequestHttpClient().useProxy(requestHttp.getRequestHttpProxy());
54+
}
55+
request.withConnectionProvider(requestHttp.getRequestHttpClient());
56+
57+
HttpResponse response = request.send();
58+
response.charset(StandardCharsets.UTF_8.name());
59+
String contentTypeHeader = response.header("Content-Type");
60+
if (MimeTypes.MIME_TEXT_PLAIN.equals(contentTypeHeader)) {
61+
String responseContent = response.bodyText();
62+
throw new WxErrorException(WxError.fromJson(responseContent, WxType.MiniApp));
63+
}
64+
try (InputStream inputStream = new ByteArrayInputStream(response.bodyBytes())) {
65+
return FileUtils.createTmpFile(inputStream, UUID.randomUUID().toString(), "jpg");
66+
}
67+
}
68+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
package cn.binarywang.wx.miniapp.executor;
2+
3+
import cn.binarywang.wx.miniapp.bean.AbstractWxMaQrcodeWrapper;
4+
import me.chanjar.weixin.common.enums.WxType;
5+
import me.chanjar.weixin.common.error.WxError;
6+
import me.chanjar.weixin.common.error.WxErrorException;
7+
import me.chanjar.weixin.common.util.http.RequestHttp;
8+
import me.chanjar.weixin.common.util.http.okhttp.OkHttpProxyInfo;
9+
import okhttp3.*;
10+
import org.apache.commons.io.IOUtils;
11+
12+
import java.io.IOException;
13+
import java.io.InputStream;
14+
15+
/**
16+
* @author wenqiang
17+
* @since 2020/12/25
18+
*/
19+
public class OkHttpQrcodeBytesRequestExecutor extends QrcodeBytesRequestExecutor<OkHttpClient, OkHttpProxyInfo> {
20+
21+
22+
public OkHttpQrcodeBytesRequestExecutor(RequestHttp<OkHttpClient, OkHttpProxyInfo> requestHttp) {
23+
super(requestHttp);
24+
}
25+
26+
/**
27+
* 执行http请求.
28+
*
29+
* @param uri uri
30+
* @param qrcodeWrapper 数据
31+
* @param wxType 微信模块类型
32+
* @return 响应结果
33+
* @throws WxErrorException 自定义异常
34+
* @throws IOException io异常
35+
*/
36+
@Override
37+
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"));
39+
Request request = new Request.Builder().url(uri).post(body).build();
40+
Response response = requestHttp.getRequestHttpClient().newCall(request).execute();
41+
String contentTypeHeader = response.header("Content-Type");
42+
if ("text/plain".equals(contentTypeHeader)) {
43+
String responseContent = response.body().string();
44+
throw new WxErrorException(WxError.fromJson(responseContent, WxType.MP));
45+
}
46+
47+
try (InputStream inputStream = response.body().byteStream()) {
48+
return IOUtils.toByteArray(inputStream);
49+
}
50+
}
51+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
package cn.binarywang.wx.miniapp.executor;
2+
3+
import cn.binarywang.wx.miniapp.bean.AbstractWxMaQrcodeWrapper;
4+
import me.chanjar.weixin.common.enums.WxType;
5+
import me.chanjar.weixin.common.error.WxError;
6+
import me.chanjar.weixin.common.error.WxErrorException;
7+
import me.chanjar.weixin.common.util.fs.FileUtils;
8+
import me.chanjar.weixin.common.util.http.RequestHttp;
9+
import me.chanjar.weixin.common.util.http.okhttp.OkHttpProxyInfo;
10+
import okhttp3.*;
11+
import org.apache.commons.lang3.StringUtils;
12+
13+
import java.io.File;
14+
import java.io.IOException;
15+
import java.io.InputStream;
16+
import java.nio.file.Paths;
17+
import java.util.UUID;
18+
19+
/**
20+
* @author wenqiang
21+
* @since 2020/12/25
22+
*/
23+
public class OkHttpQrcodeFileRequestExecutor extends QrcodeRequestExecutor<OkHttpClient, OkHttpProxyInfo> {
24+
25+
private final String filePath;
26+
27+
public OkHttpQrcodeFileRequestExecutor(RequestHttp<OkHttpClient, OkHttpProxyInfo> requestHttp, String filePath) {
28+
super(requestHttp);
29+
this.filePath = filePath;
30+
}
31+
32+
/**
33+
* 执行http请求.
34+
*
35+
* @param uri uri
36+
* @param qrcodeWrapper 数据
37+
* @param wxType 微信模块类型
38+
* @return 响应结果
39+
* @throws WxErrorException 自定义异常
40+
* @throws IOException io异常
41+
*/
42+
@Override
43+
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"));
45+
Request request = new Request.Builder().url(uri).post(body).build();
46+
Response response = requestHttp.getRequestHttpClient().newCall(request).execute();
47+
String contentTypeHeader = response.header("Content-Type");
48+
if ("text/plain".equals(contentTypeHeader)) {
49+
String responseContent = response.body().string();
50+
throw new WxErrorException(WxError.fromJson(responseContent, WxType.MP));
51+
}
52+
53+
try (InputStream inputStream = response.body().byteStream()) {
54+
if (StringUtils.isBlank(filePath)) {
55+
return FileUtils.createTmpFile(inputStream, UUID.randomUUID().toString(), "jpg");
56+
}
57+
return FileUtils.createTmpFile(inputStream, UUID.randomUUID().toString(), "jpg", Paths.get(filePath).toFile());
58+
}
59+
}
60+
}

0 commit comments

Comments
 (0)