using AUTS.Domain.Application; using AUTS.Domain.Entities; using AUTS.Services.Enums; using Newtonsoft.Json; using Newtonsoft.Json.Linq; using System; using System.Collections.Generic; using System.Linq; using System.Net; using System.Text; using System.Threading.Tasks; using System.Web; namespace AUTS.Services.Manager { public static class UserOperationLog { public static void UserLog(string Openration,string Device) { try { TBL_UTS_UserOperation tBL_UTS_UserOperation = new TBL_UTS_UserOperation(); //获得IP string ip = string.Empty; string ipAddr = System.Web.HttpContext.Current.Request.ServerVariables["HTTP_X_FORWARDED_FOR"]; if (string.IsNullOrEmpty(ipAddr)) ipAddr = System.Web.HttpContext.Current.Request.ServerVariables["REMOTE_ADDR"]; if (string.IsNullOrEmpty(ipAddr)) ipAddr = System.Web.HttpContext.Current.Request.UserHostAddress; ip = ipAddr; string browserVersions = string.Empty; HttpBrowserCapabilities hbc = System.Web.HttpContext.Current.Request.Browser; string browserType = hbc.Browser.ToString(); //获取浏览器类型 string browserVersion = hbc.Version.ToString(); //获取版本号 browserVersions = browserType + browserVersion; var phone = System.Web.HttpContext.Current.Request.Cookies["phone"]; var PC = System.Web.HttpContext.Current.Request.Cookies["PC"]; tBL_UTS_UserOperation.UserName = Users.Umodel.UserName; tBL_UTS_UserOperation.Database = "aa"; // Users.GerOnUserCustomer().DatabaseName; tBL_UTS_UserOperation.Browser = browserVersions;//浏览器 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); } catch (Exception ex) { } } /// /// 百度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; } } } }