Commit b227b039c961d04ba49af8f022278c784b4c3431

Authored by puhui999
1 parent 33e8c7ca

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

yudao-module-member/yudao-module-member-biz/src/main/java/cn/iocoder/yudao/module/member/controller/app/social/AppSocialUserController.java
1 package cn.iocoder.yudao.module.member.controller.app.social; 1 package cn.iocoder.yudao.module.member.controller.app.social;
2 2
  3 +import cn.hutool.core.codec.Base64;
  4 +import cn.hutool.core.util.ObjUtil;
3 import cn.iocoder.yudao.framework.common.enums.UserTypeEnum; 5 import cn.iocoder.yudao.framework.common.enums.UserTypeEnum;
4 import cn.iocoder.yudao.framework.common.pojo.CommonResult; 6 import cn.iocoder.yudao.framework.common.pojo.CommonResult;
5 import cn.iocoder.yudao.framework.common.util.object.BeanUtils; 7 import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
@@ -22,8 +24,6 @@ import jakarta.validation.Valid; @@ -22,8 +24,6 @@ import jakarta.validation.Valid;
22 import org.springframework.validation.annotation.Validated; 24 import org.springframework.validation.annotation.Validated;
23 import org.springframework.web.bind.annotation.*; 25 import org.springframework.web.bind.annotation.*;
24 26
25 -import java.util.Base64;  
26 -  
27 import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success; 27 import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;
28 import static cn.iocoder.yudao.framework.security.core.util.SecurityFrameworkUtils.getLoginUserId; 28 import static cn.iocoder.yudao.framework.security.core.util.SecurityFrameworkUtils.getLoginUserId;
29 29
@@ -33,6 +33,13 @@ import static cn.iocoder.yudao.framework.security.core.util.SecurityFrameworkUti @@ -33,6 +33,13 @@ import static cn.iocoder.yudao.framework.security.core.util.SecurityFrameworkUti
33 @Validated 33 @Validated
34 public class AppSocialUserController { 34 public class AppSocialUserController {
35 35
  36 + public static final String ENV_VERSION = "release"; // 小程序版本。正式版为 "release",体验版为 "trial",开发版为 "develop"
  37 + private static final String SCENE = ""; // 页面路径不能携带参数(参数请放在scene字段里)
  38 + private static final Integer WIDTH = 430; // 二维码宽度
  39 + private static final Boolean AUTO_COLOR = true; // 默认true 自动配置线条颜色,如果颜色依然是黑色,则说明不建议配置主色调
  40 + private static final Boolean CHECK_PATH = true; // 默认true 检查 page 是否存在
  41 + private static final Boolean HYALINE = true; // 是否需要透明底色, hyaline 为true时,生成透明底色的小程序码
  42 +
36 @Resource 43 @Resource
37 private SocialUserApi socialUserApi; 44 private SocialUserApi socialUserApi;
38 @Resource 45 @Resource
@@ -69,10 +76,13 @@ public class AppSocialUserController { @@ -69,10 +76,13 @@ public class AppSocialUserController {
69 @PostMapping("/wxa-qrcode") 76 @PostMapping("/wxa-qrcode")
70 @Operation(summary = "获得微信小程序码(base64 image)") 77 @Operation(summary = "获得微信小程序码(base64 image)")
71 public CommonResult<String> getWxQrcode(@RequestBody @Valid AppSocialWxQrcodeReqVO reqVO) { 78 public CommonResult<String> getWxQrcode(@RequestBody @Valid AppSocialWxQrcodeReqVO reqVO) {
72 - byte[] wxQrcode = socialClientApi.getWxaQrcode(BeanUtils.toBean(reqVO, SocialWxQrcodeReqDTO.class)  
73 - .setEnvVersion(AppSocialWxQrcodeReqVO.ENV_VERSION));  
74 - // TODO @puhui999:1)是不是 base64 返回,不拼接哈 data:image/png;base64;2)cn.hutool.core.codec.Base64.encode()  
75 - return success("data:image/png;base64," + Base64.getEncoder().encodeToString(wxQrcode)); 79 + byte[] wxQrcode = socialClientApi.getWxaQrcode(new SocialWxQrcodeReqDTO().setPath(reqVO.getPath())
  80 + .setEnvVersion(ENV_VERSION).setWidth(ObjUtil.defaultIfNull(reqVO.getWidth(), WIDTH))
  81 + .setScene(ObjUtil.defaultIfNull(reqVO.getScene(), SCENE))
  82 + .setAutoColor(ObjUtil.defaultIfNull(reqVO.getAutoColor(), AUTO_COLOR))
  83 + .setHyaline(ObjUtil.defaultIfNull(reqVO.getHyaline(), HYALINE))
  84 + .setCheckPath(ObjUtil.defaultIfNull(reqVO.getCheckPath(), CHECK_PATH)));
  85 + return success(Base64.encode(wxQrcode));
76 } 86 }
77 87
78 } 88 }
yudao-module-member/yudao-module-member-biz/src/main/java/cn/iocoder/yudao/module/member/controller/app/social/vo/AppSocialWxQrcodeReqVO.java
@@ -9,20 +9,11 @@ import lombok.Data; @@ -9,20 +9,11 @@ import lombok.Data;
9 @Data 9 @Data
10 public class AppSocialWxQrcodeReqVO { 10 public class AppSocialWxQrcodeReqVO {
11 11
12 - // TODO @puhui999: 没有默认值 getQrcodeService().createWxaCodeUnlimitBytes() 转类型会报错 🤣  
13 - // TODO @puhui999:懂了哈;default 最好在 controller 搞;对于 VO 来说,不给默认值;  
14 - public static String ENV_VERSION = "release"; // 小程序版本。正式版为 "release",体验版为 "trial",开发版为 "develop"  
15 - private static String SCENE = ""; // 页面路径不能携带参数(参数请放在scene字段里)  
16 - private static Integer WIDTH = 430; // 二维码宽度  
17 - private static Boolean AUTO_COLOR = true; // 默认true 自动配置线条颜色,如果颜色依然是黑色,则说明不建议配置主色调  
18 - private static Boolean CHECK_PATH = true; // 默认true 检查 page 是否存在  
19 - private static Boolean HYALINE = true; // 是否需要透明底色, is_hyaline 为true时,生成透明底色的小程序码  
20 -  
21 /** 12 /**
22 * 页面路径不能携带参数(参数请放在scene字段里) 13 * 页面路径不能携带参数(参数请放在scene字段里)
23 */ 14 */
24 @Schema(description = "场景值", requiredMode = Schema.RequiredMode.REQUIRED, example = "1001") 15 @Schema(description = "场景值", requiredMode = Schema.RequiredMode.REQUIRED, example = "1001")
25 - private String scene = SCENE; 16 + private String scene;
26 17
27 /** 18 /**
28 * 默认是主页,页面 page,例如 pages/index/index,根路径前不要填加 /,不能携带参数(参数请放在scene字段里), 19 * 默认是主页,页面 page,例如 pages/index/index,根路径前不要填加 /,不能携带参数(参数请放在scene字段里),
@@ -33,15 +24,15 @@ public class AppSocialWxQrcodeReqVO { @@ -33,15 +24,15 @@ public class AppSocialWxQrcodeReqVO {
33 private String path; 24 private String path;
34 25
35 @Schema(description = "二维码宽度", requiredMode = Schema.RequiredMode.REQUIRED, example = "430") 26 @Schema(description = "二维码宽度", requiredMode = Schema.RequiredMode.REQUIRED, example = "430")
36 - private Integer width = WIDTH; 27 + private Integer width;
37 28
38 @Schema(description = "是/否自动配置线条颜色", requiredMode = Schema.RequiredMode.REQUIRED, example = "true") 29 @Schema(description = "是/否自动配置线条颜色", requiredMode = Schema.RequiredMode.REQUIRED, example = "true")
39 - private Boolean autoColor = AUTO_COLOR; 30 + private Boolean autoColor;
40 31
41 @Schema(description = "是/否检查 page 是否存在", requiredMode = Schema.RequiredMode.REQUIRED, example = "true") 32 @Schema(description = "是/否检查 page 是否存在", requiredMode = Schema.RequiredMode.REQUIRED, example = "true")
42 - private Boolean checkPath = CHECK_PATH; 33 + private Boolean checkPath;
43 34
44 @Schema(description = "是/否需要透明底色", requiredMode = Schema.RequiredMode.REQUIRED, example = "true") 35 @Schema(description = "是/否需要透明底色", requiredMode = Schema.RequiredMode.REQUIRED, example = "true")
45 - private Boolean hyaline = HYALINE; 36 + private Boolean hyaline;
46 37
47 } 38 }