|
|
@@ -31,6 +31,7 @@ import cn.tr.plugin.security.bo.UserLoginInfoBO;
|
|
|
import cn.tr.plugin.security.utils.SaTokenUtils;
|
|
|
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
|
|
import lombok.SneakyThrows;
|
|
|
+import lombok.extern.slf4j.Slf4j;
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
import org.springframework.context.annotation.Lazy;
|
|
|
import org.springframework.stereotype.Component;
|
|
|
@@ -45,6 +46,7 @@ import java.util.Optional;
|
|
|
* @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>
|
|
|
*/
|
|
|
+@Slf4j
|
|
|
@Component
|
|
|
public class WxAppletOauth2UserOperator extends AbstractOAuth2PswUserOperator {
|
|
|
|
|
|
@@ -85,73 +87,80 @@ public class WxAppletOauth2UserOperator extends AbstractOAuth2PswUserOperator {
|
|
|
if (StrUtil.isEmpty(wxAppletCode)) {
|
|
|
throw new IllegalArgumentException("微信登陆码不能为空");
|
|
|
}
|
|
|
+ WxMaJscode2SessionResult sessionInfo;
|
|
|
+ try {
|
|
|
+ sessionInfo = wxMaService.getUserService().getSessionInfo(wxAppletCode);
|
|
|
+ } catch (Exception e) {
|
|
|
+ log.error("微信服务调用失败 | appId:{} | code:{} | 错误:{}",
|
|
|
+ source.getAppId(), source.getWxAppletCode(), e.getMessage(), e);
|
|
|
+ throw new ServiceException(TRExcCode.SYSTEM_ERROR_B0001, "微信授权失败: " + e.getMessage());
|
|
|
+ }
|
|
|
|
|
|
- WxMaJscode2SessionResult sessionInfo = wxMaService.getUserService().getSessionInfo(wxAppletCode);
|
|
|
- String openid = sessionInfo.getOpenid();
|
|
|
- //根据openid查询用户信息 (通过数据库进行查询)
|
|
|
- BizWxUserDTO wxUser = wxUserService.getUserByOpenidAndAppid(openid, appId);
|
|
|
- if (wxUser == null) {
|
|
|
- wxUser = new BizWxUserDTO();
|
|
|
- wxUser.setId(IdUtil.getSnowflake().nextIdStr());
|
|
|
- wxUser.setOpenId(openid);
|
|
|
- wxUser.setAppId(appId);
|
|
|
- wxUser.setNickname(username);
|
|
|
- wxUser.setRegisterTime(loginTime);
|
|
|
- wxUser.setLastLoginTime(loginTime);
|
|
|
- wxUser.setLastLoginIp(ip);
|
|
|
- wxUser.setLastLoginAddress(cityInfo);
|
|
|
- wxUser.setLastLoginTenantId(source.getTenantId());
|
|
|
- if (StrUtil.isNotBlank(sessionInfo.getUnionid())){
|
|
|
- wxUser.setUnionId(sessionInfo.getUnionid());
|
|
|
- }
|
|
|
- wxUserService.insertBizWxUser(wxUser);
|
|
|
- }else {
|
|
|
- wxUser.setLastLoginIp(ip);
|
|
|
- wxUser.setLastLoginTime(loginTime);
|
|
|
- wxUser.setLastLoginAddress(cityInfo);
|
|
|
+ String openid = sessionInfo.getOpenid();
|
|
|
+ //根据openid查询用户信息 (通过数据库进行查询)
|
|
|
+ BizWxUserDTO wxUser = wxUserService.getUserByOpenidAndAppid(openid, appId);
|
|
|
+ if (wxUser == null) {
|
|
|
+ wxUser = new BizWxUserDTO();
|
|
|
+ wxUser.setId(IdUtil.getSnowflake().nextIdStr());
|
|
|
+ wxUser.setOpenId(openid);
|
|
|
+ wxUser.setAppId(appId);
|
|
|
+ wxUser.setNickname(username);
|
|
|
+ wxUser.setRegisterTime(loginTime);
|
|
|
+ wxUser.setLastLoginTime(loginTime);
|
|
|
+ wxUser.setLastLoginIp(ip);
|
|
|
+ wxUser.setLastLoginAddress(cityInfo);
|
|
|
+ wxUser.setLastLoginTenantId(source.getTenantId());
|
|
|
+ if (StrUtil.isNotBlank(sessionInfo.getUnionid())) {
|
|
|
+ wxUser.setUnionId(sessionInfo.getUnionid());
|
|
|
}
|
|
|
+ wxUserService.insertBizWxUser(wxUser);
|
|
|
+ } else {
|
|
|
+ wxUser.setLastLoginIp(ip);
|
|
|
+ wxUser.setLastLoginTime(loginTime);
|
|
|
+ wxUser.setLastLoginAddress(cityInfo);
|
|
|
+ }
|
|
|
|
|
|
- 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(wxUser.getId())
|
|
|
- .nickname(wxUser.getNickname())
|
|
|
- .username(username)
|
|
|
- .tenantId(wxUser.getLastLoginTenantId())
|
|
|
- .token(tokenValue)
|
|
|
- .loginType(LoginTypeConstant.WX_APPLET)
|
|
|
- .loginIp(wxUser.getLastLoginIp())
|
|
|
- .loginLocation(cityInfo)
|
|
|
- .loginTime(loginTime)
|
|
|
- .browser(browser)
|
|
|
- .os(os)
|
|
|
- .orgId("")
|
|
|
- .signature("")
|
|
|
- .build();
|
|
|
-
|
|
|
- if(StrUtil.isNotBlank(source.getTenantId())){
|
|
|
- //如果有医院信息,则绑定医院关系
|
|
|
- BizWxUserHospitalPO wxUserHospital = wxUserHospitalRepository.selectOne(new LambdaQueryWrapper<BizWxUserHospitalPO>()
|
|
|
- .eq(BizWxUserHospitalPO::getWxUserId, wxUser.getId())
|
|
|
- .eq(BizWxUserHospitalPO::getTenantId, source.getTenantId())
|
|
|
- .last("limit 1"));
|
|
|
- if (ObjectUtil.isNull(wxUserHospital)) {
|
|
|
- wxUserHospital.setWxUserId(wxUser.getId());
|
|
|
- wxUserHospital.setTenantId(source.getTenantId());
|
|
|
- wxUserHospitalRepository.insert(wxUserHospital);
|
|
|
- }
|
|
|
+ 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(wxUser.getId())
|
|
|
+ .nickname(wxUser.getNickname())
|
|
|
+ .username(username)
|
|
|
+ .tenantId(wxUser.getLastLoginTenantId())
|
|
|
+ .token(tokenValue)
|
|
|
+ .loginType(LoginTypeConstant.WX_APPLET)
|
|
|
+ .loginIp(wxUser.getLastLoginIp())
|
|
|
+ .loginLocation(cityInfo)
|
|
|
+ .loginTime(loginTime)
|
|
|
+ .browser(browser)
|
|
|
+ .os(os)
|
|
|
+ .orgId("")
|
|
|
+ .signature("")
|
|
|
+ .build();
|
|
|
+
|
|
|
+ if (StrUtil.isNotBlank(source.getTenantId())) {
|
|
|
+ //如果有医院信息,则绑定医院关系
|
|
|
+ BizWxUserHospitalPO wxUserHospital = wxUserHospitalRepository.selectOne(new LambdaQueryWrapper<BizWxUserHospitalPO>()
|
|
|
+ .eq(BizWxUserHospitalPO::getWxUserId, wxUser.getId())
|
|
|
+ .eq(BizWxUserHospitalPO::getTenantId, source.getTenantId())
|
|
|
+ .last("limit 1"));
|
|
|
+ if (ObjectUtil.isNull(wxUserHospital)) {
|
|
|
+ wxUserHospital.setWxUserId(wxUser.getId());
|
|
|
+ wxUserHospital.setTenantId(source.getTenantId());
|
|
|
+ wxUserHospitalRepository.insert(wxUserHospital);
|
|
|
}
|
|
|
- setSessionUser(tokenValue,loginInfo);
|
|
|
- return tokenValue;
|
|
|
+ }
|
|
|
+ setSessionUser(tokenValue, loginInfo);
|
|
|
+ return tokenValue;
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
@@ -162,8 +171,8 @@ public class WxAppletOauth2UserOperator extends AbstractOAuth2PswUserOperator {
|
|
|
@Override
|
|
|
@TenantIgnore
|
|
|
public OAuth2PswLoginInfoDTO getUserLoginInfo() {
|
|
|
- String loginId =String.valueOf( SaTokenUtils.getStpUtil().getLoginId());
|
|
|
- return BizWxUserMapper.INSTANCE.convertPswLoginInfo(wxUserService.selectBizWxUserById(loginId));
|
|
|
+ String loginId = String.valueOf(SaTokenUtils.getStpUtil().getLoginId());
|
|
|
+ return BizWxUserMapper.INSTANCE.convertPswLoginInfo(wxUserService.selectBizWxUserById(loginId));
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
@@ -175,13 +184,13 @@ public class WxAppletOauth2UserOperator extends AbstractOAuth2PswUserOperator {
|
|
|
@TenantIgnore
|
|
|
public void updateAccountUserInfo(String userId, AccountUserInfoEditDTO source) {
|
|
|
BizWxUserPO wxUser = wxUserRepository.selectById(userId);
|
|
|
- if(ObjectUtil.isNotNull(wxUser)){
|
|
|
+ if (ObjectUtil.isNotNull(wxUser)) {
|
|
|
wxUser.setNickname(source.getNickname());
|
|
|
wxUser.setAvatar(source.getAvatar());
|
|
|
wxUser.setPhone(source.getPhone());
|
|
|
wxUserRepository.updateById(wxUser);
|
|
|
- }else {
|
|
|
- throw new ServiceException(TRExcCode.SYSTEM_ERROR_B0001,"该用户不存在");
|
|
|
+ } else {
|
|
|
+ throw new ServiceException(TRExcCode.SYSTEM_ERROR_B0001, "该用户不存在");
|
|
|
}
|
|
|
|
|
|
}
|