IMService.Chatgroups.cs 4.0 KB

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