mirror of
https://gitee.com/binary/weixin-java-tools.git
synced 2025-04-24 18:04:38 +08:00
parent
b9c9b844c8
commit
30b5a9aa8f
@ -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
|
||||
|
@ -7,6 +7,7 @@ import me.chanjar.weixin.mp.api.WxMpQrcodeService;
|
||||
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 class WxMpQrcodeServiceImpl implements WxMpQrcodeService {
|
||||
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) {
|
||||
|
@ -5,6 +5,7 @@ import me.chanjar.weixin.common.exception.WxErrorException;
|
||||
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 class WxMpQrcodeServiceImplTest {
|
||||
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 class WxMpQrcodeServiceImplTest {
|
||||
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);
|
||||
|
Loading…
Reference in New Issue
Block a user