using JCSoft.WX.Framework.Api; using Microsoft.AspNetCore.Mvc; using System; using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; using Microsoft.Extensions.Caching.Memory; using Jwt; using Microsoft.AspNetCore.Http; using Microsoft.Extensions.Primitives; using Common.Wechat; using CoreEntity.DAL; using Npoi.Mapper; using Common.Config; using SupplierWeb.Service; using Nest; using Elasticsearch.Net; namespace SupplierWeb.Controllers { public abstract class BaseController : Controller { protected readonly IMemoryCache _cache; protected readonly IApiClient _client; protected readonly IEsClientProvider _clientProvider; protected readonly ElasticClient _esclient; protected readonly ElasticLowLevelClient _LowLevelclient; public BaseController(IMemoryCache cache ,IApiClient client) { _cache = cache; _client = client; } public BaseController(IMemoryCache cache, IApiClient client, IEsClientProvider clientProvider) { _cache = cache; _client = client; _clientProvider = clientProvider; _esclient = clientProvider.GetClient(); _LowLevelclient = clientProvider.GetLowLevelClient(); } public string AppId { get { return User?.Claims?.SingleOrDefault(c => c.Type == "appid")?.Value; } } public string AppSecret { get { return User?.Claims?.SingleOrDefault(c => c.Type == "appSecret")?.Value; } } public string AccessToken { get { return User?.Claims?.SingleOrDefault(c => c.Type == "token")?.Value; } } //protected bool checkLogin() //{ // HttpRequest req = HttpContext.Request; // StringValues staffids; // req.Headers.TryGetValue("", out staffids); // string staffid = ""; // //取token // var token = (string)_cache.Get(staffid); // var secret = Config.SecretKey; // Dictionary data; // Object roleid; // Int64 roleid1; // string jsonData = ""; // if (token != null) { // try // { // data = JsonWebToken.DecodeToObject>(token, secret); // data.TryGetValue("roleid", out roleid); // roleid1 = (Int64)roleid; // var options = RoleDAL.GetPermissions(roleid1); // } // catch (SignatureVerificationException) // { // // Given token is either expired or hashed with an unsupported algorithm. // } // } // return true; //} protected string getStaffUserid(string staffId) { return this.getStaff(staffId, "userid"); } protected string getStaff(string staffId,string key) { Guid id; if (string.IsNullOrEmpty(staffId)) { HttpRequest request = HttpContext.Request; StringValues oo; request.Headers.TryGetValue("Sso-Token", out oo); if (oo.Count > 0 && oo.ToArray()[0] != "") { staffId = oo.ToArray()[0]; } } //判断参数是否合法 if (!string.IsNullOrEmpty(staffId) && Guid.TryParse(staffId, out id)) { String signtoken; _cache.TryGetValue(id.ToString(), out signtoken); var payload = new Dictionary() { //{ "userid", userid }, //{ "roleid", roleid }, //{ "permission", permission } }; var secretKey = TokenConfig.SecretKey; if (signtoken != null) { payload = Jwt.JsonWebToken.DecodeToObject(signtoken, secretKey); } else { return null; } Object userid; payload.TryGetValue(key, out userid); return userid == null ? null : userid.ToString(); } else { return null; } } protected string getStaff(string key) { StringValues oo = HttpContext.Request.Headers["Sso-Token"]; string staffid = ""; if (oo.Count > 0 && oo.ToArray()[0] != "") { staffid = oo.ToArray()[0]; } if (staffid != "") return this.getStaff(staffid, key); else return null; } } }