| | |
| | | import com.ruoyi.im.service.NeteaseTeamService; |
| | | import com.ruoyi.imenum.ErrorCodeEnum; |
| | | import com.ruoyi.system.domain.GroupWelcomeConfig; |
| | | import com.ruoyi.system.domain.InvitationBlacklist; |
| | | import com.ruoyi.system.domain.NeteaseTeam; |
| | | import com.ruoyi.system.domain.UserAccount; |
| | | import com.ruoyi.im.service.ImApiServcie; |
| | |
| | | import com.ruoyi.system.service.GroupWelcomeConfigService; |
| | | import com.ruoyi.system.service.UserAccountService; |
| | | import com.ruoyi.im.util.SymmetricCryptoUtil; |
| | | import com.ruoyi.system.service.impl.InvitationBlacklistServiceImpl; |
| | | import lombok.extern.slf4j.Slf4j; |
| | | import org.apache.commons.codec.digest.DigestUtils; |
| | | import org.apache.http.HttpResponse; |
| | |
| | | @Autowired |
| | | private NeteaseTeamMapper neteaseTeamMapper; |
| | | |
| | | @Autowired |
| | | InvitationBlacklistServiceImpl invitationBlacklistService; |
| | | |
| | | @Resource |
| | | private final YunxinApiHttpClient yunxinClient; |
| | | |
| | |
| | | return Result.error("账号已被注册!"); |
| | | } |
| | | |
| | | if(dto.getAccountType() == 0 && StringUtils.isEmpty(dto.getInvitationCode())){ |
| | | return Result.error("邀请码不能为空!"); |
| | | } |
| | | |
| | | long count = invitationBlacklistService.count(new LambdaQueryWrapper<InvitationBlacklist>() |
| | | .eq(InvitationBlacklist::getInvitationCode, dto.getInvitationCode()) |
| | | ); |
| | | if(count > 0){ |
| | | return Result.error("邀请码已被限制邀请!"); |
| | | } |
| | | |
| | | String invitationCode = getInvitationCode(); |
| | | UserAccount user = new UserAccount(); |
| | | if(dto.getAccountType() == 0 && StringUtils.isNotEmpty(dto.getInvitationCode()) && !dto.getInvitationCode().equals("00000000")){ |
| | | 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()); |
| | |
| | | userAccount.setCloudMessageAccount(dto.getAccount()); |
| | | userAccount.setPassword(SymmetricCryptoUtil.encryptPassword(dto.getPassword())); |
| | | userAccount.setCreateTime(new Date()); |
| | | userAccount.setNickname(dto.getNickname()); |
| | | userAccount.setNickname(dto.getNikeName()); |
| | | 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("保存用户账户失败"); |
| | | } |
| | | |
| | | try { |
| | | // 注册云信账号(远程调用) |
| | | Map<String, String> paramMap = new HashMap<>(); |
| | | paramMap.put("accid", dto.getAccount()); |
| | | if(StringUtils.isNotEmpty(dto.getNickname())){ |
| | | paramMap.put("name", dto.getNickname()); |
| | | } |
| | | paramMap.put("token", dto.getPassword()); |
| | | |
| | | if (StringUtils.isNotEmpty(dto.getNikeName())) { |
| | | Map<String, String> userInfoMap = new HashMap<>(); |
| | | userInfoMap.put("name", dto.getNikeName()); |
| | | |
| | | // 使用 JSON 工具将 Map 转为 JSON 字符串 |
| | | ObjectMapper objectMapper = new ObjectMapper(); |
| | | String userInfoJson = objectMapper.writeValueAsString(userInfoMap); |
| | | paramMap.put("user_information", userInfoJson); |
| | | } |
| | | YunxinApiResponse response = yunxinClient.executeV1Api(YUNXIN_CREATE_PATH, paramMap); |
| | | |
| | | // 处理云信响应 |
| | |
| | | log.error("-----------注册账号异常:"+ErrorCodeEnum.getByCode(code).getComment()+"----im信息:"+ErrorCodeEnum.getByCode(code).getDesc()); |
| | | throw new RuntimeException(errorMsg); |
| | | } |
| | | //修改昵称 |
| | | UpdateUserBusinessDto userBusinessDto = new UpdateUserBusinessDto(); |
| | | userBusinessDto.setName(dto.getNikeName()); |
| | | updateUserAvatar(dto.getAccount(),userBusinessDto); |
| | | |
| | | //默认添加邀请人为好友 |
| | | if(ObjectUtil.isNotEmpty(user)){ |
| | | addFriends(userAccount.getAccount(),user.getAccount()); |
| | | } |
| | | |
| | | // 注册成功后的其他操作 |
| | | GroupWelcomeConfig groupWelcomeConfig = groupWelcomeConfigService.getOne(new LambdaQueryWrapper<>(GroupWelcomeConfig.class) |
| | | .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("注册成功"); |
| | |
| | | } |
| | | } |
| | | |
| | | 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 = 10000000 + random.nextInt(90000000); |
| | | return String.valueOf(code); |
| | | } |
| | | |
| | | /** |
| | | * 优化的账号生成方法,使用雪花算法变体提高并发性能 |
| | |
| | | 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)); |
| | | |
| | |
| | | return result; |
| | | } |
| | | |
| | | |
| | | |
| | | /** |
| | | * 生成校验和 |
| | | */ |
| | |
| | | @Override |
| | | public AjaxResult updateUserAccount(UserAccountUpdateVo vo) { |
| | | //更新用户名片 |
| | | UpdateUserBusinessDto dto = new UpdateUserBusinessDto(); |
| | | if(StringUtils.isNotEmpty(vo.getPhoneNumber())){ |
| | | dto.setMobile(vo.getPhoneNumber()); |
| | | } |
| | | if(StringUtils.isNotEmpty(vo.getNickname())){ |
| | | dto.setName(vo.getNickname()); |
| | | } |
| | | if(StringUtils.isNotEmpty(vo.getSignature())){ |
| | | dto.setSign(vo.getSignature()); |
| | | } |
| | | Map<String, Object> map = updateUserAvatar(vo.getAccountId(), dto); |
| | | // UpdateUserBusinessDto dto = new UpdateUserBusinessDto(); |
| | | // if(StringUtils.isNotEmpty(vo.getPhoneNumber())){ |
| | | // dto.setMobile(vo.getPhoneNumber()); |
| | | // } |
| | | // if(StringUtils.isNotEmpty(vo.getNickname())){ |
| | | // dto.setName(vo.getNickname()); |
| | | // } |
| | | // 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); |
| | | //更新用户属性 状态 密码 |
| | | if ((Boolean) map.get("success")) { |
| | | AjaxResult ajaxResult = updateAccountProperties(vo.getAccountId(), vo); |
| | | // if ((Boolean) map.get("success")) { |
| | | AjaxResult ajaxResult = updateAccountProperties(vo.getAccount(), vo); |
| | | if(ajaxResult.isSuccess()){ |
| | | UserAccount userAccount = userAccountService.getById(vo.getId()); |
| | | UserAccount userAccount = userAccountService.getOne(new LambdaQueryWrapper<UserAccount>() |
| | | .eq(UserAccount::getAccount,vo.getAccount()) |
| | | ); |
| | | if (StringUtils.isNotBlank(vo.getPhoneNumber())) { |
| | | userAccount.setPhoneNumber(vo.getPhoneNumber()); |
| | | } |
| | | |
| | | if (StringUtils.isNotBlank(vo.getAccountId())) { |
| | | userAccount.setAccount(vo.getAccountId()); |
| | | if (StringUtils.isNotBlank(vo.getAccount())) { |
| | | userAccount.setAccount(vo.getAccount()); |
| | | } |
| | | |
| | | if (StringUtils.isNotBlank(vo.getNickname())) { |
| | |
| | | if (StringUtils.isNotBlank(vo.getSignature())) { |
| | | userAccount.setSignature(vo.getSignature()); |
| | | } |
| | | userAccount.setGroupPermissions(vo.getGroupPermissions()); |
| | | userAccount.setAddFriend(vo.getAddFriend()); |
| | | userAccount.setStatus(vo.getStatus()); |
| | | userAccount.setUpdateTime(new Date()); |
| | | userAccountService.updateById(userAccount); |
| | | }else{ |
| | | return AjaxResult.error("更新用户属性失败!"); |
| | | } |
| | | } else { |
| | | return AjaxResult.error("更新用户名片失败!"); |
| | | } |
| | | // } else { |
| | | // return AjaxResult.error("更新用户名片失败!"); |
| | | // } |
| | | return AjaxResult.success("更新成功!"); |
| | | } |
| | | |
| | |
| | | if(null != vo.getStatus() && vo.getStatus() == 1){ |
| | | builder.setEnabled(false); |
| | | builder.setNeedKick(true); |
| | | }else if(null != vo.getStatus() && vo.getStatus() == 0){ |
| | | builder.setEnabled(true); |
| | | }else if(StringUtils.isNotEmpty(vo.getPassword())){ |
| | | builder.setToken(vo.getPassword()); |
| | | } |
| | |
| | | NeteaseResponse neteaseResponse = objectMapper.readValue(responseString, NeteaseResponse.class); |
| | | |
| | | if (neteaseResponse.isSuccess()) { |
| | | AjaxResult.success("账号属性更新成功"); |
| | | return AjaxResult.success("账号属性更新成功"); |
| | | } else { |
| | | AjaxResult.error("账号属性更新失败"); |
| | | return AjaxResult.error("账号属性更新失败"); |
| | | } |
| | | |
| | | } catch (Exception e) { |
| | | e.printStackTrace(); |
| | | AjaxResult.error("请求网易云信API失败"); |
| | | return AjaxResult.error("请求网易云信API失败"); |
| | | } |
| | | |
| | | return AjaxResult.success(); |
| | | } |
| | | |
| | | |
| | |
| | | @Override |
| | | @Transactional(rollbackFor = Exception.class) |
| | | public Result batchRegister(RegisterDto dto) { |
| | | dto.setAccountType(1); |
| | | if(dto.getType() == 2){ |
| | | return register(dto); |
| | | }else{ |
| | |
| | | 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 { |
| | |
| | | userAccount.setCreateTime(new Date()); |
| | | userAccount.setNickname("用户_" + account.substring(7)); // 简单生成昵称 |
| | | userAccount.setAccountType(1); // 设置账号类型为1 |
| | | |
| | | userAccount.setInvitationCode(invitationCode); |
| | | accounts.add(userAccount); |
| | | } |
| | | return accounts; |