BaseController.cs 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. using JCSoft.WX.Framework.Api;
  2. using Microsoft.AspNetCore.Mvc;
  3. using System;
  4. using System.Collections.Generic;
  5. using System.Linq;
  6. using System.Threading.Tasks;
  7. using Microsoft.Extensions.Caching.Memory;
  8. using Jwt;
  9. using Microsoft.AspNetCore.Http;
  10. using Microsoft.Extensions.Primitives;
  11. using Common.Wechat;
  12. using CoreEntity.DAL;
  13. using Npoi.Mapper;
  14. using Common.Config;
  15. using SupplierWeb.Service;
  16. using Nest;
  17. using Elasticsearch.Net;
  18. namespace SupplierWeb.Controllers
  19. {
  20. public abstract class BaseController : Controller
  21. {
  22. protected readonly IMemoryCache _cache;
  23. protected readonly IApiClient _client;
  24. protected readonly IEsClientProvider _clientProvider;
  25. protected readonly ElasticClient _esclient;
  26. protected readonly ElasticLowLevelClient _LowLevelclient;
  27. public BaseController(IMemoryCache cache ,IApiClient client)
  28. {
  29. _cache = cache;
  30. _client = client;
  31. }
  32. public BaseController(IMemoryCache cache, IApiClient client, IEsClientProvider clientProvider)
  33. {
  34. _cache = cache;
  35. _client = client;
  36. _clientProvider = clientProvider;
  37. _esclient = clientProvider.GetClient();
  38. _LowLevelclient = clientProvider.GetLowLevelClient();
  39. }
  40. public string AppId
  41. {
  42. get
  43. {
  44. return User?.Claims?.SingleOrDefault(c => c.Type == "appid")?.Value;
  45. }
  46. }
  47. public string AppSecret
  48. {
  49. get
  50. {
  51. return User?.Claims?.SingleOrDefault(c => c.Type == "appSecret")?.Value;
  52. }
  53. }
  54. public string AccessToken
  55. {
  56. get
  57. {
  58. return User?.Claims?.SingleOrDefault(c => c.Type == "token")?.Value;
  59. }
  60. }
  61. //protected bool checkLogin()
  62. //{
  63. // HttpRequest req = HttpContext.Request;
  64. // StringValues staffids;
  65. // req.Headers.TryGetValue("", out staffids);
  66. // string staffid = "";
  67. // //取token
  68. // var token = (string)_cache.Get(staffid);
  69. // var secret = Config.SecretKey;
  70. // Dictionary<string, object> data;
  71. // Object roleid;
  72. // Int64 roleid1;
  73. // string jsonData = "";
  74. // if (token != null) {
  75. // try
  76. // {
  77. // data = JsonWebToken.DecodeToObject<Dictionary<string, object>>(token, secret);
  78. // data.TryGetValue("roleid", out roleid);
  79. // roleid1 = (Int64)roleid;
  80. // var options = RoleDAL.GetPermissions(roleid1);
  81. // }
  82. // catch (SignatureVerificationException)
  83. // {
  84. // // Given token is either expired or hashed with an unsupported algorithm.
  85. // }
  86. // }
  87. // return true;
  88. //}
  89. protected string getStaffUserid(string staffId)
  90. {
  91. return this.getStaff(staffId, "userid");
  92. }
  93. protected string getStaff(string staffId,string key)
  94. {
  95. Guid id;
  96. if (string.IsNullOrEmpty(staffId))
  97. {
  98. HttpRequest request = HttpContext.Request;
  99. StringValues oo;
  100. request.Headers.TryGetValue("Sso-Token", out oo);
  101. if (oo.Count > 0 && oo.ToArray()[0] != "")
  102. {
  103. staffId = oo.ToArray()[0];
  104. }
  105. }
  106. //判断参数是否合法
  107. if (!string.IsNullOrEmpty(staffId) && Guid.TryParse(staffId, out id))
  108. {
  109. String signtoken;
  110. _cache.TryGetValue<String>(id.ToString(), out signtoken);
  111. var payload = new Dictionary<string, object>()
  112. {
  113. //{ "userid", userid },
  114. //{ "roleid", roleid },
  115. //{ "permission", permission }
  116. };
  117. var secretKey = TokenConfig.SecretKey;
  118. if (signtoken != null)
  119. {
  120. payload = Jwt.JsonWebToken.DecodeToObject(signtoken, secretKey);
  121. }
  122. else
  123. {
  124. return null;
  125. }
  126. Object userid;
  127. payload.TryGetValue(key, out userid);
  128. return userid == null ? null : userid.ToString();
  129. }
  130. else
  131. {
  132. return null;
  133. }
  134. }
  135. protected string getStaff(string key)
  136. {
  137. StringValues oo = HttpContext.Request.Headers["Sso-Token"];
  138. string staffid = "";
  139. if (oo.Count > 0 && oo.ToArray()[0] != "")
  140. {
  141. staffid = oo.ToArray()[0];
  142. }
  143. if (staffid != "")
  144. return this.getStaff(staffid, key);
  145. else
  146. return null;
  147. }
  148. }
  149. }