Sfoglia il codice sorgente

add:auth方法完善

zhouzeyu 7 mesi fa
parent
commit
ea8fcdcb82

+ 4 - 0
tr-framework/src/main/java/cn/tr/core/exception/TRExcCode.java

@@ -57,6 +57,8 @@ public enum TRExcCode implements BaseCode {
     USER_ERROR_A0243("A0243", "OAuth2 认证失败"),
     USER_ERROR_A0204("A0244", "租户被禁用"),
     USER_ERROR_A0205("A0245", "租户已被删除"),
+
+    WX_LOGIN_FAIL("A0246", "微信登录失败"),
     /**** 权限 **/
     USER_ERROR_A0300("A0300", "访问权限异常"),
     USER_ERROR_A0301("A0301", "访问未授权"),
@@ -208,6 +210,8 @@ public enum TRExcCode implements BaseCode {
     SERVICE_ERROR_C0503("C0503", "邮件提醒服务失败"),
 
 
+
+
     //DataX
     CONFIG_ERROR("Common-00", "您提供的配置文件存在错误信息,请检查您的作业配置 ."),
     CONVERT_NOT_SUPPORT("Common-01", "同步数据出现业务脏数据情况,数据类型转换错误 ."),

+ 5 - 0
tr-modules/tr-module-smartFollowUp/src/main/java/cn/tr/module/smart/common/service/IBizWxUserService.java

@@ -2,6 +2,8 @@ package cn.tr.module.smart.common.service;
 
 import cn.tr.module.smart.common.dto.BizWxUserDTO;
 import cn.tr.module.smart.common.dto.BizWxUserQueryDTO;
+import cn.tr.module.smart.common.po.BizWxUserPO;
+
 import java.util.*;
 
 /**
@@ -51,4 +53,7 @@ public interface IBizWxUserService{
      * @date    2025/05/23 14:03
      */
     int removeBizWxUserByIds(Collection<String> ids);
+
+
+    BizWxUserDTO getUserByOpenidAndAppid(String openid, String appId);
 }

+ 13 - 0
tr-modules/tr-module-smartFollowUp/src/main/java/cn/tr/module/smart/common/service/impl/BizWxUserServiceImpl.java

@@ -1,6 +1,7 @@
 package cn.tr.module.smart.common.service.impl;
 
 import cn.tr.core.exception.TRExcCode;
+import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.stereotype.Service;
 import cn.hutool.core.collection.CollectionUtil;
@@ -89,4 +90,16 @@ public class BizWxUserServiceImpl implements IBizWxUserService {
         }
         return baseRepository.deleteBatchIds(ids);
     };
+
+
+    @Override
+    public BizWxUserDTO getUserByOpenidAndAppid(String openid, String appId) {
+        QueryWrapper<BizWxUserPO> queryWrapper = new QueryWrapper<>();
+        queryWrapper.eq("open_id", openid).eq("app_id", appId);
+        BizWxUserPO bizWxUserPO = baseRepository.selectOne(queryWrapper);
+        if (bizWxUserPO != null) {
+            return BizWxUserMapper.INSTANCE.convertDto(bizWxUserPO);
+        }
+        throw new ServiceException(TRExcCode.SYSTEM_ERROR_B0001, "用户不存在");
+    }
 }

+ 51 - 6
tr-modules/tr-module-smartFollowUp/src/main/java/cn/tr/module/smart/wx/config/WxAppletOauth2UserOperator.java

@@ -1,8 +1,17 @@
 package cn.tr.module.smart.wx.config;
 
 import cn.binarywang.wx.miniapp.api.WxMaService;
+import cn.binarywang.wx.miniapp.bean.WxMaJscode2SessionResult;
+import cn.binarywang.wx.miniapp.config.WxMaConfig;
+import cn.dev33.satoken.context.SaHolder;
 import cn.dev33.satoken.stp.StpLogic;
 import cn.hutool.core.util.StrUtil;
