|
|
@@ -17,12 +17,14 @@ 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.smart.web.dto.WxAppletEncodeInfoDTO;
|
|
|
import cn.tr.module.sys.oauth2.LoginTypeConstant;
|
|
|
import cn.tr.module.sys.oauth2.dto.AccountUserInfoDTO;
|
|
|
import cn.tr.module.sys.oauth2.dto.AccountUserInfoEditDTO;
|
|
|
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.module.sys.oauth2.psw.operator.LoginOAuth2PswUserOperator;
|
|
|
import cn.tr.module.sys.user.dto.SysUserDTO;
|
|
|
import cn.tr.module.sys.user.service.ISysUserService;
|
|
|
import cn.tr.plugin.security.bo.UserLoginInfoBO;
|
|
|
@@ -30,6 +32,7 @@ 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.context.annotation.Lazy;
|
|
|
import org.springframework.stereotype.Component;
|
|
|
|
|
|
import javax.servlet.http.HttpServletRequest;
|
|
|
@@ -45,6 +48,9 @@ import java.util.Optional;
|
|
|
@Component
|
|
|
public class WxAppletOauth2UserOperator extends AbstractOAuth2PswUserOperator {
|
|
|
|
|
|
+ @Autowired
|
|
|
+ @Lazy
|
|
|
+ private LoginOAuth2PswUserOperator self;
|
|
|
@Autowired
|
|
|
private WxMaService wxMaService;
|
|
|
|
|
|
@@ -59,8 +65,16 @@ public class WxAppletOauth2UserOperator extends AbstractOAuth2PswUserOperator {
|
|
|
String appId = source.getAppId();
|
|
|
String wxAppletCode = source.getWxAppletCode();
|
|
|
String username = source.getUsername();
|
|
|
- //对账号进行校验
|
|
|
- SysUserDTO user = sysUserService.selectUserByUsername(username);
|
|
|
+ Date loginTime = new Date();
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ //获取Request对象
|
|
|
+ HttpServletRequest request = ServletUtils.getRequest();
|
|
|
+ String ip = ServletUtils.getClientIP(request);
|
|
|
+ String cityInfo = IpUtil.getCityInfo(ip);
|
|
|
+
|
|
|
+ //对微信用户表进行查询
|
|
|
if (!wxMaService.switchover(appId)) {
|
|
|
throw new IllegalArgumentException(String.format("未找到对应appid=[%s]的配置,请核实!", appId));
|
|
|
}
|
|
|
@@ -69,12 +83,11 @@ public class WxAppletOauth2UserOperator extends AbstractOAuth2PswUserOperator {
|
|
|
}
|
|
|
try {
|
|
|
WxMaJscode2SessionResult sessionInfo = wxMaService.getUserService().getSessionInfo(wxAppletCode);
|
|
|
+// WxMaJscode2SessionResult sessionInfo=new WxMaJscode2SessionResult();
|
|
|
+// sessionInfo.setOpenid("0d3DO5000LXJjU1dib300i6FX31DO50t");
|
|
|
+// sessionInfo.setUnionid("123");
|
|
|
String openid = sessionInfo.getOpenid();
|
|
|
- Date loginTime = new Date();
|
|
|
- //获取Request对象
|
|
|
- HttpServletRequest request = ServletUtils.getRequest();
|
|
|
- String ip = ServletUtils.getClientIP(request);
|
|
|
- String cityInfo = IpUtil.getCityInfo(ip);
|
|
|
+// String sessionKey = sessionInfo.getSessionKey();
|
|
|
|
|
|
//根据openid查询用户信息 (通过数据库进行查询)
|
|
|
BizWxUserDTO wxUser = wxUserService.getUserByOpenidAndAppid(openid, appId);
|
|
|
@@ -82,11 +95,11 @@ public class WxAppletOauth2UserOperator extends AbstractOAuth2PswUserOperator {
|
|
|
wxUser = new BizWxUserDTO();
|
|
|
wxUser.setOpenId(openid);
|
|
|
wxUser.setAppId(appId);
|
|
|
+ wxUser.setNickname(username);
|
|
|
wxUser.setRegisterTime(loginTime);
|
|
|
wxUser.setLastLoginTime(loginTime);
|
|
|
wxUser.setLastLoginIp(ip);
|
|
|
wxUser.setLastLoginAddress(cityInfo);
|
|
|
- wxUser.setLastLoginTenantId(user.getTenantId());
|
|
|
if (StrUtil.isNotBlank(sessionInfo.getUnionid())){
|
|
|
wxUser.setUnionId(sessionInfo.getUnionid());
|
|
|
}
|
|
|
@@ -96,16 +109,19 @@ public class WxAppletOauth2UserOperator extends AbstractOAuth2PswUserOperator {
|
|
|
StpLogic stpUtil = SaTokenUtils.getStpUtil();
|
|
|
stpUtil.login(wxUser.getId());
|
|
|
String tokenValue = stpUtil.getTokenValue();
|
|
|
+ wxUserService.updateLastLoginInfo(wxUser.getId(),loginTime,ip,cityInfo);
|
|
|
+ //清除缓存
|
|
|
+ self.delUserLoginInfoCache(wxUser.getId());
|
|
|
|
|
|
UserAgent userAgent = Optional.ofNullable(UserAgentUtil.parse(ServletUtils.getRequest().getHeader("User-Agent"))).orElse(new UserAgent());
|
|
|
String browser = ObjectUtil.isEmpty(userAgent.getBrowser()) ? "未知" : userAgent.getBrowser().getName();
|
|
|
String os = ObjectUtil.isEmpty(userAgent.getOs()) ? "未知" : userAgent.getOs().getName();
|
|
|
|
|
|
UserLoginInfoBO loginInfo = UserLoginInfoBO.builder()
|
|
|
- .userId(user.getId())
|
|
|
- .nickname(user.getNickname())
|
|
|
+ .userId(wxUser.getId())
|
|
|
+ .nickname(wxUser.getNickname())
|
|
|
.username(username)
|
|
|
- .tenantId(user.getTenantId())
|
|
|
+ .tenantId(wxUser.getLastLoginTenantId())
|
|
|
.token(tokenValue)
|
|
|
.loginType(StpUtil.TYPE)
|
|
|
.loginIp(wxUser.getLastLoginIp())
|
|
|
@@ -113,14 +129,14 @@ public class WxAppletOauth2UserOperator extends AbstractOAuth2PswUserOperator {
|
|
|
.loginTime(loginTime)
|
|
|
.browser(browser)
|
|
|
.os(os)
|
|
|
- .orgId(user.getOrgId())
|
|
|
- .signature(user.getSignature())
|
|
|
+ .orgId("")
|
|
|
+ .signature("")
|
|
|
.build();
|
|
|
setSessionUser(tokenValue,loginInfo);
|
|
|
return tokenValue;
|
|
|
|
|
|
- } catch (WxErrorException e) {
|
|
|
- throw new ServiceException(TRExcCode.WX_LOGIN_FAIL, "微信登录失败:" + e.getError().getErrorMsg());
|
|
|
+ } catch (Exception e) {
|
|
|
+ throw new ServiceException(TRExcCode.WX_LOGIN_FAIL, "微信登录失败:" + e.getMessage());
|
|
|
}
|
|
|
|
|
|
}
|