diff --git a/weixin-java-mp/src/main/java/me/chanjar/weixin/mp/api/WxMpQrcodeService.java b/weixin-java-mp/src/main/java/me/chanjar/weixin/mp/api/WxMpQrcodeService.java index 7997a70d..89371208 100644 --- a/weixin-java-mp/src/main/java/me/chanjar/weixin/mp/api/WxMpQrcodeService.java +++ b/weixin-java-mp/src/main/java/me/chanjar/weixin/mp/api/WxMpQrcodeService.java @@ -18,10 +18,10 @@ public interface WxMpQrcodeService { * 详情请见: 生成带参数的二维码 * * - * @param scene_id 参数。 - * @param expire_seconds 过期秒数,默认60秒,最小60秒,最大1800秒 + * @param sceneId 参数。 + * @param expireSeconds 过期秒数,默认60秒,最小60秒,最大1800秒 */ - WxMpQrCodeTicket qrCodeCreateTmpTicket(int scene_id, Integer expire_seconds) throws WxErrorException; + WxMpQrCodeTicket qrCodeCreateTmpTicket(int sceneId, Integer expireSeconds) throws WxErrorException; /** *
@@ -29,9 +29,9 @@ public interface WxMpQrcodeService { * 详情请见: 生成带参数的二维码 ** - * @param scene_id 参数。永久二维码时最大值为100000(目前参数只支持1--100000) + * @param sceneId 参数。永久二维码时最大值为100000(目前参数只支持1--100000) */ - WxMpQrCodeTicket qrCodeCreateLastTicket(int scene_id) throws WxErrorException; + WxMpQrCodeTicket qrCodeCreateLastTicket(int sceneId) throws WxErrorException; /** *
@@ -39,9 +39,9 @@ public interface WxMpQrcodeService { * 详情请见: 生成带参数的二维码 ** - * @param scene_str 参数。字符串类型长度现在为1到64 + * @param sceneStr 参数。字符串类型长度现在为1到64 */ - WxMpQrCodeTicket qrCodeCreateLastTicket(String scene_str) throws WxErrorException; + WxMpQrCodeTicket qrCodeCreateLastTicket(String sceneStr) throws WxErrorException; /** *
diff --git a/weixin-java-mp/src/main/java/me/chanjar/weixin/mp/api/impl/WxMpQrcodeServiceImpl.java b/weixin-java-mp/src/main/java/me/chanjar/weixin/mp/api/impl/WxMpQrcodeServiceImpl.java index bb0e112c..9726b373 100644 --- a/weixin-java-mp/src/main/java/me/chanjar/weixin/mp/api/impl/WxMpQrcodeServiceImpl.java +++ b/weixin-java-mp/src/main/java/me/chanjar/weixin/mp/api/impl/WxMpQrcodeServiceImpl.java @@ -26,16 +26,20 @@ public class WxMpQrcodeServiceImpl implements WxMpQrcodeService { } @Override - public WxMpQrCodeTicket qrCodeCreateTmpTicket(int scene_id, Integer expire_seconds) throws WxErrorException { + public WxMpQrCodeTicket qrCodeCreateTmpTicket(int sceneId, Integer expireSeconds) throws WxErrorException { + if (sceneId == 0) { + throw new WxErrorException(WxError.newBuilder().setErrorCode(-1).setErrorMsg("临时二维码场景只不能为0!").build()); + } + String url = API_URL_PREFIX + "/create"; JsonObject json = new JsonObject(); json.addProperty("action_name", "QR_SCENE"); - if (expire_seconds != null) { - json.addProperty("expire_seconds", expire_seconds); + if (expireSeconds != null) { + json.addProperty("expire_seconds", expireSeconds); } JsonObject actionInfo = new JsonObject(); JsonObject scene = new JsonObject(); - scene.addProperty("scene_id", scene_id); + scene.addProperty("scene_id", sceneId); actionInfo.add("scene", scene); json.add("action_info", actionInfo); String responseContent = this.wxMpService.execute(new SimplePostRequestExecutor(), url, json.toString()); @@ -43,13 +47,17 @@ public class WxMpQrcodeServiceImpl implements WxMpQrcodeService { } @Override - public WxMpQrCodeTicket qrCodeCreateLastTicket(int scene_id) throws WxErrorException { + public WxMpQrCodeTicket qrCodeCreateLastTicket(int sceneId) throws WxErrorException { + if (sceneId < 1 || sceneId > 100000) { + throw new WxErrorException(WxError.newBuilder().setErrorCode(-1).setErrorMsg("永久二维码的场景值目前只支持1--100000!").build()); + } + String url = API_URL_PREFIX + "/create"; JsonObject json = new JsonObject(); json.addProperty("action_name", "QR_LIMIT_SCENE"); JsonObject actionInfo = new JsonObject(); JsonObject scene = new JsonObject(); - scene.addProperty("scene_id", scene_id); + scene.addProperty("scene_id", sceneId); actionInfo.add("scene", scene); json.add("action_info", actionInfo); String responseContent = this.wxMpService.execute(new SimplePostRequestExecutor(), url, json.toString()); @@ -57,13 +65,13 @@ public class WxMpQrcodeServiceImpl implements WxMpQrcodeService { } @Override - public WxMpQrCodeTicket qrCodeCreateLastTicket(String scene_str) throws WxErrorException { + public WxMpQrCodeTicket qrCodeCreateLastTicket(String sceneStr) throws WxErrorException { String url = API_URL_PREFIX + "/create"; JsonObject json = new JsonObject(); json.addProperty("action_name", "QR_LIMIT_STR_SCENE"); JsonObject actionInfo = new JsonObject(); JsonObject scene = new JsonObject(); - scene.addProperty("scene_str", scene_str); + scene.addProperty("scene_str", sceneStr); actionInfo.add("scene", scene); json.add("action_info", actionInfo); String responseContent = this.wxMpService.execute(new SimplePostRequestExecutor(), url, json.toString()); @@ -81,7 +89,7 @@ public class WxMpQrcodeServiceImpl implements WxMpQrcodeService { String url = "https://mp.weixin.qq.com/cgi-bin/showqrcode?ticket=%s"; try { String resultUrl = String.format(url, - URLEncoder.encode(ticket, StandardCharsets.UTF_8.name())); + URLEncoder.encode(ticket, StandardCharsets.UTF_8.name())); if (needShortUrl) { return this.wxMpService.shortUrl(resultUrl); } @@ -89,7 +97,7 @@ public class WxMpQrcodeServiceImpl implements WxMpQrcodeService { return resultUrl; } catch (UnsupportedEncodingException e) { WxError error = WxError.newBuilder().setErrorCode(-1) - .setErrorMsg(e.getMessage()).build(); + .setErrorMsg(e.getMessage()).build(); throw new WxErrorException(error); } } diff --git a/weixin-java-mp/src/test/java/me/chanjar/weixin/mp/api/impl/WxMpQrCodeServiceImplTest.java b/weixin-java-mp/src/test/java/me/chanjar/weixin/mp/api/impl/WxMpQrCodeServiceImplTest.java index 4c53ac17..a2ab5328 100644 --- a/weixin-java-mp/src/test/java/me/chanjar/weixin/mp/api/impl/WxMpQrCodeServiceImplTest.java +++ b/weixin-java-mp/src/test/java/me/chanjar/weixin/mp/api/impl/WxMpQrCodeServiceImplTest.java @@ -6,6 +6,7 @@ import me.chanjar.weixin.mp.api.ApiTestModule; import me.chanjar.weixin.mp.api.WxMpService; import me.chanjar.weixin.mp.bean.result.WxMpQrCodeTicket; import org.testng.Assert; +import org.testng.annotations.DataProvider; import org.testng.annotations.Guice; import org.testng.annotations.Test; @@ -19,20 +20,26 @@ import java.io.File; @Test(groups = "qrCodeAPI") @Guice(modules = ApiTestModule.class) public class WxMpQrCodeServiceImplTest { - @Inject protected WxMpService wxService; - public void testQrCodeCreateTmpTicket() throws WxErrorException { - WxMpQrCodeTicket ticket = this.wxService.getQrcodeService().qrCodeCreateTmpTicket(1, null); + @DataProvider + public Object[][] sceneIds() { + return new Object[][]{{-1}, {0}, {1}, {200000}}; + } + + @Test(dataProvider = "sceneIds") + public void testQrCodeCreateTmpTicket(int sceneId) throws WxErrorException { + WxMpQrCodeTicket ticket = this.wxService.getQrcodeService().qrCodeCreateTmpTicket(sceneId, null); Assert.assertNotNull(ticket.getUrl()); Assert.assertNotNull(ticket.getTicket()); Assert.assertTrue(ticket.getExpire_seconds() != -1); System.out.println(ticket); } - public void testQrCodeCreateLastTicket() throws WxErrorException { - WxMpQrCodeTicket ticket = this.wxService.getQrcodeService().qrCodeCreateLastTicket(1); + @Test(dataProvider = "sceneIds") + public void testQrCodeCreateLastTicket(int sceneId) throws WxErrorException { + WxMpQrCodeTicket ticket = this.wxService.getQrcodeService().qrCodeCreateLastTicket(sceneId); Assert.assertNotNull(ticket.getUrl()); Assert.assertNotNull(ticket.getTicket()); Assert.assertTrue(ticket.getExpire_seconds() == -1);