BaseController.cs 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  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. namespace SupplierWeb.Controllers
  16. {
  17. public abstract class BaseController : Controller
  18. {
  19. protected readonly IMemoryCache _cache;
  20. protected readonly IApiClient _client;
  21. public BaseController(IMemoryCache cache ,IApiClient client)
  22. {
  23. _cache = cache;
  24. _client = client;
  25. }
  26. public string AppId
  27. {
  28. get
  29. {
  30. return User?.Claims?.SingleOrDefault(c => c.Type == "appid")?.Value;
  31. }
  32. }
  33. public string AppSecret
  34. {
  35. get
  36. {
  37. return User?.Claims?.SingleOrDefault(c => c.Type == "appSecret")?.Value;
  38. }
  39. }
  40. public string AccessToken
  41. {
  42. get
  43. {
  44. return User?.Claims?.SingleOrDefault(c => c.Type == "token")?.Value;
  45. }
  46. }
  47. //protected bool checkLogin()
  48. //{
  49. // HttpRequest req = HttpContext.Request;
  50. // StringValues staffids;
  51. // req.Headers.TryGetValue("", out staffids);
  52. // string staffid = "";
  53. // //取token
  54. // var token = (string)_cache.Get(staffid);
  55. // var secret = Config.SecretKey;
  56. // Dictionary<string, object> data;
  57. // Object roleid;
  58. // Int64 roleid1;
  59. // string jsonData = "";
  60. // if (token != null) {
  61. // try
  62. // {
  63. // data = JsonWebToken.DecodeToObject<Dictionary<string, object>>(token, secret);
  64. // data.TryGetValue("roleid", out roleid);
  65. // roleid1 = (Int64)roleid;
  66. // var options = RoleDAL.GetPermissions(roleid1);
  67. // }
  68. // catch (SignatureVerificationException)
  69. // {
  70. // // Given token is either expired or hashed with an unsupported algorithm.
  71. // }
  72. // }
  73. // return true;
  74. //}
  75. protected string getStaffUserid(string staffId)
  76. {
  77. return this.getStaff(staffId, "userid");
  78. }
  79. protected string getStaff(string staffId,string key)
  80. {
  81. Guid id;
  82. if (string.IsNullOrEmpty(staffId))
  83. {
  84. HttpRequest request = HttpContext.Request;
  85. StringValues oo;
  86. request.Headers.TryGetValue("Sso-Token", out oo);
  87. if (oo.Count > 0 && oo.ToArray()[0] != "")
  88. {
  89. staffId = oo.ToArray()[0];
  90. }
  91. }
  92. //判断参数是否合法
  93. if (!string.IsNullOrEmpty(staffId) && Guid.TryParse(staffId, out id))
  94. {
  95. String signtoken;
  96. _cache.TryGetValue<String>(id.ToString(), out signtoken);
  97. var payload = new Dictionary<string, object>()
  98. {
  99. //{ "userid", userid },
  100. //{ "roleid", roleid },
  101. //{ "permission", permission }
  102. };
  103. var secretKey = TokenConfig.SecretKey;
  104. if (signtoken != null)
  105. {
  106. payload = Jwt.JsonWebToken.DecodeToObject(signtoken, secretKey);
  107. }
  108. else
  109. {
  110. return null;
  111. }
  112. Object userid;
  113. payload.TryGetValue(key, out userid);
  114. return userid == null ? null : userid.ToString();
  115. }
  116. else
  117. {
  118. return null;
  119. }
  120. }
  121. protected string getStaff(string key)
  122. {
  123. StringValues oo = HttpContext.Request.Headers["Sso-Token"];
  124. string staffid = "";
  125. if (oo.Count > 0 && oo.ToArray()[0] != "")
  126. {
  127. staffid = oo.ToArray()[0];
  128. }
  129. if (staffid != "")
  130. return this.getStaff(staffid, key);
  131. else
  132. return null;
  133. }
  134. }
  135. }