Skip to content

Commit

Permalink
#853 小程序模块获取二维码和小程序码接口增加对应返回byte数组的实现方法
Browse files Browse the repository at this point in the history
  • Loading branch information
binarywang committed Dec 8, 2018
1 parent 812de02 commit d59dff2
Show file tree
Hide file tree
Showing 8 changed files with 307 additions and 66 deletions.
Original file line number Diff line number Diff line change
@@ -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 <T> 返回值类型
* @param <E> 请求参数类型
Expand All @@ -14,6 +14,8 @@
public interface RequestExecutor<T, E> {

/**
* 执行http请求.
*
* @param uri uri
* @param data 数据
*/
Expand Down
Original file line number Diff line number Diff line change
@@ -1,24 +1,29 @@
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;
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<InputStream> {

public static final ResponseHandler<InputStream> 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());
}
Expand Down
Original file line number Diff line number Diff line change
@@ -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>
* 二维码相关操作接口.
Expand Down Expand Up @@ -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 异常
*/
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;

/**
Expand All @@ -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 异常
*/
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;
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;

}
Original file line number Diff line number Diff line change
@@ -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>
*/
Expand All @@ -21,10 +22,16 @@ public WxMaQrcodeServiceImpl(WxMaService wxMaService) {
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
Expand All @@ -33,15 +40,29 @@ public File createQrcode(String path) throws WxErrorException {
}

@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
Expand All @@ -51,21 +72,36 @@ public File createWxaCode(String path, int width) throws WxErrorException {

@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);
wxaCodeUnlimit.setWidth(width);
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
Expand Down
Loading

0 comments on commit d59dff2

Please sign in to comment.