using EntrustSettle.Common; using EntrustSettle.Common.Caches; using EntrustSettle.Common.Const; using EntrustSettle.IRepository.Base; using EntrustSettle.Model.Models.DJY; using Microsoft.AspNetCore.Http; using System; using System.Threading.Tasks; namespace EntrustSettle.Extensions.Middlewares { public class SetAppUserMiddleware { private readonly RequestDelegate _next; private readonly ICaching _caching; public SetAppUserMiddleware(RequestDelegate next, IBaseRepository repository, ICaching caching) { _next = next; _caching = caching; } public async Task Invoke(HttpContext context) { var userId = App.User?.ID; if (!string.IsNullOrEmpty(userId) && !App.User.IsSetUser) { var user = _caching.Get(CacheConst.KeyUserInfoCache + userId); if (user != null) { App.User.SetUser(user); } else { var resp = App.GetService>(); user = await resp.QueryFirst(x => x.GID == App.User.ID && !x.IsLeave); if (user != null) { _caching.Set(CacheConst.KeyUserInfoCache + userId, user, TimeSpan.FromMinutes(AppSettings.app("Startup", "UserInfoCacheTime").ObjToInt())); App.User.SetUser(user); } } } await _next(context); } } }