TestController.cs 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  1. using Dapr;
  2. using Hotline.Application.TimeLimits;
  3. using Hotline.CallCenter.BlackLists;
  4. using Hotline.CallCenter.Devices;
  5. using Hotline.CallCenter.Ivrs;
  6. using Hotline.Identity.Roles;
  7. using Hotline.Realtimes;
  8. using Hotline.Repository.SqlSugar;
  9. using Hotline.Share.Dtos.Realtime;
  10. using Hotline.Share.Dtos.Settings;
  11. using Hotline.Share.Enums.Settings;
  12. using Hotline.Users;
  13. using MediatR;
  14. using Microsoft.AspNetCore.Authorization;
  15. using Microsoft.AspNetCore.Mvc;
  16. using Microsoft.AspNetCore.SignalR;
  17. using Microsoft.Extensions.Options;
  18. using NewRock.Sdk;
  19. using NewRock.Sdk.Security;
  20. using SqlSugar;
  21. using Wex.Sdk;
  22. using Wex.Sdk.Tel;
  23. using XC.RSAUtil;
  24. using XF.Domain.Authentications;
  25. using XF.Domain.Cache;
  26. using XF.Domain.Exceptions;
  27. using XF.Domain.Locks;
  28. namespace Hotline.Api.Controllers;
  29. /// <summary>
  30. ///
  31. /// </summary>
  32. [AllowAnonymous]
  33. public class TestController : BaseController
  34. {
  35. private readonly ILogger<TestController> _logger;
  36. private readonly IAuthorizeGenerator _authorizeGenerator;
  37. private readonly IOptionsSnapshot<CallCenterConfiguration> _options;
  38. private readonly ISessionContext _sessionContext;
  39. private readonly IUserRepository _userRepository;
  40. private readonly ITypedCache<User> _cache;
  41. private readonly IRealtimeService _realtimeService;
  42. private readonly IBlacklistDomainService _blacklistDomainService;
  43. private readonly IIvrDomainService _ivrDomainService;
  44. private readonly ISugarUnitOfWork<HotlineDbContext> _uow;
  45. private readonly IRoleRepository _roleRepository;
  46. private readonly IMediator _mediator;
  47. private readonly ITimeLimitApplication _timeLimitApplication;
  48. private readonly IDistributedLock _distributedLock;
  49. private readonly IWexClient _wexClient;
  50. private readonly IGroupManager _goupManager;
  51. //private readonly ITypedCache<List<User>> _cache;
  52. //private readonly ICacheManager<User> _cache;
  53. public TestController(
  54. INewRockClient client,
  55. ILogger<TestController> logger,
  56. IAuthorizeGenerator authorizeGenerator,
  57. IOptionsSnapshot<CallCenterConfiguration> options,
  58. ISessionContext sessionContext,
  59. IUserRepository userRepository,
  60. //ICacheManager<User> cache
  61. //ITypedCache<List<User>> cache
  62. ITypedCache<User> cache,
  63. IRealtimeService realtimeService,
  64. IBlacklistDomainService blacklistDomainService,
  65. IIvrDomainService ivrDomainService,
  66. ISugarUnitOfWork<HotlineDbContext> uow,
  67. IRoleRepository roleRepository,
  68. IMediator mediator,
  69. ITimeLimitApplication timeLimitApplication,
  70. IDistributedLock distributedLock,
  71. IWexClient wexClient
  72. )
  73. {
  74. _logger = logger;
  75. _authorizeGenerator = authorizeGenerator;
  76. _options = options;
  77. _sessionContext = sessionContext;
  78. _userRepository = userRepository;
  79. _cache = cache;
  80. _realtimeService = realtimeService;
  81. _blacklistDomainService = blacklistDomainService;
  82. _ivrDomainService = ivrDomainService;
  83. _uow = uow;
  84. _roleRepository = roleRepository;
  85. _mediator = mediator;
  86. _timeLimitApplication = timeLimitApplication;
  87. _distributedLock = distributedLock;
  88. _wexClient = wexClient;
  89. }
  90. [HttpGet("time")]
  91. public async Task<string> GetTime()
  92. {
  93. //var rsp = await _wexClient.QueryTelsAsync(new QueryTelRequest { StaffNo = "123" }, HttpContext.RequestAborted);
  94. return DateTime.Now.ToString("F");
  95. }
  96. [HttpGet("pgsql")]
  97. public async Task<string> Pgsql()
  98. {
  99. var role = new Role
  100. {
  101. Name = $"test_role_{TimeOnly.FromDateTime(DateTime.Now)}",
  102. DisplayName = "test_role_display",
  103. ClientId = "test"
  104. };
  105. var roleId = await _roleRepository.AddAsync(role, HttpContext.RequestAborted);
  106. role.Description = "Description";
  107. await _roleRepository.UpdateAsync(role, HttpContext.RequestAborted);
  108. return roleId;
  109. }
  110. [AllowAnonymous]
  111. [HttpGet("roles")]
  112. public async Task<List<Role>> GetRoles()
  113. {
  114. using var lockManager = new LockManager(_distributedLock, $"{nameof(TestController)}.{nameof(GetRoles)}");
  115. lockManager.InvokeInLock(() =>
  116. {
  117. Console.WriteLine("do something");
  118. }, TimeSpan.FromSeconds(10));
  119. await lockManager.InvokeInLockAsync(
  120. d => _roleRepository.Queryable(includeDeleted: true).ToListAsync(),
  121. TimeSpan.FromSeconds(10),
  122. HttpContext.RequestAborted);
  123. var a = await _roleRepository.Queryable()
  124. //.Where(d => !d.IsDeleted)
  125. .ToListAsync();
  126. //var a = await db.Queryable<Role>().ToListAsync();
  127. var b = await _roleRepository.Queryable(includeDeleted: true).ToListAsync();
  128. return a;
  129. }
  130. [AllowAnonymous]
  131. [HttpGet("testtime")]
  132. public async Task<TimeResult> TestTime(DateTime beginTime, ETimeType timeType, int timeValue)
  133. {
  134. return _timeLimitApplication.CalcEndTime(beginTime, timeType, timeValue);
  135. }
  136. [AllowAnonymous]
  137. [HttpGet("timeend")]
  138. public async Task<DateTime> GetWorkDayEnd(DateTime date, int days)
  139. {
  140. return _timeLimitApplication.GetEndDateWork(date, days);
  141. }
  142. //[AllowAnonymous]
  143. [HttpGet("hash")]
  144. public async Task Hash()
  145. {
  146. var s = _sessionContext;
  147. }
  148. /// <summary>
  149. /// signalR测试(method: Ring)
  150. /// </summary>
  151. /// <returns></returns>
  152. [HttpGet("ring")]
  153. public async Task RingTest()
  154. {
  155. await _realtimeService.RingAsync(_sessionContext.RequiredUserId, new RingDto { Id = new Guid().ToString(), From = _sessionContext.Phone ?? "未知号码", To = "12345" }, HttpContext.RequestAborted);
  156. }
  157. /// <summary>
  158. /// signalR测试(method: Answered)
  159. /// </summary>
  160. /// <returns></returns>
  161. [HttpGet("answered")]
  162. public async Task AnsweredTest()
  163. {
  164. await _realtimeService.AnsweredAsync(_sessionContext.RequiredUserId, new AnsweredDto() { Id = new Guid().ToString(), From = _sessionContext.Phone ?? "未知号码", To = "12345" }, HttpContext.RequestAborted);
  165. }
  166. /// <summary>
  167. /// signalR测试(method: Bye)
  168. /// </summary>
  169. /// <returns></returns>
  170. [HttpGet("bye")]
  171. public async Task ByeTest()
  172. {
  173. await _realtimeService.ByeAsync(_sessionContext.RequiredUserId, new ByeDto() { Id = new Guid().ToString() }, HttpContext.RequestAborted);
  174. }
  175. /// <summary>
  176. ///
  177. /// </summary>
  178. /// <returns></returns>
  179. [AllowAnonymous]
  180. [HttpGet("t2")]
  181. public async Task GetVoiceEndAnswerAsyncTest()
  182. {
  183. //var answer = await _ivrDomainService.GetVoiceEndAnswerAsync("3", HttpContext.RequestAborted);
  184. //Console.WriteLine(answer.Content);
  185. throw new UserFriendlyException(2001, "test");
  186. }
  187. [AllowAnonymous]
  188. [HttpGet("wfdefine")]
  189. public async Task GetWorkflowDefine()
  190. {
  191. }
  192. [AllowAnonymous]
  193. [Topic("pubsub", "test")]
  194. [HttpPost("t3")]
  195. public Task<int> TestDaprPubsub(int data)
  196. {
  197. _logger.LogDebug("receive dapr event, params: {0}", data);
  198. return Task.FromResult(data);
  199. }
  200. [HttpGet("rsa")]
  201. public async Task<string> Rsa()
  202. {
  203. var keyList = RsaKeyGenerator.Pkcs1Key(2048, true);
  204. var privateKey = keyList[0];
  205. var publicKey = keyList[1];
  206. return $"{publicKey} \r\n {privateKey}";
  207. }
  208. }