using CommonUtil;
|
using Microsoft.AspNetCore.Http;
|
using Microsoft.Extensions.Caching.Memory;
|
using Operater.DAL;
|
using Operater.DbModel;
|
using Operater.DTO.System;
|
using System;
|
using System.Collections.Generic;
|
using System.Linq;
|
using System.Text;
|
using System.Threading.Tasks;
|
|
namespace Operater.Service.System
|
{
|
public class LoginUserInfoService : ILoginUserInfoService
|
{
|
private readonly IHttpContextAccessor _accessor;
|
private readonly IMemoryCache _cache;
|
/// <summary>
|
/// 构造函数
|
/// </summary>
|
/// <param name="accessor"></param>
|
public LoginUserInfoService(IHttpContextAccessor accessor, IMemoryCache memoryCache)
|
{
|
_accessor = accessor;
|
_cache = memoryCache;
|
}
|
|
/// <summary>
|
/// 获取用户信息
|
/// </summary>
|
/// <returns></returns>
|
public LoginUserInfo GetUserInfo()
|
{
|
if (_accessor.HttpContext.Request.Headers["userinfo"].IsNull())
|
{
|
return null;
|
}
|
string headUserInfo = _accessor.HttpContext.Request.Headers["userinfo"].ToString();
|
if (headUserInfo.IsNullOrEmpty())
|
{
|
return null;
|
}
|
LoginUserInfo userInfo = JSONUtil.JsonToObject<LoginUserInfo>(headUserInfo);
|
var appset = GetAppSet();
|
userInfo.AlibabaToken = appset.AlibabaToken;
|
userInfo.YzToken = appset.YzToken;
|
userInfo.YzShopId = appset.YzShopid;
|
return userInfo;
|
}
|
|
private Appset GetAppSet()
|
{
|
string cacheName = "appsetcat";
|
var cache = _cache.Get(cacheName);
|
if (cache.IsNull())
|
{//从数据库获取
|
var appSet = new AppsetDAL().ListGet(t => t.Id != null).ToList();
|
_cache.Set(cacheName, JSONUtil.ObjectToJson(appSet[0]), new MemoryCacheEntryOptions().SetSlidingExpiration(TimeSpan.FromHours(1)));
|
return appSet[0];
|
}
|
return JSONUtil.JsonToObject<Appset>(cache.ToString());
|
}
|
|
public async Task<string> Test()
|
{
|
return "oks";
|
}
|
}
|
}
|