// Admin.NET 项目的版权、商标、专利和其他相关权利均受相应法律法规的保护。使用本项目应遵守相关法律法规和许可证的要求。 // // 本项目主要遵循 MIT 许可证和 Apache 许可证(版本 2.0)进行分发和使用。许可证位于源代码树根目录中的 LICENSE-MIT 和 LICENSE-APACHE 文件。 // // 不得利用本项目从事危害国家安全、扰乱社会秩序、侵犯他人合法权益等法律法规禁止的活动!任何基于本项目二次开发而产生的一切法律纠纷和责任,我们不承担任何责任! using System.Text.Json; namespace Admin.NET.Core; /// /// 当前登录用户信息 /// public class UserManager( SysCacheService sysCacheService, IHttpContextAccessor httpContextAccessor) : UserSessionDao, IScoped { /// /// 用户Session /// [System.Text.Json.Serialization.JsonIgnore] [Newtonsoft.Json.JsonIgnore] protected virtual UserSessionDao _session { get; set; } /// /// 代理对象 /// [System.Text.Json.Serialization.JsonIgnore] [Newtonsoft.Json.JsonIgnore] protected virtual UserSessionDao Session { get { if (_session == null || _session.UserId != UserId) _session = sysCacheService.Get(CacheConst.KeyUserSession + UserId); return _session; } } /// /// 用户Id /// [System.Text.Json.Serialization.JsonIgnore] [Newtonsoft.Json.JsonIgnore] public override long UserId => (httpContextAccessor.HttpContext?.User.FindFirst(nameof(UserId))?.Value).ToLong(); /// /// 应用Id /// public override long? AppId => Session?.AppId; /// /// 租户Id /// public override long? TenantId => Session?.TenantId; /// /// 用户账号 /// public override string Account => Session?.Account; /// /// 真实姓名 /// public override string RealName => Session?.RealName; /// /// 昵称 /// public override string NickName => Session?.NickName; /// /// 账号类型 /// public override AccountTypeEnum? AccountType => Session?.AccountType; /// /// 组织机构Id /// public override long OrgId => Session?.OrgId ?? 0; /// /// 组织机构名称 /// public override string OrgName => Session?.OrgName; /// /// 组织机构Id /// public override string OrgType => Session?.OrgType; /// /// 组织机构级别 /// public override int? OrgLevel => Session?.OrgLevel; /// /// 登录模式 /// public override LoginModeEnum? LoginMode => Session?.LoginMode; /// /// 微信OpenId /// public override string OpenId => Session?.OpenId; /// /// 扩展属性 /// public override Dictionary ExtProps => Session?.ExtProps; /// /// 用户Session是否存在 /// public bool ExistSession(long userId) { return sysCacheService.ExistKey(CacheConst.KeyUserSession + userId); } /// /// 设置用户Session /// public void SetSession(UserSessionDao userSession, TimeSpan expire) { sysCacheService.Set(CacheConst.KeyUserSession + userSession.UserId, userSession, expire); } /// /// 清除指定用户Session /// public void RemoveSession(long userId) { sysCacheService.Remove(CacheConst.KeyUserSession + userId); } /// /// 获取指定用户Session /// public UserSessionDao GetSession(dynamic userId) { return sysCacheService.Get(CacheConst.KeyUserSession + userId); } /// /// 获取扩展属性 /// public T GetExtProp(string key, T defaultValue = default) { if (ExtProps == null || !ExtProps.TryGetValue(key, out var value) || value is null) return defaultValue; if (value is JsonElement element) return element.Deserialize(); try { return (T)Convert.ChangeType(value, typeof(T)); } catch { return defaultValue; } } }