Skip to content

Commit

Permalink
#289 增加临时二维码的场景值支持字符串的接口
Browse files Browse the repository at this point in the history
* 更新接口:临时二维码的场景值支持字符串

* 新增临时二维码的场景值为字符串的单元测试
  • Loading branch information
Hyseen authored and binarywang committed Sep 2, 2017
1 parent 1a9b4a3 commit 6638ca7
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,18 @@ public interface WxMpQrcodeService {
*/
WxMpQrCodeTicket qrCodeCreateTmpTicket(int sceneId, Integer expireSeconds) throws WxErrorException;


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

/**
* <pre>
* 换取永久二维码ticket
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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.*;

Expand All @@ -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);
Expand All @@ -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);
Expand Down

0 comments on commit 6638ca7

Please sign in to comment.