mirror of
				https://gitee.com/binary/weixin-java-tools.git
				synced 2025-10-31 10:38:42 +08:00 
			
		
		
		
	issue #6 长链接转短链接接口
This commit is contained in:
		| @ -305,6 +305,17 @@ public interface WxService { | |||||||
|    */ |    */ | ||||||
|   public File qrCodePicture(WxQrCodeTicket ticket) throws WxErrorException; |   public File qrCodePicture(WxQrCodeTicket ticket) throws WxErrorException; | ||||||
|  |  | ||||||
|  |   /** | ||||||
|  |    * <pre> | ||||||
|  |    * 长链接转短链接接口 | ||||||
|  |    * 详情请见: http://mp.weixin.qq.com/wiki/index.php?title=长链接转短链接接口 | ||||||
|  |    * </pre> | ||||||
|  |    * @param long_url | ||||||
|  |    * @return | ||||||
|  |    * @throws WxErrorException | ||||||
|  |    */ | ||||||
|  |   public String shortUrl(String long_url) throws WxErrorException; | ||||||
|  |    | ||||||
|   /** |   /** | ||||||
|    * 注入 {@link WxConfigStorage} 的实现 |    * 注入 {@link WxConfigStorage} 的实现 | ||||||
|    * @param wxConfigProvider |    * @param wxConfigProvider | ||||||
|  | |||||||
| @ -5,7 +5,6 @@ import java.io.IOException; | |||||||
| import java.io.InputStream; | import java.io.InputStream; | ||||||
| import java.io.StringReader; | import java.io.StringReader; | ||||||
| import java.security.MessageDigest; | import java.security.MessageDigest; | ||||||
| import java.text.MessageFormat; |  | ||||||
| import java.util.Arrays; | import java.util.Arrays; | ||||||
| import java.util.List; | import java.util.List; | ||||||
| import java.util.UUID; | import java.util.UUID; | ||||||
| @ -226,7 +225,9 @@ public class WxServiceImpl implements WxService { | |||||||
|    |    | ||||||
|   public long groupQueryUserGroup(String openid) throws WxErrorException { |   public long groupQueryUserGroup(String openid) throws WxErrorException { | ||||||
|     String url = "https://api.weixin.qq.com/cgi-bin/groups/getid"; |     String url = "https://api.weixin.qq.com/cgi-bin/groups/getid"; | ||||||
|     String responseContent = execute(new SimplePostRequestExecutor(), url, MessageFormat.format("'{'\"openid\":\"{0}\"}", openid)); |     JsonObject o = new JsonObject(); | ||||||
|  |     o.addProperty("openid", openid); | ||||||
|  |     String responseContent = execute(new SimplePostRequestExecutor(), url, o.toString()); | ||||||
|     JsonElement tmpJsonElement = Streams.parse(new JsonReader(new StringReader(responseContent))); |     JsonElement tmpJsonElement = Streams.parse(new JsonReader(new StringReader(responseContent))); | ||||||
|     return GsonHelper.getAsLong(tmpJsonElement.getAsJsonObject().get("groupid")); |     return GsonHelper.getAsLong(tmpJsonElement.getAsJsonObject().get("groupid")); | ||||||
|   } |   } | ||||||
| @ -299,6 +300,16 @@ public class WxServiceImpl implements WxService { | |||||||
|     return execute(new QrCodeRequestExecutor(), url, ticket); |     return execute(new QrCodeRequestExecutor(), url, ticket); | ||||||
|   } |   } | ||||||
|    |    | ||||||
|  |   public String shortUrl(String long_url) throws WxErrorException { | ||||||
|  |     String url = "https://api.weixin.qq.com/cgi-bin/shorturl"; | ||||||
|  |     JsonObject o = new JsonObject(); | ||||||
|  |     o.addProperty("action", "long2short"); | ||||||
|  |     o.addProperty("long_url", long_url); | ||||||
|  |     String responseContent = execute(new SimplePostRequestExecutor(), url, o.toString()); | ||||||
|  |     JsonElement tmpJsonElement = Streams.parse(new JsonReader(new StringReader(responseContent))); | ||||||
|  |     return tmpJsonElement.getAsJsonObject().get("short_url").getAsString(); | ||||||
|  |   } | ||||||
|  |    | ||||||
|   /** |   /** | ||||||
|    * 向微信端发送请求,在这里执行的策略是当发生access_token过期时才去刷新,然后重新执行请求,而不是全局定时请求 |    * 向微信端发送请求,在这里执行的策略是当发生access_token过期时才去刷新,然后重新执行请求,而不是全局定时请求 | ||||||
|    * @param executor |    * @param executor | ||||||
|  | |||||||
							
								
								
									
										28
									
								
								src/test/java/chanjarster/weixin/api/WxShortUrlAPITest.java
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										28
									
								
								src/test/java/chanjarster/weixin/api/WxShortUrlAPITest.java
									
									
									
									
									
										Normal file
									
								
							| @ -0,0 +1,28 @@ | |||||||
|  | package chanjarster.weixin.api; | ||||||
|  |  | ||||||
|  | import org.testng.Assert; | ||||||
|  | import org.testng.annotations.Guice; | ||||||
|  | import org.testng.annotations.Test; | ||||||
|  |  | ||||||
|  | import chanjarster.weixin.exception.WxErrorException; | ||||||
|  |  | ||||||
|  | import com.google.inject.Inject; | ||||||
|  |  | ||||||
|  | /** | ||||||
|  |  * 测试用户相关的接口 | ||||||
|  |  *  | ||||||
|  |  * @author chanjarster | ||||||
|  |  */ | ||||||
|  | @Test(groups = "shortURLAPI", dependsOnGroups = { "baseAPI" }) | ||||||
|  | @Guice(modules = ApiTestModule.class) | ||||||
|  | public class WxShortUrlAPITest { | ||||||
|  |  | ||||||
|  |   @Inject | ||||||
|  |   protected WxServiceImpl wxService; | ||||||
|  |  | ||||||
|  |   public void testShortUrl() throws WxErrorException { | ||||||
|  |     String shortUrl = wxService.shortUrl("www.baidu.com"); | ||||||
|  |     Assert.assertNotNull(shortUrl); | ||||||
|  |   } | ||||||
|  |  | ||||||
|  | } | ||||||
| @ -11,6 +11,8 @@ | |||||||
| 			<class name="chanjarster.weixin.api.WxMediaAPITest" /> | 			<class name="chanjarster.weixin.api.WxMediaAPITest" /> | ||||||
| 			<class name="chanjarster.weixin.api.WxMassMessageAPITest" /> | 			<class name="chanjarster.weixin.api.WxMassMessageAPITest" /> | ||||||
|       <class name="chanjarster.weixin.api.WxUserAPITest" /> |       <class name="chanjarster.weixin.api.WxUserAPITest" /> | ||||||
|  |       <class name="chanjarster.weixin.api.WxQrCodeAPITest" /> | ||||||
|  |       <class name="chanjarster.weixin.api.WxShortUrlAPITest" /> | ||||||
| 		</classes> | 		</classes> | ||||||
| 	</test> | 	</test> | ||||||
|  |  | ||||||
|  | |||||||
		Reference in New Issue
	
	Block a user
	 Daniel Qian
					Daniel Qian