mirror of
				https://gitee.com/binary/weixin-java-tools.git
				synced 2025-11-01 03:25:35 +08:00 
			
		
		
		
	#853 小程序模块获取二维码和小程序码接口增加对应返回byte数组的实现方法
This commit is contained in:
		| @ -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; | ||||
|  | ||||
| /** | ||||
|  * <pre> | ||||
|  * 二维码相关操作接口. | ||||
| @ -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: 获取小程序页面二维码. | ||||
|    * <pre> | ||||
|    * 适用于需要的码数量较少的业务场景 | ||||
|    * 通过该接口,仅能生成已发布的小程序的二维码。 | ||||
|    * 可以在开发者工具预览时生成开发版的带参二维码。 | ||||
|    * 带参二维码只有 100000 个,请谨慎调用。 | ||||
|    * </pre> | ||||
|    * | ||||
|    * @param path  不能为空,最大长度 128 字节 | ||||
|    * @param width 默认430 二维码的宽度 | ||||
|    * @return 文件对象 | ||||
|    * @throws WxErrorException 异常 | ||||
|    */ | ||||
|   File createQrcode(String path, int width) throws WxErrorException; | ||||
|  | ||||
|   /** | ||||
|    * 接口C: 获取小程序页面二维码. | ||||
|    * <pre> | ||||
|    * 适用于需要的码数量较少的业务场景 | ||||
|    * 通过该接口,仅能生成已发布的小程序的二维码。 | ||||
|    * 可以在开发者工具预览时生成开发版的带参二维码。 | ||||
|    * 带参二维码只有 100000 个,请谨慎调用。 | ||||
|    * </pre> | ||||
|    * | ||||
|    * @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 异常 | ||||
|    */ | ||||
|   File createWxaCode(String path, int width, boolean autoColor, WxMaCodeLineColor lineColor, boolean is_hyaline) 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 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 | ||||
|    * </pre> | ||||
|    * | ||||
|    * @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 异常 | ||||
|    */ | ||||
|   File createWxaCodeUnlimit(String scene, String page, int width, boolean autoColor, WxMaCodeLineColor lineColor, boolean isHyaline) throws WxErrorException; | ||||
|   byte[] createWxaCodeUnlimitBytes(String scene, String page, int width, boolean autoColor, | ||||
|                                    WxMaCodeLineColor lineColor, boolean isHyaline) throws WxErrorException; | ||||
|  | ||||
|   /** | ||||
|    * 接口B: 获取小程序码(永久有效、数量暂无限制). | ||||
|    * <pre> | ||||
|    * 通过该接口生成的小程序码,永久有效,数量暂无限制。 | ||||
|    * 用户扫描该码进入小程序后,将统一打开首页,开发者需在对应页面根据获取的码中 scene 字段的值,再做处理逻辑。 | ||||
|    * 使用如下代码可以获取到二维码中的 scene 字段的值。 | ||||
|    * 调试阶段可以使用开发工具的条件编译自定义参数 scene=xxxx 进行模拟,开发工具模拟时的 scene 的参数值需要进行 urlencode | ||||
|    * </pre> | ||||
|    * | ||||
|    * @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; | ||||
|  | ||||
|   /** | ||||
|    * 接口B: 获取小程序码(永久有效、数量暂无限制). | ||||
|    * <pre> | ||||
|    * 通过该接口生成的小程序码,永久有效,数量暂无限制。 | ||||
|    * 用户扫描该码进入小程序后,将统一打开首页,开发者需在对应页面根据获取的码中 scene 字段的值,再做处理逻辑。 | ||||
|    * 使用如下代码可以获取到二维码中的 scene 字段的值。 | ||||
|    * 调试阶段可以使用开发工具的条件编译自定义参数 scene=xxxx 进行模拟,开发工具模拟时的 scene 的参数值需要进行 urlencode | ||||
|    * </pre> | ||||
|    * | ||||
|    * @param scene 最大32个可见字符,只支持数字,大小写英文以及部分特殊字符:!#$&'()*+,/:;=?@-._~, | ||||
|    *              其它字符请自行编码为合法字符(因不支持%,中文无法使用 urlencode 处理,请使用其他编码方式) | ||||
|    * @param page  必须是已经发布的小程序页面,例如 "pages/index/index" ,如果不填写这个字段,默认跳主页面 | ||||
|    * @return 文件对象 | ||||
|    * @throws WxErrorException 异常 | ||||
|    */ | ||||
|   File createWxaCodeUnlimit(String scene, String page) throws WxErrorException; | ||||
|  | ||||
| } | ||||
|  | ||||
| @ -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 <a href="https://github.com/binarywang">Binary Wang</a> | ||||
|  */ | ||||
| @ -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 | ||||
|  | ||||
| @ -1,36 +1,48 @@ | ||||
| package cn.binarywang.wx.miniapp.bean; | ||||
| 
 | ||||
| import cn.binarywang.wx.miniapp.util.json.WxMaGsonBuilder; | ||||
| import com.google.gson.annotations.SerializedName; | ||||
| import lombok.Data; | ||||
| import lombok.EqualsAndHashCode; | ||||
| 
 | ||||
| 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 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); | ||||
|   } | ||||
| 
 | ||||
| } | ||||
| @ -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 <a href="https://github.com/binarywang">Binary Wang</a> | ||||
|  */ | ||||
| public class QrcodeBytesRequestExecutor implements RequestExecutor<byte[], AbstractWxMaQrcodeWrapper> { | ||||
|   protected RequestHttp<CloseableHttpClient, HttpHost> 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(); | ||||
|     } | ||||
|   } | ||||
| } | ||||
| @ -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 <a href="https://github.com/binarywang">Binary Wang</a> | ||||
|  */ | ||||
| public class QrCodeRequestExecutor implements RequestExecutor<File, AbstractWxMaQrcodeWrapper> { | ||||
| public class QrcodeRequestExecutor implements RequestExecutor<File, AbstractWxMaQrcodeWrapper> { | ||||
|   protected RequestHttp<CloseableHttpClient, HttpHost> 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() | ||||
| @ -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 <a href="https://github.com/binarywang">Binary Wang</a> | ||||
| @ -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(); | ||||
|   } | ||||
| } | ||||
|  | ||||
		Reference in New Issue
	
	Block a user
	 Binary Wang
					Binary Wang