Browse Source

fix: 获得微信小程序码(base64 image)

puhui999 1 year ago
parent
commit
4c05bd340e

+ 8 - 7
yudao-module-member/yudao-module-member-biz/src/main/java/cn/iocoder/yudao/module/member/controller/app/social/AppSocialUserController.java

@@ -8,6 +8,7 @@ import cn.iocoder.yudao.module.member.controller.app.social.vo.AppSocialUserBind
 import cn.iocoder.yudao.module.member.controller.app.social.vo.AppSocialUserRespVO;
 import cn.iocoder.yudao.module.member.controller.app.social.vo.AppSocialUserUnbindReqVO;
 import cn.iocoder.yudao.module.member.controller.app.social.vo.AppSocialWxQrcodeReqVO;
+import cn.iocoder.yudao.module.system.api.social.SocialClientApi;
 import cn.iocoder.yudao.module.system.api.social.SocialUserApi;
 import cn.iocoder.yudao.module.system.api.social.dto.SocialUserBindReqDTO;
 import cn.iocoder.yudao.module.system.api.social.dto.SocialUserRespDTO;
@@ -34,6 +35,8 @@ public class AppSocialUserController {
 
     @Resource
     private SocialUserApi socialUserApi;
+    @Resource
+    private SocialClientApi socialClientApi;
 
     @PostMapping("/bind")
     @Operation(summary = "社交绑定,使用 code 授权码")
@@ -63,14 +66,12 @@ public class AppSocialUserController {
         return success(BeanUtils.toBean(socialUser, AppSocialUserRespVO.class));
     }
 
-    // TODO @puhui999:是不是 url 叫 wxa-qrcode?然后相关的方法,都做下调整哈;因为是微信小程序的二维码
-    @PostMapping("/wxacode")
-    @Operation(summary = "获得微信小程序码")
-    @PreAuthenticated // TODO @puhui999:可能不需要登录
+    @PostMapping("/wxa-qrcode")
+    @Operation(summary = "获得微信小程序码(base64 image)")
     public CommonResult<String> getWxQrcode(@RequestBody @Valid AppSocialWxQrcodeReqVO reqVO) {
-        byte[] wxQrcode = socialUserApi.getWxQrcode(BeanUtils.toBean(reqVO, SocialWxQrcodeReqDTO.class).setUserId(getLoginUserId())
-                .setUserType(UserTypeEnum.MEMBER.getValue()).setSocialType(reqVO.getType()));
-        return success(Base64.getEncoder().encodeToString(wxQrcode));
+        byte[] wxQrcode = socialClientApi.getWxaQrcode(BeanUtils.toBean(reqVO, SocialWxQrcodeReqDTO.class)
+                .setUserId(getLoginUserId()).setUserType(UserTypeEnum.MEMBER.getValue()).setSocialType(reqVO.getType()));
+        return success("data:image/png;base64," + Base64.getEncoder().encodeToString(wxQrcode));
     }
 
 }

+ 7 - 2
yudao-module-member/yudao-module-member-biz/src/main/java/cn/iocoder/yudao/module/member/controller/app/social/vo/AppSocialWxQrcodeReqVO.java

