|
@@ -1,8 +1,17 @@
|
|
|
package cn.tr.module.smart.wx.config;
|
|
package cn.tr.module.smart.wx.config;
|
|
|
|
|
|
|
|
import cn.binarywang.wx.miniapp.api.WxMaService;
|
|
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.dev33.satoken.stp.StpLogic;
|
|
|
import cn.hutool.core.util.StrUtil;
|
|
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.LoginTypeConstant;
|
|
|
import cn.tr.module.sys.oauth2.dto.AccountUserInfoDTO;
|
|
import cn.tr.module.sys.oauth2.dto.AccountUserInfoDTO;
|
|
|
import cn.tr.module.sys.oauth2.dto.AccountUserInfoEditDTO;
|
|
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.dto.OAuth2PswReqDTO;
|
|
|
import cn.tr.module.sys.oauth2.psw.operator.AbstractOAuth2PswUserOperator;
|
|
import cn.tr.module.sys.oauth2.psw.operator.AbstractOAuth2PswUserOperator;
|
|
|
import cn.tr.plugin.security.utils.SaTokenUtils;
|
|
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.beans.factory.annotation.Autowired;
|
|
|
import org.springframework.stereotype.Component;
|
|
import org.springframework.stereotype.Component;
|
|
|
|
|
|
|
|
|
|
+import javax.servlet.http.HttpServletRequest;
|
|
|
|
|
+import java.util.Date;
|
|
|
|
|
+
|
|
|
/**
|
|
/**
|
|
|
* @ClassName : WxAppletOauth2UserOperator
|
|
* @ClassName : WxAppletOauth2UserOperator
|
|
|
|
|
+ * @Date: 2025年05月19日
|
|
|
* @see <a href ="https://i-blog.csdnimg.cn/blog_migrate/34d657a0a813d1d774a9bc7eb8908311.png"/>微信登录流程图</a>
|
|
* @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>
|
|
* @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
|
|
@Component
|
|
|
public class WxAppletOauth2UserOperator extends AbstractOAuth2PswUserOperator {
|
|
public class WxAppletOauth2UserOperator extends AbstractOAuth2PswUserOperator {
|
|
|
|
|
|
|
|
@Autowired
|
|
@Autowired
|
|
|
private WxMaService wxMaService;
|
|
private WxMaService wxMaService;
|
|
|
|
|
+
|
|
|
|
|
+ @Autowired
|
|
|
|
|
+ private IBizWxUserService wxUserService;
|
|
|
|
|
+
|
|
|
@Override
|
|
@Override
|
|
|
public String auth(OAuth2PswReqDTO source) {
|
|
public String auth(OAuth2PswReqDTO source) {
|
|
|
String appId = source.getAppId();
|
|
String appId = source.getAppId();
|
|
@@ -31,15 +49,42 @@ public class WxAppletOauth2UserOperator extends AbstractOAuth2PswUserOperator {
|
|
|
if (!wxMaService.switchover(appId)) {
|
|
if (!wxMaService.switchover(appId)) {
|
|
|
throw new IllegalArgumentException(String.format("未找到对应appid=[%s]的配置,请核实!", appId));
|
|
throw new IllegalArgumentException(String.format("未找到对应appid=[%s]的配置,请核实!", appId));
|
|
|
}
|
|
}
|
|
|
- if(StrUtil.isEmpty(wxAppletCode)){
|
|
|
|
|
|
|
+ if (StrUtil.isEmpty(wxAppletCode)) {
|
|
|
throw new IllegalArgumentException("微信登陆码不能为空");
|
|
throw new IllegalArgumentException("微信登陆码不能为空");
|
|
|
}
|
|
}
|
|
|
//todo
|
|
//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
|
|
@Override
|