using Newtonsoft.Json.Linq; using Newtonsoft.Json; using System.Net; using System.Text; using uts_manage; using XAct.Users; namespace AUTS_Server.Service { public class UserOperationLog : IUserOperationLog { private readonly IHttpContextAccessor _httpContextAccessor; public UserOperationLog(IHttpContextAccessor httpContextAccessor) { _httpContextAccessor = httpContextAccessor; } public void UserLog(string Openration, string Device,string name) { tbl_uts_useroperation tBL_UTS_UserOperation = new tbl_uts_useroperation(); string ip = string.Empty; if (_httpContextAccessor.HttpContext.Request.Headers.ContainsKey("X-Forwarded-For")) { ip = _httpContextAccessor.HttpContext.Request.Headers["X-Forwarded-For"].FirstOrDefault(); } else { var remoteIp = _httpContextAccessor.HttpContext.Connection.RemoteIpAddress; if (remoteIp != null) { if (remoteIp.AddressFamily == System.Net.Sockets.AddressFamily.InterNetwork) { ip = remoteIp.MapToIPv4().ToString(); } else { ip = remoteIp.MapToIPv6().ToString(); } } } string browserType = _httpContextAccessor.HttpContext.Request.Headers["User-Agent"].ToString(); string phone = _httpContextAccessor.HttpContext.Request.Cookies.TryGetValue("phone", out var phoneValue) ? phoneValue : null; string PC = _httpContextAccessor.HttpContext.Request.Cookies.TryGetValue("PC", out var pcValue) ? pcValue : null; tBL_UTS_UserOperation.UserName = name; tBL_UTS_UserOperation.Database = "aa"; // Users.GerOnUserCustomer().DatabaseName; tBL_UTS_UserOperation.Browser = browserType;//浏览器 tBL_UTS_UserOperation.Ip = ip;//ip tBL_UTS_UserOperation.Device = Device; tBL_UTS_UserOperation.CreationTime = DateTime.Now;//时间 tBL_UTS_UserOperation.Operation = Openration; tBL_UTS_UserOperation.Location = GetBaiduIp(ip); //SqlConnect.SqlInsertUserLog(tBL_UTS_UserOperation); } /// /// 百度api /// /// public static string GetBaiduIp(string ip) { string location = ""; try { string url = $"https://sp0.baidu.com/8aQDcjqpAAV3otqbppnN2DJv/api.php?query={ip}&co=&resource_id=6006&oe=utf8"; WebClient client = new WebClient(); var buffer = client.DownloadData(url); string jsonText = Encoding.UTF8.GetString(buffer); JObject jo = JObject.Parse(jsonText); Root root = JsonConvert.DeserializeObject(jo.ToString()); foreach (var item in root.data) { location = item.location; } return location; } catch (Exception ex) { //Console.WriteLine(ex); return location; } } } public class Root { public List data { get; set; } } public class DataItem { /// /// /// public string ExtendedLocation { get; set; } /// /// /// public string OriginQuery { get; set; } /// /// /// public string appinfo { get; set; } /// /// /// public int disp_type { get; set; } /// /// /// public string fetchkey { get; set; } /// /// 本地局域网 /// public string location { get; set; } /// /// /// public string origip { get; set; } /// /// /// public string origipquery { get; set; } /// /// /// public string resourceid { get; set; } /// /// /// public int role_id { get; set; } /// /// /// public int shareImage { get; set; } /// /// /// public int showLikeShare { get; set; } /// /// /// public string showlamp { get; set; } /// /// IP地址查询 /// public string titlecont { get; set; } /// /// /// public string tplt { get; set; } } }