IMService.RemoveUsers.cs 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. using System.Collections.Generic;
  2. using System.Linq;
  3. using System.Net;
  4. using System.Threading.Tasks;
  5. using GxPress.Common.Exceptions;
  6. using GxPress.Common.Http;
  7. using GxPress.Entity;
  8. using GxPress.Request.App.IM;
  9. using GxPress.Common.Extensions;
  10. using GxPress.Common.Tools;
  11. namespace GxPress.Service.Implement.IM
  12. {
  13. /// <summary>
  14. /// 移除群成员
  15. /// </summary>
  16. public partial class IMService
  17. {
  18. /// <summary>
  19. /// 删除群成员
  20. /// </summary>
  21. /// <param name="request"></param>
  22. /// <returns></returns>
  23. public async Task<bool> RemoveGroupChatUsersAsync(InsertGroupChatUsersRequest request)
  24. {
  25. var token = await _cache.GetAsync<string>("ImToken");
  26. var imServiceUrl = ConfigHelper.GetValue("ServiceAddress:ImUrl");
  27. string userValues = string.Empty;
  28. //获取成员
  29. var users = await _userRepository.UserListInsAsync(request.UserIds);
  30. var enumerable = users as User[] ?? users.ToArray();
  31. foreach (var value in enumerable.Select(n => n.ImId))
  32. {
  33. userValues += value + ",";
  34. }
  35. if (userValues.Length > 0)
  36. userValues = userValues.Remove(userValues.Length - 1, 1);
  37. //创建群
  38. var url = $"{imServiceUrl}/chatgroups/{request.GroupChatImId}/users/{userValues}";
  39. var headers = new Dictionary<string, string> { { "Authorization", "Bearer " + token } };
  40. var response =
  41. await HttpClientHelper.DeleteAsync(url, headers);
  42. if (response.IsSuccessStatusCode)
  43. {
  44. //删除成员
  45. return await _groupChatRepository.DeleteUsersIntoGroupChatAsync(request);
  46. }
  47. if (response.StatusCode == HttpStatusCode.Forbidden)
  48. throw new BusinessException("该成员不在此群组");
  49. if (response.StatusCode == HttpStatusCode.NotFound)
  50. throw new BusinessException("此群组ID不存在");
  51. if (response.StatusCode == HttpStatusCode.Unauthorized)
  52. throw new BusinessException("未授权[无token、token错误、token过期]");
  53. if (response.StatusCode == HttpStatusCode.BadRequest)
  54. throw new BusinessException("owner用户不存在");
  55. if (response.StatusCode == HttpStatusCode.TooManyRequests)
  56. throw new BusinessException("接口被限流");
  57. return false;
  58. }
  59. /// <summary>
  60. /// 根据IMID删除用户
  61. /// </summary>
  62. /// <param name="imId"></param>
  63. /// <returns></returns>
  64. public async Task<bool> DeleteUserAsync(string imId)
  65. {
  66. var imServiceUrl = ConfigHelper.GetValue("ServiceAddress:ImUrl");
  67. var token = await _cache.GetAsync<string>("ImToken");
  68. var url = $"{imServiceUrl}/users/{imId}";
  69. var headers = new Dictionary<string, string> { { "Authorization", "Bearer " + token } };
  70. var response =
  71. await HttpClientHelper.DeleteAsync(url, headers);
  72. if (response.IsSuccessStatusCode)
  73. {
  74. //删除成员
  75. return true;
  76. }
  77. if (response.StatusCode == HttpStatusCode.NotFound)
  78. return true;
  79. if (response.StatusCode == HttpStatusCode.Unauthorized)
  80. throw new BusinessException("未授权[无token、token错误、token过期]");
  81. if (response.StatusCode == HttpStatusCode.TooManyRequests)
  82. throw new BusinessException("接口被限流");
  83. return true;
  84. }
  85. }
  86. }