mirror of
				https://gitee.com/binary/weixin-java-tools.git
				synced 2025-11-01 03:25:35 +08:00 
			
		
		
		
	生成带参数的二维码时加入场景值的校验 #106
This commit is contained in:
		| @ -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> | ||||
|  | ||||
| @ -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()); | ||||
| @ -81,7 +89,7 @@ public class WxMpQrcodeServiceImpl implements WxMpQrcodeService { | ||||
|     String url = "https://mp.weixin.qq.com/cgi-bin/showqrcode?ticket=%s"; | ||||
|     try { | ||||
|       String resultUrl = String.format(url, | ||||
|               URLEncoder.encode(ticket, StandardCharsets.UTF_8.name())); | ||||
|         URLEncoder.encode(ticket, StandardCharsets.UTF_8.name())); | ||||
|       if (needShortUrl) { | ||||
|         return this.wxMpService.shortUrl(resultUrl); | ||||
|       } | ||||
| @ -89,7 +97,7 @@ public class WxMpQrcodeServiceImpl implements WxMpQrcodeService { | ||||
|       return resultUrl; | ||||
|     } catch (UnsupportedEncodingException e) { | ||||
|       WxError error = WxError.newBuilder().setErrorCode(-1) | ||||
|               .setErrorMsg(e.getMessage()).build(); | ||||
|         .setErrorMsg(e.getMessage()).build(); | ||||
|       throw new WxErrorException(error); | ||||
|     } | ||||
|   } | ||||
|  | ||||
| @ -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); | ||||
|  | ||||
		Reference in New Issue
	
	Block a user
	 Binary Wang
					Binary Wang