🎨 【小程序】获取小程序码的createWxaCode相关接口支持envVersion参数

This commit is contained in:
Binary Wang 2022-03-01 15:47:28 +08:00
parent 533ea253d6
commit abaec2faf9
6 changed files with 71 additions and 50 deletions

View File

@ -101,45 +101,48 @@ public interface WxMaQrcodeService {
/** /**
* 接口A: 获取小程序码. * 接口A: 获取小程序码.
* *
* @param path 不能为空最大长度 128 字节 * @param path 不能为空最大长度 128 字节
* @param width 默认430 二维码的宽度 * @param envVersion 默认"release" 要打开的小程序版本正式版为 "release"体验版为 "trial"开发版为 "develop"
* @param autoColor 默认true 自动配置线条颜色如果颜色依然是黑色则说明不建议配置主色调 * @param width 默认430 二维码的宽度
* @param lineColor autoColor false 时生效使用 rgb 设置颜色 例如 {"r":"xxx","g":"xxx","b":"xxx"} * @param autoColor 默认true 自动配置线条颜色如果颜色依然是黑色则说明不建议配置主色调
* @param isHyaline 是否需要透明底色 isHyaline 为true时生成透明底色的小程序码 * @param lineColor autoColor false 时生效使用 rgb 设置颜色 例如 {"r":"xxx","g":"xxx","b":"xxx"}
* @param isHyaline 是否需要透明底色 isHyaline 为true时生成透明底色的小程序码
* @return 文件内容字节数组 * @return 文件内容字节数组
* @throws WxErrorException 异常 * @throws WxErrorException 异常
*/ */
byte[] createWxaCodeBytes(String path, int width, boolean autoColor, WxMaCodeLineColor lineColor, boolean isHyaline) byte[] createWxaCodeBytes(String path, String envVersion, int width, boolean autoColor, WxMaCodeLineColor lineColor,
throws WxErrorException; boolean isHyaline) throws WxErrorException;
/** /**
* 接口A: 获取小程序码. * 接口A: 获取小程序码.
* *
* @param path 不能为空最大长度 128 字节 * @param path 不能为空最大长度 128 字节
* @param width 默认430 二维码的宽度 * @param envVersion 默认"release" 要打开的小程序版本正式版为 "release"体验版为 "trial"开发版为 "develop"
* @param filePath 二维码生成的文件路径例如: /var/temp * @param width 默认430 二维码的宽度
* @param autoColor 默认true 自动配置线条颜色如果颜色依然是黑色则说明不建议配置主色调 * @param filePath 二维码生成的文件路径例如: /var/temp
* @param lineColor autoColor false 时生效使用 rgb 设置颜色 例如 {"r":"xxx","g":"xxx","b":"xxx"} * @param autoColor 默认true 自动配置线条颜色如果颜色依然是黑色则说明不建议配置主色调
* @param isHyaline 是否需要透明底色 isHyaline 为true时生成透明底色的小程序码 * @param lineColor autoColor false 时生效使用 rgb 设置颜色 例如 {"r":"xxx","g":"xxx","b":"xxx"}
* @param isHyaline 是否需要透明底色 isHyaline 为true时生成透明底色的小程序码
* @return 文件对象 * @return 文件对象
* @throws WxErrorException 异常 * @throws WxErrorException 异常
*/ */
File createWxaCode(String path, int width, String filePath, boolean autoColor, WxMaCodeLineColor lineColor, boolean isHyaline) File createWxaCode(String path, String envVersion, int width, String filePath, boolean autoColor,
throws WxErrorException; WxMaCodeLineColor lineColor, boolean isHyaline) throws WxErrorException;
/** /**
* 接口A: 获取小程序码. * 接口A: 获取小程序码.
* *
* @param path 不能为空最大长度 128 字节 * @param path 不能为空最大长度 128 字节
* @param width 默认430 二维码的宽度 * @param envVersion 默认"release" 要打开的小程序版本正式版为 "release"体验版为 "trial"开发版为 "develop"
* @param autoColor 默认true 自动配置线条颜色如果颜色依然是黑色则说明不建议配置主色调 * @param width 默认430 二维码的宽度
* @param lineColor autoColor false 时生效使用 rgb 设置颜色 例如 {"r":"xxx","g":"xxx","b":"xxx"} * @param autoColor 默认true 自动配置线条颜色如果颜色依然是黑色则说明不建议配置主色调
* @param isHyaline 是否需要透明底色 isHyaline 为true时生成透明底色的小程序码 * @param lineColor autoColor false 时生效使用 rgb 设置颜色 例如 {"r":"xxx","g":"xxx","b":"xxx"}
* @param isHyaline 是否需要透明底色 isHyaline 为true时生成透明底色的小程序码
* @return 文件对象 * @return 文件对象
* @throws WxErrorException 异常 * @throws WxErrorException 异常
*/ */
File createWxaCode(String path, int width, boolean autoColor, WxMaCodeLineColor lineColor, boolean isHyaline) File createWxaCode(String path, String envVersion, int width, boolean autoColor, WxMaCodeLineColor lineColor,
throws WxErrorException; boolean isHyaline) throws WxErrorException;
/** /**
* 接口A: 获取小程序码. * 接口A: 获取小程序码.
@ -229,8 +232,8 @@ public interface WxMaQrcodeService {
* @return 文件对象 * @return 文件对象
* @throws WxErrorException 异常 * @throws WxErrorException 异常
*/ */
File createWxaCodeUnlimit(String scene, String page, String filePath, boolean checkPath, String envVersion, int width, boolean autoColor, File createWxaCodeUnlimit(String scene, String page, String filePath, boolean checkPath, String envVersion, int width,
WxMaCodeLineColor lineColor, boolean isHyaline) throws WxErrorException; boolean autoColor, WxMaCodeLineColor lineColor, boolean isHyaline) throws WxErrorException;
/** /**
* 接口B: 获取小程序码永久有效数量暂无限制. * 接口B: 获取小程序码永久有效数量暂无限制.

View File

@ -10,10 +10,12 @@ import cn.binarywang.wx.miniapp.executor.QrcodeBytesRequestExecutor;
import cn.binarywang.wx.miniapp.executor.QrcodeRequestExecutor; import cn.binarywang.wx.miniapp.executor.QrcodeRequestExecutor;
import lombok.RequiredArgsConstructor; import lombok.RequiredArgsConstructor;
import me.chanjar.weixin.common.error.WxErrorException; import me.chanjar.weixin.common.error.WxErrorException;
import org.apache.commons.lang3.StringUtils;
import java.io.File; import java.io.File;
import static cn.binarywang.wx.miniapp.constant.WxMaApiUrlConstants.Qrcode.*; import static cn.binarywang.wx.miniapp.constant.WxMaApiUrlConstants.Qrcode.*;
import static cn.binarywang.wx.miniapp.constant.WxMaConstants.DEFAULT_ENV_VERSION;
/** /**
* @author <a href="https://github.com/binarywang">Binary Wang</a> * @author <a href="https://github.com/binarywang">Binary Wang</a>
@ -40,11 +42,12 @@ public class WxMaQrcodeServiceImpl implements WxMaQrcodeService {
} }
@Override @Override
public byte[] createWxaCodeBytes(String path, int width, boolean autoColor, WxMaCodeLineColor lineColor, boolean isHyaline) public byte[] createWxaCodeBytes(String path, String envVersion, int width, boolean autoColor,
throws WxErrorException { WxMaCodeLineColor lineColor, boolean isHyaline) throws WxErrorException {
return this.service.execute(QrcodeBytesRequestExecutor.create(this.service.getRequestHttp()), GET_WXACODE_URL, return this.service.execute(QrcodeBytesRequestExecutor.create(this.service.getRequestHttp()), GET_WXACODE_URL,
WxaCode.builder() WxaCode.builder()
.path(path) .path(path)
.envVersion(StringUtils.defaultIfEmpty(envVersion, DEFAULT_ENV_VERSION))
.width(width) .width(width)
.autoColor(autoColor) .autoColor(autoColor)
.lineColor(lineColor) .lineColor(lineColor)
@ -53,11 +56,12 @@ public class WxMaQrcodeServiceImpl implements WxMaQrcodeService {
} }
@Override @Override
public File createWxaCode(String path, int width, boolean autoColor, WxMaCodeLineColor lineColor, boolean isHyaline) public File createWxaCode(String path, String envVersion, int width, boolean autoColor, WxMaCodeLineColor lineColor,
throws WxErrorException { boolean isHyaline) throws WxErrorException {
return this.service.execute(QrcodeRequestExecutor.create(this.service.getRequestHttp()), GET_WXACODE_URL, return this.service.execute(QrcodeRequestExecutor.create(this.service.getRequestHttp()), GET_WXACODE_URL,
WxaCode.builder() WxaCode.builder()
.path(path) .path(path)
.envVersion(StringUtils.defaultIfEmpty(envVersion, DEFAULT_ENV_VERSION))
.width(width) .width(width)
.autoColor(autoColor) .autoColor(autoColor)
.lineColor(lineColor) .lineColor(lineColor)
@ -67,7 +71,7 @@ public class WxMaQrcodeServiceImpl implements WxMaQrcodeService {
@Override @Override
public File createWxaCode(String path, int width) throws WxErrorException { public File createWxaCode(String path, int width) throws WxErrorException {
return this.createWxaCode(path, width, true, null, false); return this.createWxaCode(path, null, width, true, null, false);
} }
@Override @Override
@ -76,21 +80,22 @@ public class WxMaQrcodeServiceImpl implements WxMaQrcodeService {
} }
@Override @Override
public byte[] createWxaCodeUnlimitBytes(String scene, String page, boolean checkPath, String envVersion, int width, boolean autoColor, public byte[] createWxaCodeUnlimitBytes(String scene, String page, boolean checkPath, String envVersion, int width,
WxMaCodeLineColor lineColor, boolean isHyaline) throws WxErrorException { boolean autoColor, WxMaCodeLineColor lineColor, boolean isHyaline)
throws WxErrorException {
return this.service.execute(QrcodeBytesRequestExecutor.create(this.service.getRequestHttp()), return this.service.execute(QrcodeBytesRequestExecutor.create(this.service.getRequestHttp()),
GET_WXACODE_UNLIMIT_URL, this.buildWxaCodeUnlimit(scene, page, checkPath, envVersion, width, autoColor, lineColor, isHyaline)); GET_WXACODE_UNLIMIT_URL, this.buildWxaCodeUnlimit(scene, page, checkPath, envVersion, width, autoColor, lineColor, isHyaline));
} }
@Override @Override
public File createWxaCodeUnlimit(String scene, String page, boolean checkPath, String envVersion, int width, boolean autoColor, public File createWxaCodeUnlimit(String scene, String page, boolean checkPath, String envVersion, int width,
WxMaCodeLineColor lineColor, boolean isHyaline) throws WxErrorException { boolean autoColor, WxMaCodeLineColor lineColor, boolean isHyaline) throws WxErrorException {
return this.service.execute(QrcodeRequestExecutor.create(this.service.getRequestHttp()), return this.service.execute(QrcodeRequestExecutor.create(this.service.getRequestHttp()),
GET_WXACODE_UNLIMIT_URL, this.buildWxaCodeUnlimit(scene, page, checkPath, envVersion, width, autoColor, lineColor, isHyaline)); GET_WXACODE_UNLIMIT_URL, this.buildWxaCodeUnlimit(scene, page, checkPath, envVersion, width, autoColor, lineColor, isHyaline));
} }
private WxaCodeUnlimit buildWxaCodeUnlimit(String scene, String page, boolean checkPath, String envVersion, int width, boolean autoColor, private WxaCodeUnlimit buildWxaCodeUnlimit(String scene, String page, boolean checkPath, String envVersion, int width,
WxMaCodeLineColor lineColor, boolean isHyaline) { boolean autoColor, WxMaCodeLineColor lineColor, boolean isHyaline) {
WxaCodeUnlimit wxaCodeUnlimit = new WxaCodeUnlimit(); WxaCodeUnlimit wxaCodeUnlimit = new WxaCodeUnlimit();
wxaCodeUnlimit.setScene(scene); wxaCodeUnlimit.setScene(scene);
wxaCodeUnlimit.setPage(page); wxaCodeUnlimit.setPage(page);
@ -106,7 +111,7 @@ public class WxMaQrcodeServiceImpl implements WxMaQrcodeService {
@Override @Override
public File createWxaCodeUnlimit(String scene, String page) throws WxErrorException { public File createWxaCodeUnlimit(String scene, String page) throws WxErrorException {
return this.createWxaCodeUnlimit(scene, page, true, "release", 430, true, null, false); return this.createWxaCodeUnlimit(scene, page, true, DEFAULT_ENV_VERSION, 430, true, null, false);
} }
@Override @Override
@ -121,12 +126,12 @@ public class WxMaQrcodeServiceImpl implements WxMaQrcodeService {
} }
@Override @Override
public File createWxaCode(String path, int width, String filePath, boolean autoColor, WxMaCodeLineColor lineColor, public File createWxaCode(String path, String envVersion, int width, String filePath, boolean autoColor,
boolean isHyaline) WxMaCodeLineColor lineColor, boolean isHyaline) throws WxErrorException {
throws WxErrorException {
return this.service.execute(QrcodeRequestExecutor.create(this.service.getRequestHttp(), filePath), GET_WXACODE_URL, return this.service.execute(QrcodeRequestExecutor.create(this.service.getRequestHttp(), filePath), GET_WXACODE_URL,
WxaCode.builder() WxaCode.builder()
.path(path) .path(path)
.envVersion(StringUtils.defaultIfEmpty(envVersion, DEFAULT_ENV_VERSION))
.width(width) .width(width)
.autoColor(autoColor) .autoColor(autoColor)
.lineColor(lineColor) .lineColor(lineColor)
@ -136,7 +141,7 @@ public class WxMaQrcodeServiceImpl implements WxMaQrcodeService {
@Override @Override
public File createWxaCode(String path, int width, String filePath) throws WxErrorException { public File createWxaCode(String path, int width, String filePath) throws WxErrorException {
return this.createWxaCode(path, width, filePath, true, null, false); return this.createWxaCode(path, null, width, filePath, true, null, false);
} }
@Override @Override
@ -145,15 +150,16 @@ public class WxMaQrcodeServiceImpl implements WxMaQrcodeService {
} }
@Override @Override
public File createWxaCodeUnlimit(String scene, String page, String filePath, boolean checkPath, String envVersion, int width, boolean autoColor, public File createWxaCodeUnlimit(String scene, String page, String filePath, boolean checkPath, String envVersion,
WxMaCodeLineColor lineColor, boolean isHyaline) throws WxErrorException { int width, boolean autoColor, WxMaCodeLineColor lineColor, boolean isHyaline)
throws WxErrorException {
return this.service.execute(QrcodeRequestExecutor.create(this.service.getRequestHttp(), filePath), return this.service.execute(QrcodeRequestExecutor.create(this.service.getRequestHttp(), filePath),
GET_WXACODE_UNLIMIT_URL, this.buildWxaCodeUnlimit(scene, page, checkPath, envVersion, width, autoColor, lineColor, isHyaline)); GET_WXACODE_UNLIMIT_URL, this.buildWxaCodeUnlimit(scene, page, checkPath, envVersion, width, autoColor, lineColor, isHyaline));
} }
@Override @Override
public File createWxaCodeUnlimit(String scene, String page, String filePath) throws WxErrorException { public File createWxaCodeUnlimit(String scene, String page, String filePath) throws WxErrorException {
return this.createWxaCodeUnlimit(scene, page, filePath, true, "release", 430, true, null, false); return this.createWxaCodeUnlimit(scene, page, filePath, true, DEFAULT_ENV_VERSION, 430, true, null, false);
} }
} }

View File

@ -10,6 +10,8 @@ import lombok.Data;
import lombok.EqualsAndHashCode; import lombok.EqualsAndHashCode;
import lombok.NoArgsConstructor; import lombok.NoArgsConstructor;
import static cn.binarywang.wx.miniapp.constant.WxMaConstants.DEFAULT_ENV_VERSION;
/** /**
* 小程序码. * 小程序码.
* *
@ -26,6 +28,9 @@ public class WxaCode extends AbstractWxMaQrcodeWrapper implements Serializable {
private String path; private String path;
@SerializedName("env_version")
private String envVersion = DEFAULT_ENV_VERSION;
@Builder.Default @Builder.Default
private int width = 430; private int width = 430;

View File

@ -7,6 +7,8 @@ import lombok.EqualsAndHashCode;
import java.io.Serializable; import java.io.Serializable;
import static cn.binarywang.wx.miniapp.constant.WxMaConstants.DEFAULT_ENV_VERSION;
/** /**
* 小程序码接口B. * 小程序码接口B.
* *
@ -24,7 +26,7 @@ public class WxaCodeUnlimit extends AbstractWxMaQrcodeWrapper implements Seriali
private boolean checkPath = true; private boolean checkPath = true;
@SerializedName("env_version") @SerializedName("env_version")
private String envVersion = "release"; private String envVersion = DEFAULT_ENV_VERSION;
private int width = 430; private int width = 430;

View File

@ -11,6 +11,11 @@ public abstract class WxMaConstants {
private WxMaConstants() { private WxMaConstants() {
} }
/**
* 默认的env_version值
*/
public static final String DEFAULT_ENV_VERSION = "release";
/** /**
* 微信接口返回的参数errcode. * 微信接口返回的参数errcode.
*/ */

View File

@ -1,13 +1,13 @@
package cn.binarywang.wx.miniapp.api.impl; package cn.binarywang.wx.miniapp.api.impl;
import java.io.File;
import org.testng.annotations.*;
import cn.binarywang.wx.miniapp.api.WxMaService; import cn.binarywang.wx.miniapp.api.WxMaService;
import cn.binarywang.wx.miniapp.test.ApiTestModule; import cn.binarywang.wx.miniapp.test.ApiTestModule;
import com.google.inject.Inject; import com.google.inject.Inject;
import me.chanjar.weixin.common.error.WxErrorException; import me.chanjar.weixin.common.error.WxErrorException;
import org.testng.annotations.Guice;
import org.testng.annotations.Test;
import java.io.File;
import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.api.Assertions.assertThat;
@ -46,7 +46,7 @@ public class WxMaQrcodeServiceImplTest {
@Test @Test
public void testCreateWxaCodeBytes() throws WxErrorException { public void testCreateWxaCodeBytes() throws WxErrorException {
final byte[] wxCode = this.wxService.getQrcodeService().createWxaCodeBytes("111", 122, true, null, false); final byte[] wxCode = this.wxService.getQrcodeService().createWxaCodeBytes("111", null, 122, true, null, false);
assertThat(wxCode).isNotNull(); assertThat(wxCode).isNotNull();
} }
@ -70,7 +70,7 @@ public class WxMaQrcodeServiceImplTest {
@Test @Test
public void testCreateQrcodeUnlimitByFile() throws WxErrorException { public void testCreateQrcodeUnlimitByFile() throws WxErrorException {
final File wxCode = this.wxService.getQrcodeService().createWxaCodeUnlimit("111",null,"/opt/logs"); final File wxCode = this.wxService.getQrcodeService().createWxaCodeUnlimit("111", null, "/opt/logs");
assertThat(wxCode).isNotNull(); assertThat(wxCode).isNotNull();
} }
} }