提交 41cb5db8 authored 作者: hzh's avatar hzh

代码优化

上级 4ab38072
...@@ -21,11 +21,12 @@ public interface RemoteUserService { ...@@ -21,11 +21,12 @@ public interface RemoteUserService {
* 通过用户名查询用户信息 * 通过用户名查询用户信息
* *
* @param username 用户名 * @param username 用户名
* @param phone 手机号
* @param tenantId 租户id * @param tenantId 租户id
* @param userType 用户类型 * @param userType 用户类型
* @return 结果 * @return 结果
*/ */
LoginUser getUserInfo(String username, String tenantId, String userType) throws UserException; LoginUser getUserInfo(String username, String phone, String tenantId, String userType) throws UserException;
/** /**
* 通过用户id查询用户信息 * 通过用户id查询用户信息
......
...@@ -38,33 +38,28 @@ public abstract class AbstractMallStrategy implements IAuthStrategy { ...@@ -38,33 +38,28 @@ public abstract class AbstractMallStrategy implements IAuthStrategy {
@Nullable @Nullable
Long getMemberId(LoginBody loginBody, XcxLoginUser loginUser) { Long getMemberId(LoginBody loginBody, XcxLoginUser loginUser) {
// 增加商城逻辑 // 增加商城逻辑
Long thirdUserId = null; Long thirdUserId;
if (StringUtils.equals(loginBody.getTenantId(), DEFAULT_TENANT_ID) && StringUtils.equals(loginUser.getUserType(), UserType.APP_USER.getUserType())) {
thirdUserId = loginUser.getUserId(); String tenantId = DEFAULT_TENANT_ID;
} else { String username = StringUtils.isNotEmpty(loginUser.getPhone()) ? loginUser.getPhone() : loginUser.getUsername();
// 创建用户 String userType = UserType.APP_USER.getUserType();
RemoteUserBo remoteUserBo = new RemoteUserBo(); String phone = loginUser.getPhone();
remoteUserBo.setTenantId(DEFAULT_TENANT_ID);
remoteUserBo.setUserName(StringUtils.isNotEmpty(loginUser.getPhone()) ? loginUser.getPhone() : loginUser.getUsername());
remoteUserBo.setNickName(remoteUserBo.getUserName());
remoteUserBo.setUserType(UserType.APP_USER.getUserType());
remoteUserBo.setPhonenumber(loginUser.getPhone());
try { try {
thirdUserId = remoteUserService.getUserInfo(remoteUserBo.getUserName(), remoteUserBo.getTenantId(), remoteUserBo.getUserType()).getUserId(); thirdUserId = remoteUserService.getUserInfo(username, phone, tenantId, userType).getUserId();
} catch (Exception e) { } catch (Exception e) {
log.info("用户不存在:{},租户id:{},需要进行注册", remoteUserBo.getUserName(), remoteUserBo.getTenantId()); // 创建用户
} RemoteUserBo remoteUserBo = new RemoteUserBo();
remoteUserBo.setTenantId(tenantId);
if (thirdUserId == null) { remoteUserBo.setUserName(username);
remoteUserBo.setNickName(remoteUserBo.getUserName());
remoteUserBo.setUserType(userType);
remoteUserBo.setPhonenumber(phone);
try { try {
thirdUserId = remoteUserService.registerUserInfo(remoteUserBo); thirdUserId = remoteUserService.registerUserInfo(remoteUserBo);
} catch (Exception e) { } catch (Exception ex) {
log.info("用户已存在:{},租户id:{}", remoteUserBo.getUserName(), remoteUserBo.getTenantId()); thirdUserId = remoteUserService.getUserInfo(username, phone, tenantId, userType).getUserId();
thirdUserId = remoteUserService.getUserInfo(remoteUserBo.getUserName(), remoteUserBo.getTenantId(), remoteUserBo.getUserType()).getUserId();
}
} }
} }
Long memberId = null; Long memberId = null;
...@@ -74,7 +69,7 @@ public abstract class AbstractMallStrategy implements IAuthStrategy { ...@@ -74,7 +69,7 @@ public abstract class AbstractMallStrategy implements IAuthStrategy {
rm = remoteMemberService.saveMember( rm = remoteMemberService.saveMember(
new RemoteMemberSave() new RemoteMemberSave()
.setThirdUserId(thirdUserId) .setThirdUserId(thirdUserId)
.setPhonenumber(loginUser.getPhone()) .setPhonenumber(phone)
.setRegisterIp(getClientIP()) .setRegisterIp(getClientIP())
); );
} }
......
...@@ -66,14 +66,16 @@ public class RemoteUserServiceImpl implements RemoteUserService { ...@@ -66,14 +66,16 @@ public class RemoteUserServiceImpl implements RemoteUserService {
* 通过用户名查询用户信息 * 通过用户名查询用户信息
* *
* @param username 用户名 * @param username 用户名
* @param phone 手机号
* @param tenantId 租户id * @param tenantId 租户id
* @return 结果 * @return 结果
*/ */
@Override @Override
public LoginUser getUserInfo(String username, String tenantId, String userType) throws UserException { public LoginUser getUserInfo(String username, String phone, String tenantId, String userType) throws UserException {
return TenantHelper.dynamic(tenantId, () -> { return TenantHelper.dynamic(tenantId, () -> {
SysUserVo sysUser = userMapper.selectVoOne(new LambdaQueryWrapper<SysUser>() SysUserVo sysUser = userMapper.selectVoOne(new LambdaQueryWrapper<SysUser>()
.eq(SysUser::getUserName, username) .eq(SysUser::getUserName, username)
.eq(StringUtils.isNotBlank(phone), SysUser::getPhonenumber, phone)
.eq(StringUtils.isNotBlank(userType), SysUser::getUserType, userType) .eq(StringUtils.isNotBlank(userType), SysUser::getUserType, userType)
); );
if (ObjectUtil.isNull(sysUser)) { if (ObjectUtil.isNull(sysUser)) {
...@@ -165,7 +167,8 @@ public class RemoteUserServiceImpl implements RemoteUserService { ...@@ -165,7 +167,8 @@ public class RemoteUserServiceImpl implements RemoteUserService {
*/ */
@Override @Override
public XcxLoginUser getUserInfoByOpenid(String openid, String phone, String tenantId) throws UserException { public XcxLoginUser getUserInfoByOpenid(String openid, String phone, String tenantId) throws UserException {
WxUserVo wxUser = wxUserService.selectUserByOpenId(openid, phone, tenantId); List<WxUserVo> wxUserList = wxUserService.selectUserByOpenId(openid, phone, tenantId);
WxUserVo wxUser = CollectionUtils.isEmpty(wxUserList) ? null : wxUserList.get(0);
if (!ObjectUtil.isNotNull(wxUser)) { if (!ObjectUtil.isNotNull(wxUser)) {
return null; return null;
} }
...@@ -213,6 +216,7 @@ public class RemoteUserServiceImpl implements RemoteUserService { ...@@ -213,6 +216,7 @@ public class RemoteUserServiceImpl implements RemoteUserService {
} }
return userMapper.exists(new LambdaQueryWrapper<SysUser>() return userMapper.exists(new LambdaQueryWrapper<SysUser>()
.eq(SysUser::getUserName, sysUserBo.getUserName()) .eq(SysUser::getUserName, sysUserBo.getUserName())
.eq(StringUtils.isNotBlank(sysUserBo.getPhonenumber()), SysUser::getPhonenumber, sysUserBo.getPhonenumber())
.eq(SysUser::getUserType, remoteUserBo.getUserType()) .eq(SysUser::getUserType, remoteUserBo.getUserType())
); );
}); });
...@@ -227,8 +231,8 @@ public class RemoteUserServiceImpl implements RemoteUserService { ...@@ -227,8 +231,8 @@ public class RemoteUserServiceImpl implements RemoteUserService {
public Boolean registerWxUserInfo(RemoteWxUserBo remoteWxUserBo) { public Boolean registerWxUserInfo(RemoteWxUserBo remoteWxUserBo) {
WxUserBo user = BeanUtil.copyProperties(remoteWxUserBo, WxUserBo.class); WxUserBo user = BeanUtil.copyProperties(remoteWxUserBo, WxUserBo.class);
WxUserVo wx = wxUserService.selectUserByOpenId(remoteWxUserBo.getOpenId(), remoteWxUserBo.getPhonenumber(), remoteWxUserBo.getTenantId()); List<WxUserVo> wxUserVoList = wxUserService.selectUserByOpenId(remoteWxUserBo.getOpenId(), remoteWxUserBo.getPhonenumber(), remoteWxUserBo.getTenantId());
if (ObjectUtil.isNotNull(wx)) { if (CollectionUtils.isNotEmpty(wxUserVoList)) {
return true; return true;
} }
......
...@@ -3,6 +3,8 @@ package org.dromara.system.service; ...@@ -3,6 +3,8 @@ package org.dromara.system.service;
import org.dromara.system.domain.bo.WxUserBo; import org.dromara.system.domain.bo.WxUserBo;
import org.dromara.system.domain.vo.WxUserVo; import org.dromara.system.domain.vo.WxUserVo;
import java.util.List;
/** /**
* 用户 业务层 * 用户 业务层
* *
...@@ -18,7 +20,7 @@ public interface IWxUserService { ...@@ -18,7 +20,7 @@ public interface IWxUserService {
* @param tenantId 租户ID * @param tenantId 租户ID
* @return 用户对象信息 * @return 用户对象信息
*/ */
WxUserVo selectUserByOpenId(String openId, String phone, String tenantId); List<WxUserVo> selectUserByOpenId(String openId, String phone, String tenantId);
/** /**
* 注册用户 * 注册用户
......
...@@ -12,6 +12,8 @@ import org.dromara.system.mapper.WxUserMapper; ...@@ -12,6 +12,8 @@ import org.dromara.system.mapper.WxUserMapper;
import org.dromara.system.service.IWxUserService; import org.dromara.system.service.IWxUserService;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import java.util.List;
/** /**
* 用户 业务层处理 * 用户 业务层处理
* *
...@@ -33,8 +35,8 @@ public class WxUserServiceImpl implements IWxUserService { ...@@ -33,8 +35,8 @@ public class WxUserServiceImpl implements IWxUserService {
* @return 用户对象信息 * @return 用户对象信息
*/ */
@Override @Override
public WxUserVo selectUserByOpenId(String openId, String phone, String tenantId) { public List<WxUserVo> selectUserByOpenId(String openId, String phone, String tenantId) {
return baseMapper.selectVoOne( return baseMapper.selectVoList(
new LambdaQueryWrapper<WxUser>() new LambdaQueryWrapper<WxUser>()
.eq(WxUser::getOpenId, openId) .eq(WxUser::getOpenId, openId)
.eq(StringUtils.isNotBlank(phone), WxUser::getPhonenumber, phone) .eq(StringUtils.isNotBlank(phone), WxUser::getPhonenumber, phone)
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论