颐和api

SignTokenService.cs 3.6KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. using System.Net.Http;
  2. using System.Threading.Tasks;
  3. using Microsoft.AspNetCore.Mvc;
  4. using Microsoft.Extensions.Logging;
  5. using Pivotal.Discovery.Client;
  6. using System.Collections.Generic;
  7. namespace Api.SignToken
  8. {
  9. public class SignTokenService : ISignTokenService
  10. {
  11. DiscoveryHttpClientHandler _handler;
  12. ILogger<SignTokenService> _logger;
  13. /// <summary>
  14. /// URL
  15. /// </summary>
  16. private const string URL = "http://signtokenapi/api/";
  17. public SignTokenService(IDiscoveryClient client, ILoggerFactory logFactory)
  18. {
  19. _handler = new DiscoveryHttpClientHandler(client, logFactory.CreateLogger<DiscoveryHttpClientHandler>());
  20. _logger = logFactory.CreateLogger<SignTokenService>();
  21. }
  22. private HttpClient GetClient()
  23. {
  24. var client = new HttpClient(_handler, false);
  25. return client;
  26. }
  27. public async Task<string> Wechat_Admin_LoginAsync(string usercode, string password, string weixin, string weixin_name, string weixin_img, int channel = 4, string returnUrl = null)
  28. {
  29. var client = GetClient();
  30. HttpContent postContent = new FormUrlEncodedContent(new Dictionary<string, string>()
  31. {
  32. {"usercode", usercode},
  33. {"password", password},
  34. {"weixin", weixin},
  35. {"weixin_name", weixin_name},
  36. {"weixin_img", weixin_img},
  37. {"channel", channel.ToString()},
  38. {"returnUrl", returnUrl}
  39. });
  40. var response = await client.PostAsync(URL + "token/wechat_admin_login", postContent);
  41. return await response.Content.ReadAsStringAsync();
  42. //_logger.LogInformation("返回的权限信息为: {0}", result);
  43. }
  44. public async Task<string> Wechat_LoginAsync(string weixin, string weixin_name, string weixin_img, int channel = 4, string returnUrl = null)
  45. {
  46. var client = GetClient();
  47. HttpContent postContent = new FormUrlEncodedContent(new Dictionary<string, string>()
  48. {
  49. {"weixin", weixin},
  50. {"weixin_name", weixin_name},
  51. {"weixin_img", weixin_img},
  52. {"channel", channel.ToString()},
  53. {"returnUrl", returnUrl}
  54. });
  55. var response = await client.PostAsync(URL + "token/wechat_login", postContent);
  56. return await response.Content.ReadAsStringAsync();
  57. //_logger.LogInformation("返回的权限信息为: {0}", result);
  58. }
  59. public string GetInfo()
  60. {
  61. var client = GetClient();
  62. var result = client.GetStringAsync(URL + "info").Result;
  63. _logger.LogInformation("返回的权限信息为: {0}", result);
  64. return result;
  65. }
  66. public async Task<string> GetUserInfoAsync(string usercode)
  67. {
  68. var client = GetClient();
  69. var response = await client.GetAsync(URL + "useraccount/getuserinfo?usercode=" + usercode);
  70. return await response.Content.ReadAsStringAsync();
  71. }
  72. public async Task<string> Wechat_BindAsync(string usercode, string openid, int channel = 4)
  73. {
  74. var client = GetClient();
  75. HttpContent postContent = new FormUrlEncodedContent(new Dictionary<string, string>()
  76. {
  77. {"usercode", usercode},
  78. {"openid", openid},
  79. {"channel", channel.ToString()}
  80. });
  81. var response = await client.PostAsync(URL + "token/wechat_bind", postContent);
  82. return await response.Content.ReadAsStringAsync();
  83. }
  84. }
  85. }