From 02d6a517f7d4dac2d5271cefe421a628d838414b Mon Sep 17 00:00:00 2001
From: zj <1772600164@qq.com>
Date: Sat, 20 Sep 2025 03:35:55 +0800
Subject: [PATCH] 1
---
ruoyi-admin/src/main/java/com/ruoyi/im/service/impl/ImApiServcieImpl.java | 59 +++++++++++++++++++++++++++++++++++++++++++++++++++++++----
1 files changed, 55 insertions(+), 4 deletions(-)
diff --git a/ruoyi-admin/src/main/java/com/ruoyi/im/service/impl/ImApiServcieImpl.java b/ruoyi-admin/src/main/java/com/ruoyi/im/service/impl/ImApiServcieImpl.java
index ada2ea4..fb95e3b 100644
--- a/ruoyi-admin/src/main/java/com/ruoyi/im/service/impl/ImApiServcieImpl.java
+++ b/ruoyi-admin/src/main/java/com/ruoyi/im/service/impl/ImApiServcieImpl.java
@@ -121,6 +121,20 @@
return Result.error("账号已被注册!");
}
+ if(dto.getAccountType() == 0 && StringUtils.isEmpty(dto.getInvitationCode())){
+ return Result.error("邀请码不能为空!");
+ }
+
+ String invitationCode = getInvitationCode();
+ UserAccount user = new UserAccount();
+ if(dto.getAccountType() == 0 && StringUtils.isNotEmpty(dto.getInvitationCode()) && !dto.getInvitationCode().equals("000000")){
+ user = userAccountService.getOne(new LambdaQueryWrapper<UserAccount>()
+ .eq(UserAccount::getInvitationCode, dto.getInvitationCode()).last(" limit 1"));
+ if(ObjectUtil.isEmpty(user)){
+ return Result.error("邀请码错误");
+ }
+ }
+
// 创建本地用户账户记录
UserAccount userAccount = new UserAccount();
userAccount.setAccount(dto.getAccount());
@@ -131,6 +145,8 @@
userAccount.setNickname(dto.getNickname());
userAccount.setCreateTime(new Date());
userAccount.setUpdateTime(new Date());
+ userAccount.setInvitationCode(invitationCode);
+ userAccount.setInvitationAccount(ObjectUtil.isNotEmpty(user.getAccount()) ? user.getAccount() : "");
if (!userAccountService.save(userAccount)) {
throw new RuntimeException("保存用户账户失败");
@@ -166,13 +182,15 @@
.eq(GroupWelcomeConfig::getConfigurationName, "IM-BASICS").last(" limit 1"));
NeteaseTeam neteaseTeam = neteaseTeamMapper.selectOne(new LambdaQueryWrapper<NeteaseTeam>().eq(NeteaseTeam::getTid,groupWelcomeConfig.getGroupId()));
- if(ObjectUtil.isNotEmpty(groupWelcomeConfig) || ObjectUtil.isNotEmpty(neteaseTeam)){
+ if(ObjectUtil.isNotEmpty(groupWelcomeConfig) && ObjectUtil.isNotEmpty(groupWelcomeConfig.getUserAccid())){
addFriends(userAccount.getAccount(),groupWelcomeConfig.getUserAccid());
+ }
+ if(ObjectUtil.isNotEmpty(groupWelcomeConfig) && ObjectUtil.isNotEmpty(groupWelcomeConfig.getGroupId()) && ObjectUtil.isNotEmpty(neteaseTeam)){
List<String> accountList = new ArrayList<>();
accountList.add(userAccount.getAccount());
AddTeamMembersRequest request = new AddTeamMembersRequest();
request.setInviteAccountIds(accountList);
- request.setGroupId(neteaseTeam.getId().toString());
+ request.setGroupId(ObjectUtil.isNotEmpty(neteaseTeam.getId().toString()) ? neteaseTeam.getId().toString() : null);
neteaseTeamService.inviteTeamMembers(request);
}
return Result.success("注册成功");
@@ -184,6 +202,38 @@
}
}
+ private String getInvitationCode() {
+ String invitationCode = null;
+ int maxAttempts = 100; // 最大尝试次数
+ int attempts = 0;
+
+ while (attempts < maxAttempts) {
+ invitationCode = generateInvitationCode();
+ long count = userAccountService.count(new LambdaQueryWrapper<UserAccount>()
+ .eq(UserAccount::getInvitationCode, invitationCode));
+ if(count <= 0){
+ break;
+ }
+ attempts++;
+ }
+
+ if (attempts >= maxAttempts) {
+ log.error("生成邀请码已超最大尝试次数!");
+ throw new RuntimeException("无法生成唯一的邀请码,请稍后重试");
+ }
+ return invitationCode;
+ }
+
+
+ /**
+ * 生成邀请码
+ * @return
+ */
+ public static String generateInvitationCode() {
+ Random random = new Random();
+ int code = 100000 + random.nextInt(900000);
+ return String.valueOf(code);
+ }
/**
* 优化的账号生成方法,使用雪花算法变体提高并发性能
@@ -440,6 +490,7 @@
@Override
@Transactional(rollbackFor = Exception.class)
public Result batchRegister(RegisterDto dto) {
+ dto.setAccountType(1);
if(dto.getType() == 2){
return register(dto);
}else{
@@ -492,7 +543,7 @@
List<UserAccount> accounts = new ArrayList<>(count);
Set<String> generatedAccounts = new HashSet<>(count); // 用于内存中去重
Random random = new Random();
-
+ String invitationCode = getInvitationCode();
for (int i = 0; i < count; i++) {
String account;
do {
@@ -510,7 +561,7 @@
userAccount.setCreateTime(new Date());
userAccount.setNickname("用户_" + account.substring(7)); // 简单生成昵称
userAccount.setAccountType(1); // 设置账号类型为1
-
+ userAccount.setInvitationCode(invitationCode);
accounts.add(userAccount);
}
return accounts;
--
Gitblit v1.9.3