🎨 【小程序】获取小程序码的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

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

View File

@ -10,10 +10,12 @@ import cn.binarywang.wx.miniapp.executor.QrcodeBytesRequestExecutor;
import cn.binarywang.wx.miniapp.executor.QrcodeRequestExecutor;
import lombok.RequiredArgsConstructor;
import me.chanjar.weixin.common.error.WxErrorException;
import org.apache.commons.lang3.StringUtils;
import java.io.File;
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>
@ -40,11 +42,12 @@ public class WxMaQrcodeServiceImpl implements WxMaQrcodeService {
}
@Override
public byte[] createWxaCodeBytes(String path, int width, boolean autoColor, WxMaCodeLineColor lineColor, boolean isHyaline)
throws WxErrorException {
public byte[] createWxaCodeBytes(String path, String envVersion, int width, boolean autoColor,
WxMaCodeLineColor lineColor, boolean isHyaline) throws WxErrorException {
return this.service.execute(QrcodeBytesRequestExecutor.create(this.service.getRequestHttp()), GET_WXACODE_URL,
WxaCode.builder()
.path(path)
.envVersion(StringUtils.defaultIfEmpty(envVersion, DEFAULT_ENV_VERSION))
.width(width)
.autoColor(autoColor)
.lineColor(lineColor)
@ -53,11 +56,12 @@ public class WxMaQrcodeServiceImpl implements WxMaQrcodeService {
}
@Override
public File createWxaCode(String path, int width, boolean autoColor, WxMaCodeLineColor lineColor, boolean isHyaline)
throws WxErrorException {
public File createWxaCode(String path, String envVersion, int width, boolean autoColor, WxMaCodeLineColor lineColor,
boolean isHyaline) throws WxErrorException {
return this.service.execute(QrcodeRequestExecutor.create(this.service.getRequestHttp()), GET_WXACODE_URL,
WxaCode.builder()
.path(path)
.envVersion(StringUtils.defaultIfEmpty(envVersion, DEFAULT_ENV_VERSION))
.width(width)
.autoColor(autoColor)
.lineColor(lineColor)
@ -67,7 +71,7 @@ public class WxMaQrcodeServiceImpl implements WxMaQrcodeService {
@Override
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
@ -76,21 +80,22 @@ public class WxMaQrcodeServiceImpl implements WxMaQrcodeService {
}
@Override
public byte[] createWxaCodeUnlimitBytes(String scene, String page, boolean checkPath, String envVersion, int width, boolean autoColor,
WxMaCodeLineColor lineColor, boolean isHyaline) throws WxErrorException {
public byte[] createWxaCodeUnlimitBytes(String scene, String page, boolean checkPath, String envVersion, int width,
boolean autoColor, WxMaCodeLineColor lineColor, boolean isHyaline)
throws WxErrorException {
return this.service.execute(QrcodeBytesRequestExecutor.create(this.service.getRequestHttp()),
GET_WXACODE_UNLIMIT_URL, this.buildWxaCodeUnlimit(scene, page, checkPath, envVersion, width, autoColor, lineColor, isHyaline));
}
@Override
public File createWxaCodeUnlimit(String scene, String page, boolean checkPath, String envVersion, int width, boolean autoColor,
WxMaCodeLineColor lineColor, boolean isHyaline) throws WxErrorException {
public File createWxaCodeUnlimit(String scene, String page, boolean checkPath, String envVersion, int width,
boolean autoColor, WxMaCodeLineColor lineColor, boolean isHyaline) throws WxErrorException {
return this.service.execute(QrcodeRequestExecutor.create(this.service.getRequestHttp()),
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,
WxMaCodeLineColor lineColor, boolean isHyaline) {
private WxaCodeUnlimit buildWxaCodeUnlimit(String scene, String page, boolean checkPath, String envVersion, int width,
boolean autoColor, WxMaCodeLineColor lineColor, boolean isHyaline) {
WxaCodeUnlimit wxaCodeUnlimit = new WxaCodeUnlimit();
wxaCodeUnlimit.setScene(scene);
wxaCodeUnlimit.setPage(page);
@ -106,7 +111,7 @@ public class WxMaQrcodeServiceImpl implements WxMaQrcodeService {
@Override
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
@ -121,12 +126,12 @@ public class WxMaQrcodeServiceImpl implements WxMaQrcodeService {
}
@Override
public File createWxaCode(String path, int width, String filePath, boolean autoColor, WxMaCodeLineColor lineColor,
boolean isHyaline)
throws WxErrorException {
public File createWxaCode(String path, String envVersion, int width, String filePath, boolean autoColor,
WxMaCodeLineColor lineColor, boolean isHyaline) throws WxErrorException {
return this.service.execute(QrcodeRequestExecutor.create(this.service.getRequestHttp(), filePath), GET_WXACODE_URL,
WxaCode.builder()
.path(path)
.envVersion(StringUtils.defaultIfEmpty(envVersion, DEFAULT_ENV_VERSION))
.width(width)
.autoColor(autoColor)
.lineColor(lineColor)
@ -136,7 +141,7 @@ public class WxMaQrcodeServiceImpl implements WxMaQrcodeService {
@Override
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
@ -145,15 +150,16 @@ public class WxMaQrcodeServiceImpl implements WxMaQrcodeService {
}
@Override
public File createWxaCodeUnlimit(String scene, String page, String filePath, boolean checkPath, String envVersion, int width, boolean autoColor,
WxMaCodeLineColor lineColor, boolean isHyaline) throws WxErrorException {
public File createWxaCodeUnlimit(String scene, String page, String filePath, boolean checkPath, String envVersion,
int width, boolean autoColor, WxMaCodeLineColor lineColor, boolean isHyaline)
throws WxErrorException {
return this.service.execute(QrcodeRequestExecutor.create(this.service.getRequestHttp(), filePath),
GET_WXACODE_UNLIMIT_URL, this.buildWxaCodeUnlimit(scene, page, checkPath, envVersion, width, autoColor, lineColor, isHyaline));
}
@Override
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.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;
@SerializedName("env_version")
private String envVersion = DEFAULT_ENV_VERSION;
@Builder.Default
private int width = 430;

View File

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

View File

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

View File

@ -1,13 +1,13 @@
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.test.ApiTestModule;
import com.google.inject.Inject;
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;
@ -46,7 +46,7 @@ public class WxMaQrcodeServiceImplTest {
@Test
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();
}