| | |
| | | import com.ruoyi.im.config.*; |
| | | import com.ruoyi.im.dto.UpdateUserBusinessDto; |
| | | import com.ruoyi.im.service.NeteaseTeamService; |
| | | import com.ruoyi.imenum.ErrorCodeEnum; |
| | | import com.ruoyi.system.domain.GroupWelcomeConfig; |
| | | import com.ruoyi.system.domain.NeteaseTeam; |
| | | import com.ruoyi.system.domain.UserAccount; |
| | | import com.ruoyi.im.service.ImApiServcie; |
| | | import com.ruoyi.im.dto.RegisterDto; |
| | | import com.ruoyi.system.domain.vo.UserAccountUpdateVo; |
| | | import com.ruoyi.system.mapper.NeteaseTeamMapper; |
| | | import com.ruoyi.system.service.GroupWelcomeConfigService; |
| | | import com.ruoyi.system.service.UserAccountService; |
| | | import com.ruoyi.im.util.SymmetricCryptoUtil; |
| | |
| | | |
| | | @Autowired |
| | | private JdbcTemplate jdbcTemplate; |
| | | @Autowired |
| | | private NeteaseTeamMapper neteaseTeamMapper; |
| | | |
| | | @Resource |
| | | private final YunxinApiHttpClient yunxinClient; |
| | |
| | | @Override |
| | | @Transactional(rollbackFor = Exception.class) |
| | | public Result register(RegisterDto dto) { |
| | | // 验证手机号是否已存在 |
| | | List<UserAccount> accounts = userAccountService.list( |
| | | new LambdaQueryWrapper<>(UserAccount.class) |
| | | .eq(UserAccount::getAccount, dto.getAccount()) |
| | | ); |
| | | |
| | | if (!CollectionUtils.isEmpty(accounts)) { |
| | | return Result.error("账号已被注册!"); |
| | | } |
| | | |
| | | // 创建本地用户账户记录 |
| | | UserAccount userAccount = new UserAccount(); |
| | | userAccount.setAccount(dto.getAccount()); |
| | | userAccount.setPhoneNumber(dto.getAccount()); |
| | | userAccount.setCloudMessageAccount(dto.getAccount()); |
| | | userAccount.setPassword(SymmetricCryptoUtil.encryptPassword(dto.getPassword())); |
| | | userAccount.setCreateTime(new Date()); |
| | | userAccount.setNickname(dto.getNickname()); |
| | | userAccount.setCreateTime(new Date()); |
| | | userAccount.setUpdateTime(new Date()); |
| | | |
| | | if (!userAccountService.save(userAccount)) { |
| | | throw new RuntimeException("保存用户账户失败"); |
| | | } |
| | | |
| | | try { |
| | | // 验证手机号是否已存在 |
| | | List<UserAccount> accounts = userAccountService.list( |
| | | new LambdaQueryWrapper<>(UserAccount.class) |
| | | .eq(UserAccount::getAccount, dto.getAccount()) |
| | | ); |
| | | |
| | | if (!CollectionUtils.isEmpty(accounts)) { |
| | | return Result.error("账号已被注册!"); |
| | | } |
| | | |
| | | // 创建本地用户账户记录 |
| | | UserAccount userAccount = new UserAccount(); |
| | | userAccount.setAccount(dto.getAccount()); |
| | | userAccount.setPhoneNumber(dto.getAccount()); |
| | | userAccount.setCloudMessageAccount(dto.getAccount()); |
| | | userAccount.setPassword(SymmetricCryptoUtil.encryptPassword(dto.getPassword())); |
| | | userAccount.setCreateTime(new Date()); |
| | | userAccount.setNickname(dto.getNickname()); |
| | | |
| | | if (!userAccountService.save(userAccount)) { |
| | | throw new RuntimeException("保存用户账户失败"); // 改为抛出异常 |
| | | } |
| | | |
| | | // 注册云信账号(远程调用) |
| | | Map<String, String> paramMap = new HashMap<>(); |
| | | paramMap.put("accid", dto.getAccount()); |
| | |
| | | JSONObject json = JSONObject.parseObject(data); |
| | | int code = json.getIntValue("code"); |
| | | |
| | | if (code == 200) { |
| | | GroupWelcomeConfig groupWelcomeConfig = groupWelcomeConfigService.getOne(new LambdaQueryWrapper<>(GroupWelcomeConfig.class) |
| | | .eq(GroupWelcomeConfig::getConfigurationName, "IM-BASICS").last(" limit 1")); |
| | | if(ObjectUtil.isNotEmpty(groupWelcomeConfig)){ |
| | | //添加默认好友 |
| | | addFriends(userAccount.getAccount(),groupWelcomeConfig.getUserAccid()); |
| | | //加入默认群聊 |
| | | List<String> accountList = new ArrayList<>(); |
| | | accountList.add(userAccount.getAccount()); |
| | | AddTeamMembersRequest request = new AddTeamMembersRequest(); |
| | | request.setInviteAccountIds(accountList); |
| | | request.setOperatorId(groupWelcomeConfig.getUserAccid()); |
| | | neteaseTeamService.inviteTeamMembers(request); |
| | | if (code != 200) { |
| | | String errorMsg = ""; |
| | | if(code == 102405){ |
| | | errorMsg = "用户已存在"; |
| | | } |
| | | return Result.success("注册成功"); |
| | | } else { |
| | | // 所有非200状态码都抛出异常触发回滚 |
| | | throw new RuntimeException("云信注册失败,错误码: " + code + ", 响应: " + data); |
| | | log.error("-----------注册账号异常:"+ErrorCodeEnum.getByCode(code).getComment()+"----im信息:"+ErrorCodeEnum.getByCode(code).getDesc()); |
| | | throw new RuntimeException(errorMsg); |
| | | } |
| | | |
| | | // 注册成功后的其他操作 |
| | | 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)){ |
| | | addFriends(userAccount.getAccount(),groupWelcomeConfig.getUserAccid()); |
| | | List<String> accountList = new ArrayList<>(); |
| | | accountList.add(userAccount.getAccount()); |
| | | AddTeamMembersRequest request = new AddTeamMembersRequest(); |
| | | request.setInviteAccountIds(accountList); |
| | | request.setGroupId(neteaseTeam.getId().toString()); |
| | | neteaseTeamService.inviteTeamMembers(request); |
| | | } |
| | | return Result.success("注册成功"); |
| | | |
| | | } catch (Exception e) { |
| | | // 记录异常日志 |
| | | log.error("注册过程发生异常", e); |
| | | // 重新抛出异常以确保事务回滚 |
| | | throw new RuntimeException("注册失败: " + e.getMessage(), e); |
| | | // 将异常包装为Result并抛出RuntimeException触发回滚 |
| | | throw new RuntimeException(Result.error("注册失败: " + e.getMessage()).toString(), e); |
| | | } |
| | | } |
| | | |
| | |
| | | return yunxinResult; |
| | | } |
| | | |
| | | return Result.success("成功批量注册 " + count + " 个账号"); |
| | | return Result.success("成功批量注册 " + count + " 个账号",yunxinResult.getData()); |
| | | |
| | | } catch (Exception e) { |
| | | // 其他异常,触发回滚 |
| | |
| | | .map(CompletableFuture::join) |
| | | .collect(Collectors.toList()); |
| | | |
| | | List<String> acccountList = new ArrayList<>(); |
| | | // 检查所有响应结果 |
| | | for (YunxinApiResponse response : responses) { |
| | | String data = response.getData(); |
| | | JSONObject json = JSONObject.parseObject(data); |
| | | int code = json.getIntValue("code"); |
| | | if (code != 200) { |
| | | // 记录具体的错误信息 |
| | | String errorAccid = json.getString("accid"); // 如果返回了accid |
| | | log.error("云信账号注册失败,accid: {}, 响应: {}, traceId: {}", errorAccid, data, response.getTraceId()); |
| | | // 返回第一个遇到的错误 |
| | | return Result.error("云信注册失败,错误码: " + code + (errorAccid != null ? ", 账号: " + errorAccid : "")); |
| | | } |
| | | } |
| | | JSONObject info = json.getJSONObject("info"); |
| | | String accid = info.getString("accid"); |
| | | |
| | | return Result.success("所有云信账号注册成功"); |
| | | if (code != 200) { |
| | | log.error("-----------云信账号注册失败:"+ ErrorCodeEnum.getByCode(code).getComment()+"----im信息:"+ErrorCodeEnum.getByCode(code).getDesc()); |
| | | return Result.error("云信注册失败:"+ErrorCodeEnum.getByCode(code).getComment()); |
| | | } |
| | | acccountList.add(accid); |
| | | } |
| | | return Result.success(acccountList); |
| | | } |
| | | |
| | | |