123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167 |
- 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<string, object> data;
- // Object roleid;
- // Int64 roleid1;
- // string jsonData = "";
- // if (token != null) {
- // try
- // {
- // data = JsonWebToken.DecodeToObject<Dictionary<string, object>>(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<String>(id.ToString(), out signtoken);
- var payload = new Dictionary<string, object>()
- {
- //{ "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;
- }
- }
- }
|