Skip to content

Commit e6a550d

Browse files
authored
🆕 #1831 生成小程序二维码的相关接口增加指定文件路径参数的重载方法
1 parent aa26d6b commit e6a550d

File tree

4 files changed

+256
-2
lines changed

4 files changed

+256
-2
lines changed

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

Lines changed: 112 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
package cn.binarywang.wx.miniapp.api;
22

3-
import java.io.File;
4-
53
import cn.binarywang.wx.miniapp.bean.WxMaCodeLineColor;
64
import me.chanjar.weixin.common.error.WxErrorException;
75

6+
import java.io.File;
7+
88
/**
99
* <pre>
1010
* 二维码相关操作接口.
@@ -37,6 +37,23 @@ public interface WxMaQrcodeService {
3737
*/
3838
byte[] createQrcodeBytes(String path, int width) throws WxErrorException;
3939

40+
/**
41+
* 接口C: 获取小程序页面二维码.
42+
* <pre>
43+
* 适用于需要的码数量较少的业务场景
44+
* 通过该接口,仅能生成已发布的小程序的二维码。
45+
* 可以在开发者工具预览时生成开发版的带参二维码。
46+
* 带参二维码只有 100000 个,请谨慎调用。
47+
* </pre>
48+
*
49+
* @param path 不能为空,最大长度 128 字节
50+
* @param width 默认430 二维码的宽度
51+
* @param filePath 二维码生成的文件路径,例如: /var/temp
52+
* @return 文件对象
53+
* @throws WxErrorException 异常
54+
*/
55+
File createQrcode(String path, int width, String filePath) throws WxErrorException;
56+
4057
/**
4158
* 接口C: 获取小程序页面二维码.
4259
* <pre>
@@ -53,6 +70,22 @@ public interface WxMaQrcodeService {
5370
*/
5471
File createQrcode(String path, int width) throws WxErrorException;
5572

73+
/**
74+
* 接口C: 获取小程序页面二维码.
75+
* <pre>
76+
* 适用于需要的码数量较少的业务场景
77+
* 通过该接口,仅能生成已发布的小程序的二维码。
78+
* 可以在开发者工具预览时生成开发版的带参二维码。
79+
* 带参二维码只有 100000 个,请谨慎调用。
80+
* </pre>
81+
*
82+
* @param path 不能为空,最大长度 128 字节
83+
* @param filePath 二维码生成的文件路径,例如: /var/temp
84+
* @return 文件对象
85+
* @throws WxErrorException 异常
86+
*/
87+
File createQrcode(String path, String filePath) throws WxErrorException;
88+
5689
/**
5790
* 接口C: 获取小程序页面二维码.
5891
* <pre>
@@ -82,6 +115,21 @@ public interface WxMaQrcodeService {
82115
byte[] createWxaCodeBytes(String path, int width, boolean autoColor, WxMaCodeLineColor lineColor, boolean isHyaline)
83116
throws WxErrorException;
84117

118+
/**
119+
* 接口A: 获取小程序码.
120+
*
121+
* @param path 不能为空,最大长度 128 字节
122+
* @param width 默认430 二维码的宽度
123+
* @param filePath 二维码生成的文件路径,例如: /var/temp
124+
* @param autoColor 默认true 自动配置线条颜色,如果颜色依然是黑色,则说明不建议配置主色调
125+
* @param lineColor auth_color 为 false 时生效,使用 rgb 设置颜色 例如 {"r":"xxx","g":"xxx","b":"xxx"}
126+
* @param isHyaline 是否需要透明底色, isHyaline 为true时,生成透明底色的小程序码
127+
* @return 文件对象
128+
* @throws WxErrorException 异常
129+
*/
130+
File createWxaCode(String path, int width, String filePath, boolean autoColor, WxMaCodeLineColor lineColor, boolean isHyaline)
131+
throws WxErrorException;
132+
85133
/**
86134
* 接口A: 获取小程序码.
87135
*
@@ -96,6 +144,17 @@ byte[] createWxaCodeBytes(String path, int width, boolean autoColor, WxMaCodeLin
96144
File createWxaCode(String path, int width, boolean autoColor, WxMaCodeLineColor lineColor, boolean isHyaline)
97145
throws WxErrorException;
98146

147+
/**
148+
* 接口A: 获取小程序码.
149+
*
150+
* @param path 不能为空,最大长度 128 字节
151+
* @param width 默认430 二维码的宽度
152+
* @param filePath 二维码生成的文件路径,例如: /var/temp
153+
* @return 文件对象
154+
* @throws WxErrorException 异常
155+
*/
156+
File createWxaCode(String path, int width, String filePath) throws WxErrorException;
157+
99158
/**
100159
* 接口A: 获取小程序码.
101160
*
@@ -106,6 +165,16 @@ File createWxaCode(String path, int width, boolean autoColor, WxMaCodeLineColor
106165
*/
107166
File createWxaCode(String path, int width) throws WxErrorException;
108167

