issue #5 生成带参数的二维码 - ticket

This commit is contained in:
Daniel Qian 2014-08-26 14:41:07 +08:00
parent 4bbb729ac7
commit 37dbee6efc
5 changed files with 141 additions and 1 deletions

View File

@ -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;
/**
* <pre>
* 换取临时二维码ticket
* 详情请见: http://mp.weixin.qq.com/wiki/index.php?title=生成带参数的二维码
* </pre>
* @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;
/**
* <pre>
* 换取永久二维码ticket
* 详情请见: http://mp.weixin.qq.com/wiki/index.php?title=生成带参数的二维码
* </pre>
* @param scene_id 参数目前只支持1--100000
* @return
* @throws WxErrorException
*/
public WxQrCodeTicket qrCodeCreateLastTicket(int scene_id) throws WxErrorException;
/**
* 注入 {@link WxConfigStorage} 的实现
* @param wxConfigProvider

View File

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

View File

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

View File

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

View File

@ -10,6 +10,7 @@
<class name="chanjarster.weixin.api.WxMassMessageAPITest" />
<class name="chanjarster.weixin.api.WxMediaAPITest" />
<class name="chanjarster.weixin.api.WxMassMessageAPITest" />
<class name="chanjarster.weixin.api.WxUserAPITest" />
</classes>
</test>