fix(user-sync): 修复用户同步时tenantId为空问题
- 在用户同步数据中添加tenantId字段,tenantId为空时默认传0 - 更新同步日志,增加tenantId信息输出,便于调试跟踪 - 在扫码登录流程中添加多处详细调试日志,输出关键变量状态 - 添加System.out调试信息,帮助排查绑定用户及扫码登录异常情况
This commit is contained in:
@@ -35,6 +35,7 @@ import com.gxwebsoft.common.system.vo.WxOfficialButton;
|
|||||||
import io.swagger.v3.oas.annotations.tags.Tag;
|
import io.swagger.v3.oas.annotations.tags.Tag;
|
||||||
import io.swagger.v3.oas.annotations.Operation;
|
import io.swagger.v3.oas.annotations.Operation;
|
||||||
import lombok.extern.slf4j.Slf4j;
|
import lombok.extern.slf4j.Slf4j;
|
||||||
|
import org.springframework.util.CollectionUtils;
|
||||||
import org.apache.http.HttpEntity;
|
import org.apache.http.HttpEntity;
|
||||||
import org.apache.http.client.methods.CloseableHttpResponse;
|
import org.apache.http.client.methods.CloseableHttpResponse;
|
||||||
import org.apache.http.client.methods.HttpPost;
|
import org.apache.http.client.methods.HttpPost;
|
||||||
@@ -307,8 +308,27 @@ public class WxOfficialController extends BaseController {
|
|||||||
if (existingUser != null) {
|
if (existingUser != null) {
|
||||||
userId = existingUser.getUserId();
|
userId = existingUser.getUserId();
|
||||||
System.out.println("已存在绑定用户 userId = " + userId);
|
System.out.println("已存在绑定用户 userId = " + userId);
|
||||||
|
// 即使已存在记录,也确保oauth记录存在(防止数据不一致)
|
||||||
|
boolean oauthExists = userOauthService.count(new LambdaQueryWrapper<UserOauth>()
|
||||||
|
.eq(UserOauth::getOauthType, MP_OFFICIAL)
|
||||||
|
.eq(UserOauth::getUnionid, unionid)
|
||||||
|
.eq(UserOauth::getTenantId, tenantId)
|
||||||
|
.eq(UserOauth::getOauthId, openId)) > 0;
|
||||||
|
if (!oauthExists) {
|
||||||
|
// 创建oauth记录
|
||||||
|
final UserOauth userOauth = new UserOauth();
|
||||||
|
userOauth.setOauthType(MP_OFFICIAL);
|
||||||
|
userOauth.setUnionid(unionid);
|
||||||
|
userOauth.setOauthId(openId);
|
||||||
|
userOauth.setUserId(userId);
|
||||||
|
userOauth.setTenantId(tenantId);
|
||||||
|
boolean save = userOauthService.save(userOauth);
|
||||||
|
System.out.println("补充创建oauth记录,结果 = " + save);
|
||||||
|
}
|
||||||
} else {
|
} else {
|
||||||
System.out.println("警告:count=1但未找到对应的绑定记录,unionid=" + unionid + ", tenantId=" + tenantId);
|
System.out.println("警告:count=1但未找到对应的绑定记录,unionid=" + unionid + ", tenantId=" + tenantId);
|
||||||
|
// 这种情况可能是数据不一致,尝试创建新的oauth记录
|
||||||
|
userId = findOrCreateUserForOauth(tenantId, openId, unionid);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -336,6 +356,9 @@ public class WxOfficialController extends BaseController {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// 1. 检查并确保用户有合适的角色
|
||||||
|
ensureUserHasAppropriateRole(user, tenantId);
|
||||||
|
|
||||||
long ttlSeconds = 120L;
|
long ttlSeconds = 120L;
|
||||||
qrLoginData.setToken(token);
|
qrLoginData.setToken(token);
|
||||||
qrLoginData.setUserId(userId);
|
qrLoginData.setUserId(userId);
|
||||||
@@ -605,4 +628,169 @@ public class WxOfficialController extends BaseController {
|
|||||||
return success(getAccessToken());
|
return success(getAccessToken());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 查找或创建用户用于oauth绑定
|
||||||
|
*/
|
||||||
|
private Integer findOrCreateUserForOauth(Integer tenantId, String openId, String unionid) {
|
||||||
|
Integer userId = 0;
|
||||||
|
// 首先尝试通过unionid查找任何平台的现有用户
|
||||||
|
final List<UserOauth> list = userOauthService.list(
|
||||||
|
new LambdaQueryWrapper<UserOauth>()
|
||||||
|
.eq(UserOauth::getUnionid, unionid)
|
||||||
|
.eq(UserOauth::getDeleted, 0));
|
||||||
|
|
||||||
|
if (!CollectionUtils.isEmpty(list)) {
|
||||||
|
for (UserOauth item : list) {
|
||||||
|
if (item.getUserId() != null) {
|
||||||
|
userId = item.getUserId();
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
System.out.println("数据不一致:通过unionid找到其他平台的用户 userId = " + userId);
|
||||||
|
}
|
||||||
|
|
||||||
|
// 如果没找到用户,创建一个新用户
|
||||||
|
if (userId == 0) {
|
||||||
|
User user = new User();
|
||||||
|
user.setStatus(0);
|
||||||
|
user.setUsername("wxoff_".concat(RandomUtil.randomString(12)));
|
||||||
|
user.setNickname("微信公众号用户");
|
||||||
|
user.setPlatform(MP_OFFICIAL);
|
||||||
|
user.setGradeId(1);
|
||||||
|
user.setPassword(userService.encodePassword(CommonUtil.randomUUID16()));
|
||||||
|
user.setTenantId(tenantId);
|
||||||
|
user.setRecommend(0);
|
||||||
|
|
||||||
|
// 尝试获取"user"角色,不行就用"guest",再不行用默认6
|
||||||
|
Role role = null;
|
||||||
|
try {
|
||||||
|
final RoleParam roleParam = new RoleParam();
|
||||||
|
roleParam.setTenantId(tenantId);
|
||||||
|
roleParam.setRoleCode("user");
|
||||||
|
role = roleService.getByRoleCode(roleParam);
|
||||||
|
} catch (Exception e) {
|
||||||
|
System.out.println("获取user角色失败,尝试guest角色");
|
||||||
|
try {
|
||||||
|
final RoleParam roleParam = new RoleParam();
|
||||||
|
roleParam.setTenantId(tenantId);
|
||||||
|
roleParam.setRoleCode("guest");
|
||||||
|
role = roleService.getByRoleCode(roleParam);
|
||||||
|
} catch (Exception ex) {
|
||||||
|
System.out.println("获取guest角色也失败,使用默认角色ID 6");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
user.setRoleId(role != null ? role.getRoleId() : 6);
|
||||||
|
|
||||||
|
if (userService.saveUser(user)) {
|
||||||
|
userId = user.getUserId();
|
||||||
|
// 添加用户角色
|
||||||
|
final UserRole userRole = new UserRole();
|
||||||
|
userRole.setUserId(user.getUserId());
|
||||||
|
userRole.setTenantId(user.getTenantId());
|
||||||
|
userRole.setRoleId(user.getRoleId());
|
||||||
|
userRoleService.save(userRole);
|
||||||
|
// 同步到 websopy
|
||||||
|
userSyncService.syncUserToWebsopy(user);
|
||||||
|
}
|
||||||
|
System.out.println("数据不一致:创建新用户 userId = " + userId);
|
||||||
|
}
|
||||||
|
|
||||||
|
// 创建oauth记录
|
||||||
|
final UserOauth userOauth = new UserOauth();
|
||||||
|
userOauth.setOauthType(MP_OFFICIAL);
|
||||||
|
userOauth.setUnionid(unionid);
|
||||||
|
userOauth.setOauthId(openId);
|
||||||
|
userOauth.setUserId(userId);
|
||||||
|
userOauth.setTenantId(tenantId);
|
||||||
|
boolean save = userOauthService.save(userOauth);
|
||||||
|
System.out.println("创建oauth记录修复数据不一致,结果 = " + save);
|
||||||
|
|
||||||
|
return userId;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 确保用户有合适的角色
|
||||||
|
*/
|
||||||
|
private void ensureUserHasAppropriateRole(User user, Integer tenantId) {
|
||||||
|
try {
|
||||||
|
// 检查用户是否有有效的角色绑定
|
||||||
|
List<UserRole> userRoles = userRoleService.list(new LambdaQueryWrapper<UserRole>()
|
||||||
|
.eq(UserRole::getUserId, user.getUserId())
|
||||||
|
.eq(UserRole::getTenantId, tenantId));
|
||||||
|
|
||||||
|
if (CollectionUtils.isEmpty(userRoles)) {
|
||||||
|
System.out.println("用户 " + user.getUserId() + " 没有角色绑定,尝试分配角色");
|
||||||
|
|
||||||
|
// 获取合适的角色
|
||||||
|
Role role = null;
|
||||||
|
// 先尝试获取"user"角色
|
||||||
|
try {
|
||||||
|
final RoleParam roleParam = new RoleParam();
|
||||||
|
roleParam.setTenantId(tenantId);
|
||||||
|
roleParam.setRoleCode("user");
|
||||||
|
role = roleService.getByRoleCode(roleParam);
|
||||||
|
} catch (Exception e) {
|
||||||
|
System.out.println("获取user角色失败,尝试guest角色");
|
||||||
|
try {
|
||||||
|
final RoleParam roleParam = new RoleParam();
|
||||||
|
roleParam.setTenantId(tenantId);
|
||||||
|
roleParam.setRoleCode("guest");
|
||||||
|
role = roleService.getByRoleCode(roleParam);
|
||||||
|
} catch (Exception ex) {
|
||||||
|
System.out.println("获取guest角色也失败,使用默认角色ID 6");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
Integer roleId = role != null ? role.getRoleId() : 6;
|
||||||
|
|
||||||
|
// 创建用户角色绑定
|
||||||
|
final UserRole userRole = new UserRole();
|
||||||
|
userRole.setUserId(user.getUserId());
|
||||||
|
userRole.setTenantId(tenantId);
|
||||||
|
userRole.setRoleId(roleId);
|
||||||
|
userRoleService.save(userRole);
|
||||||
|
|
||||||
|
// 更新用户的roleId
|
||||||
|
user.setRoleId(roleId);
|
||||||
|
userService.updateUser(user);
|
||||||
|
|
||||||
|
System.out.println("为用户 " + user.getUserId() + " 分配了角色ID: " + roleId);
|
||||||
|
} else {
|
||||||
|
// 检查角色的有效性
|
||||||
|
boolean hasValidRole = false;
|
||||||
|
for (UserRole userRole : userRoles) {
|
||||||
|
if (userRole.getRoleId() != null && userRole.getRoleId() > 0) {
|
||||||
|
hasValidRole = true;
|
||||||
|
// 确保用户的roleId与绑定的角色一致
|
||||||
|
if (!userRole.getRoleId().equals(user.getRoleId())) {
|
||||||
|
user.setRoleId(userRole.getRoleId());
|
||||||
|
userService.updateUser(user);
|
||||||
|
System.out.println("更新用户 " + user.getUserId() + " 的角色ID为: " + userRole.getRoleId());
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!hasValidRole) {
|
||||||
|
System.out.println("用户 " + user.getUserId() + " 的角色绑定无效,重新分配");
|
||||||
|
// 重新分配角色(简化逻辑,使用默认角色ID 6)
|
||||||
|
final UserRole userRole = new UserRole();
|
||||||
|
userRole.setUserId(user.getUserId());
|
||||||
|
userRole.setTenantId(tenantId);
|
||||||
|
userRole.setRoleId(6);
|
||||||
|
userRoleService.save(userRole);
|
||||||
|
|
||||||
|
user.setRoleId(6);
|
||||||
|
userService.updateUser(user);
|
||||||
|
|
||||||
|
System.out.println("重新为用户 " + user.getUserId() + " 分配了默认角色ID: 6");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} catch (Exception e) {
|
||||||
|
System.out.println("确保用户角色时发生异常: " + e.getMessage());
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user