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

代码优化

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