From 37dbee6efc22e26921a26cc9a36df26959483649 Mon Sep 17 00:00:00 2001 From: Daniel Qian Date: Tue, 26 Aug 2014 14:41:07 +0800 Subject: [PATCH] =?UTF-8?q?issue=20#5=20=E7=94=9F=E6=88=90=E5=B8=A6?= =?UTF-8?q?=E5=8F=82=E6=95=B0=E7=9A=84=E4=BA=8C=E7=BB=B4=E7=A0=81=20-=20ti?= =?UTF-8?q?cket?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../chanjarster/weixin/api/WxService.java | 26 ++++++++++- .../chanjarster/weixin/api/WxServiceImpl.java | 30 ++++++++++++ .../weixin/bean/result/WxQrCodeTicket.java | 46 +++++++++++++++++++ .../weixin/api/WxQrCodeAPITest.java | 39 ++++++++++++++++ src/test/resources/testng.xml | 1 + 5 files changed, 141 insertions(+), 1 deletion(-) create mode 100644 src/main/java/chanjarster/weixin/bean/result/WxQrCodeTicket.java create mode 100644 src/test/java/chanjarster/weixin/api/WxQrCodeAPITest.java diff --git a/src/main/java/chanjarster/weixin/api/WxService.java b/src/main/java/chanjarster/weixin/api/WxService.java index 5d2dcf67..b9d85249 100644 --- a/src/main/java/chanjarster/weixin/api/WxService.java +++ b/src/main/java/chanjarster/weixin/api/WxService.java @@ -15,6 +15,7 @@ import chanjarster.weixin.bean.WxMenu; import chanjarster.weixin.bean.result.WxMassSendResult; import chanjarster.weixin.bean.result.WxMassUploadResult; import chanjarster.weixin.bean.result.WxMediaUploadResult; +import chanjarster.weixin.bean.result.WxQrCodeTicket; import chanjarster.weixin.bean.result.WxUser; import chanjarster.weixin.bean.result.WxUserList; import chanjarster.weixin.exception.WxErrorException; @@ -269,7 +270,30 @@ public interface WxService { * @throws WxErrorException */ public WxUserList userList(String next_openid) throws WxErrorException; - + + /** + *
+   * 换取临时二维码ticket
+   * 详情请见: http://mp.weixin.qq.com/wiki/index.php?title=生成带参数的二维码
+   * 
+ * @param scene_id 参数。目前只支持1--100000 + * @param expire_seconds 过期秒数,默认60秒,最小60秒,最大1800秒 + * @return + * @throws WxErrorException + */ + public WxQrCodeTicket qrCodeCreateTmpTicket(int scene_id, Integer expire_seconds) throws WxErrorException; + + /** + *
+   * 换取永久二维码ticket
+   * 详情请见: http://mp.weixin.qq.com/wiki/index.php?title=生成带参数的二维码
+   * 
+ * @param scene_id 参数。目前只支持1--100000 + * @return + * @throws WxErrorException + */ + public WxQrCodeTicket qrCodeCreateLastTicket(int scene_id) throws WxErrorException; + /** * 注入 {@link WxConfigStorage} 的实现 * @param wxConfigProvider diff --git a/src/main/java/chanjarster/weixin/api/WxServiceImpl.java b/src/main/java/chanjarster/weixin/api/WxServiceImpl.java index 7f4d8916..e175ad3d 100644 --- a/src/main/java/chanjarster/weixin/api/WxServiceImpl.java +++ b/src/main/java/chanjarster/weixin/api/WxServiceImpl.java @@ -31,6 +31,7 @@ import chanjarster.weixin.bean.result.WxError; import chanjarster.weixin.bean.result.WxMassSendResult; import chanjarster.weixin.bean.result.WxMassUploadResult; import chanjarster.weixin.bean.result.WxMediaUploadResult; +import chanjarster.weixin.bean.result.WxQrCodeTicket; import chanjarster.weixin.bean.result.WxUser; import chanjarster.weixin.bean.result.WxUserList; import chanjarster.weixin.exception.WxErrorException; @@ -263,6 +264,35 @@ public class WxServiceImpl implements WxService { return WxUserList.fromJson(responseContent); } + public WxQrCodeTicket qrCodeCreateTmpTicket(int scene_id, Integer expire_seconds) throws WxErrorException { + String url = "https://api.weixin.qq.com/cgi-bin/qrcode/create"; + JsonObject json = new JsonObject(); + json.addProperty("action_name", "QR_SCENE"); + if(expire_seconds != null) { + json.addProperty("expire_seconds", expire_seconds); + } + JsonObject actionInfo = new JsonObject(); + JsonObject scene = new JsonObject(); + scene.addProperty("scene_id", scene_id); + actionInfo.add("scene", scene); + json.add("action_info", actionInfo); + String responseContent = execute(new SimplePostRequestExecutor(), url, json.toString()); + return WxQrCodeTicket.fromJson(responseContent); + } + + public WxQrCodeTicket qrCodeCreateLastTicket(int scene_id) throws WxErrorException { + String url = "https://api.weixin.qq.com/cgi-bin/qrcode/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); + actionInfo.add("scene", scene); + json.add("action_info", actionInfo); + String responseContent = execute(new SimplePostRequestExecutor(), url, json.toString()); + return WxQrCodeTicket.fromJson(responseContent); + } + /** * 向微信端发送请求,在这里执行的策略是当发生access_token过期时才去刷新,然后重新执行请求,而不是全局定时请求 * @param executor diff --git a/src/main/java/chanjarster/weixin/bean/result/WxQrCodeTicket.java b/src/main/java/chanjarster/weixin/bean/result/WxQrCodeTicket.java new file mode 100644 index 00000000..28ee95ba --- /dev/null +++ b/src/main/java/chanjarster/weixin/bean/result/WxQrCodeTicket.java @@ -0,0 +1,46 @@ +package chanjarster.weixin.bean.result; + +import chanjarster.weixin.util.json.WxGsonBuilder; + +/** + * 换取二维码的Ticket + * + * @author chanjarster + */ +public class WxQrCodeTicket { + + protected String ticket; + protected int expire_seconds = -1; + protected String url; + + public String getTicket() { + return ticket; + } + + public void setTicket(String ticket) { + this.ticket = ticket; + } + + /** + * 如果返回-1说明是永久 + */ + public int getExpire_seconds() { + return expire_seconds; + } + + public void setExpire_seconds(int expire_seconds) { + this.expire_seconds = expire_seconds; + } + + public String getUrl() { + return url; + } + + public void setUrl(String url) { + this.url = url; + } + + public static WxQrCodeTicket fromJson(String json) { + return WxGsonBuilder.INSTANCE.create().fromJson(json, WxQrCodeTicket.class); + } +} diff --git a/src/test/java/chanjarster/weixin/api/WxQrCodeAPITest.java b/src/test/java/chanjarster/weixin/api/WxQrCodeAPITest.java new file mode 100644 index 00000000..3252443a --- /dev/null +++ b/src/test/java/chanjarster/weixin/api/WxQrCodeAPITest.java @@ -0,0 +1,39 @@ +package chanjarster.weixin.api; + + +import org.testng.Assert; +import org.testng.annotations.Guice; +import org.testng.annotations.Test; + +import chanjarster.weixin.bean.result.WxQrCodeTicket; +import chanjarster.weixin.exception.WxErrorException; + +import com.google.inject.Inject; + +/** + * 测试用户相关的接口 + * @author chanjarster + * + */ +@Test(groups = "qrCodeAPI", dependsOnGroups = { "baseAPI" }) +@Guice(modules = ApiTestModule.class) +public class WxQrCodeAPITest { + + @Inject + protected WxServiceImpl wxService; + + public void testQrCodeCreateTmpTicket() throws WxErrorException { + WxQrCodeTicket ticket = wxService.qrCodeCreateTmpTicket(1, null); + Assert.assertNotNull(ticket.getUrl()); + Assert.assertNotNull(ticket.getTicket()); + Assert.assertTrue(ticket.getExpire_seconds() != -1); + } + + public void testQrCodeCreateLastTicket() throws WxErrorException { + WxQrCodeTicket ticket = wxService.qrCodeCreateLastTicket(1); + Assert.assertNotNull(ticket.getUrl()); + Assert.assertNotNull(ticket.getTicket()); + Assert.assertTrue(ticket.getExpire_seconds() == -1); + } + +} diff --git a/src/test/resources/testng.xml b/src/test/resources/testng.xml index e74f1ad1..5e7e7237 100644 --- a/src/test/resources/testng.xml +++ b/src/test/resources/testng.xml @@ -10,6 +10,7 @@ +