初始化
This commit is contained in:
306
Face.Services/Tool/DateTimeDiff.cs
Normal file
306
Face.Services/Tool/DateTimeDiff.cs
Normal file
@@ -0,0 +1,306 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace Face.Services.Tool
|
||||
{
|
||||
public class DateTimeDiff
|
||||
{
|
||||
public DateTimeDiff()
|
||||
{
|
||||
//
|
||||
// TODO: 在此处添加构造函数逻辑
|
||||
//
|
||||
}
|
||||
/// <summary>
|
||||
/// 把秒转换成分钟
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
public static int SecondToMinute(int Second)
|
||||
{
|
||||
decimal mm = (decimal)((decimal)Second / (decimal)60);
|
||||
return Convert.ToInt32(Math.Ceiling(mm));
|
||||
}
|
||||
/// <summary>
|
||||
/// 计算两个时间的时间间隔
|
||||
/// </summary>
|
||||
/// <param name="DateTimeOld">较早的日期和时间</param>
|
||||
/// <param name="DateTimeNew">较后的日期和时间</param>
|
||||
/// <returns></returns>
|
||||
public static string DateDiff(DateTime DateTimeOld, DateTime DateTimeNew)
|
||||
{
|
||||
string dateDiff = "";
|
||||
TimeSpan ts1 = new TimeSpan(DateTimeOld.Ticks);
|
||||
TimeSpan ts2 = new TimeSpan(DateTimeNew.Ticks);
|
||||
TimeSpan ts = ts1.Subtract(ts2).Duration();
|
||||
int day = ts.Days;
|
||||
int hou = ts.Hours;
|
||||
int minu = ts.Minutes;
|
||||
int sec = ts.Seconds;
|
||||
if (day > 0)
|
||||
{
|
||||
if (day > 30)
|
||||
{
|
||||
if (day > 364)
|
||||
{
|
||||
dateDiff += day / 365 + "年";
|
||||
}
|
||||
else
|
||||
{
|
||||
dateDiff += day / 30 + "个月";
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
dateDiff += day.ToString() + "天";
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
if (hou > 0)
|
||||
{
|
||||
dateDiff += hou.ToString() + "小时";
|
||||
}
|
||||
else
|
||||
{
|
||||
if (minu > 0)
|
||||
{
|
||||
dateDiff += minu.ToString() + "分钟";
|
||||
}
|
||||
else
|
||||
{
|
||||
if (sec > 0)
|
||||
{
|
||||
dateDiff += sec.ToString() + "秒";
|
||||
}
|
||||
else
|
||||
{
|
||||
dateDiff += "0秒";
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
if (DateTimeNew.CompareTo(DateTimeOld) > 0)
|
||||
{
|
||||
dateDiff += "前";
|
||||
}
|
||||
else
|
||||
{
|
||||
dateDiff += "后";
|
||||
}
|
||||
return dateDiff;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 返回两个日期之间的时间间隔(y:年份间隔、M:月份间隔、d:天数间隔、h:小时间隔、m:分钟间隔、s:秒钟间隔、ms:微秒间隔)
|
||||
/// </summary>
|
||||
/// <param name="Date1">开始日期</param>
|
||||
/// <param name="Date2">结束日期</param>
|
||||
/// <param name="Interval">间隔标志</param>
|
||||
/// <returns>返回间隔标志指定的时间间隔</returns>
|
||||
public static int DateDiff(System.DateTime Date1, System.DateTime Date2, string Interval)
|
||||
{
|
||||
double dblYearLen = 365;//年的长度,365天
|
||||
double dblMonthLen = (365 / 12);//每个月平均的天数
|
||||
System.TimeSpan objT;
|
||||
objT = Date2.Subtract(Date1);
|
||||
switch (Interval)
|
||||
{
|
||||
case "y"://返回日期的年份间隔
|
||||
return System.Convert.ToInt32(objT.Days / dblYearLen);
|
||||
case "M"://返回日期的月份间隔
|
||||
return System.Convert.ToInt32(objT.Days / dblMonthLen);
|
||||
case "d"://返回日期的天数间隔
|
||||
return objT.Days;
|
||||
case "h"://返回日期的小时间隔
|
||||
return objT.Hours;
|
||||
case "m"://返回日期的分钟间隔
|
||||
return objT.Minutes;
|
||||
case "s"://返回日期的秒钟间隔
|
||||
return objT.Seconds;
|
||||
case "ms"://返回时间的微秒间隔
|
||||
return objT.Milliseconds;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
///判断是否于1分钟之前
|
||||
/// </summary>
|
||||
/// <param name="DateTimeOld">较早的日期和时间</param>
|
||||
/// <returns></returns>
|
||||
public static bool DateDiff_minu(DateTime DateTimeOld)
|
||||
{
|
||||
TimeSpan ts1 = new TimeSpan(DateTimeOld.Ticks);
|
||||
TimeSpan ts2 = new TimeSpan(DateTime.Now.Ticks);
|
||||
TimeSpan ts = ts1.Subtract(ts2).Duration();
|
||||
int minu = ts.Minutes;
|
||||
if (minu > 1)
|
||||
{
|
||||
return true;
|
||||
}
|
||||
else
|
||||
{
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
///判断是否于m分钟之前
|
||||
/// </summary>
|
||||
/// <param name="DateTimeOld">较早的日期和时间</param>
|
||||
/// <returns></returns>
|
||||
public static bool DateDiff_minu(DateTime DateTimeOld, int m)
|
||||
{
|
||||
TimeSpan ts1 = new TimeSpan(DateTimeOld.Ticks);
|
||||
TimeSpan ts2 = new TimeSpan(DateTime.Now.Ticks);
|
||||
TimeSpan ts = ts1.Subtract(ts2).Duration();
|
||||
int minu = ts.Minutes;
|
||||
if (minu > m)
|
||||
{
|
||||
return true;
|
||||
}
|
||||
else
|
||||
{
|
||||
return false;
|
||||
}
|
||||
}
|
||||
public static bool DateDiff_1minu(DateTime DateTimeOld)
|
||||
{
|
||||
TimeSpan ts1 = new TimeSpan(DateTimeOld.Ticks);
|
||||
TimeSpan ts2 = new TimeSpan(DateTime.Now.Ticks);
|
||||
TimeSpan ts = ts1.Subtract(ts2).Duration();
|
||||
int minu = ts.Seconds;
|
||||
if (minu > 10)
|
||||
{
|
||||
return true;
|
||||
}
|
||||
else
|
||||
{
|
||||
return false;
|
||||
}
|
||||
}
|
||||
/// <summary>
|
||||
/// 与当前时间比较,重载时间比较函数,只有一个参数
|
||||
/// </summary>
|
||||
/// <param name="DateTimeOld">较早的日期和时间</param>
|
||||
/// <returns></returns>
|
||||
public static string DateDiff(DateTime DateTimeOld)
|
||||
{
|
||||
return DateDiff(DateTimeOld, DateTime.Now);
|
||||
}
|
||||
/// <summary>
|
||||
/// 日期比较,返回精确的几分几秒
|
||||
/// </summary>
|
||||
/// <param name="DateTime1">较早的日期和时间</param>
|
||||
/// <param name="DateTime2">较迟的日期和时间</param>
|
||||
/// <returns></returns>
|
||||
public static string DateDiff_full(DateTime DateTime1, DateTime DateTime2)
|
||||
{
|
||||
string dateDiff = null;
|
||||
TimeSpan ts1 = new TimeSpan(DateTime1.Ticks);
|
||||
TimeSpan ts2 = new TimeSpan(DateTime2.Ticks);
|
||||
TimeSpan ts = ts1.Subtract(ts2).Duration();
|
||||
dateDiff = ts.Days.ToString() + "天" + ts.Hours.ToString() + "时" + ts.Minutes.ToString() + "分" + ts.Seconds.ToString() + "秒";
|
||||
return dateDiff;
|
||||
}
|
||||
/// <summary>
|
||||
/// 时间比较,返回精确的几秒
|
||||
/// </summary>
|
||||
/// <param name="DateTime1">较早的日期和时间</param>
|
||||
/// <param name="DateTime2">较迟的日期和时间</param>
|
||||
/// <returns></returns>
|
||||
public static int DateDiff_Sec(DateTime DateTime1, DateTime DateTime2)
|
||||
{
|
||||
TimeSpan ts1 = new TimeSpan(DateTime1.Ticks);
|
||||
TimeSpan ts2 = new TimeSpan(DateTime2.Ticks);
|
||||
TimeSpan ts = ts1.Subtract(ts2).Duration();
|
||||
int dateDiff = ts.Days * 86400 + ts.Hours * 3600 + ts.Minutes * 60 + ts.Seconds;
|
||||
return dateDiff;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 日期比较
|
||||
/// </summary>
|
||||
/// <param name="today">当前日期</param>
|
||||
/// <param name="writeDate">输入日期</param>
|
||||
/// <param name="n">比较天数</param>
|
||||
/// <returns>大于天数返回true,小于返回false</returns>
|
||||
public static bool CompareDate(string today, string writeDate, int n)
|
||||
{
|
||||
DateTime Today = Convert.ToDateTime(today);
|
||||
DateTime WriteDate = Convert.ToDateTime(writeDate);
|
||||
WriteDate = WriteDate.AddDays(n);
|
||||
if (Today >= WriteDate)
|
||||
return false;
|
||||
else
|
||||
return true;
|
||||
}
|
||||
|
||||
public static string FormatProgress(DateTime StartTime, DateTime EndTime)
|
||||
{
|
||||
|
||||
|
||||
if (DateTime.Now > DateTime.Parse(EndTime.ToShortDateString() + " 23:59:59"))
|
||||
return "活动结束";
|
||||
else if (DateTime.Now < DateTime.Parse(StartTime.ToShortDateString() + " 0:0:0"))
|
||||
return "即将开始";
|
||||
else
|
||||
{
|
||||
int totalDay = DateTimeDiff.DateDiff(StartTime, EndTime, "d");
|
||||
int inDay = DateTimeDiff.DateDiff(StartTime, DateTime.Now, "d");
|
||||
if ((float)inDay / (float)totalDay < 0.2f)
|
||||
return "刚刚开始";
|
||||
else if ((float)inDay / (float)totalDay >= 0.2f && (float)inDay / (float)totalDay < 0.4)
|
||||
return "正在进行";
|
||||
else if ((float)inDay / (float)totalDay >= 0.4 && (float)inDay / (float)totalDay < 0.6)
|
||||
return "活动过半";
|
||||
else if ((float)inDay / (float)totalDay > 0.6 && (float)inDay / (float)totalDay <= 0.8)
|
||||
return "进入尾声";
|
||||
else
|
||||
return "即将结束";
|
||||
}
|
||||
}
|
||||
|
||||
// 时间戳转为C#格式时间
|
||||
public static DateTime StampToDateTime(string timeStamp)
|
||||
{
|
||||
DateTime dateTimeStart = TimeZone.CurrentTimeZone.ToLocalTime(new DateTime(1970, 1, 1));
|
||||
long lTime = long.Parse(timeStamp + "0000000");
|
||||
TimeSpan toNow = new TimeSpan(lTime);
|
||||
|
||||
return dateTimeStart.Add(toNow);
|
||||
}
|
||||
|
||||
// DateTime时间格式转换为Unix时间戳格式
|
||||
public static double DateTimeToStamp(System.DateTime time)
|
||||
{
|
||||
System.DateTime startTime = TimeZone.CurrentTimeZone.ToLocalTime(new System.DateTime(1970, 1, 1));
|
||||
return (time - startTime).TotalMilliseconds;
|
||||
}
|
||||
/// <summary>
|
||||
///判断是否于多少分钟之前
|
||||
/// </summary>
|
||||
/// <param name="DateTimeOld">较早的日期和时间</param>
|
||||
/// <returns></returns>
|
||||
public static bool allDateDiff_minu(DateTime DateTimeOld, int m)
|
||||
{
|
||||
TimeSpan ts1 = new TimeSpan(DateTimeOld.Ticks);
|
||||
TimeSpan ts2 = new TimeSpan(DateTime.Now.Ticks);
|
||||
TimeSpan ts = ts1.Subtract(ts2).Duration();
|
||||
int minu = ts.Minutes;
|
||||
if (minu > m)
|
||||
{
|
||||
return true;
|
||||
}
|
||||
else
|
||||
{
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
53
Face.Services/Tool/ModelHelper.cs
Normal file
53
Face.Services/Tool/ModelHelper.cs
Normal file
@@ -0,0 +1,53 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Data;
|
||||
using System.Linq;
|
||||
using System.Reflection;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace Face.Services.Tool
|
||||
{
|
||||
/// <summary>
|
||||
/// 实体帮助
|
||||
/// </summary>
|
||||
public static class ModelHelper
|
||||
{
|
||||
/// <summary>
|
||||
/// 获取实体属性名
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
public static string GetModelPropertyName<T>() where T : class, new()
|
||||
{
|
||||
string nameStr = "";
|
||||
|
||||
foreach (PropertyInfo pi in (new T()).GetType().GetProperties())
|
||||
{
|
||||
//获得属性的名字,后面就可以根据名字判断来进行些自己想要的操作
|
||||
var name = pi.Name;
|
||||
//var value = pi.GetValue(list, null);//用pi.GetValue获得值
|
||||
//var type = value?.GetType() ?? typeof(object);//获得属性的类型
|
||||
|
||||
nameStr += "`" + name + "`,";
|
||||
}
|
||||
return nameStr.Substring(0, nameStr.Length - 1);//nameStr; 去除最后一个逗号
|
||||
}
|
||||
|
||||
public static List<Dictionary<string, object>> TableToRow(DataTable tbl)
|
||||
{
|
||||
List<Dictionary<string, object>> rows = new List<Dictionary<string, object>>();
|
||||
foreach (DataRow Row in tbl.Rows)//循环行
|
||||
{
|
||||
Dictionary<string, object> row = new Dictionary<string, object>();
|
||||
for (int i = 0; i < Row.ItemArray.Length; i++)
|
||||
{
|
||||
row.Add(tbl.Columns[i].ColumnName, Row[i].ToString());
|
||||
}
|
||||
rows.Add(row);
|
||||
|
||||
}
|
||||
return rows;
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
1125
Face.Services/Tool/RedisHelper.cs
Normal file
1125
Face.Services/Tool/RedisHelper.cs
Normal file
File diff suppressed because it is too large
Load Diff
17
Face.Services/Tool/System/ComputerHelp.cs
Normal file
17
Face.Services/Tool/System/ComputerHelp.cs
Normal file
@@ -0,0 +1,17 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace Face.Services.Tool
|
||||
{
|
||||
public class ComputerHelp
|
||||
{
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
}
|
||||
}
|
||||
162
Face.Services/Tool/System/ConfigHelper.cs
Normal file
162
Face.Services/Tool/System/ConfigHelper.cs
Normal file
@@ -0,0 +1,162 @@
|
||||
using Face.Services.Extensions;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Configuration;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using System.Web.Configuration;
|
||||
|
||||
namespace Face.Services.Tool
|
||||
{
|
||||
/// <summary>
|
||||
/// web.config操作类
|
||||
/// Copyright (C) Maticsoft
|
||||
/// </summary>
|
||||
public sealed class ConfigHelper
|
||||
{
|
||||
/// <summary>
|
||||
/// 得到AppSettings中的配置字符串信息
|
||||
/// </summary>
|
||||
/// <param name="key"></param>
|
||||
/// <returns></returns>
|
||||
public static string GetConfigString(string key)
|
||||
{
|
||||
string CacheKey = "AppSettings-" + key;
|
||||
object objModel = CacheExtensions.GetCache<object>(CacheKey);
|
||||
if (objModel == null)
|
||||
{
|
||||
try
|
||||
{
|
||||
objModel = ConfigurationManager.AppSettings[key];
|
||||
if (objModel != null)
|
||||
{
|
||||
CacheExtensions.SetCache(CacheKey, objModel, Enums.CacheTimeType.ByHours, 3);
|
||||
}
|
||||
}
|
||||
catch
|
||||
{ }
|
||||
}
|
||||
return objModel.ToString();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 得到AppSettings中的配置Bool信息
|
||||
/// </summary>
|
||||
/// <param name="key"></param>
|
||||
/// <returns></returns>
|
||||
public static bool GetConfigBool(string key)
|
||||
{
|
||||
bool result = false;
|
||||
string cfgVal = GetConfigString(key);
|
||||
if (null != cfgVal && string.Empty != cfgVal)
|
||||
{
|
||||
try
|
||||
{
|
||||
result = bool.Parse(cfgVal);
|
||||
}
|
||||
catch (FormatException)
|
||||
{
|
||||
// Ignore format exceptions.
|
||||
}
|
||||
}
|
||||
return result;
|
||||
}
|
||||
/// <summary>
|
||||
/// 得到AppSettings中的配置Decimal信息
|
||||
/// </summary>
|
||||
/// <param name="key"></param>
|
||||
/// <returns></returns>
|
||||
public static decimal GetConfigDecimal(string key)
|
||||
{
|
||||
decimal result = 0;
|
||||
string cfgVal = GetConfigString(key);
|
||||
if (null != cfgVal && string.Empty != cfgVal)
|
||||
{
|
||||
try
|
||||
{
|
||||
result = decimal.Parse(cfgVal);
|
||||
}
|
||||
catch (FormatException)
|
||||
{
|
||||
// Ignore format exceptions.
|
||||
}
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
/// <summary>
|
||||
/// 得到AppSettings中的配置int信息
|
||||
/// </summary>
|
||||
/// <param name="key"></param>
|
||||
/// <returns></returns>
|
||||
public static int GetConfigInt(string key)
|
||||
{
|
||||
int result = 0;
|
||||
string cfgVal = GetConfigString(key);
|
||||
if (null != cfgVal && string.Empty != cfgVal)
|
||||
{
|
||||
try
|
||||
{
|
||||
result = int.Parse(cfgVal);
|
||||
}
|
||||
catch (FormatException)
|
||||
{
|
||||
// Ignore format exceptions.
|
||||
}
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 修改AppSettings中的配置信息
|
||||
/// </summary>
|
||||
/// <param name="key"></param>
|
||||
/// <returns></returns>
|
||||
public static void UpdatetConfig(string key, string value)
|
||||
{
|
||||
Configuration objConfig = WebConfigurationManager.OpenWebConfiguration("~");
|
||||
AppSettingsSection objAppSettings = (AppSettingsSection)objConfig.GetSection("appSettings");
|
||||
if (objAppSettings != null)
|
||||
{
|
||||
objAppSettings.Settings[key].Value = value;
|
||||
objConfig.Save();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// 加载配置文件 app升级配置文件
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
public static Dictionary<String, String> loadCfg()
|
||||
{
|
||||
string cfgPath = Path.GetDirectoryName(AppDomain.CurrentDomain.SetupInformation.ApplicationBase)
|
||||
+ Path.DirectorySeparatorChar + "appset" + Path.DirectorySeparatorChar + "appconfig.properties";
|
||||
Dictionary<String, String> cfg = new Dictionary<string, string>();
|
||||
using (StreamReader sr = new StreamReader(cfgPath))
|
||||
{
|
||||
while (sr.Peek() >= 0)
|
||||
{
|
||||
string line = sr.ReadLine();
|
||||
if (line.StartsWith("#"))
|
||||
{
|
||||
continue;
|
||||
}
|
||||
int startInd = line.IndexOf("=");
|
||||
string key = line.Substring(0, startInd);
|
||||
string val = line.Substring(startInd + 1, line.Length - (startInd + 1));
|
||||
if (!cfg.ContainsKey(key) && !string.IsNullOrEmpty(val))
|
||||
{
|
||||
cfg.Add(key, val);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return cfg;
|
||||
}
|
||||
}
|
||||
}
|
||||
43
Face.Services/Tool/System/EnumHelper.cs
Normal file
43
Face.Services/Tool/System/EnumHelper.cs
Normal file
@@ -0,0 +1,43 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.ComponentModel;
|
||||
using System.Linq;
|
||||
using System.Reflection;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace Face.Services.Tool
|
||||
{
|
||||
/// <summary>
|
||||
/// 枚举帮助类
|
||||
/// </summary>
|
||||
public static class EnumHelper
|
||||
{
|
||||
|
||||
///// <summary>
|
||||
///// 获取枚举的Short类型
|
||||
///// </summary>
|
||||
///// <param name="value"></param>
|
||||
///// <returns></returns>
|
||||
//public static short GetShortValue(Enum value)
|
||||
//{
|
||||
// return short.Parse(((int)Enum.Parse(value.GetType(), value.ToString())).ToString());
|
||||
//}
|
||||
|
||||
///// <summary>
|
||||
///// 得到枚举中文备注
|
||||
///// </summary>
|
||||
///// <param name="enumValue"></param>
|
||||
///// <returns></returns>
|
||||
//public static string GetEnumDesc(Enum enumValue)
|
||||
//{
|
||||
// string value = enumValue.ToString();
|
||||
// FieldInfo field = enumValue.GetType().GetField(value);
|
||||
// object[] objs = field.GetCustomAttributes(typeof(DescriptionAttribute), false); //获取描述属性
|
||||
// if (objs.Length == 0) //当描述属性没有时,直接返回名称
|
||||
// return value;
|
||||
// DescriptionAttribute descriptionAttribute = (DescriptionAttribute)objs[0];
|
||||
// return descriptionAttribute.Description;
|
||||
//}
|
||||
}
|
||||
}
|
||||
94
Face.Services/Tool/System/IPHelper.cs
Normal file
94
Face.Services/Tool/System/IPHelper.cs
Normal file
@@ -0,0 +1,94 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Net;
|
||||
using System.Text;
|
||||
using System.Text.RegularExpressions;
|
||||
using System.Threading.Tasks;
|
||||
using System.Web;
|
||||
|
||||
namespace Face.Services.Tool
|
||||
{
|
||||
public class IPHelper
|
||||
{
|
||||
public static string GetIP()
|
||||
{
|
||||
if (HttpContext.Current == null)
|
||||
{
|
||||
throw new Exception("HttpContext.Current为null");
|
||||
}
|
||||
|
||||
string ip = string.Empty;
|
||||
if (!string.IsNullOrEmpty(HttpContext.Current.Request.ServerVariables["HTTP_VIA"]))
|
||||
ip = Convert.ToString(HttpContext.Current.Request.ServerVariables["HTTP_X_FORWARDED_FOR"]);
|
||||
if (string.IsNullOrEmpty(ip))
|
||||
ip = Convert.ToString(HttpContext.Current.Request.ServerVariables["REMOTE_ADDR"]);
|
||||
if (IPAddress.TryParse(ip, out var ips))
|
||||
{
|
||||
byte[] ipBytes = ips.GetAddressBytes();
|
||||
if (ipBytes[0] == 10) return ip;
|
||||
if (ipBytes[0] == 172 && ipBytes[1] >= 16 && ipBytes[1] <= 31) return ip;
|
||||
if (ipBytes[0] == 192 && ipBytes[1] == 168) return ip;
|
||||
}
|
||||
return ip;
|
||||
}
|
||||
|
||||
/// <summary>获取客户端的IP,可以取到代理后的IP
|
||||
///
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
public static string GetClientIp()
|
||||
{
|
||||
string result = HttpContext.Current.Request.ServerVariables["HTTP_X_FORWARDED_FOR"];
|
||||
if (!string.IsNullOrEmpty(result))
|
||||
{
|
||||
//可能有代理
|
||||
if (result.IndexOf(".", StringComparison.Ordinal) == -1)
|
||||
result = null;
|
||||
else
|
||||
{
|
||||
if (result.IndexOf(",", StringComparison.Ordinal) != -1)
|
||||
{
|
||||
//有“,”,估计多个代理。取第一个不是内网的IP。
|
||||
result = result.Replace(" ", "").Replace("'", "");
|
||||
string[] temparyip = result.Split(",;".ToCharArray());
|
||||
foreach (string t in temparyip)
|
||||
{
|
||||
if (IsIpAddress(t)
|
||||
&& t.Substring(0, 3) != "10."
|
||||
&& t.Substring(0, 7) != "192.168"
|
||||
&& t.Substring(0, 7) != "172.16.")
|
||||
{
|
||||
return t; //找到不是内网的地址
|
||||
}
|
||||
}
|
||||
}
|
||||
else if (IsIpAddress(result)) //代理即是IP格式
|
||||
return result;
|
||||
else
|
||||
result = null; //代理中的内容 非IP,取IP
|
||||
}
|
||||
}
|
||||
if (string.IsNullOrEmpty(result))
|
||||
result = HttpContext.Current.Request.ServerVariables["REMOTE_ADDR"];
|
||||
if (string.IsNullOrEmpty(result))
|
||||
result = HttpContext.Current.Request.UserHostAddress;
|
||||
return result;
|
||||
}
|
||||
|
||||
/// <summary>判断是否是IP地址格式 0.0.0.0
|
||||
///
|
||||
/// </summary>
|
||||
/// <param name="str">待判断的IP地址</param>
|
||||
/// <returns>true or false</returns>
|
||||
public static bool IsIpAddress(string str)
|
||||
{
|
||||
if (string.IsNullOrEmpty(str) || str.Length < 7 || str.Length > 15) return false;
|
||||
|
||||
const string regformat = @"^d{1,3}[.]d{1,3}[.]d{1,3}[.]d{1,3}$";
|
||||
|
||||
Regex regex = new Regex(regformat, RegexOptions.IgnoreCase);
|
||||
return regex.IsMatch(str);
|
||||
}
|
||||
}
|
||||
}
|
||||
1194
Face.Services/Tool/System/StringHelp.cs
Normal file
1194
Face.Services/Tool/System/StringHelp.cs
Normal file
File diff suppressed because it is too large
Load Diff
Reference in New Issue
Block a user