Skip to content

Commit c70bf9b

Browse files
committed
生成带参数的二维码时加入场景值的校验 binarywang#106
1 parent 637fc87 commit c70bf9b

File tree

3 files changed

+37
-22
lines changed

3 files changed

+37
-22
lines changed

weixin-java-mp/src/main/java/me/chanjar/weixin/mp/api/WxMpQrcodeService.java

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -18,30 +18,30 @@ public interface WxMpQrcodeService {
1818
* 详情请见: <a href="http://mp.weixin.qq.com/wiki/18/167e7d94df85d8389df6c94a7a8f78ba.html">生成带参数的二维码</a>
1919
* </pre>
2020
*
21-
* @param scene_id 参数。
22-
* @param expire_seconds 过期秒数,默认60秒,最小60秒,最大1800秒
21+
* @param sceneId 参数。
22+
* @param expireSeconds 过期秒数,默认60秒,最小60秒,最大1800秒
2323
*/
24-
WxMpQrCodeTicket qrCodeCreateTmpTicket(int scene_id, Integer expire_seconds) throws WxErrorException;
24+
WxMpQrCodeTicket qrCodeCreateTmpTicket(int sceneId, Integer expireSeconds) throws WxErrorException;
2525

2626
/**
2727
* <pre>
2828
* 换取永久二维码ticket
2929
* 详情请见: <a href="http://mp.weixin.qq.com/wiki/18/167e7d94df85d8389df6c94a7a8f78ba.html">生成带参数的二维码</a>
3030
* </pre>
3131
*
32-
* @param scene_id 参数。永久二维码时最大值为100000(目前参数只支持1--100000)
32+
* @param sceneId 参数。永久二维码时最大值为100000(目前参数只支持1--100000)
3333
*/
34-
WxMpQrCodeTicket qrCodeCreateLastTicket(int scene_id) throws WxErrorException;
34+
WxMpQrCodeTicket qrCodeCreateLastTicket(int sceneId) throws WxErrorException;
3535

3636
/**
3737
* <pre>
3838
* 换取永久字符串二维码ticket
3939
* 详情请见: <a href="http://mp.weixin.qq.com/wiki/18/167e7d94df85d8389df6c94a7a8f78ba.html">生成带参数的二维码</a>
4040
* </pre>
4141
*
42-
* @param scene_str 参数。字符串类型长度现在为1到64
42+
* @param sceneStr 参数。字符串类型长度现在为1到64
4343
*/
44-
WxMpQrCodeTicket qrCodeCreateLastTicket(String scene_str) throws WxErrorException;
44+
WxMpQrCodeTicket qrCodeCreateLastTicket(String sceneStr) throws WxErrorException;
4545

4646
/**
4747
* <pre>

weixin-java-mp/src/main/java/me/chanjar/weixin/mp/api/impl/WxMpQrcodeServiceImpl.java

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -26,44 +26,52 @@ public WxMpQrcodeServiceImpl(WxMpService wxMpService) {
2626
}
2727

2828
@Override
29-
public WxMpQrCodeTicket qrCodeCreateTmpTicket(int scene_id, Integer expire_seconds) throws WxErrorException {
29+
public WxMpQrCodeTicket qrCodeCreateTmpTicket(int sceneId, Integer expireSeconds) throws WxErrorException {
30+
if (sceneId == 0) {
31+
throw new WxErrorException(WxError.newBuilder().setErrorCode(-1).setErrorMsg("临时二维码场景只不能为0!").build());
32+
}
33+
3034
String url = API_URL_PREFIX + "/create";
3135
JsonObject json = new JsonObject();
3236
json.addProperty("action_name", "QR_SCENE");
33-
if (expire_seconds != null) {
34-
json.addProperty("expire_seconds", expire_seconds);
37+
if (expireSeconds != null) {
38+
json.addProperty("expire_seconds", expireSeconds);
3539
}
3640
JsonObject actionInfo = new JsonObject();
3741
JsonObject scene = new JsonObject();
38-
scene.addProperty("scene_id", scene_id);
42+
scene.addProperty("scene_id", sceneId);
3943
actionInfo.add("scene", scene);
4044
json.add("action_info", actionInfo);
4145
String responseContent = this.wxMpService.execute(new SimplePostRequestExecutor(), url, json.toString());
4246
return WxMpQrCodeTicket.fromJson(responseContent);
4347
}
4448

4549
@Override
46-
public WxMpQrCodeTicket qrCodeCreateLastTicket(int scene_id) throws WxErrorException {
50+
public WxMpQrCodeTicket qrCodeCreateLastTicket(int sceneId) throws WxErrorException {
51+
if (sceneId < 1 || sceneId > 100000) {
52+
throw new WxErrorException(WxError.newBuilder().setErrorCode(-1).setErrorMsg("永久二维码的场景值目前只支持1--100000!").build());
53+
}
54+
4755
String url = API_URL_PREFIX + "/create";
4856
JsonObject json = new JsonObject();
4957
json.addProperty("action_name", "QR_LIMIT_SCENE");
5058
JsonObject actionInfo = new JsonObject();
5159
JsonObject scene = new JsonObject();
52-
scene.addProperty("scene_id", scene_id);
60+
scene.addProperty("scene_id", sceneId);
5361
actionInfo.add("scene", scene);
5462
json.add("action_info", actionInfo);
5563
String responseContent = this.wxMpService.execute(new SimplePostRequestExecutor(), url, json.toString());
5664
return WxMpQrCodeTicket.fromJson(responseContent);
5765
}
5866

5967
@Override
60-
public WxMpQrCodeTicket qrCodeCreateLastTicket(String scene_str) throws WxErrorException {
68+
public WxMpQrCodeTicket qrCodeCreateLastTicket(String sceneStr) throws WxErrorException {
6169
String url = API_URL_PREFIX + "/create";
6270
JsonObject json = new JsonObject();
6371
json.addProperty("action_name", "QR_LIMIT_STR_SCENE");
6472
JsonObject actionInfo = new JsonObject();
6573
JsonObject scene = new JsonObject();
66-
scene.addProperty("scene_str", scene_str);
74+
scene.addProperty("scene_str", sceneStr);
6775
actionInfo.add("scene", scene);
6876
json.add("action_info", actionInfo);
6977
String responseContent = this.wxMpService.execute(new SimplePostRequestExecutor(), url, json.toString());
@@ -81,15 +89,15 @@ public String qrCodePictureUrl(String ticket, boolean needShortUrl) throws WxErr
8189
String url = "https://mp.weixin.qq.com/cgi-bin/showqrcode?ticket=%s";
8290
try {
8391
String resultUrl = String.format(url,
84-
URLEncoder.encode(ticket, StandardCharsets.UTF_8.name()));
92+
URLEncoder.encode(ticket, StandardCharsets.UTF_8.name()));
8593
if (needShortUrl) {
8694
return this.wxMpService.shortUrl(resultUrl);
8795
}
8896

8997
return resultUrl;
9098
} catch (UnsupportedEncodingException e) {
9199
WxError error = WxError.newBuilder().setErrorCode(-1)
92-
.setErrorMsg(e.getMessage()).build();
100+
.setErrorMsg(e.getMessage()).build();
93101
throw new WxErrorException(error);
94102
}
95103
}

weixin-java-mp/src/test/java/me/chanjar/weixin/mp/api/impl/WxMpQrCodeServiceImplTest.java

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import me.chanjar.weixin.mp.api.WxMpService;
77
import me.chanjar.weixin.mp.bean.result.WxMpQrCodeTicket;
88
import org.testng.Assert;
9+
import org.testng.annotations.DataProvider;
910
import org.testng.annotations.Guice;
1011
import org.testng.annotations.Test;
1112

@@ -19,20 +20,26 @@
1920
@Test(groups = "qrCodeAPI")
2021
@Guice(modules = ApiTestModule.class)
2122
public class WxMpQrCodeServiceImplTest {
22-
2323
@Inject
2424
protected WxMpService wxService;
2525

26-
public void testQrCodeCreateTmpTicket() throws WxErrorException {
27-
WxMpQrCodeTicket ticket = this.wxService.getQrcodeService().qrCodeCreateTmpTicket(1, null);
26+
@DataProvider
27+
public Object[][] sceneIds() {
28+
return new Object[][]{{-1}, {0}, {1}, {200000}};
29+
}
30+
31+
@Test(dataProvider = "sceneIds")
32+
public void testQrCodeCreateTmpTicket(int sceneId) throws WxErrorException {
33+
WxMpQrCodeTicket ticket = this.wxService.getQrcodeService().qrCodeCreateTmpTicket(sceneId, null);
2834
Assert.assertNotNull(ticket.getUrl());
2935
Assert.assertNotNull(ticket.getTicket());
3036
Assert.assertTrue(ticket.getExpire_seconds() != -1);
3137
System.out.println(ticket);
3238
}
3339

34-
public void testQrCodeCreateLastTicket() throws WxErrorException {
35-
WxMpQrCodeTicket ticket = this.wxService.getQrcodeService().qrCodeCreateLastTicket(1);
40+
@Test(dataProvider = "sceneIds")
41+
public void testQrCodeCreateLastTicket(int sceneId) throws WxErrorException {
42+
WxMpQrCodeTicket ticket = this.wxService.getQrcodeService().qrCodeCreateLastTicket(sceneId);
3643
Assert.assertNotNull(ticket.getUrl());
3744
Assert.assertNotNull(ticket.getTicket());
3845
Assert.assertTrue(ticket.getExpire_seconds() == -1);

0 commit comments

Comments
 (0)