Blame view

Yi.Abp.Net8/module/rbac/Yi.Framework.Rbac.Domain/Entities/LoginLogAggregateRoot.cs 3.12 KB
515fceeb   “wangming”   框架初始化
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
  using IPTools.Core;
  using Microsoft.AspNetCore.Http;
  using SqlSugar;
  using UAParser;
  using Volo.Abp.Auditing;
  using Volo.Abp.Domain.Entities;
  using Yi.Framework.Core.Extensions;
  
  namespace Yi.Framework.Rbac.Domain.Entities
  {
      [SugarTable("LoginLog")]
      [SugarIndex($"index_{nameof(LoginUser)}", nameof(LoginUser), OrderByType.Asc)]
      public class LoginLogAggregateRoot : AggregateRoot<Guid>, ICreationAuditedObject
      {
          [SugarColumn(ColumnName = "Id", IsPrimaryKey = true)]
          public override Guid Id { get; protected set; }
          public DateTime CreationTime { get; set; }
  
          /// <summary>
          /// 登录用户 
          ///</summary>
          [SugarColumn(ColumnName = "LoginUser")]
          public string? LoginUser { get; set; }
          /// <summary>
          /// 登录地点 
          ///</summary>
          [SugarColumn(ColumnName = "LoginLocation")]
          public string? LoginLocation { get; set; }
          /// <summary>
          /// 登录Ip 
          ///</summary>
          [SugarColumn(ColumnName = "LoginIp")]
          public string? LoginIp { get; set; }
          /// <summary>
          /// 浏览器 
          ///</summary>
          [SugarColumn(ColumnName = "Browser")]
          public string? Browser { get; set; }
          /// <summary>
          /// 操作系统 
          ///</summary>
          [SugarColumn(ColumnName = "Os")]
          public string? Os { get; set; }
          /// <summary>
          /// 登录信息 
          ///</summary>
          [SugarColumn(ColumnName = "LogMsg")]
          public string? LogMsg { get; set; }
  
          public Guid? CreatorId { get; set; }
  
  
          public LoginLogAggregateRoot GetInfoByHttpContext(HttpContext context)
          {
              ClientInfo GetClientInfo(HttpContext context)
              {
                  var str = context.GetUserAgent();
                  var uaParser = Parser.GetDefault();
                  ClientInfo c;
                  try
                  {
                       c = uaParser.Parse(str);
                  }
                  catch
                  {
                      c = new ClientInfo("null",new OS("null", "null", "null", "null", "null"),new Device("null","null","null"), new UserAgent("null", "null", "null", "null"));
                  }
                  return c;
              }
              var ipAddr = context.GetClientIp();
              IpInfo location;
              if (ipAddr == "127.0.0.1")
              {
                  location = new IpInfo() { Province = "本地", City = "本机" };
              }
              else
              {
                  try
                  {
                      location = IpTool.Search(ipAddr);
                  }
                  catch
                  {
                      location = new IpInfo() { Province = ipAddr, City = "未知地区" };
                  }
              }
              ClientInfo clientInfo = GetClientInfo(context);
              LoginLogAggregateRoot entity = new()
              {
                  Browser = clientInfo.Device.Family,
                  Os = clientInfo.OS.ToString(),
                  LoginIp = ipAddr,
                  LoginLocation = location.Province + "-" + location.City
              };
              return entity;
          }
      }
  
  }