@@ -12,8 +12,9 @@ import lombok.Data;
 @Data
 public class AppSocialWxQrcodeReqVO {
 
-    // TODO @puhui999:这个后续不用前端传递,应该是后端搞的
-    private static String SCENE = "1011"; // 默认场景值 1011 扫描二维码
+    // TODO @puhui999:这个后续不用前端传递,应该是后端搞的。
+    //      页面路径不能携带参数(参数请放在scene字段里)
+    private static String SCENE = ""; // 默认场景值 1011 扫描二维码
     // TODO @puhui999:这个默认是不是 release 哈?
     private static String ENV_VERSION = "develop"; // 小程序版本。正式版为 "release",体验版为 "trial",开发版为 "develop"
     // TODO @puhui999:这个去掉;因为本身就是 430 啦;
@@ -28,6 +29,10 @@ public class AppSocialWxQrcodeReqVO {
     @Schema(description = "场景值", requiredMode = Schema.RequiredMode.REQUIRED, example = "1001")
     private String scene = SCENE;
 
+    /**
+     * 默认是主页,页面 page,例如 pages/index/index,根路径前不要填加 /,不能携带参数(参数请放在scene字段里),
+     * 如果不填写这个字段,默认跳主页面。scancode_time为系统保留参数,不允许配置
+     */
     @Schema(description = "页面路径", requiredMode = Schema.RequiredMode.REQUIRED, example = "pages/goods/index")
     @NotEmpty(message = "页面路径不能为空")
     private String path;

+ 10 - 0
yudao-module-system/yudao-module-system-api/src/main/java/cn/iocoder/yudao/module/system/api/social/SocialClientApi.java

@@ -2,7 +2,9 @@ package cn.iocoder.yudao.module.system.api.social;
 
 import cn.iocoder.yudao.module.system.api.social.dto.SocialWxJsapiSignatureRespDTO;
 import cn.iocoder.yudao.module.system.api.social.dto.SocialWxPhoneNumberInfoRespDTO;
+import cn.iocoder.yudao.module.system.api.social.dto.SocialWxQrcodeReqDTO;
 import cn.iocoder.yudao.module.system.enums.social.SocialTypeEnum;
+import jakarta.validation.Valid;
 
 /**
  * 社交应用的 API 接口
@@ -39,4 +41,12 @@ public interface SocialClientApi {
      */
     SocialWxPhoneNumberInfoRespDTO getWxMaPhoneNumberInfo(Integer userType, String phoneCode);
 
+    /**
+     * 获得小程序二维码
+     *
+     * @param reqVO 请求信息
+     * @return 小程序二维码
+     */
+    byte[] getWxaQrcode(@Valid SocialWxQrcodeReqDTO reqVO);
+
 }

+ 0 - 8
yudao-module-system/yudao-module-system-api/src/main/java/cn/iocoder/yudao/module/system/api/social/SocialUserApi.java

@@ -52,12 +52,4 @@ public interface SocialUserApi {
      */
     SocialUserRespDTO getSocialUserByCode(Integer userType, Integer socialType, String code, String state);
 
-    /**
-     * 获得小程序二维码
-     *
-     * @param reqVO 请求信息
-     * @return 小程序二维码
-     */
-    byte[] getWxQrcode(@Valid SocialWxQrcodeReqDTO reqVO);
-
 }

+ 6 - 0
yudao-module-system/yudao-module-system-biz/src/main/java/cn/iocoder/yudao/module/system/api/social/SocialClientApiImpl.java

@@ -4,6 +4,7 @@ import cn.binarywang.wx.miniapp.bean.WxMaPhoneNumberInfo;
 import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
 import cn.iocoder.yudao.module.system.api.social.dto.SocialWxJsapiSignatureRespDTO;
 import cn.iocoder.yudao.module.system.api.social.dto.SocialWxPhoneNumberInfoRespDTO;
+import cn.iocoder.yudao.module.system.api.social.dto.SocialWxQrcodeReqDTO;
 import cn.iocoder.yudao.module.system.service.social.SocialClientService;
 import me.chanjar.weixin.common.bean.WxJsapiSignature;
 import org.springframework.stereotype.Service;
@@ -40,4 +41,9 @@ public class SocialClientApiImpl implements SocialClientApi {
         return BeanUtils.toBean(info, SocialWxPhoneNumberInfoRespDTO.class);
     }
 
+    @Override
+    public byte[] getWxaQrcode(SocialWxQrcodeReqDTO reqVO) {
+        return socialClientService.getWxaQrcode(reqVO);
+    }
+
 }

+ 0 - 6
yudao-module-system/yudao-module-system-biz/src/main/java/cn/iocoder/yudao/module/system/api/social/SocialUserApiImpl.java

@@ -43,10 +43,4 @@ public class SocialUserApiImpl implements SocialUserApi {
        return socialUserService.getSocialUserByCode(userType, socialType, code, state);
     }
 
-    // TODO @puhui999:貌似搞到 SocialClientApiImpl 更合适,二维码和用户关系不大;这样 socialUserService 也不用绕一次了
-    @Override
-    public byte[] getWxQrcode(SocialWxQrcodeReqDTO reqVO) {
-        return socialUserService.getWxQrcode(reqVO);
-    }
-
 }

+ 1 - 1
yudao-module-system/yudao-module-system-biz/src/main/java/cn/iocoder/yudao/module/system/service/social/SocialClientService.java

@@ -67,7 +67,7 @@ public interface SocialClientService {
      * @param reqVO 请求信息
      * @return 小程序二维码
      */
-    byte[] getWxQrcode(SocialWxQrcodeReqDTO reqVO);
+    byte[] getWxaQrcode(SocialWxQrcodeReqDTO reqVO);
 
     // =================== 客户端管理 ===================
 

+ 1 - 1
yudao-module-system/yudao-module-system-biz/src/main/java/cn/iocoder/yudao/module/system/service/social/SocialClientServiceImpl.java

@@ -229,7 +229,7 @@ public class SocialClientServiceImpl implements SocialClientService {
     }
 
     @Override
-    public byte[] getWxQrcode(SocialWxQrcodeReqDTO reqVO) {
+    public byte[] getWxaQrcode(SocialWxQrcodeReqDTO reqVO) {
         WxMaService service = getWxMaService(reqVO.getUserType());
         try {
             return service.getQrcodeService().createWxaCodeUnlimitBytes(reqVO.getScene(), reqVO.getPath(),

+ 0 - 8
yudao-module-system/yudao-module-system-biz/src/main/java/cn/iocoder/yudao/module/system/service/social/SocialUserService.java

@@ -87,12 +87,4 @@ public interface SocialUserService {
      */
     PageResult<SocialUserDO> getSocialUserPage(SocialUserPageReqVO pageReqVO);
 
-    /**
-     * 获得小程序二维码
-     *
-     * @param reqVO 请求信息
-     * @return 小程序二维码
-     */
-    byte[] getWxQrcode(SocialWxQrcodeReqDTO reqVO);
-
 }

+ 0 - 5
yudao-module-system/yudao-module-system-biz/src/main/java/cn/iocoder/yudao/module/system/service/social/SocialUserServiceImpl.java

@@ -171,9 +171,4 @@ public class SocialUserServiceImpl implements SocialUserService {
         return socialUserMapper.selectPage(pageReqVO);
     }
 
-    @Override
-    public byte[] getWxQrcode(SocialWxQrcodeReqDTO reqVO) {
-        return socialClientService.getWxQrcode(reqVO);
-    }
-
 }