MEMBER: 根据【代码评审】完善微信小程序码

This commit is contained in:
puhui999 2024-05-23 17:14:35 +08:00
parent 33e8c7ca2e
commit b227b039c9
2 changed files with 21 additions and 20 deletions

View File

@ -1,5 +1,7 @@
package cn.iocoder.yudao.module.member.controller.app.social;
import cn.hutool.core.codec.Base64;
import cn.hutool.core.util.ObjUtil;
import cn.iocoder.yudao.framework.common.enums.UserTypeEnum;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
@ -22,8 +24,6 @@ import jakarta.validation.Valid;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import java.util.Base64;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;
import static cn.iocoder.yudao.framework.security.core.util.SecurityFrameworkUtils.getLoginUserId;
@ -33,6 +33,13 @@ import static cn.iocoder.yudao.framework.security.core.util.SecurityFrameworkUti
@Validated
public class AppSocialUserController {
public static final String ENV_VERSION = "release"; // 小程序版本正式版为 "release"体验版为 "trial"开发版为 "develop"
private static final String SCENE = ""; // 页面路径不能携带参数参数请放在scene字段里
private static final Integer WIDTH = 430; // 二维码宽度
private static final Boolean AUTO_COLOR = true; // 默认true 自动配置线条颜色如果颜色依然是黑色则说明不建议配置主色调
private static final Boolean CHECK_PATH = true; // 默认true 检查 page 是否存在
private static final Boolean HYALINE = true; // 是否需要透明底色 hyaline 为true时生成透明底色的小程序码
@Resource
private SocialUserApi socialUserApi;
@Resource
@ -69,10 +76,13 @@ public class AppSocialUserController {
@PostMapping("/wxa-qrcode")
@Operation(summary = "获得微信小程序码(base64 image)")
public CommonResult<String> getWxQrcode(@RequestBody @Valid AppSocialWxQrcodeReqVO reqVO) {
byte[] wxQrcode = socialClientApi.getWxaQrcode(BeanUtils.toBean(reqVO, SocialWxQrcodeReqDTO.class)
.setEnvVersion(AppSocialWxQrcodeReqVO.ENV_VERSION));
// TODO @puhui9991是不是 base64 返回不拼接哈 data:image/png;base642cn.hutool.core.codec.Base64.encode()
return success("data:image/png;base64," + Base64.getEncoder().encodeToString(wxQrcode));
byte[] wxQrcode = socialClientApi.getWxaQrcode(new SocialWxQrcodeReqDTO().setPath(reqVO.getPath())
.setEnvVersion(ENV_VERSION).setWidth(ObjUtil.defaultIfNull(reqVO.getWidth(), WIDTH))
.setScene(ObjUtil.defaultIfNull(reqVO.getScene(), SCENE))
.setAutoColor(ObjUtil.defaultIfNull(reqVO.getAutoColor(), AUTO_COLOR))
.setHyaline(ObjUtil.defaultIfNull(reqVO.getHyaline(), HYALINE))
.setCheckPath(ObjUtil.defaultIfNull(reqVO.getCheckPath(), CHECK_PATH)));
return success(Base64.encode(wxQrcode));
}
}

View File

@ -9,20 +9,11 @@ import lombok.Data;
@Data
public class AppSocialWxQrcodeReqVO {
// TODO @puhui999: 没有默认值 getQrcodeService().createWxaCodeUnlimitBytes() 转类型会报错 🤣
// TODO @puhui999懂了哈default 最好在 controller 对于 VO 来说不给默认值
public static String ENV_VERSION = "release"; // 小程序版本正式版为 "release"体验版为 "trial"开发版为 "develop"
private static String SCENE = ""; // 页面路径不能携带参数参数请放在scene字段里
private static Integer WIDTH = 430; // 二维码宽度
private static Boolean AUTO_COLOR = true; // 默认true 自动配置线条颜色如果颜色依然是黑色则说明不建议配置主色调
private static Boolean CHECK_PATH = true; // 默认true 检查 page 是否存在
private static Boolean HYALINE = true; // 是否需要透明底色 is_hyaline 为true时生成透明底色的小程序码
/**
* 页面路径不能携带参数参数请放在scene字段里
*/
@Schema(description = "场景值", requiredMode = Schema.RequiredMode.REQUIRED, example = "1001")
private String scene = SCENE;
private String scene;
/**
* 默认是主页页面 page例如 pages/index/index根路径前不要填加 /不能携带参数参数请放在scene字段里
@ -33,15 +24,15 @@ public class AppSocialWxQrcodeReqVO {
private String path;
@Schema(description = "二维码宽度", requiredMode = Schema.RequiredMode.REQUIRED, example = "430")
private Integer width = WIDTH;
private Integer width;
@Schema(description = "是/否自动配置线条颜色", requiredMode = Schema.RequiredMode.REQUIRED, example = "true")
private Boolean autoColor = AUTO_COLOR;
private Boolean autoColor;
@Schema(description = "是/否检查 page 是否存在", requiredMode = Schema.RequiredMode.REQUIRED, example = "true")
private Boolean checkPath = CHECK_PATH;
private Boolean checkPath;
@Schema(description = "是/否需要透明底色", requiredMode = Schema.RequiredMode.REQUIRED, example = "true")
private Boolean hyaline = HYALINE;
private Boolean hyaline;
}