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 46fc97d2b..ececed036 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 @@ -101,45 +101,48 @@ public interface WxMaQrcodeService { /** * 接口A: 获取小程序码. * - * @param path 不能为空,最大长度 128 字节 - * @param width 默认430 二维码的宽度 - * @param autoColor 默认true 自动配置线条颜色,如果颜色依然是黑色,则说明不建议配置主色调 - * @param lineColor autoColor 为 false 时生效,使用 rgb 设置颜色 例如 {"r":"xxx","g":"xxx","b":"xxx"} - * @param isHyaline 是否需要透明底色, isHyaline 为true时,生成透明底色的小程序码 + * @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"} + * @param isHyaline 是否需要透明底色, isHyaline 为true时,生成透明底色的小程序码 * @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 width 默认430 二维码的宽度 - * @param filePath 二维码生成的文件路径,例如: /var/temp - * @param autoColor 默认true 自动配置线条颜色,如果颜色依然是黑色,则说明不建议配置主色调 - * @param lineColor autoColor 为 false 时生效,使用 rgb 设置颜色 例如 {"r":"xxx","g":"xxx","b":"xxx"} - * @param isHyaline 是否需要透明底色, isHyaline 为true时,生成透明底色的小程序码 + * @param path 不能为空,最大长度 128 字节 + * @param envVersion 默认"release" 要打开的小程序版本。正式版为 "release",体验版为 "trial",开发版为 "develop" + * @param width 默认430 二维码的宽度 + * @param filePath 二维码生成的文件路径,例如: /var/temp + * @param autoColor 默认true 自动配置线条颜色,如果颜色依然是黑色,则说明不建议配置主色调 + * @param lineColor autoColor 为 false 时生效,使用 rgb 设置颜色 例如 {"r":"xxx","g":"xxx","b":"xxx"} + * @param isHyaline 是否需要透明底色, isHyaline 为true时,生成透明底色的小程序码 * @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 width 默认430 二维码的宽度 - * @param autoColor 默认true 自动配置线条颜色,如果颜色依然是黑色,则说明不建议配置主色调 - * @param lineColor autoColor 为 false 时生效,使用 rgb 设置颜色 例如 {"r":"xxx","g":"xxx","b":"xxx"} - * @param isHyaline 是否需要透明底色, isHyaline 为true时,生成透明底色的小程序码 + * @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"} + * @param isHyaline 是否需要透明底色, isHyaline 为true时,生成透明底色的小程序码 * @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: 获取小程序码(永久有效、数量暂无限制). 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 072119bc2..cc7fc51fe 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 @@ -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 Binary Wang @@ -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); } } diff --git a/weixin-java-miniapp/src/main/java/cn/binarywang/wx/miniapp/bean/WxaCode.java b/weixin-java-miniapp/src/main/java/cn/binarywang/wx/miniapp/bean/WxaCode.java index 2361355ad..2711ccb24 100644 --- a/weixin-java-miniapp/src/main/java/cn/binarywang/wx/miniapp/bean/WxaCode.java +++ b/weixin-java-miniapp/src/main/java/cn/binarywang/wx/miniapp/bean/WxaCode.java @@ -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; diff --git a/weixin-java-miniapp/src/main/java/cn/binarywang/wx/miniapp/bean/WxaCodeUnlimit.java b/weixin-java-miniapp/src/main/java/cn/binarywang/wx/miniapp/bean/WxaCodeUnlimit.java index 32e713ad8..351ee5c9e 100644 --- a/weixin-java-miniapp/src/main/java/cn/binarywang/wx/miniapp/bean/WxaCodeUnlimit.java +++ b/weixin-java-miniapp/src/main/java/cn/binarywang/wx/miniapp/bean/WxaCodeUnlimit.java @@ -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; diff --git a/weixin-java-miniapp/src/main/java/cn/binarywang/wx/miniapp/constant/WxMaConstants.java b/weixin-java-miniapp/src/main/java/cn/binarywang/wx/miniapp/constant/WxMaConstants.java index 8ac322aa5..646d909fc 100644 --- a/weixin-java-miniapp/src/main/java/cn/binarywang/wx/miniapp/constant/WxMaConstants.java +++ b/weixin-java-miniapp/src/main/java/cn/binarywang/wx/miniapp/constant/WxMaConstants.java @@ -11,6 +11,11 @@ public abstract class WxMaConstants { private WxMaConstants() { } + /** + * 默认的env_version值 + */ + public static final String DEFAULT_ENV_VERSION = "release"; + /** * 微信接口返回的参数errcode. */ 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 25988f5fd..d45651ba2 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 @@ -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(); } @@ -70,7 +70,7 @@ public class WxMaQrcodeServiceImplTest { @Test 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(); } }