168+
/**
169+
* 接口A: 获取小程序码.
170+
*
171+
* @param path 不能为空,最大长度 128 字节
172+
* @param filePath 二维码生成的文件路径,例如: /var/temp
173+
* @return 文件对象
174+
* @throws WxErrorException 异常
175+
*/
176+
File createWxaCode(String path, String filePath) throws WxErrorException;
177+
109178
/**
110179
* 接口A: 获取小程序码.
111180
*
@@ -137,6 +206,29 @@ File createWxaCode(String path, int width, boolean autoColor, WxMaCodeLineColor
137206
byte[] createWxaCodeUnlimitBytes(String scene, String page, int width, boolean autoColor,
138207
WxMaCodeLineColor lineColor, boolean isHyaline) throws WxErrorException;
139208

209+
/**
210+
* 接口B: 获取小程序码(永久有效、数量暂无限制).
211+
* <pre>
212+
* 通过该接口生成的小程序码,永久有效,数量暂无限制。
213+
* 用户扫描该码进入小程序后,将统一打开首页,开发者需在对应页面根据获取的码中 scene 字段的值,再做处理逻辑。
214+
* 使用如下代码可以获取到二维码中的 scene 字段的值。
215+
* 调试阶段可以使用开发工具的条件编译自定义参数 scene=xxxx 进行模拟,开发工具模拟时的 scene 的参数值需要进行 urlencode
216+
* </pre>
217+
*
218+
* @param scene 最大32个可见字符,只支持数字,大小写英文以及部分特殊字符:!#$&'()*+,/:;=?@-._~,
219+
* 其它字符请自行编码为合法字符(因不支持%,中文无法使用 urlencode 处理,请使用其他编码方式)
220+
* @param page 必须是已经发布的小程序页面,例如 "pages/index/index" ,如果不填写这个字段,默认跳主页面
221+
* @param filePath 二维码生成的文件路径,例如: /var/temp
222+
* @param width 默认false 自动配置线条颜色,如果颜色依然是黑色,则说明不建议配置主色调
223+
* @param autoColor 默认true 自动配置线条颜色,如果颜色依然是黑色,则说明不建议配置主色调
224+
* @param lineColor auth_color 为 false 时生效,使用 rgb 设置颜色 例如 {"r":"xxx","g":"xxx","b":"xxx"}
225+
* @param isHyaline 是否需要透明底色, is_hyaline 为true时,生成透明底色的小程序码
226+
* @return 文件对象
227+
* @throws WxErrorException 异常
228+
*/
229+
File createWxaCodeUnlimit(String scene, String page, String filePath, int width, boolean autoColor,
230+
WxMaCodeLineColor lineColor, boolean isHyaline) throws WxErrorException;
231+
140232
/**
141233
* 接口B: 获取小程序码(永久有效、数量暂无限制).
142234
* <pre>
@@ -159,6 +251,24 @@ byte[] createWxaCodeUnlimitBytes(String scene, String page, int width, boolean a
159251
File createWxaCodeUnlimit(String scene, String page, int width, boolean autoColor,
160252
WxMaCodeLineColor lineColor, boolean isHyaline) throws WxErrorException;
161253

254+
/**
255+
* 接口B: 获取小程序码(永久有效、数量暂无限制).
256+
* <pre>
257+
* 通过该接口生成的小程序码,永久有效,数量暂无限制。
258+
* 用户扫描该码进入小程序后,将统一打开首页,开发者需在对应页面根据获取的码中 scene 字段的值,再做处理逻辑。
259+
* 使用如下代码可以获取到二维码中的 scene 字段的值。
260+
* 调试阶段可以使用开发工具的条件编译自定义参数 scene=xxxx 进行模拟,开发工具模拟时的 scene 的参数值需要进行 urlencode
261+
* </pre>
262+
*
263+
* @param scene 最大32个可见字符,只支持数字,大小写英文以及部分特殊字符:!#$&'()*+,/:;=?@-._~,
264+
* 其它字符请自行编码为合法字符(因不支持%,中文无法使用 urlencode 处理,请使用其他编码方式)
265+
* @param page 必须是已经发布的小程序页面,例如 "pages/index/index" ,如果不填写这个字段,默认跳主页面
266+
* @param filePath 二维码生成的文件路径,例如: /var/temp
267+
* @return 文件对象
268+
* @throws WxErrorException 异常
269+
*/
270+
File createWxaCodeUnlimit(String scene, String page, String filePath) throws WxErrorException;
271+
162272
/**
163273
* 接口B: 获取小程序码(永久有效、数量暂无限制).
164274
* <pre>

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

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
import cn.binarywang.wx.miniapp.bean.WxaCode;
88
import cn.binarywang.wx.miniapp.bean.WxaCodeUnlimit;
99
import cn.binarywang.wx.miniapp.util.QrcodeBytesRequestExecutor;
10+
import cn.binarywang.wx.miniapp.util.QrcodeFileRequestExecutor;
1011
import cn.binarywang.wx.miniapp.util.QrcodeRequestExecutor;
1112
import lombok.AllArgsConstructor;
1213
import me.chanjar.weixin.common.error.WxErrorException;
@@ -107,4 +108,51 @@ public File createWxaCodeUnlimit(String scene, String page) throws WxErrorExcept
107108
return this.createWxaCodeUnlimit(scene, page, 430, true, null, false);
108109
}
109110

111+
@Override
112+
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));
115+
}
116+
117+
@Override
118+
public File createQrcode(String path, String filePath) throws WxErrorException {
119+
return createQrcode(path, 430, filePath);
120+
}
121+
122+
@Override
123+
public File createWxaCode(String path, int width, String filePath, boolean autoColor, WxMaCodeLineColor lineColor, boolean isHyaline)
124+
throws WxErrorException {
125+
final QrcodeFileRequestExecutor executor = new QrcodeFileRequestExecutor(this.wxMaService.getRequestHttp(), filePath);
126+
return this.wxMaService.execute(executor, GET_WXACODE_URL, WxaCode.builder()
127+
.path(path)
128+
.width(width)
129+
.autoColor(autoColor)
130+
.lineColor(lineColor)
131+
.isHyaline(isHyaline)
132+
.build());
133+
}
134+
135+
@Override
136+
public File createWxaCode(String path, int width, String filePath) throws WxErrorException {
137+
return this.createWxaCode(path, width, filePath, true, null, false);
138+
}
139+
140+
@Override
141+
public File createWxaCode(String path, String filePath) throws WxErrorException {
142+
return this.createWxaCode(path, 430, filePath);
143+
}
144+
145+
@Override
146+
public File createWxaCodeUnlimit(String scene, String page, String filePath, int width, boolean autoColor,
147+
WxMaCodeLineColor lineColor, boolean isHyaline) throws WxErrorException {
148+
return this.wxMaService.execute(new QrcodeFileRequestExecutor(this.wxMaService.getRequestHttp(), filePath),
149+
GET_WXACODE_UNLIMIT_URL,
150+
this.buildWxaCodeUnlimit(scene, page, width, autoColor, lineColor, isHyaline));
151+
}
152+
153+
@Override
154+
public File createWxaCodeUnlimit(String scene, String page, String filePath) throws WxErrorException {
155+
return this.createWxaCodeUnlimit(scene, page, filePath, 430, true, null, false);
156+
}
157+
110158
}
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
package cn.binarywang.wx.miniapp.util;
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.apache.InputStreamResponseHandler;
10+
import me.chanjar.weixin.common.util.http.apache.Utf8ResponseHandler;
11+
import org.apache.commons.lang3.StringUtils;
12+
import org.apache.http.Header;
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+
19+
import java.io.File;
20+
import java.io.IOException;
21+
import java.io.InputStream;
22+
import java.nio.file.Paths;
23+
import java.util.UUID;
24+
25+
/**
26+
* @author <a href="https://github.com/gentryhuang">gentryhuang</a>
27+
*/
28+
public class QrcodeFileRequestExecutor extends QrcodeRequestExecutor {
29+
/**
30+
* 二维码生成的文件路径,例如: /var/temp
31+
*/
32+
private final String filePath;
33+
34+
public QrcodeFileRequestExecutor(RequestHttp 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+
HttpPost httpPost = new HttpPost(uri);
52+
if (requestHttp.getRequestHttpProxy() != null) {
53+
httpPost.setConfig(
54+
RequestConfig.custom().setProxy(requestHttp.getRequestHttpProxy()).build()
55+
);
56+
}
57+
58+
httpPost.setEntity(new StringEntity(qrcodeWrapper.toJson(), ContentType.APPLICATION_JSON));
59+
60+
try (final CloseableHttpResponse response = requestHttp.getRequestHttpClient().execute(httpPost);
61+
final InputStream inputStream = InputStreamResponseHandler.INSTANCE.handleResponse(response)) {
62+
Header[] contentTypeHeader = response.getHeaders("Content-Type");
63+
if (contentTypeHeader != null && contentTypeHeader.length > 0
64+
&& ContentType.APPLICATION_JSON.getMimeType()
65+
.equals(ContentType.parse(contentTypeHeader[0].getValue()).getMimeType())) {
66+
String responseContent = Utf8ResponseHandler.INSTANCE.handleResponse(response);
67+
throw new WxErrorException(WxError.fromJson(responseContent, wxType));
68+
}
69+
if (StringUtils.isBlank(filePath)) {
70+
return FileUtils.createTmpFile(inputStream, UUID.randomUUID().toString(), "jpg");
71+
}
72+
73+
return FileUtils.createTmpFile(inputStream, UUID.randomUUID().toString(), "jpg", Paths.get(filePath).toFile());
74+
} finally {
75+
httpPost.releaseConnection();
76+
}
77+
}
78+
}

