|
|
@@ -5,9 +5,14 @@ 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.StpUtil;
|
|
|
+import cn.hutool.core.util.ObjectUtil;
|
|
|
import cn.hutool.core.util.StrUtil;
|
|
|
+import cn.hutool.http.useragent.UserAgent;
|
|
|
+import cn.hutool.http.useragent.UserAgentUtil;
|
|
|
import cn.tr.core.exception.ServiceException;
|
|
|
import cn.tr.core.exception.TRExcCode;
|
|
|
+import cn.tr.core.utils.IpUtil;
|
|
|
import cn.tr.core.utils.ServletUtils;
|
|
|
import cn.tr.module.smart.common.dto.BizWxUserDTO;
|
|
|
import cn.tr.module.smart.common.po.BizWxUserPO;
|
|
|
@@ -18,6 +23,9 @@ 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.user.dto.SysUserDTO;
|
|
|
+import cn.tr.module.sys.user.service.ISysUserService;
|
|
|
+import cn.tr.plugin.security.bo.UserLoginInfoBO;
|
|
|
import cn.tr.plugin.security.utils.SaTokenUtils;
|
|
|
import me.chanjar.weixin.common.error.WxErrorException;
|
|
|
import org.checkerframework.checker.units.qual.A;
|
|
|
@@ -26,6 +34,7 @@ import org.springframework.stereotype.Component;
|
|
|
|
|
|
import javax.servlet.http.HttpServletRequest;
|
|
|
import java.util.Date;
|
|
|
+import java.util.Optional;
|
|
|
|
|
|
/**
|
|
|
* @ClassName : WxAppletOauth2UserOperator
|
|
|
@@ -42,24 +51,30 @@ public class WxAppletOauth2UserOperator extends AbstractOAuth2PswUserOperator {
|
|
|
@Autowired
|
|
|
private IBizWxUserService wxUserService;
|
|
|
|
|
|
+ @Autowired
|
|
|
+ private ISysUserService sysUserService;
|
|
|
+
|
|
|
@Override
|
|
|
public String auth(OAuth2PswReqDTO source) {
|
|
|
String appId = source.getAppId();
|
|
|
String wxAppletCode = source.getWxAppletCode();
|
|
|
+ String username = source.getUsername();
|
|
|
+ //对账号进行校验
|
|
|
+ SysUserDTO user = sysUserService.selectUserByUsername(username);
|
|
|
if (!wxMaService.switchover(appId)) {
|
|
|
throw new IllegalArgumentException(String.format("未找到对应appid=[%s]的配置,请核实!", appId));
|
|
|
}
|
|
|
if (StrUtil.isEmpty(wxAppletCode)) {
|
|
|
throw new IllegalArgumentException("微信登陆码不能为空");
|
|
|
}
|
|
|
- //todo
|
|
|
- //调用微信接口获取session信息
|
|
|
try {
|
|
|
WxMaJscode2SessionResult sessionInfo = wxMaService.getUserService().getSessionInfo(wxAppletCode);
|
|
|
String openid = sessionInfo.getOpenid();
|
|
|
- String sessionKey = sessionInfo.getSessionKey();
|
|
|
+ Date loginTime = new Date();
|
|
|
//获取Request对象
|
|
|
HttpServletRequest request = ServletUtils.getRequest();
|
|
|
+ String ip = ServletUtils.getClientIP(request);
|
|
|
+ String cityInfo = IpUtil.getCityInfo(ip);
|
|
|
|
|
|
//根据openid查询用户信息 (通过数据库进行查询)
|
|
|
BizWxUserDTO wxUser = wxUserService.getUserByOpenidAndAppid(openid, appId);
|
|
|
@@ -67,9 +82,11 @@ public class WxAppletOauth2UserOperator extends AbstractOAuth2PswUserOperator {
|
|
|
wxUser = new BizWxUserDTO();
|
|
|
wxUser.setOpenId(openid);
|
|
|
wxUser.setAppId(appId);
|
|
|
- wxUser.setRegisterTime(new Date());
|
|
|
- wxUser.setLastLoginTime(new Date());
|
|
|
- wxUser.setLastLoginIp(ServletUtils.getClientIP(request));
|
|
|
+ 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());
|
|
|
}
|
|
|
@@ -78,8 +95,29 @@ public class WxAppletOauth2UserOperator extends AbstractOAuth2PswUserOperator {
|
|
|
|
|
|
StpLogic stpUtil = SaTokenUtils.getStpUtil();
|
|
|
stpUtil.login(wxUser.getId());
|
|
|
- stpUtil.getSession().set("wxSessionKey", sessionKey);
|
|
|
- return stpUtil.getTokenValue();
|
|
|
+ String tokenValue = stpUtil.getTokenValue();
|
|
|
+
|
|
|
+ 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())
|
|
|
+ .username(username)
|
|
|
+ .tenantId(user.getTenantId())
|
|
|
+ .token(tokenValue)
|
|
|
+ .loginType(StpUtil.TYPE)
|
|
|
+ .loginIp(wxUser.getLastLoginIp())
|
|
|
+ .loginLocation(cityInfo)
|
|
|
+ .loginTime(loginTime)
|
|
|
+ .browser(browser)
|
|
|
+ .os(os)
|
|
|
+ .orgId(user.getOrgId())
|
|
|
+ .signature(user.getSignature())
|
|
|
+ .build();
|
|
|
+ setSessionUser(tokenValue,loginInfo);
|
|
|
+ return tokenValue;
|
|
|
|
|
|
} catch (WxErrorException e) {
|
|
|
throw new ServiceException(TRExcCode.WX_LOGIN_FAIL, "微信登录失败:" + e.getError().getErrorMsg());
|