Selaa lähdekoodia

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

master
Daniel Qian 10 vuotta sitten
vanhempi
commit
37dbee6efc
5 muutettua tiedostoa jossa 141 lisäystä ja 1 poistoa
  1. +25
    -1
      src/main/java/chanjarster/weixin/api/WxService.java
  2. +30
    -0
      src/main/java/chanjarster/weixin/api/WxServiceImpl.java
  3. +46
    -0
      src/main/java/chanjarster/weixin/bean/result/WxQrCodeTicket.java
  4. +39
    -0
      src/test/java/chanjarster/weixin/api/WxQrCodeAPITest.java
  5. +1
    -0
      src/test/resources/testng.xml

+ 25
- 1
src/main/java/chanjarster/weixin/api/WxService.java Näytä tiedosto

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


+ 30
- 0
src/main/java/chanjarster/weixin/api/WxServiceImpl.java Näytä tiedosto

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


+ 46
- 0
src/main/java/chanjarster/weixin/bean/result/WxQrCodeTicket.java Näytä tiedosto

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

+ 39
- 0
src/test/java/chanjarster/weixin/api/WxQrCodeAPITest.java Näytä tiedosto

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

+ 1
- 0
src/test/resources/testng.xml Näytä tiedosto

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



Ladataan…
Peruuta
Tallenna