weixin-java-miniapp/src/test/java/cn/binarywang/wx/miniapp/api/impl/WxMaQrcodeServiceImplTest.java

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,4 +55,22 @@ public void testCreateWxaCodeUnlimitBytes() throws WxErrorException {
5555
final byte[] wxCode = this.wxService.getQrcodeService().createWxaCodeUnlimitBytes("111", null, 122, true, null, false);
5656
assertThat(wxCode).isNotNull();
5757
}
58+
59+
@Test
60+
public void testCreateQrcodeByFile() throws WxErrorException {
61+
final File qrCode = this.wxService.getQrcodeService().createQrcode("111", "/opt/logs");
62+
assertThat(qrCode).isNotNull();
63+
}
64+
65+
@Test
66+
public void testCreateWxaCodeByFile() throws WxErrorException {
67+
final File wxCode = this.wxService.getQrcodeService().createWxaCode("111", "/opt/logs");
68+
assertThat(wxCode).isNotNull();
69+
}
70+
71+
@Test
72+
public void testCreateQrcodeUnlimitByFile() throws WxErrorException {
73+
final File wxCode = this.wxService.getQrcodeService().createWxaCodeUnlimit("111",null,"/opt/logs");
74+
assertThat(wxCode).isNotNull();
75+
}
5876
}

0 commit comments

Comments
 (0)