From a54dbbb68dfe2fb904b291c8004091dc13a4d7cd Mon Sep 17 00:00:00 2001
From: zj <1772600164@qq.com>
Date: Mon, 22 Sep 2025 17:31:09 +0800
Subject: [PATCH] 1
---
ruoyi-admin/src/main/java/com/ruoyi/im/service/impl/ImApiServcieImpl.java | 79 +++++++++++++++++++++++++++++----------
1 files changed, 59 insertions(+), 20 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 3019413..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);
+ }
/**
* 优化的账号生成方法,使用雪花算法变体提高并发性能
@@ -276,23 +326,8 @@
httpPatch.setHeader("CurTime", curTime);
httpPatch.setHeader("CheckSum", checkSum);
- // 构建请求体
-// UpdateUserInfoRequest builder = new UpdateUserInfoRequest();
-// if(StringUtils.isNotEmpty(dto.getMobile())){
-// builder.setMobile(dto.getMobile());
-// }
-// if(StringUtils.isNotEmpty(dto.getName())){
-// builder.setName(dto.getName());
-// }
-// if(StringUtils.isNotEmpty(dto.getSign())){
-// builder.setSign(dto.getSign());
-// }
-// if(StringUtils.isNotEmpty(dto.getAvatar())){
-// builder.setAvatar(dto.getAvatar());
-// }
-// String body = builder.build();
UpdateUserInfoRequest requestBody = new UpdateUserInfoRequest(dto.getAvatar(),
- dto.getName(),dto.getSign(),dto.getEmail(),dto.getMobile());
+ dto.getName(),dto.getSign(),dto.getEmail(),dto.getMobile(),dto.getGender());
String jsonBody = objectMapper.writeValueAsString(requestBody);
httpPatch.setEntity(new StringEntity(jsonBody, StandardCharsets.UTF_8));
@@ -345,6 +380,9 @@
}
if(StringUtils.isNotEmpty(vo.getSignature())){
dto.setSign(vo.getSignature());
+ }
+ if(ObjectUtil.isNotEmpty(vo.getGender())){
+ dto.setGender(vo.getGender());
}
Map<String, Object> map = updateUserAvatar(vo.getAccountId(), dto);
//更新用户属性 状态 密码
@@ -452,6 +490,7 @@
@Override
@Transactional(rollbackFor = Exception.class)
public Result batchRegister(RegisterDto dto) {
+ dto.setAccountType(1);
if(dto.getType() == 2){
return register(dto);
}else{
@@ -504,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 {
@@ -522,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