Skip to content

Commit 6638ca7

Browse files
Hyseenbinarywang
authored andcommitted
#289 增加临时二维码的场景值支持字符串的接口
* 更新接口:临时二维码的场景值支持字符串 * 新增临时二维码的场景值为字符串的单元测试
1 parent 1a9b4a3 commit 6638ca7

File tree

3 files changed

+61
-0
lines changed

3 files changed

+61
-0
lines changed

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

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,18 @@ public interface WxMpQrcodeService {
2626
*/
2727
WxMpQrCodeTicket qrCodeCreateTmpTicket(int sceneId, Integer expireSeconds) throws WxErrorException;
2828

29+
30+
/**
31+
* <pre>
32+
* 换取临时二维码ticket
33+
* 详情请见: <a href="https://mp.weixin.qq.com/wiki?action=doc&id=mp1443433542&t=0.9274944716856435">生成带参数的二维码</a>
34+
* </pre>
35+
*
36+
* @param sceneStr 场景值ID(字符串形式的ID),字符串类型,长度限制为1到64
37+
* @param expireSeconds 该二维码有效时间,以秒为单位。 最大不超过2592000(即30天),此字段如果不填,则默认有效期为30秒。
38+
*/
39+
WxMpQrCodeTicket qrCodeCreateTmpTicket(String sceneStr, Integer expireSeconds) throws WxErrorException;
40+
2941
/**
3042
* <pre>
3143
* 换取永久二维码ticket

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

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
import me.chanjar.weixin.mp.api.WxMpService;
88
import me.chanjar.weixin.mp.bean.result.WxMpQrCodeTicket;
99
import me.chanjar.weixin.mp.util.http.QrCodeRequestExecutor;
10+
import org.apache.commons.lang3.StringUtils;
1011

1112
import java.io.File;
1213
import java.io.UnsupportedEncodingException;
@@ -54,6 +55,38 @@ public WxMpQrCodeTicket qrCodeCreateTmpTicket(int sceneId, Integer expireSeconds
5455
return WxMpQrCodeTicket.fromJson(responseContent);
5556
}
5657

58+
59+
@Override
60+
public WxMpQrCodeTicket qrCodeCreateTmpTicket(String sceneStr, Integer expireSeconds) throws WxErrorException {
61+
if (StringUtils.isBlank(sceneStr)) {
62+
throw new WxErrorException(WxError.newBuilder().setErrorCode(-1).setErrorMsg("临时二维码场景值不能为空!").build());
63+
}
64+
65+
//expireSeconds 该二维码有效时间,以秒为单位。 最大不超过2592000(即30天),此字段如果不填,则默认有效期为30秒。
66+
if (expireSeconds != null && expireSeconds > 2592000) {
67+
throw new WxErrorException(WxError.newBuilder().setErrorCode(-1)
68+
.setErrorMsg("临时二维码有效时间最大不能超过2592000(即30天)!").build());
69+
}
70+
71+
if (expireSeconds == null) {
72+
expireSeconds = 30;
73+
}
74+
75+
String url = API_URL_PREFIX + "/create";
76+
JsonObject json = new JsonObject();
77+
json.addProperty("action_name", "QR_STR_SCENE");
78+
json.addProperty("expire_seconds", expireSeconds);
79+
80+
JsonObject actionInfo = new JsonObject();
81+
JsonObject scene = new JsonObject();
82+
scene.addProperty("scene_str", sceneStr);
83+
actionInfo.add("scene", scene);
84+
json.add("action_info", actionInfo);
85+
String responseContent = this.wxMpService.post(url, json.toString());
86+
return WxMpQrCodeTicket.fromJson(responseContent);
87+
}
88+
89+
5790
@Override
5891
public WxMpQrCodeTicket qrCodeCreateLastTicket(int sceneId) throws WxErrorException {
5992
if (sceneId < 1 || sceneId > 100000) {

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

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import me.chanjar.weixin.mp.api.WxMpService;
66
import me.chanjar.weixin.mp.api.test.ApiTestModule;
77
import me.chanjar.weixin.mp.bean.result.WxMpQrCodeTicket;
8+
import org.apache.commons.lang3.RandomStringUtils;
89
import org.testng.*;
910
import org.testng.annotations.*;
1011

@@ -26,6 +27,11 @@ public Object[][] sceneIds() {
2627
return new Object[][]{{-1}, {0}, {1}, {200000}};
2728
}
2829

30+
@DataProvider
31+
public Object[][] sceneStrs() {
32+
return new Object[][]{{null}, {""}, {"test"}, {RandomStringUtils.randomAlphanumeric(100)}};
33+
}
34+
2935
@Test(dataProvider = "sceneIds")
3036
public void testQrCodeCreateTmpTicket(int sceneId) throws WxErrorException {
3137
WxMpQrCodeTicket ticket = this.wxService.getQrcodeService().qrCodeCreateTmpTicket(sceneId, null);
@@ -35,6 +41,16 @@ public void testQrCodeCreateTmpTicket(int sceneId) throws WxErrorException {
3541
System.out.println(ticket);
3642
}
3743

44+
45+
@Test(dataProvider = "sceneStrs")
46+
public void testQrCodeCreateTmpTicketWithSceneStr(String sceneStr) throws WxErrorException {
47+
WxMpQrCodeTicket ticket = this.wxService.getQrcodeService().qrCodeCreateTmpTicket(sceneStr, null);
48+
Assert.assertNotNull(ticket.getUrl());
49+
Assert.assertNotNull(ticket.getTicket());
50+
Assert.assertTrue(ticket.getExpire_seconds() != -1);
51+
System.out.println(ticket);
52+
}
53+
3854
@Test(dataProvider = "sceneIds")
3955
public void testQrCodeCreateLastTicket(int sceneId) throws WxErrorException {
4056
WxMpQrCodeTicket ticket = this.wxService.getQrcodeService().qrCodeCreateLastTicket(sceneId);

0 commit comments

Comments
 (0)