From 6638ca74272e9ca6f8cc2f0a2cb6b78e7153c953 Mon Sep 17 00:00:00 2001 From: Hyseen Date: Wed, 26 Jul 2017 14:34:57 +0800 Subject: [PATCH] =?UTF-8?q?#289=20=E5=A2=9E=E5=8A=A0=E4=B8=B4=E6=97=B6?= =?UTF-8?q?=E4=BA=8C=E7=BB=B4=E7=A0=81=E7=9A=84=E5=9C=BA=E6=99=AF=E5=80=BC?= =?UTF-8?q?=E6=94=AF=E6=8C=81=E5=AD=97=E7=AC=A6=E4=B8=B2=E7=9A=84=E6=8E=A5?= =?UTF-8?q?=E5=8F=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * 更新接口:临时二维码的场景值支持字符串 * 新增临时二维码的场景值为字符串的单元测试 --- .../weixin/mp/api/WxMpQrcodeService.java | 12 +++++++ .../mp/api/impl/WxMpQrcodeServiceImpl.java | 33 +++++++++++++++++++ .../api/impl/WxMpQrcodeServiceImplTest.java | 16 +++++++++ 3 files changed, 61 insertions(+) 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 e2e17eb359..37ed7dcd12 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 @@ -26,6 +26,18 @@ public interface WxMpQrcodeService { */ WxMpQrCodeTicket qrCodeCreateTmpTicket(int sceneId, Integer expireSeconds) throws WxErrorException; + + /** + *
+   * 换取临时二维码ticket
+   * 详情请见: 生成带参数的二维码
+   * 
+ * + * @param sceneStr 场景值ID(字符串形式的ID),字符串类型,长度限制为1到64 + * @param expireSeconds 该二维码有效时间,以秒为单位。 最大不超过2592000(即30天),此字段如果不填,则默认有效期为30秒。 + */ + WxMpQrCodeTicket qrCodeCreateTmpTicket(String sceneStr, Integer expireSeconds) throws WxErrorException; + /** *
    * 换取永久二维码ticket
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 817a0b68f2..5f6c3ad54f 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
@@ -7,6 +7,7 @@
 import me.chanjar.weixin.mp.api.WxMpService;
 import me.chanjar.weixin.mp.bean.result.WxMpQrCodeTicket;
 import me.chanjar.weixin.mp.util.http.QrCodeRequestExecutor;
+import org.apache.commons.lang3.StringUtils;
 
 import java.io.File;
 import java.io.UnsupportedEncodingException;
@@ -54,6 +55,38 @@ public WxMpQrCodeTicket qrCodeCreateTmpTicket(int sceneId, Integer expireSeconds
     return WxMpQrCodeTicket.fromJson(responseContent);
   }
 
+
+  @Override
+  public WxMpQrCodeTicket qrCodeCreateTmpTicket(String sceneStr, Integer expireSeconds) throws WxErrorException {
+    if (StringUtils.isBlank(sceneStr)) {
+      throw new WxErrorException(WxError.newBuilder().setErrorCode(-1).setErrorMsg("临时二维码场景值不能为空!").build());
+    }
+
+    //expireSeconds 该二维码有效时间,以秒为单位。 最大不超过2592000(即30天),此字段如果不填,则默认有效期为30秒。
+    if (expireSeconds != null && expireSeconds > 2592000) {
+      throw new WxErrorException(WxError.newBuilder().setErrorCode(-1)
+        .setErrorMsg("临时二维码有效时间最大不能超过2592000(即30天)!").build());
+    }
+
+    if (expireSeconds == null) {
+      expireSeconds = 30;
+    }
+
+    String url = API_URL_PREFIX + "/create";
+    JsonObject json = new JsonObject();
+    json.addProperty("action_name", "QR_STR_SCENE");
+    json.addProperty("expire_seconds", expireSeconds);
+
+    JsonObject actionInfo = new JsonObject();
+    JsonObject scene = new JsonObject();
+    scene.addProperty("scene_str", sceneStr);
+    actionInfo.add("scene", scene);
+    json.add("action_info", actionInfo);
+    String responseContent = this.wxMpService.post(url, json.toString());
+    return WxMpQrCodeTicket.fromJson(responseContent);
+  }
+
+
   @Override
   public WxMpQrCodeTicket qrCodeCreateLastTicket(int sceneId) throws WxErrorException {
     if (sceneId < 1 || sceneId > 100000) {
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 c61e38339c..216eec77f0 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
@@ -5,6 +5,7 @@
 import me.chanjar.weixin.mp.api.WxMpService;
 import me.chanjar.weixin.mp.api.test.ApiTestModule;
 import me.chanjar.weixin.mp.bean.result.WxMpQrCodeTicket;
+import org.apache.commons.lang3.RandomStringUtils;
 import org.testng.*;
 import org.testng.annotations.*;
 
@@ -26,6 +27,11 @@ public Object[][] sceneIds() {
     return new Object[][]{{-1}, {0}, {1}, {200000}};
   }
 
+  @DataProvider
+  public Object[][] sceneStrs() {
+    return new Object[][]{{null}, {""}, {"test"}, {RandomStringUtils.randomAlphanumeric(100)}};
+  }
+
   @Test(dataProvider = "sceneIds")
   public void testQrCodeCreateTmpTicket(int sceneId) throws WxErrorException {
     WxMpQrCodeTicket ticket = this.wxService.getQrcodeService().qrCodeCreateTmpTicket(sceneId, null);
@@ -35,6 +41,16 @@ public void testQrCodeCreateTmpTicket(int sceneId) throws WxErrorException {
     System.out.println(ticket);
   }
 
+
+  @Test(dataProvider = "sceneStrs")
+  public void testQrCodeCreateTmpTicketWithSceneStr(String sceneStr) throws WxErrorException {
+    WxMpQrCodeTicket ticket = this.wxService.getQrcodeService().qrCodeCreateTmpTicket(sceneStr, null);
+    Assert.assertNotNull(ticket.getUrl());
+    Assert.assertNotNull(ticket.getTicket());
+    Assert.assertTrue(ticket.getExpire_seconds() != -1);
+    System.out.println(ticket);
+  }
+
   @Test(dataProvider = "sceneIds")
   public void testQrCodeCreateLastTicket(int sceneId) throws WxErrorException {
     WxMpQrCodeTicket ticket = this.wxService.getQrcodeService().qrCodeCreateLastTicket(sceneId);