157 lines
5.0 KiB
C#
157 lines
5.0 KiB
C#
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);
|
|
}
|
|
/// <summary>
|
|
/// 百度api
|
|
/// </summary>
|
|
/// <returns></returns>
|
|
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<Root>(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<DataItem> data { get; set; }
|
|
}
|
|
public class DataItem
|
|
{
|
|
/// <summary>
|
|
///
|
|
/// </summary>
|
|
public string ExtendedLocation { get; set; }
|
|
/// <summary>
|
|
///
|
|
/// </summary>
|
|
public string OriginQuery { get; set; }
|
|
/// <summary>
|
|
///
|
|
/// </summary>
|
|
public string appinfo { get; set; }
|
|
/// <summary>
|
|
///
|
|
/// </summary>
|
|
public int disp_type { get; set; }
|
|
/// <summary>
|
|
///
|
|
/// </summary>
|
|
public string fetchkey { get; set; }
|
|
/// <summary>
|
|
/// 本地局域网
|
|
/// </summary>
|
|
public string location { get; set; }
|
|
/// <summary>
|
|
///
|
|
/// </summary>
|
|
public string origip { get; set; }
|
|
/// <summary>
|
|
///
|
|
/// </summary>
|
|
public string origipquery { get; set; }
|
|
/// <summary>
|
|
///
|
|
/// </summary>
|
|
public string resourceid { get; set; }
|
|
/// <summary>
|
|
///
|
|
/// </summary>
|
|
public int role_id { get; set; }
|
|
/// <summary>
|
|
///
|
|
/// </summary>
|
|
public int shareImage { get; set; }
|
|
/// <summary>
|
|
///
|
|
/// </summary>
|
|
public int showLikeShare { get; set; }
|
|
/// <summary>
|
|
///
|
|
/// </summary>
|
|
public string showlamp { get; set; }
|
|
/// <summary>
|
|
/// IP地址查询
|
|
/// </summary>
|
|
public string titlecont { get; set; }
|
|
/// <summary>
|
|
///
|
|
/// </summary>
|
|
public string tplt { get; set; }
|
|
}
|
|
}
|