生成带参数的二维码时加入场景值的校验 #106

This commit is contained in:
Binary Wang 2016-12-23 14:14:01 +08:00
parent 637fc87c0d
commit c70bf9b0c3
3 changed files with 37 additions and 22 deletions

View File

@ -18,10 +18,10 @@ public interface WxMpQrcodeService {
* 详情请见: <a href="http://mp.weixin.qq.com/wiki/18/167e7d94df85d8389df6c94a7a8f78ba.html">生成带参数的二维码</a>
* </pre>
*
* @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;
/**
* <pre>
@ -29,9 +29,9 @@ public interface WxMpQrcodeService {
* 详情请见: <a href="http://mp.weixin.qq.com/wiki/18/167e7d94df85d8389df6c94a7a8f78ba.html">生成带参数的二维码</a>
* </pre>
*
* @param scene_id 参数永久二维码时最大值为100000目前参数只支持1--100000
* @param sceneId 参数永久二维码时最大值为100000目前参数只支持1--100000
*/
WxMpQrCodeTicket qrCodeCreateLastTicket(int scene_id) throws WxErrorException;
WxMpQrCodeTicket qrCodeCreateLastTicket(int sceneId) throws WxErrorException;
/**
* <pre>
@ -39,9 +39,9 @@ public interface WxMpQrcodeService {
* 详情请见: <a href="http://mp.weixin.qq.com/wiki/18/167e7d94df85d8389df6c94a7a8f78ba.html">生成带参数的二维码</a>
* </pre>
*
* @param scene_str 参数字符串类型长度现在为1到64
* @param sceneStr 参数字符串类型长度现在为1到64
*/
WxMpQrCodeTicket qrCodeCreateLastTicket(String scene_str) throws WxErrorException;
WxMpQrCodeTicket qrCodeCreateLastTicket(String sceneStr) throws WxErrorException;
/**
* <pre>

View File

@ -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());

View File

@ -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);