From b2dfa5d48c4198284a526aa6a98aa690be282ba6 Mon Sep 17 00:00:00 2001 From: Daniel Qian Date: Tue, 26 Aug 2014 14:53:55 +0800 Subject: [PATCH] =?UTF-8?q?issue=20#5=20=E7=94=9F=E6=88=90=E5=B8=A6?= =?UTF-8?q?=E5=8F=82=E6=95=B0=E7=9A=84=E4=BA=8C=E7=BB=B4=E7=A0=81=20-=20sh?= =?UTF-8?q?owcode?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../chanjarster/weixin/api/WxService.java | 11 ++++ .../chanjarster/weixin/api/WxServiceImpl.java | 10 +++- .../util/fs/{FileUtil.java => FileUtils.java} | 2 +- .../http/MediaDownloadRequestExecutor.java | 6 +- .../util/http/QrCodeRequestExecutor.java | 56 +++++++++++++++++++ .../weixin/api/WxQrCodeAPITest.java | 19 +++++-- 6 files changed, 92 insertions(+), 12 deletions(-) rename src/main/java/chanjarster/weixin/util/fs/{FileUtil.java => FileUtils.java} (97%) create mode 100644 src/main/java/chanjarster/weixin/util/http/QrCodeRequestExecutor.java diff --git a/src/main/java/chanjarster/weixin/api/WxService.java b/src/main/java/chanjarster/weixin/api/WxService.java index b9d85249..f298d7fa 100644 --- a/src/main/java/chanjarster/weixin/api/WxService.java +++ b/src/main/java/chanjarster/weixin/api/WxService.java @@ -294,6 +294,17 @@ public interface WxService { */ public WxQrCodeTicket qrCodeCreateLastTicket(int scene_id) throws WxErrorException; + /** + *
+   * 换取二维码图片
+   * 详情请见: http://mp.weixin.qq.com/wiki/index.php?title=生成带参数的二维码
+   * 
+ * @param ticket 二维码ticket + * @return + * @throws WxErrorException + */ + public File qrCodePicture(WxQrCodeTicket ticket) throws WxErrorException; + /** * 注入 {@link WxConfigStorage} 的实现 * @param wxConfigProvider diff --git a/src/main/java/chanjarster/weixin/api/WxServiceImpl.java b/src/main/java/chanjarster/weixin/api/WxServiceImpl.java index e175ad3d..ec9838e9 100644 --- a/src/main/java/chanjarster/weixin/api/WxServiceImpl.java +++ b/src/main/java/chanjarster/weixin/api/WxServiceImpl.java @@ -35,9 +35,10 @@ import chanjarster.weixin.bean.result.WxQrCodeTicket; import chanjarster.weixin.bean.result.WxUser; import chanjarster.weixin.bean.result.WxUserList; import chanjarster.weixin.exception.WxErrorException; -import chanjarster.weixin.util.fs.FileUtil; +import chanjarster.weixin.util.fs.FileUtils; import chanjarster.weixin.util.http.MediaDownloadRequestExecutor; import chanjarster.weixin.util.http.MediaUploadRequestExecutor; +import chanjarster.weixin.util.http.QrCodeRequestExecutor; import chanjarster.weixin.util.http.RequestExecutor; import chanjarster.weixin.util.http.SimpleGetRequestExecutor; import chanjarster.weixin.util.http.SimplePostRequestExecutor; @@ -161,7 +162,7 @@ public class WxServiceImpl implements WxService { } public WxMediaUploadResult mediaUpload(String mediaType, String fileType, InputStream inputStream) throws WxErrorException, IOException { - return mediaUpload(mediaType,FileUtil.createTmpFile(inputStream, UUID.randomUUID().toString(), fileType)); + return mediaUpload(mediaType,FileUtils.createTmpFile(inputStream, UUID.randomUUID().toString(), fileType)); } public WxMediaUploadResult mediaUpload(String mediaType, File file) throws WxErrorException { @@ -293,6 +294,11 @@ public class WxServiceImpl implements WxService { return WxQrCodeTicket.fromJson(responseContent); } + public File qrCodePicture(WxQrCodeTicket ticket) throws WxErrorException { + String url = "https://mp.weixin.qq.com/cgi-bin/showqrcode"; + return execute(new QrCodeRequestExecutor(), url, ticket); + } + /** * 向微信端发送请求,在这里执行的策略是当发生access_token过期时才去刷新,然后重新执行请求,而不是全局定时请求 * @param executor diff --git a/src/main/java/chanjarster/weixin/util/fs/FileUtil.java b/src/main/java/chanjarster/weixin/util/fs/FileUtils.java similarity index 97% rename from src/main/java/chanjarster/weixin/util/fs/FileUtil.java rename to src/main/java/chanjarster/weixin/util/fs/FileUtils.java index 3faeb4dd..85386b60 100644 --- a/src/main/java/chanjarster/weixin/util/fs/FileUtil.java +++ b/src/main/java/chanjarster/weixin/util/fs/FileUtils.java @@ -5,7 +5,7 @@ import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; -public class FileUtil { +public class FileUtils { /** * 创建临时文件 diff --git a/src/main/java/chanjarster/weixin/util/http/MediaDownloadRequestExecutor.java b/src/main/java/chanjarster/weixin/util/http/MediaDownloadRequestExecutor.java index 46e563d5..0433b783 100644 --- a/src/main/java/chanjarster/weixin/util/http/MediaDownloadRequestExecutor.java +++ b/src/main/java/chanjarster/weixin/util/http/MediaDownloadRequestExecutor.java @@ -15,10 +15,10 @@ import org.apache.http.entity.ContentType; import chanjarster.weixin.bean.result.WxError; import chanjarster.weixin.exception.WxErrorException; -import chanjarster.weixin.util.fs.FileUtil; +import chanjarster.weixin.util.fs.FileUtils; /** - * 下载媒体文件请求执行器,请求的参数是String, 返回的结果是String + * 下载媒体文件请求执行器,请求的参数是String, 返回的结果是File * @author chanjarster * */ @@ -52,7 +52,7 @@ public class MediaDownloadRequestExecutor implements RequestExecutor { + + @Override + public File execute(String uri, WxQrCodeTicket ticket) throws WxErrorException, ClientProtocolException, IOException { + if (ticket != null) { + if (uri.indexOf('?') == -1) { + uri += '?'; + } + uri += uri.endsWith("?") ? + "ticket=" + URLEncoder.encode(ticket.getTicket(), "UTF-8") + : + "&ticket=" + URLEncoder.encode(ticket.getTicket(), "UTF-8"); + } + + HttpGet httpGet = new HttpGet(uri); + CloseableHttpResponse response = httpclient.execute(httpGet); + + Header[] contentTypeHeader = response.getHeaders("Content-Type"); + if (contentTypeHeader != null && contentTypeHeader.length > 0) { + // 出错 + if (ContentType.TEXT_PLAIN.getMimeType().equals(contentTypeHeader[0].getValue())) { + String responseContent = Utf8ResponseHandler.INSTANCE.handleResponse(response); + throw new WxErrorException(WxError.fromJson(responseContent)); + } + } + InputStream inputStream = InputStreamResponseHandler.INSTANCE.handleResponse(response); + + File localFile = FileUtils.createTmpFile(inputStream, UUID.randomUUID().toString(), "jpg"); + return localFile; + } + +} diff --git a/src/test/java/chanjarster/weixin/api/WxQrCodeAPITest.java b/src/test/java/chanjarster/weixin/api/WxQrCodeAPITest.java index 3252443a..c95fb863 100644 --- a/src/test/java/chanjarster/weixin/api/WxQrCodeAPITest.java +++ b/src/test/java/chanjarster/weixin/api/WxQrCodeAPITest.java @@ -1,5 +1,6 @@ package chanjarster.weixin.api; +import java.io.File; import org.testng.Assert; import org.testng.annotations.Guice; @@ -12,8 +13,8 @@ import com.google.inject.Inject; /** * 测试用户相关的接口 + * * @author chanjarster - * */ @Test(groups = "qrCodeAPI", dependsOnGroups = { "baseAPI" }) @Guice(modules = ApiTestModule.class) @@ -21,19 +22,25 @@ public class WxQrCodeAPITest { @Inject protected WxServiceImpl wxService; - - public void testQrCodeCreateTmpTicket() throws WxErrorException { + + 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 { + + public void testQrCodeCreateLastTicket() throws WxErrorException { WxQrCodeTicket ticket = wxService.qrCodeCreateLastTicket(1); Assert.assertNotNull(ticket.getUrl()); Assert.assertNotNull(ticket.getTicket()); Assert.assertTrue(ticket.getExpire_seconds() == -1); } - + + public void testQrCodePicture() throws WxErrorException { + WxQrCodeTicket ticket = wxService.qrCodeCreateLastTicket(1); + File file = wxService.qrCodePicture(ticket); + Assert.assertNotNull(file); + } + }