TestController.cs 7.8 KB

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