Ver código fonte

用户信息失效时 long.Parse 会抛出异常,改用ToLong转换

Signed-off-by: 奔向天边 <kno3sc@qq.com>
奔向天边 2 anos atrás
pai
commit
e1b9b821cc
1 arquivos alterados com 35 adições e 45 exclusões
  1. 35 45
      Admin.NET/Admin.NET.Core/Service/User/UserManager.cs

+ 35 - 45
Admin.NET/Admin.NET.Core/Service/User/UserManager.cs

@@ -1,4 +1,4 @@
-// 麻省理工学院许可证
+// 麻省理工学院许可证
 //
 // 版权所有 (c) 2021-2023 zuohuaijun,大名科技(天津)有限公司  联系电话/微信:18020030720  QQ:515096995
 //
@@ -15,51 +15,41 @@ namespace Admin.NET.Core;
 public class UserManager : IScoped
 {
     private readonly IHttpContextAccessor _httpContextAccessor;
-    private long _tenantId;
 
-    public long UserId
-    {
-        get => long.Parse(_httpContextAccessor.HttpContext?.User.FindFirst(ClaimConst.UserId)?.Value);
-    }
-
-    public long TenantId
-    {
-        get
-        {
-            var tId = _httpContextAccessor.HttpContext?.User.FindFirst(ClaimConst.TenantId)?.Value;
-            return string.IsNullOrWhiteSpace(tId) ? _tenantId : long.Parse(tId);
-        }
-        set => _tenantId = value;
-    }
-
-    public string Account
-    {
-        get => _httpContextAccessor.HttpContext?.User.FindFirst(ClaimConst.Account)?.Value;
-    }
-
-    public string RealName
-    {
-        get => _httpContextAccessor.HttpContext?.User.FindFirst(ClaimConst.RealName)?.Value;
-    }
-
-    public bool SuperAdmin
-    {
-        get => _httpContextAccessor.HttpContext?.User.FindFirst(ClaimConst.AccountType)?.Value == ((int)AccountTypeEnum.SuperAdmin).ToString();
-    }
-
-    public long OrgId
-    {
-        get
-        {
-            var orgId = _httpContextAccessor.HttpContext?.User.FindFirst(ClaimConst.OrgId)?.Value;
-            return string.IsNullOrWhiteSpace(orgId) ? 0 : long.Parse(orgId);
-        }
-    }
-
-    public string OpenId
-    {
-        get => _httpContextAccessor.HttpContext?.User.FindFirst(ClaimConst.OpenId)?.Value;
-    }
+    /// <summary>
+    /// 用户ID
+    /// </summary>
+    public long UserId => (_httpContextAccessor.HttpContext?.User.FindFirst(ClaimConst.UserId)?.Value).ToLong();
+
+    /// <summary>
+    /// 租户ID
+    /// </summary>
+    public long TenantId => (_httpContextAccessor.HttpContext?.User.FindFirst(ClaimConst.TenantId)?.Value).ToLong();
+
+    /// <summary>
+    /// 用户账号
+    /// </summary>
+    public string Account => _httpContextAccessor.HttpContext?.User.FindFirst(ClaimConst.Account)?.Value;
+
+    /// <summary>
+    /// 真实姓名
+    /// </summary>
+    public string RealName => _httpContextAccessor.HttpContext?.User.FindFirst(ClaimConst.RealName)?.Value;
+
+    /// <summary>
+    /// 是否超级管理员
+    /// </summary>
+    public bool SuperAdmin => _httpContextAccessor.HttpContext?.User.FindFirst(ClaimConst.AccountType)?.Value == ((int)AccountTypeEnum.SuperAdmin).ToString();
+
+    /// <summary>
+    /// 组织机构Id
+    /// </summary>
+    public long OrgId => (_httpContextAccessor.HttpContext?.User.FindFirst(ClaimConst.OrgId)?.Value).ToLong();
+
+    /// <summary>
+    /// 微信OpenId
+    /// </summary>
+    public string OpenId => _httpContextAccessor.HttpContext?.User.FindFirst(ClaimConst.OpenId)?.Value;
 
     public UserManager(IHttpContextAccessor httpContextAccessor)
     {