+import cn.tr.core.exception.ServiceException;
+import cn.tr.core.exception.TRExcCode;
+import cn.tr.core.utils.ServletUtils;
+import cn.tr.module.smart.common.dto.BizWxUserDTO;
+import cn.tr.module.smart.common.po.BizWxUserPO;
+import cn.tr.module.smart.common.service.IBizWxUserService;
 import cn.tr.module.sys.oauth2.LoginTypeConstant;
 import cn.tr.module.sys.oauth2.dto.AccountUserInfoDTO;
 import cn.tr.module.sys.oauth2.dto.AccountUserInfoEditDTO;
@@ -10,20 +19,29 @@ import cn.tr.module.sys.oauth2.dto.OAuth2PswLoginInfoDTO;
 import cn.tr.module.sys.oauth2.dto.OAuth2PswReqDTO;
 import cn.tr.module.sys.oauth2.psw.operator.AbstractOAuth2PswUserOperator;
 import cn.tr.plugin.security.utils.SaTokenUtils;
+import me.chanjar.weixin.common.error.WxErrorException;
+import org.checkerframework.checker.units.qual.A;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.stereotype.Component;
 
+import javax.servlet.http.HttpServletRequest;
+import java.util.Date;
+
 /**
  * @ClassName : WxAppletOauth2UserOperator
+ * @Date: 2025年05月19日
  * @see <a href ="https://i-blog.csdnimg.cn/blog_migrate/34d657a0a813d1d774a9bc7eb8908311.png"/>微信登录流程图</a>
  * @see <a href="https://gitee.com/binary/weixin-java-miniapp-demo/blob/master/src/main/java/com/github/binarywang/demo/wx/miniapp/controller/WxMaUserController.java">调用接口示例</a>
- * @Date: 2025年05月19日
  */
 @Component
 public class WxAppletOauth2UserOperator extends AbstractOAuth2PswUserOperator {
 
     @Autowired
     private WxMaService wxMaService;
+
+    @Autowired
+    private IBizWxUserService wxUserService;
+
     @Override
     public String auth(OAuth2PswReqDTO source) {
         String appId = source.getAppId();
@@ -31,15 +49,42 @@ public class WxAppletOauth2UserOperator extends AbstractOAuth2PswUserOperator {
         if (!wxMaService.switchover(appId)) {
             throw new IllegalArgumentException(String.format("未找到对应appid=[%s]的配置,请核实!", appId));
         }
-        if(StrUtil.isEmpty(wxAppletCode)){
+        if (StrUtil.isEmpty(wxAppletCode)) {
             throw new IllegalArgumentException("微信登陆码不能为空");
         }
         //todo
+        //调用微信接口获取session信息
+        try {
+            WxMaJscode2SessionResult sessionInfo = wxMaService.getUserService().getSessionInfo(wxAppletCode);
+            String openid = sessionInfo.getOpenid();
+            String sessionKey = sessionInfo.getSessionKey();
+            //获取Request对象
+            HttpServletRequest request = ServletUtils.getRequest();
+
+            //根据openid查询用户信息 (通过数据库进行查询)
+            BizWxUserDTO wxUser = wxUserService.getUserByOpenidAndAppid(openid,  appId);
+            if (wxUser == null) {
+                wxUser = new BizWxUserDTO();
+                wxUser.setOpenId(openid);
+                wxUser.setAppId(appId);
+                wxUser.setRegisterTime(new Date());
+                wxUser.setLastLoginTime(new Date());
+                wxUser.setLastLoginIp(ServletUtils.getClientIP(request));
+                if (StrUtil.isNotBlank(sessionInfo.getUnionid())){
+                    wxUser.setUnionId(sessionInfo.getUnionid());
+                }
+                wxUserService.insertBizWxUser(wxUser);
+            }
+
+            StpLogic stpUtil = SaTokenUtils.getStpUtil();
+            stpUtil.login(wxUser.getId());
+            stpUtil.getSession().set("wxSessionKey", sessionKey);
+            return stpUtil.getTokenValue();
+
+        } catch (WxErrorException e) {
+            throw new ServiceException(TRExcCode.WX_LOGIN_FAIL, "微信登录失败:" + e.getError().getErrorMsg());
+        }
 
-        StpLogic stpUtil = SaTokenUtils.getStpUtil();
-        stpUtil.login("123");
-        String tokenValue = stpUtil.getTokenValue();
-        return tokenValue;
     }
 
     @Override