using Newtonsoft.Json.Linq;
using System;
using System.IO;
using System.Net;
using System.Net.Mail;
using System.Security.Cryptography;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading;
using System.Web;
using System.Xml;
namespace Common
{
public class Utils
{
private static int[] pyvalue = new int[396]
{
-20319,
-20317,
-20304,
-20295,
-20292,
-20283,
-20265,
-20257,
-20242,
-20230,
-20051,
-20036,
-20032,
-20026,
-20002,
-19990,
-19986,
-19982,
-19976,
-19805,
-19784,
-19775,
-19774,
-19763,
-19756,
-19751,
-19746,
-19741,
-19739,
-19728,
-19725,
-19715,
-19540,
-19531,
-19525,
-19515,
-19500,
-19484,
-19479,
-19467,
-19289,
-19288,
-19281,
-19275,
-19270,
-19263,
-19261,
-19249,
-19243,
-19242,
-19238,
-19235,
-19227,
-19224,
-19218,
-19212,
-19038,
-19023,
-19018,
-19006,
-19003,
-18996,
-18977,
-18961,
-18952,
-18783,
-18774,
-18773,
-18763,
-18756,
-18741,
-18735,
-18731,
-18722,
-18710,
-18697,
-18696,
-18526,
-18518,
-18501,
-18490,
-18478,
-18463,
-18448,
-18447,
-18446,
-18239,
-18237,
-18231,
-18220,
-18211,
-18201,
-18184,
-18183,
-18181,
-18012,
-17997,
-17988,
-17970,
-17964,
-17961,
-17950,
-17947,
-17931,
-17928,
-17922,
-17759,
-17752,
-17733,
-17730,
-17721,
-17703,
-17701,
-17697,
-17692,
-17683,
-17676,
-17496,
-17487,
-17482,
-17468,
-17454,
-17433,
-17427,
-17417,
-17202,
-17185,
-16983,
-16970,
-16942,
-16915,
-16733,
-16708,
-16706,
-16689,
-16664,
-16657,
-16647,
-16474,
-16470,
-16465,
-16459,
-16452,
-16448,
-16433,
-16429,
-16427,
-16423,
-16419,
-16412,
-16407,
-16403,
-16401,
-16393,
-16220,
-16216,
-16212,
-16205,
-16202,
-16187,
-16180,
-16171,
-16169,
-16158,
-16155,
-15959,
-15958,
-15944,
-15933,
-15920,
-15915,
-15903,
-15889,
-15878,
-15707,
-15701,
-15681,
-15667,
-15661,
-15659,
-15652,
-15640,
-15631,
-15625,
-15454,
-15448,
-15436,
-15435,
-15419,
-15416,
-15408,
-15394,
-15385,
-15377,
-15375,
-15369,
-15363,
-15362,
-15183,
-15180,
-15165,
-15158,
-15153,
-15150,
-15149,
-15144,
-15143,
-15141,
-15140,
-15139,
-15128,
-15121,
-15119,
-15117,
-15110,
-15109,
-14941,
-14937,
-14933,
-14930,
-14929,
-14928,
-14926,
-14922,
-14921,
-14914,
-14908,
-14902,
-14894,
-14889,
-14882,
-14873,
-14871,
-14857,
-14678,
-14674,
-14670,
-14668,
-14663,
-14654,
-14645,
-14630,
-14594,
-14429,
-14407,
-14399,
-14384,
-14379,
-14368,
-14355,
-14353,
-14345,
-14170,
-14159,
-14151,
-14149,
-14145,
-14140,
-14137,
-14135,
-14125,
-14123,
-14122,
-14112,
-14109,
-14099,
-14097,
-14094,
-14092,
-14090,
-14087,
-14083,
-13917,
-13914,
-13910,
-13907,
-13906,
-13905,
-13896,
-13894,
-13878,
-13870,
-13859,
-13847,
-13831,
-13658,
-13611,
-13601,
-13406,
-13404,
-13400,
-13398,
-13395,
-13391,
-13387,
-13383,
-13367,
-13359,
-13356,
-13343,
-13340,
-13329,
-13326,
-13318,
-13147,
-13138,
-13120,
-13107,
-13096,
-13095,
-13091,
-13076,
-13068,
-13063,
-13060,
-12888,
-12875,
-12871,
-12860,
-12858,
-12852,
-12849,
-12838,
-12831,
-12829,
-12812,
-12802,
-12607,
-12597,
-12594,
-12585,
-12556,
-12359,
-12346,
-12320,
-12300,
-12120,
-12099,
-12089,
-12074,
-12067,
-12058,
-12039,
-11867,
-11861,
-11847,
-11831,
-11798,
-11781,
-11604,
-11589,
-11536,
-11358,
-11340,
-11339,
-11324,
-11303,
-11097,
-11077,
-11067,
-11055,
-11052,
-11045,
-11041,
-11038,
-11024,
-11020,
-11019,
-11018,
-11014,
-10838,
-10832,
-10815,
-10800,
-10790,
-10780,
-10764,
-10587,
-10544,
-10533,
-10519,
-10331,
-10329,
-10328,
-10322,
-10315,
-10309,
-10307,
-10296,
-10281,
-10274,
-10270,
-10262,
-10260,
-10256,
-10254
};
private static string[] pystr = new string[396]
{
"a",
"ai",
"an",
"ang",
"ao",
"ba",
"bai",
"ban",
"bang",
"bao",
"bei",
"ben",
"beng",
"bi",
"bian",
"biao",
"bie",
"bin",
"bing",
"bo",
"bu",
"ca",
"cai",
"can",
"cang",
"cao",
"ce",
"ceng",
"cha",
"chai",
"chan",
"chang",
"chao",
"che",
"chen",
"cheng",
"chi",
"chong",
"chou",
"chu",
"chuai",
"chuan",
"chuang",
"chui",
"chun",
"chuo",
"ci",
"cong",
"cou",
"cu",
"cuan",
"cui",
"cun",
"cuo",
"da",
"dai",
"dan",
"dang",
"dao",
"de",
"deng",
"di",
"dian",
"diao",
"die",
"ding",
"diu",
"dong",
"dou",
"du",
"duan",
"dui",
"dun",
"duo",
"e",
"en",
"er",
"fa",
"fan",
"fang",
"fei",
"fen",
"feng",
"fo",
"fou",
"fu",
"ga",
"gai",
"gan",
"gang",
"gao",
"ge",
"gei",
"gen",
"geng",
"gong",
"gou",
"gu",
"gua",
"guai",
"guan",
"guang",
"gui",
"gun",
"guo",
"ha",
"hai",
"han",
"hang",
"hao",
"he",
"hei",
"hen",
"heng",
"hong",
"hou",
"hu",
"hua",
"huai",
"huan",
"huang",
"hui",
"hun",
"huo",
"ji",
"jia",
"jian",
"jiang",
"jiao",
"jie",
"jin",
"jing",
"jiong",
"jiu",
"ju",
"juan",
"jue",
"jun",
"ka",
"kai",
"kan",
"kang",
"kao",
"ke",
"ken",
"keng",
"kong",
"kou",
"ku",
"kua",
"kuai",
"kuan",
"kuang",
"kui",
"kun",
"kuo",
"la",
"lai",
"lan",
"lang",
"lao",
"le",
"lei",
"leng",
"li",
"lia",
"lian",
"liang",
"liao",
"lie",
"lin",
"ling",
"liu",
"long",
"lou",
"lu",
"lv",
"luan",
"lue",
"lun",
"luo",
"ma",
"mai",
"man",
"mang",
"mao",
"me",
"mei",
"men",
"meng",
"mi",
"mian",
"miao",
"mie",
"min",
"ming",
"miu",
"mo",
"mou",
"mu",
"na",
"nai",
"nan",
"nang",
"nao",
"ne",
"nei",
"nen",
"neng",
"ni",
"nian",
"niang",
"niao",
"nie",
"nin",
"ning",
"niu",
"nong",
"nu",
"nv",
"nuan",
"nue",
"nuo",
"o",
"ou",
"pa",
"pai",
"pan",
"pang",
"pao",
"pei",
"pen",
"peng",
"pi",
"pian",
"piao",
"pie",
"pin",
"ping",
"po",
"pu",
"qi",
"qia",
"qian",
"qiang",
"qiao",
"qie",
"qin",
"qing",
"qiong",
"qiu",
"qu",
"quan",
"que",
"qun",
"ran",
"rang",
"rao",
"re",
"ren",
"reng",
"ri",
"rong",
"rou",
"ru",
"ruan",
"rui",
"run",
"ruo",
"sa",
"sai",
"san",
"sang",
"sao",
"se",
"sen",
"seng",
"sha",
"shai",
"shan",
"shang",
"shao",
"she",
"shen",
"sheng",
"shi",
"shou",
"shu",
"shua",
"shuai",
"shuan",
"shuang",
"shui",
"shun",
"shuo",
"si",
"song",
"sou",
"su",
"suan",
"sui",
"sun",
"suo",
"ta",
"tai",
"tan",
"tang",
"tao",
"te",
"teng",
"ti",
"tian",
"tiao",
"tie",
"ting",
"tong",
"tou",
"tu",
"tuan",
"tui",
"tun",
"tuo",
"wa",
"wai",
"wan",
"wang",
"wei",
"wen",
"weng",
"wo",
"wu",
"xi",
"xia",
"xian",
"xiang",
"xiao",
"xie",
"xin",
"xing",
"xiong",
"xiu",
"xu",
"xuan",
"xue",
"xun",
"ya",
"yan",
"yang",
"yao",
"ye",
"yi",
"yin",
"ying",
"yo",
"yong",
"you",
"yu",
"yuan",
"yue",
"yun",
"za",
"zai",
"zan",
"zang",
"zao",
"ze",
"zei",
"zen",
"zeng",
"zha",
"zhai",
"zhan",
"zhang",
"zhao",
"zhe",
"zhen",
"zheng",
"zhi",
"zhong",
"zhou",
"zhu",
"zhua",
"zhuai",
"zhuan",
"zhuang",
"zhui",
"zhun",
"zhuo",
"zi",
"zong",
"zou",
"zu",
"zuan",
"zui",
"zun",
"zuo"
};
public static string GetIp()
{
string str = string.Empty;
return HttpContext.Current.Request.ServerVariables["HTTP_VIA"] == null ? (HttpContext.Current.Request.ServerVariables["REMOTE_ADDR"] == null ? "0.0.0.0" : HttpContext.Current.Request.ServerVariables["REMOTE_ADDR"].ToString()) : (HttpContext.Current.Request.ServerVariables["HTTP_X_FORWARDED_FOR"] != null ? HttpContext.Current.Request.ServerVariables["HTTP_X_FORWARDED_FOR"].ToString() : (HttpContext.Current.Request.ServerVariables["HTTP_CLIENT_IP"] == null ? (HttpContext.Current.Request.ServerVariables["REMOTE_ADDR"] == null ? "0.0.0.0" : HttpContext.Current.Request.ServerVariables["REMOTE_ADDR"].ToString()) : HttpContext.Current.Request.ServerVariables["HTTP_CLIENT_IP"].ToString()));
}
///
/// 获取客户端IP地址
///
/// 若失败则返回回送地址
public static string GetCurrentUserIp()
{
//如果客户端使用了代理服务器,则利用HTTP_X_FORWARDED_FOR找到客户端IP地址
string userHostAddress = string.IsNullOrEmpty(HttpContext.Current.Request.ServerVariables["HTTP_X_FORWARDED_FOR"]) ? null : HttpContext.Current.Request.ServerVariables["HTTP_X_FORWARDED_FOR"].ToString().Split(',')[0].Trim();
//否则直接读取REMOTE_ADDR获取客户端IP地址
if (string.IsNullOrEmpty(userHostAddress))
{
userHostAddress = HttpContext.Current.Request.ServerVariables["REMOTE_ADDR"];
}
//前两者均失败,则利用Request.UserHostAddress属性获取IP地址,但此时无法确定该IP是客户端IP还是代理IP
if (string.IsNullOrEmpty(userHostAddress))
{
userHostAddress = HttpContext.Current.Request.UserHostAddress;
}
//最后判断获取是否成功,并检查IP地址的格式(检查其格式非常重要)
if (!string.IsNullOrEmpty(userHostAddress) && IsIP(userHostAddress))
{
return userHostAddress;
}
return "127.0.0.1";
}
//获取本地IP地址,如:192.168.1.102
public static string GetLocalIPAddress()
{
var host = Dns.GetHostEntry(Dns.GetHostName());
foreach (var ip in host.AddressList)
{
if (ip.AddressFamily == System.Net.Sockets.AddressFamily.InterNetwork)
{
return ip.ToString();
}
}
throw new Exception("No network adapters with an IPv4 address in the system!");
}
///
/// 根据IP 获取物理地址
///
/// Ip地址
/// 返回地址类别:0-全地址,1-国家编码, 2-国家,3-地区编码,4-地区名称,5-城市名称
///
public static string GetIpAddress(string ip, string addressClasses)
{
string url = "http://ip-api.com/json/" + ip + "?lang=zh-CN";
string result = "";
WebRequest wrt = null;
WebResponse wrp = null;
try
{
wrt = WebRequest.Create(url);
wrt.Credentials = CredentialCache.DefaultCredentials;
wrp = wrt.GetResponse();
StreamReader sr = new StreamReader(wrp.GetResponseStream(), Encoding.UTF8);
//获取到的是Json数据
string html = sr.ReadToEnd();
if (string.IsNullOrEmpty(html))
return null;
//Newtonsoft.Json读取数据
JObject obj = Newtonsoft.Json.JsonConvert.DeserializeObject(html);
string countryCode = obj["countryCode"] == null ? "" : obj["countryCode"].ToString(); //国家编码
string country = obj["country"] == null ? "" : obj["country"].ToString(); //国家
string region = obj["region"] == null ? "" : obj["region"].ToString(); //地区编码
string regionName = obj["regionName"] == null ? "" : obj["regionName"].ToString(); //地区名称
string city = obj["city"] == null ? "" : obj["city"].ToString(); //城市名称
//string province = obj["regionName"].ToString();
//地址全称
if (addressClasses.Equals("0"))
return country + (city.Equals(regionName) ? city : (regionName + city));
//国家编码
if (addressClasses.Equals("1"))
return countryCode;
//国家名称
if (addressClasses.Equals("2"))
return country;
//地区编码
if (addressClasses.Equals("3"))
return region;
//地区名称
if (addressClasses.Equals("4"))
return regionName;
//城市名称
if (addressClasses.Equals("5"))
//return city.Equals(regionName) ? city : (regionName + city);
return city;
else
return country + (city.Equals(regionName) ? city : (regionName + city));
}
catch (Exception)
{
}
finally
{
if (wrp != null)
wrp.Close();
if (wrt != null)
wrt.Abort();
}
return result;
}
///
/// 检查IP地址格式
///
///
///
public static bool IsIP(string ip)
{
return System.Text.RegularExpressions.Regex.IsMatch(ip, @"^((2[0-4]\d|25[0-5]|[01]?\d\d?)\.){3}(2[0-4]\d|25[0-5]|[01]?\d\d?)$");
}
public static string GetTrueWebSitePath()
{
string path = HttpContext.Current.Request.Path;
return path.LastIndexOf("/") == path.IndexOf("/") ? "/" : path.Substring(path.IndexOf("/"), path.LastIndexOf("/") + 1);
}
public static string GetSaveFilePath(int rondom)
{
DateTime now = DateTime.Now;
string str1 = now.ToString("yyyyMMdd");
string path = PubConstant.Path + "clientsite\\sendfile\\" + str1;
if (!Directory.Exists(path))
Directory.CreateDirectory(path);
string str2 = "";
int num1 = 0;
while (num1 <= 50)
{
++num1;
Thread.Sleep(1);
int num2 = new Random(DateTime.Now.Millisecond).Next(100000, 999999);
str2 = str1 + (object) "\\" + now.ToString("HHmmssffff") + (object) num2 + "_" + (object) rondom + "_" + (object) num1 + ".txt";
if (!System.IO.File.Exists(PubConstant.Path + "clientsite\\sendfile\\" + str2))
break;
}
if (str2 == "")
str2 = str1 + (object) "\\" + now.ToString("HHmmssffff") + "temp" + (object) rondom + ".txt";
return str2;
}
public static bool ProcessSqlStr(string Str)
{
bool flag = true;
try
{
if (Str.Trim() != "")
{
string str1 = "and .exec .insert .select .delete .update .count .* .chr .mid .master .truncate .char .declare";
char[] chArray = new char[1]
{
'.'
};
foreach (string str2 in str1.Split(chArray))
{
if (Str.ToLower().IndexOf(str2) >= 0)
{
flag = false;
break;
}
}
}
}
catch
{
flag = false;
}
return flag;
}
public static string GetMapPath(string strPath)
{
if (HttpContext.Current != null)
return HttpContext.Current.Server.MapPath(strPath);
return Path.Combine(AppDomain.CurrentDomain.BaseDirectory, strPath);
}
public static long DateDiff(Utils.DateInterval Interval, DateTime StartDate, DateTime EndDate)
{
long num = 0L;
TimeSpan timeSpan = new TimeSpan(EndDate.Ticks - StartDate.Ticks);
switch (Interval)
{
case Utils.DateInterval.Second:
num = (long) timeSpan.TotalSeconds;
break;
case Utils.DateInterval.Minute:
num = (long) timeSpan.TotalMinutes;
break;
case Utils.DateInterval.Hour:
num = (long) timeSpan.TotalHours;
break;
case Utils.DateInterval.Day:
num = (long) timeSpan.Days;
break;
case Utils.DateInterval.Week:
num = (long) (timeSpan.Days / 7);
break;
case Utils.DateInterval.Month:
num = (long) (timeSpan.Days / 30);
break;
case Utils.DateInterval.Quarter:
num = (long) (timeSpan.Days / 30 / 3);
break;
case Utils.DateInterval.Year:
num = (long) (timeSpan.Days / 365);
break;
}
return num;
}
public static string SubStr(string tosub, int length)
{
byte[] bytes = Encoding.Unicode.GetBytes(tosub);
int num = 0;
int count = 0;
string str = "";
if (length == 0)
length = bytes.GetLength(0);
else if (length < bytes.GetLength(0))
str = " ... ";
for (; count < bytes.GetLength(0) && num < length; ++count)
{
if (count % 2 == 0)
++num;
else if ((int) bytes[count] > 0)
++num;
}
if (count % 2 == 1)
{
if ((int) bytes[count] > 0)
--count;
else
++count;
}
return Encoding.Unicode.GetString(bytes, 0, count) + str;
}
public string GetPYString(string str)
{
if (this.IsEnglishCharacter(str))
return str;
string str1 = "";
foreach (char ch in str)
str1 = (int) ch < 33 || (int) ch > 126 ? str1 + this.GetPYChar(ch.ToString()) : str1 + ch.ToString();
return str1;
}
public bool IsEnglishCharacter(string str)
{
for (int index = 0; index < str.Length; ++index)
{
char ch = str.ToCharArray()[index];
if (((int) ch > 122 || (int) ch < 97) && ((int) ch > 90 || (int) ch < 65))
return false;
}
return true;
}
protected bool HaveChineseCode(string testString)
{
return Regex.IsMatch(testString, "[\\u4e00-\\u9fa5]+");
}
public string GetPYChar(string c)
{
byte[] numArray = new byte[2];
byte[] bytes = Encoding.Default.GetBytes(c);
int num = (int) (short) bytes[0] * 256 + (int) (short) bytes[1];
if (num < 45217)
return "*";
if (num < 45253)
return "a";
if (num < 45761)
return "b";
if (num < 46318)
return "c";
if (num < 46826)
return "d";
if (num < 47010)
return "e";
if (num < 47297)
return "f";
if (num < 47614)
return "g";
if (num < 48119)
return "h";
if (num < 49062)
return "g";
if (num < 49324)
return "k";
if (num < 49896)
return "l";
if (num < 50371)
return "m";
if (num < 50614)
return "n";
if (num < 50622)
return "o";
if (num < 50906)
return "p";
if (num < 51387)
return "q";
if (num < 51446)
return "r";
if (num < 52218)
return "s";
if (num < 52698)
return "t";
if (num < 52980)
return "w";
if (num < 53689)
return "x";
if (num < 54481)
return "y";
return num < 55290 ? "z" : "*";
}
public static string convert(string chrstr)
{
byte[] numArray = new byte[2];
string str = "";
char[] chArray = chrstr.ToCharArray();
for (int index1 = 0; index1 < chArray.Length; ++index1)
{
byte[] bytes = Encoding.Default.GetBytes(chArray[index1].ToString());
int num = (int) bytes[0] * 256 + (int) bytes[1] - 65536;
if (num > 0 && num < 160)
{
str += chArray[index1];
}
else
{
for (int index2 = Utils.pyvalue.Length - 1; index2 >= 0; --index2)
{
if (Utils.pyvalue[index2] <= num)
{
str += Utils.pystr[index2];
break;
}
}
}
}
return str;
}
public static string getPingYin(string word)
{
int length = Encoding.Default.GetBytes(word).Length;
string str = "";
if (length == word.Length)
return "";
for (int index = 0; index < word.Length; ++index)
str = (int) word[index] <= 128 ? str + word[index].ToString() : str + Utils.convert(word[index].ToString());
return str;
}
public static int getPayInfo(string payInfo)
{
if (string.IsNullOrEmpty(payInfo))
payInfo = "";
if (payInfo.Equals("面议"))
return 1;
if (payInfo.Equals("押一付三"))
return 2;
if (payInfo.Equals("押一付二"))
return 3;
if (payInfo.Equals("押一付一"))
return 4;
if (payInfo.Equals("押二付一"))
return 5;
if (payInfo.Equals("年付"))
return 6;
return payInfo.Equals("半年付") ? 7 : 1;
}
public static int getRoom(string room)
{
if (string.IsNullOrEmpty(room))
room = "";
if (room.Equals("一"))
return 1;
if (room.Equals("二"))
return 2;
if (room.Equals("三"))
return 3;
if (room.Equals("四"))
return 4;
if (room.Equals("五"))
return 5;
if (room.Equals("六"))
return 6;
return room.Equals("七") ? 7 : 0;
}
public static string getDecoration(string decoration)
{
if (string.IsNullOrEmpty(decoration))
decoration = "简单装修";
return decoration.Split(",".ToCharArray())[0];
}
public static string MD5Encrypt(string strText)
{
return Encoding.Default.GetString(new MD5CryptoServiceProvider().ComputeHash(Encoding.Default.GetBytes(strText)));
}
public static bool sendmail(string to, string title, string content)
{
string str = "smtp.163.com";
string userName = "w158357686@163.com";
string password = "wang123456";
string from = "w158357686@163.com";
SmtpClient smtpClient = new SmtpClient();
smtpClient.DeliveryMethod = SmtpDeliveryMethod.Network;
smtpClient.Host = str;
smtpClient.Credentials = (ICredentialsByHost) new NetworkCredential(userName, password);
MailMessage message = new MailMessage(from, to);
message.Subject = title;
message.Body = content;
message.BodyEncoding = Encoding.UTF8;
message.IsBodyHtml = true;
message.Priority = MailPriority.High;
try
{
smtpClient.Send(message);
return true;
}
catch
{
return false;
}
}
public static string GetRegValue(string RegexString, string Str)
{
string str = "";
Match match = new Regex(RegexString).Match(Str);
if (match.Success)
str = match.Value;
return str;
}
public enum DateInterval
{
Second,
Minute,
Hour,
Day,
Week,
Month,
Quarter,
Year,
}
}
}