diff --git a/weixin-java-common/src/main/java/me/chanjar/weixin/common/util/http/RequestExecutor.java b/weixin-java-common/src/main/java/me/chanjar/weixin/common/util/http/RequestExecutor.java index a5a9423f..b9cf5fe2 100644 --- a/weixin-java-common/src/main/java/me/chanjar/weixin/common/util/http/RequestExecutor.java +++ b/weixin-java-common/src/main/java/me/chanjar/weixin/common/util/http/RequestExecutor.java @@ -1,11 +1,11 @@ package me.chanjar.weixin.common.util.http; -import me.chanjar.weixin.common.error.WxErrorException; - import java.io.IOException; +import me.chanjar.weixin.common.error.WxErrorException; + /** - * http请求执行器 + * http请求执行器. * * @param 返回值类型 * @param 请求参数类型 @@ -14,6 +14,8 @@ import java.io.IOException; public interface RequestExecutor { /** + * 执行http请求. + * * @param uri uri * @param data 数据 */ diff --git a/weixin-java-common/src/main/java/me/chanjar/weixin/common/util/http/apache/InputStreamResponseHandler.java b/weixin-java-common/src/main/java/me/chanjar/weixin/common/util/http/apache/InputStreamResponseHandler.java index d4cea91d..5c72744c 100644 --- a/weixin-java-common/src/main/java/me/chanjar/weixin/common/util/http/apache/InputStreamResponseHandler.java +++ b/weixin-java-common/src/main/java/me/chanjar/weixin/common/util/http/apache/InputStreamResponseHandler.java @@ -1,5 +1,8 @@ package me.chanjar.weixin.common.util.http.apache; +import java.io.IOException; +import java.io.InputStream; + import org.apache.http.HttpEntity; import org.apache.http.HttpResponse; import org.apache.http.StatusLine; @@ -7,18 +10,20 @@ import org.apache.http.client.HttpResponseException; import org.apache.http.client.ResponseHandler; import org.apache.http.util.EntityUtils; -import java.io.IOException; -import java.io.InputStream; - +/** + * 输入流响应处理器. + * + * @author Daniel Qian + */ public class InputStreamResponseHandler implements ResponseHandler { - public static final ResponseHandler INSTANCE = new InputStreamResponseHandler(); + private static final int STATUS_CODE_300 = 300; @Override public InputStream handleResponse(final HttpResponse response) throws IOException { final StatusLine statusLine = response.getStatusLine(); final HttpEntity entity = response.getEntity(); - if (statusLine.getStatusCode() >= 300) { + if (statusLine.getStatusCode() >= STATUS_CODE_300) { EntityUtils.consume(entity); throw new HttpResponseException(statusLine.getStatusCode(), statusLine.getReasonPhrase()); } diff --git a/weixin-java-miniapp/src/main/java/cn/binarywang/wx/miniapp/api/WxMaQrcodeService.java b/weixin-java-miniapp/src/main/java/cn/binarywang/wx/miniapp/api/WxMaQrcodeService.java index 29037374..f2c9e8f1 100644 --- a/weixin-java-miniapp/src/main/java/cn/binarywang/wx/miniapp/api/WxMaQrcodeService.java +++ b/weixin-java-miniapp/src/main/java/cn/binarywang/wx/miniapp/api/WxMaQrcodeService.java @@ -1,10 +1,10 @@ package cn.binarywang.wx.miniapp.api; +import java.io.File; + import cn.binarywang.wx.miniapp.bean.WxMaCodeLineColor; import me.chanjar.weixin.common.error.WxErrorException; -import java.io.File; - /** *
  * 二维码相关操作接口.
@@ -32,24 +32,87 @@ public interface WxMaQrcodeService {
    *
    * @param path  不能为空,最大长度 128 字节
    * @param width 默认430 二维码的宽度
+   * @return 文件内容字节数组
+   * @throws WxErrorException 异常
+   */
+  byte[] createQrcodeBytes(String path, int width) throws WxErrorException;
+
+  /**
+   * 接口C: 获取小程序页面二维码.
+   * 
+   * 适用于需要的码数量较少的业务场景
+   * 通过该接口,仅能生成已发布的小程序的二维码。
+   * 可以在开发者工具预览时生成开发版的带参二维码。
+   * 带参二维码只有 100000 个,请谨慎调用。
+   * 
+ * + * @param path 不能为空,最大长度 128 字节 + * @param width 默认430 二维码的宽度 + * @return 文件对象 + * @throws WxErrorException 异常 */ File createQrcode(String path, int width) throws WxErrorException; + /** + * 接口C: 获取小程序页面二维码. + *
+   * 适用于需要的码数量较少的业务场景
+   * 通过该接口,仅能生成已发布的小程序的二维码。
+   * 可以在开发者工具预览时生成开发版的带参二维码。
+   * 带参二维码只有 100000 个,请谨慎调用。
+   * 
+ * + * @param path 不能为空,最大长度 128 字节 + * @return 文件对象 + * @throws WxErrorException 异常 + */ File createQrcode(String path) throws WxErrorException; /** * 接口A: 获取小程序码. * - * @param path 不能为空,最大长度 128 字节 - * @param width 默认430 二维码的宽度 - * @param autoColor 默认true 自动配置线条颜色,如果颜色依然是黑色,则说明不建议配置主色调 - * @param lineColor auth_color 为 false 时生效,使用 rgb 设置颜色 例如 {"r":"xxx","g":"xxx","b":"xxx"} - * @param is_hyaline 是否需要透明底色, is_hyaline 为true时,生成透明底色的小程序码 + * @param path 不能为空,最大长度 128 字节 + * @param width 默认430 二维码的宽度 + * @param autoColor 默认true 自动配置线条颜色,如果颜色依然是黑色,则说明不建议配置主色调 + * @param lineColor auth_color 为 false 时生效,使用 rgb 设置颜色 例如 {"r":"xxx","g":"xxx","b":"xxx"} + * @param isHyaline 是否需要透明底色, isHyaline 为true时,生成透明底色的小程序码 + * @return 文件内容字节数组 + * @throws WxErrorException 异常 + */ + byte[] createWxaCodeBytes(String path, int width, boolean autoColor, WxMaCodeLineColor lineColor, boolean isHyaline) + throws WxErrorException; + + /** + * 接口A: 获取小程序码. + * + * @param path 不能为空,最大长度 128 字节 + * @param width 默认430 二维码的宽度 + * @param autoColor 默认true 自动配置线条颜色,如果颜色依然是黑色,则说明不建议配置主色调 + * @param lineColor auth_color 为 false 时生效,使用 rgb 设置颜色 例如 {"r":"xxx","g":"xxx","b":"xxx"} + * @param isHyaline 是否需要透明底色, isHyaline 为true时,生成透明底色的小程序码 + * @return 文件对象 + * @throws WxErrorException 异常 */ - File createWxaCode(String path, int width, boolean autoColor, WxMaCodeLineColor lineColor, boolean is_hyaline) throws WxErrorException; + File createWxaCode(String path, int width, boolean autoColor, WxMaCodeLineColor lineColor, boolean isHyaline) + throws WxErrorException; + /** + * 接口A: 获取小程序码. + * + * @param path 不能为空,最大长度 128 字节 + * @param width 默认430 二维码的宽度 + * @return 文件对象 + * @throws WxErrorException 异常 + */ File createWxaCode(String path, int width) throws WxErrorException; + /** + * 接口A: 获取小程序码. + * + * @param path 不能为空,最大长度 128 字节 + * @return 文件对象 + * @throws WxErrorException 异常 + */ File createWxaCode(String path) throws WxErrorException; /** @@ -61,15 +124,56 @@ public interface WxMaQrcodeService { * 调试阶段可以使用开发工具的条件编译自定义参数 scene=xxxx 进行模拟,开发工具模拟时的 scene 的参数值需要进行 urlencode *
* - * @param scene 最大32个可见字符,只支持数字,大小写英文以及部分特殊字符:!#$&'()*+,/:;=?@-._~,其它字符请自行编码为合法字符(因不支持%,中文无法使用 urlencode 处理,请使用其他编码方式) + * @param scene 最大32个可见字符,只支持数字,大小写英文以及部分特殊字符:!#$&'()*+,/:;=?@-._~, + * 其它字符请自行编码为合法字符(因不支持%,中文无法使用 urlencode 处理,请使用其他编码方式) + * @param page 必须是已经发布的小程序页面,例如 "pages/index/index" ,如果不填写这个字段,默认跳主页面 + * @param width 默认false 自动配置线条颜色,如果颜色依然是黑色,则说明不建议配置主色调 + * @param autoColor 默认true 自动配置线条颜色,如果颜色依然是黑色,则说明不建议配置主色调 + * @param lineColor auth_color 为 false 时生效,使用 rgb 设置颜色 例如 {"r":"xxx","g":"xxx","b":"xxx"} + * @param isHyaline 是否需要透明底色, is_hyaline 为true时,生成透明底色的小程序码 + * @return 文件内容字节数组 + * @throws WxErrorException 异常 + */ + byte[] createWxaCodeUnlimitBytes(String scene, String page, int width, boolean autoColor, + WxMaCodeLineColor lineColor, boolean isHyaline) throws WxErrorException; + + /** + * 接口B: 获取小程序码(永久有效、数量暂无限制). + *
+   * 通过该接口生成的小程序码,永久有效,数量暂无限制。
+   * 用户扫描该码进入小程序后,将统一打开首页,开发者需在对应页面根据获取的码中 scene 字段的值,再做处理逻辑。
+   * 使用如下代码可以获取到二维码中的 scene 字段的值。
+   * 调试阶段可以使用开发工具的条件编译自定义参数 scene=xxxx 进行模拟,开发工具模拟时的 scene 的参数值需要进行 urlencode
+   * 
+ * + * @param scene 最大32个可见字符,只支持数字,大小写英文以及部分特殊字符:!#$&'()*+,/:;=?@-._~, + * 其它字符请自行编码为合法字符(因不支持%,中文无法使用 urlencode 处理,请使用其他编码方式) * @param page 必须是已经发布的小程序页面,例如 "pages/index/index" ,如果不填写这个字段,默认跳主页面 * @param width 默认false 自动配置线条颜色,如果颜色依然是黑色,则说明不建议配置主色调 * @param autoColor 默认true 自动配置线条颜色,如果颜色依然是黑色,则说明不建议配置主色调 * @param lineColor auth_color 为 false 时生效,使用 rgb 设置颜色 例如 {"r":"xxx","g":"xxx","b":"xxx"} * @param isHyaline 是否需要透明底色, is_hyaline 为true时,生成透明底色的小程序码 + * @return 文件对象 + * @throws WxErrorException 异常 */ - File createWxaCodeUnlimit(String scene, String page, int width, boolean autoColor, WxMaCodeLineColor lineColor, boolean isHyaline) throws WxErrorException; + File createWxaCodeUnlimit(String scene, String page, int width, boolean autoColor, + WxMaCodeLineColor lineColor, boolean isHyaline) throws WxErrorException; + /** + * 接口B: 获取小程序码(永久有效、数量暂无限制). + *
+   * 通过该接口生成的小程序码,永久有效,数量暂无限制。
+   * 用户扫描该码进入小程序后,将统一打开首页,开发者需在对应页面根据获取的码中 scene 字段的值,再做处理逻辑。
+   * 使用如下代码可以获取到二维码中的 scene 字段的值。
+   * 调试阶段可以使用开发工具的条件编译自定义参数 scene=xxxx 进行模拟,开发工具模拟时的 scene 的参数值需要进行 urlencode
+   * 
+ * + * @param scene 最大32个可见字符,只支持数字,大小写英文以及部分特殊字符:!#$&'()*+,/:;=?@-._~, + * 其它字符请自行编码为合法字符(因不支持%,中文无法使用 urlencode 处理,请使用其他编码方式) + * @param page 必须是已经发布的小程序页面,例如 "pages/index/index" ,如果不填写这个字段,默认跳主页面 + * @return 文件对象 + * @throws WxErrorException 异常 + */ File createWxaCodeUnlimit(String scene, String page) throws WxErrorException; } diff --git a/weixin-java-miniapp/src/main/java/cn/binarywang/wx/miniapp/api/impl/WxMaQrcodeServiceImpl.java b/weixin-java-miniapp/src/main/java/cn/binarywang/wx/miniapp/api/impl/WxMaQrcodeServiceImpl.java index 277418e7..8e7d6b17 100644 --- a/weixin-java-miniapp/src/main/java/cn/binarywang/wx/miniapp/api/impl/WxMaQrcodeServiceImpl.java +++ b/weixin-java-miniapp/src/main/java/cn/binarywang/wx/miniapp/api/impl/WxMaQrcodeServiceImpl.java @@ -1,16 +1,17 @@ package cn.binarywang.wx.miniapp.api.impl; +import java.io.File; + import cn.binarywang.wx.miniapp.api.WxMaQrcodeService; import cn.binarywang.wx.miniapp.api.WxMaService; import cn.binarywang.wx.miniapp.bean.WxMaCodeLineColor; import cn.binarywang.wx.miniapp.bean.WxMaQrcode; -import cn.binarywang.wx.miniapp.bean.WxMaWxcode; +import cn.binarywang.wx.miniapp.bean.WxaCode; import cn.binarywang.wx.miniapp.bean.WxaCodeUnlimit; -import cn.binarywang.wx.miniapp.util.http.QrCodeRequestExecutor; +import cn.binarywang.wx.miniapp.util.QrcodeBytesRequestExecutor; +import cn.binarywang.wx.miniapp.util.QrcodeRequestExecutor; import me.chanjar.weixin.common.error.WxErrorException; -import java.io.File; - /** * @author Binary Wang */ @@ -21,10 +22,16 @@ public class WxMaQrcodeServiceImpl implements WxMaQrcodeService { this.wxMaService = wxMaService; } + @Override + public byte[] createQrcodeBytes(String path, int width) throws WxErrorException { + final QrcodeBytesRequestExecutor executor = new QrcodeBytesRequestExecutor(this.wxMaService.getRequestHttp()); + return this.wxMaService.execute(executor, CREATE_QRCODE_URL, new WxMaQrcode(path, width)); + } + @Override public File createQrcode(String path, int width) throws WxErrorException { - return this.wxMaService.execute(new QrCodeRequestExecutor(this.wxMaService.getRequestHttp()), - CREATE_QRCODE_URL, new WxMaQrcode(path, width)); + final QrcodeRequestExecutor executor = new QrcodeRequestExecutor(this.wxMaService.getRequestHttp()); + return this.wxMaService.execute(executor, CREATE_QRCODE_URL, new WxMaQrcode(path, width)); } @Override @@ -33,15 +40,29 @@ public class WxMaQrcodeServiceImpl implements WxMaQrcodeService { } @Override - public File createWxaCode(String path, int width, boolean autoColor, WxMaCodeLineColor lineColor, boolean isHyaline) throws WxErrorException { - WxMaWxcode wxMaWxcode = new WxMaWxcode(); - wxMaWxcode.setPath(path); - wxMaWxcode.setWidth(width); - wxMaWxcode.setAutoColor(autoColor); - wxMaWxcode.setLineColor(lineColor); - wxMaWxcode.setHyaline(isHyaline); - return this.wxMaService.execute(new QrCodeRequestExecutor(this.wxMaService.getRequestHttp()), - GET_WXACODE_URL, wxMaWxcode); + public byte[] createWxaCodeBytes(String path, int width, boolean autoColor, WxMaCodeLineColor lineColor, boolean isHyaline) + throws WxErrorException { + final QrcodeBytesRequestExecutor executor = new QrcodeBytesRequestExecutor(this.wxMaService.getRequestHttp()); + return this.wxMaService.execute(executor, GET_WXACODE_URL, WxaCode.builder() + .path(path) + .width(width) + .autoColor(autoColor) + .lineColor(lineColor) + .isHyaline(isHyaline) + .build()); + } + + @Override + public File createWxaCode(String path, int width, boolean autoColor, WxMaCodeLineColor lineColor, boolean isHyaline) + throws WxErrorException { + final QrcodeRequestExecutor executor = new QrcodeRequestExecutor(this.wxMaService.getRequestHttp()); + return this.wxMaService.execute(executor, GET_WXACODE_URL, WxaCode.builder() + .path(path) + .width(width) + .autoColor(autoColor) + .lineColor(lineColor) + .isHyaline(isHyaline) + .build()); } @Override @@ -51,12 +72,27 @@ public class WxMaQrcodeServiceImpl implements WxMaQrcodeService { @Override public File createWxaCode(String path) throws WxErrorException { - return this.createWxaCode(path, 430, true, null, false); + return this.createWxaCode(path, 430); } @Override - public File createWxaCodeUnlimit(String scene, String page, int width, boolean autoColor, WxMaCodeLineColor lineColor, boolean isHyaline) - throws WxErrorException { + public byte[] createWxaCodeUnlimitBytes(String scene, String page, int width, boolean autoColor, + WxMaCodeLineColor lineColor, boolean isHyaline) throws WxErrorException { + return this.wxMaService.execute(new QrcodeBytesRequestExecutor(this.wxMaService.getRequestHttp()), + GET_WXACODE_UNLIMIT_URL, + this.buildWxaCodeUnlimit(scene, page, width, autoColor, lineColor, isHyaline)); + } + + @Override + public File createWxaCodeUnlimit(String scene, String page, int width, boolean autoColor, + WxMaCodeLineColor lineColor, boolean isHyaline) throws WxErrorException { + return this.wxMaService.execute(new QrcodeRequestExecutor(this.wxMaService.getRequestHttp()), + GET_WXACODE_UNLIMIT_URL, + this.buildWxaCodeUnlimit(scene, page, width, autoColor, lineColor, isHyaline)); + } + + private WxaCodeUnlimit buildWxaCodeUnlimit(String scene, String page, int width, boolean autoColor, + WxMaCodeLineColor lineColor, boolean isHyaline) { WxaCodeUnlimit wxaCodeUnlimit = new WxaCodeUnlimit(); wxaCodeUnlimit.setScene(scene); wxaCodeUnlimit.setPage(page); @@ -64,8 +100,8 @@ public class WxMaQrcodeServiceImpl implements WxMaQrcodeService { wxaCodeUnlimit.setAutoColor(autoColor); wxaCodeUnlimit.setLineColor(lineColor); wxaCodeUnlimit.setHyaline(isHyaline); - return this.wxMaService.execute(new QrCodeRequestExecutor(this.wxMaService.getRequestHttp()), - GET_WXACODE_UNLIMIT_URL, wxaCodeUnlimit); + + return wxaCodeUnlimit; } @Override diff --git a/weixin-java-miniapp/src/main/java/cn/binarywang/wx/miniapp/bean/WxMaWxcode.java b/weixin-java-miniapp/src/main/java/cn/binarywang/wx/miniapp/bean/WxaCode.java similarity index 62% rename from weixin-java-miniapp/src/main/java/cn/binarywang/wx/miniapp/bean/WxMaWxcode.java rename to weixin-java-miniapp/src/main/java/cn/binarywang/wx/miniapp/bean/WxaCode.java index e538a1bb..2d94f05c 100644 --- a/weixin-java-miniapp/src/main/java/cn/binarywang/wx/miniapp/bean/WxMaWxcode.java +++ b/weixin-java-miniapp/src/main/java/cn/binarywang/wx/miniapp/bean/WxaCode.java @@ -1,36 +1,48 @@ package cn.binarywang.wx.miniapp.bean; +import java.io.Serializable; + import cn.binarywang.wx.miniapp.util.json.WxMaGsonBuilder; import com.google.gson.annotations.SerializedName; +import lombok.AllArgsConstructor; +import lombok.Builder; import lombok.Data; import lombok.EqualsAndHashCode; - -import java.io.Serializable; +import lombok.NoArgsConstructor; /** + * 小程序码. * * @author Element * @date 2017/7/27 */ @Data @EqualsAndHashCode(callSuper = false) -public class WxMaWxcode extends AbstractWxMaQrcodeWrapper implements Serializable { +@Builder +@NoArgsConstructor +@AllArgsConstructor +public class WxaCode extends AbstractWxMaQrcodeWrapper implements Serializable { private static final long serialVersionUID = 1287399621649210322L; private String path; + + @Builder.Default private int width = 430; @SerializedName("auto_color") + @Builder.Default private boolean autoColor = true; @SerializedName("is_hyaline") + @Builder.Default private boolean isHyaline = false; @SerializedName("line_color") + @Builder.Default private WxMaCodeLineColor lineColor = new WxMaCodeLineColor("0", "0", "0"); - public static WxMaWxcode fromJson(String json) { - return WxMaGsonBuilder.create().fromJson(json, WxMaWxcode.class); + public static WxaCode fromJson(String json) { + return WxMaGsonBuilder.create().fromJson(json, WxaCode.class); } } diff --git a/weixin-java-miniapp/src/main/java/cn/binarywang/wx/miniapp/util/QrcodeBytesRequestExecutor.java b/weixin-java-miniapp/src/main/java/cn/binarywang/wx/miniapp/util/QrcodeBytesRequestExecutor.java new file mode 100644 index 00000000..0d131a75 --- /dev/null +++ b/weixin-java-miniapp/src/main/java/cn/binarywang/wx/miniapp/util/QrcodeBytesRequestExecutor.java @@ -0,0 +1,60 @@ +package cn.binarywang.wx.miniapp.util; + +import java.io.IOException; +import java.io.InputStream; + +import org.apache.commons.io.IOUtils; +import org.apache.http.Header; +import org.apache.http.HttpHost; +import org.apache.http.client.config.RequestConfig; +import org.apache.http.client.methods.CloseableHttpResponse; +import org.apache.http.client.methods.HttpPost; +import org.apache.http.entity.ContentType; +import org.apache.http.entity.StringEntity; +import org.apache.http.impl.client.CloseableHttpClient; + +import cn.binarywang.wx.miniapp.bean.AbstractWxMaQrcodeWrapper; +import me.chanjar.weixin.common.error.WxError; +import me.chanjar.weixin.common.error.WxErrorException; +import me.chanjar.weixin.common.util.http.RequestExecutor; +import me.chanjar.weixin.common.util.http.RequestHttp; +import me.chanjar.weixin.common.util.http.apache.InputStreamResponseHandler; +import me.chanjar.weixin.common.util.http.apache.Utf8ResponseHandler; + +/** + * @author Binary Wang + */ +public class QrcodeBytesRequestExecutor implements RequestExecutor { + protected RequestHttp requestHttp; + + public QrcodeBytesRequestExecutor(RequestHttp requestHttp) { + this.requestHttp = requestHttp; + } + + @Override + public byte[] execute(String uri, AbstractWxMaQrcodeWrapper qrcodeWrapper) throws WxErrorException, IOException { + HttpPost httpPost = new HttpPost(uri); + if (requestHttp.getRequestHttpProxy() != null) { + httpPost.setConfig( + RequestConfig.custom().setProxy(requestHttp.getRequestHttpProxy()).build() + ); + } + + httpPost.setEntity(new StringEntity(qrcodeWrapper.toJson())); + + try (final CloseableHttpResponse response = requestHttp.getRequestHttpClient().execute(httpPost); + final InputStream inputStream = InputStreamResponseHandler.INSTANCE.handleResponse(response)) { + Header[] contentTypeHeader = response.getHeaders("Content-Type"); + if (contentTypeHeader != null && contentTypeHeader.length > 0 + && ContentType.APPLICATION_JSON.getMimeType() + .equals(ContentType.parse(contentTypeHeader[0].getValue()).getMimeType())) { + String responseContent = Utf8ResponseHandler.INSTANCE.handleResponse(response); + throw new WxErrorException(WxError.fromJson(responseContent)); + } + + return IOUtils.toByteArray(inputStream); + } finally { + httpPost.releaseConnection(); + } + } +} diff --git a/weixin-java-miniapp/src/main/java/cn/binarywang/wx/miniapp/util/http/QrCodeRequestExecutor.java b/weixin-java-miniapp/src/main/java/cn/binarywang/wx/miniapp/util/QrcodeRequestExecutor.java similarity index 78% rename from weixin-java-miniapp/src/main/java/cn/binarywang/wx/miniapp/util/http/QrCodeRequestExecutor.java rename to weixin-java-miniapp/src/main/java/cn/binarywang/wx/miniapp/util/QrcodeRequestExecutor.java index cb8097db..d39968bb 100644 --- a/weixin-java-miniapp/src/main/java/cn/binarywang/wx/miniapp/util/http/QrCodeRequestExecutor.java +++ b/weixin-java-miniapp/src/main/java/cn/binarywang/wx/miniapp/util/QrcodeRequestExecutor.java @@ -1,13 +1,10 @@ -package cn.binarywang.wx.miniapp.util.http; +package cn.binarywang.wx.miniapp.util; + +import java.io.File; +import java.io.IOException; +import java.io.InputStream; +import java.util.UUID; -import cn.binarywang.wx.miniapp.bean.AbstractWxMaQrcodeWrapper; -import me.chanjar.weixin.common.error.WxError; -import me.chanjar.weixin.common.error.WxErrorException; -import me.chanjar.weixin.common.util.fs.FileUtils; -import me.chanjar.weixin.common.util.http.RequestExecutor; -import me.chanjar.weixin.common.util.http.RequestHttp; -import me.chanjar.weixin.common.util.http.apache.InputStreamResponseHandler; -import me.chanjar.weixin.common.util.http.apache.Utf8ResponseHandler; import org.apache.http.Header; import org.apache.http.HttpHost; import org.apache.http.client.config.RequestConfig; @@ -17,33 +14,38 @@ import org.apache.http.entity.ContentType; import org.apache.http.entity.StringEntity; import org.apache.http.impl.client.CloseableHttpClient; -import java.io.File; -import java.io.IOException; -import java.io.InputStream; -import java.util.UUID; +import cn.binarywang.wx.miniapp.bean.AbstractWxMaQrcodeWrapper; +import me.chanjar.weixin.common.error.WxError; +import me.chanjar.weixin.common.error.WxErrorException; +import me.chanjar.weixin.common.util.fs.FileUtils; +import me.chanjar.weixin.common.util.http.RequestExecutor; +import me.chanjar.weixin.common.util.http.RequestHttp; +import me.chanjar.weixin.common.util.http.apache.InputStreamResponseHandler; +import me.chanjar.weixin.common.util.http.apache.Utf8ResponseHandler; /** * @author Binary Wang */ -public class QrCodeRequestExecutor implements RequestExecutor { +public class QrcodeRequestExecutor implements RequestExecutor { protected RequestHttp requestHttp; - public QrCodeRequestExecutor(RequestHttp requestHttp) { + public QrcodeRequestExecutor(RequestHttp requestHttp) { this.requestHttp = requestHttp; } @Override - public File execute(String uri, AbstractWxMaQrcodeWrapper ticket) throws WxErrorException, IOException { + public File execute(String uri, AbstractWxMaQrcodeWrapper qrcodeWrapper) throws WxErrorException, IOException { HttpPost httpPost = new HttpPost(uri); if (requestHttp.getRequestHttpProxy() != null) { httpPost.setConfig( RequestConfig.custom().setProxy(requestHttp.getRequestHttpProxy()).build() ); } - httpPost.setEntity(new StringEntity(ticket.toJson())); - try (CloseableHttpResponse response = requestHttp.getRequestHttpClient().execute(httpPost); - InputStream inputStream = InputStreamResponseHandler.INSTANCE.handleResponse(response);) { + httpPost.setEntity(new StringEntity(qrcodeWrapper.toJson())); + + try (final CloseableHttpResponse response = requestHttp.getRequestHttpClient().execute(httpPost); + final InputStream inputStream = InputStreamResponseHandler.INSTANCE.handleResponse(response)) { Header[] contentTypeHeader = response.getHeaders("Content-Type"); if (contentTypeHeader != null && contentTypeHeader.length > 0 && ContentType.APPLICATION_JSON.getMimeType() diff --git a/weixin-java-miniapp/src/test/java/cn/binarywang/wx/miniapp/api/impl/WxMaQrcodeServiceImplTest.java b/weixin-java-miniapp/src/test/java/cn/binarywang/wx/miniapp/api/impl/WxMaQrcodeServiceImplTest.java index ae2e8d8f..e73fec42 100644 --- a/weixin-java-miniapp/src/test/java/cn/binarywang/wx/miniapp/api/impl/WxMaQrcodeServiceImplTest.java +++ b/weixin-java-miniapp/src/test/java/cn/binarywang/wx/miniapp/api/impl/WxMaQrcodeServiceImplTest.java @@ -7,6 +7,9 @@ import org.testng.annotations.*; import cn.binarywang.wx.miniapp.api.WxMaService; import cn.binarywang.wx.miniapp.test.ApiTestModule; import com.google.inject.Inject; +import me.chanjar.weixin.common.error.WxErrorException; + +import static org.assertj.core.api.Assertions.assertThat; /** * @author Binary Wang @@ -18,21 +21,38 @@ public class WxMaQrcodeServiceImplTest { private WxMaService wxService; @Test - public void testCreateQrCode() throws Exception { + public void testCreateQrcode() throws Exception { final File qrCode = this.wxService.getQrcodeService().createQrcode("111", 122); - System.out.println(qrCode); + assertThat(qrCode).isNotNull(); } @Test public void testCreateWxaCode() throws Exception { final File wxCode = this.wxService.getQrcodeService().createWxaCode("111", 122); - System.out.println(wxCode); + assertThat(wxCode).isNotNull(); } @Test public void testCreateWxaCodeUnlimit() throws Exception { final File wxCode = this.wxService.getQrcodeService().createWxaCodeUnlimit("111", null); - System.out.println(wxCode); + assertThat(wxCode).isNotNull(); } + @Test + public void testCreateQrcodeBytes() throws WxErrorException { + final byte[] qrCode = this.wxService.getQrcodeService().createQrcodeBytes("111", 122); + assertThat(qrCode).isNotNull(); + } + + @Test + public void testCreateWxaCodeBytes() throws WxErrorException { + final byte[] wxCode = this.wxService.getQrcodeService().createWxaCodeBytes("111", 122, true, null, false); + assertThat(wxCode).isNotNull(); + } + + @Test + public void testCreateWxaCodeUnlimitBytes() throws WxErrorException { + final byte[] wxCode = this.wxService.getQrcodeService().createWxaCodeUnlimitBytes("111", null, 122, true, null, false); + assertThat(wxCode).isNotNull(); + } }