IMService.Chatgroups.cs 3.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Net;
  4. using System.Threading.Tasks;
  5. using GxPress.Common.Exceptions;
  6. using GxPress.Common.Http;
  7. using GxPress.Request.GroupChat;
  8. using Newtonsoft.Json;
  9. using GxPress.Common.Extensions;
  10. using GxPress.Result.App.IM;
  11. using GxPress.Common.Tools;
  12. namespace GxPress.Service.Implement.IM
  13. {
  14. /// <summary>
  15. /// 创建群组
  16. /// </summary>
  17. public partial class IMService
  18. {
  19. /// <summary>
  20. /// 创建群
  21. /// </summary>
  22. /// <returns></returns>
  23. public async Task<CreateGroupChatResult> CreateGroupChatAsync(GroupChatInRequest request)
  24. {
  25. var imServiceUrl = ConfigHelper.GetValue("ServiceAddress:ImUrl");
  26. var result = new CreateGroupChatResult();
  27. if (string.IsNullOrEmpty(request.Name))
  28. throw new BusinessException("群名称不能为空");
  29. if (string.IsNullOrEmpty(request.AvatarUrl))
  30. throw new BusinessException("群头像不能为空");
  31. // if (string.IsNullOrEmpty(request.Introduce))
  32. // throw new BusinessException("群介绍不能为空");
  33. try
  34. {
  35. var token = await _cache.GetAsync<string>("ImToken");
  36. //创建群
  37. var url = $"{imServiceUrl}/chatgroups";
  38. var headers = new Dictionary<string, string> { { "Authorization", "Bearer " + token } };
  39. // //获取成员
  40. var users = await _userRepository.UserListInsAsync(request.UserIds);
  41. //获取用户
  42. var user = await _userRepository.GetAsync(request.UserId);
  43. // var list = new List<string>();
  44. // list.AddRange(users.Select(n => n.ImId));
  45. // //构造数据
  46. // string userValues = string.Empty;
  47. // foreach (var value in list)
  48. // userValues += "\"" + value + "\",";
  49. // if (userValues.Length > 0)
  50. // userValues = userValues.Remove(userValues.Length - 1, 1);
  51. var members_only = request.IsAdmin ? "true" : "false";
  52. var jsonString =
  53. "{\"groupname\":\"" + request.Name + "\",\"desc\":\"" + request.Introduce +
  54. "\",\"public\":true,\"maxusers\":1000,\"members_only\":" + members_only + ",\"owner\":\"" +
  55. user.ImId + "\",\"members\":[]}";
  56. var response =
  57. await HttpClientHelper.PostAsync(url, JsonConvert.DeserializeObject(jsonString), headers);
  58. var tokenJson = await response.Content.ReadAsStringAsync();
  59. if (response.IsSuccessStatusCode)
  60. {
  61. result.Success = true;
  62. var dataJsonDynamic = JsonConvert.DeserializeObject<dynamic>(tokenJson);
  63. //获取groupChatImId
  64. request.GroupChatImId = dataJsonDynamic.data.groupid.ToString();
  65. result.GroupChatImId = request.GroupChatImId;
  66. //创建群写入数据库
  67. request.UserIds = new List<int>();
  68. await _groupChatRepository.InsertAsync(request);
  69. return result;
  70. }
  71. if (response.StatusCode == HttpStatusCode.Unauthorized)
  72. throw new BusinessException("未授权[无token、token错误、token过期]");
  73. if (response.StatusCode == HttpStatusCode.BadRequest)
  74. throw new BusinessException("owner用户不存在");
  75. if (response.StatusCode == HttpStatusCode.TooManyRequests)
  76. throw new BusinessException("接口被限流");
  77. result.Success = false;
  78. }
  79. catch (Exception e)
  80. {
  81. throw new BusinessException(e.Message);
  82. }
  83. return result;
  84. }
  85. }
  86. }