You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

54 lines
1.6 KiB
C#

9 months ago
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<User> 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<User>(CacheConst.KeyUserInfoCache + userId);
if (user != null)
{
App.User.SetUser(user);
}
else
{
var resp = App.GetService<IBaseRepository<User>>();
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);
}
}
}