| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629 |
- using BizCom;
- using Newtonsoft.Json;
- using Newtonsoft.Json.Linq;
- using System;
- using System.Collections.Generic;
- using System.Net;
- using System.Net.Sockets;
- using System.Reflection;
- using System.Runtime.InteropServices;
- using System.Text;
- using System.Web;
- using Utils;
- namespace SiteCore
- {
- public class WebHelper
- {
- static int exp_page_size = 9999;
- public static List<Dictionary<string, object>> getComReportData(string url, string parms)
- {
- HttpCookie lcCookie = HttpContext.Current.Request.Cookies[webConfig.CookieName];
- string name = HttpUtility.UrlDecode(lcCookie.Values["User"]);
- string ticket = lcCookie.Values["Ticket"];
- if (parms == "") parms = "pageSize=" + exp_page_size;
- else parms += "&pageSize=" + exp_page_size;
- parms += "&_expt=1";
- //parms = HttpUtility.UrlEncode(parms);
- HttpHelper http = new HttpHelper();
- HttpItem item = new HttpItem()
- {
- URL = url,
- Method = "POST",
- ContentType = "application/x-www-form-urlencoded; charset=UTF-8",
- Cookie = webConfig.CookieName + "=user=" + name + "&ticket=" + ticket,
- Postdata = parms
- };
- HttpResult hresult = http.GetHtml(item);
- string html = hresult.Html;
- JObject jobj = (JObject)Newtonsoft.Json.JsonConvert.DeserializeObject(html);
- if (jobj["total"] != null)
- {
- List<Dictionary<string, object>> lists_sec;
- List<Dictionary<string, object>> lists = JsonConvert.DeserializeObject<List<Dictionary<string, object>>>(jobj["data"].ToString());
- int pg = (Convert.ToInt32(jobj["total"]) - 1) / exp_page_size + 1; // exp_page_size;
- if (pg > 1)
- {
- for (int i = 2; i <= pg; i++)
- {
- if (i == 2) parms = parms.Replace("pageIndex=0", "pageIndex=" + i);
- parms = parms.Replace("pageIndex=" + (i - 1), "pageIndex=" + i);
- item = new HttpItem()
- {
- URL = url,
- Method = "POST",
- ContentType = "application/x-www-form-urlencoded; charset=UTF-8",
- Cookie = webConfig.CookieName + "=user=" + name + "&ticket=" + ticket,
- Postdata = parms
- };
- hresult = http.GetHtml(item);
- html = hresult.Html;
- jobj = (JObject)Newtonsoft.Json.JsonConvert.DeserializeObject(html);
- if (jobj["total"] != null)
- {
- lists_sec = JsonConvert.DeserializeObject<List<Dictionary<string, object>>>(jobj["data"].ToString());
- lists.AddRange(lists_sec);
- }
- }
- }
- return lists;
- //Hashtable htable= JsonConvert.DeserializeObject<Hashtable>(jobj["data"].ToString());
- }
- return null;
- }
- public static object GetReflectionObject(string className)
- {
- if (string.IsNullOrEmpty(className)) return null;
- string tmpName = "BizCom." + className;
- return Assembly.Load("BizCom").CreateInstance(tmpName);
- }
- private static void sendSign(string str, ref StringBuilder sql)
- {
- if (str != "" && str != "|")
- {
- List<string> lst = new List<string>();
- string[] sArr = str.Split('|');
- string[] tArr;
- for (int i = 0; i < sArr.Length; i++)
- {
- tArr = sArr[i].Split(',');
- if (tArr.Length < 7 || tArr[6] == "") continue;
- //WxPush.sendSignMsg(tArr, lst);
- }
- if (lst.Count > 0)
- {
- sql.AppendFormat("delete from S_WxFormId where formids in ({0}) ;", string.Join(",", lst.ToArray()));
- }
- }
- }
- private static void sendExpire(string str, ref StringBuilder sql)
- {
- if (str != "" && str != "|")
- {
- List<string> lst = new List<string>();
- string[] sArr = str.Split('|');
- string[] tArr;
- for (int i = 0; i < sArr.Length; i++)
- {
- tArr = sArr[i].Split(',');
- if (tArr.Length < 7 || tArr[6] == "") continue;
- //WxPush.sendExpireMsg(tArr, lst);
- }
- if (lst.Count > 0)
- {
- sql.AppendFormat("delete from S_WxFormId where formids in ({0}) ;", string.Join(",", lst.ToArray()));
- }
- }
- }
- /// <summary>
- /// 重置密码发送邮件
- /// </summary>
- /// <param name="to"></param>
- /// <param name="userName"></param>
- /// <param name="pwd"></param>
- /// <param name="errMsg"></param>
- /// <returns></returns>
- public static bool SendResetPwdMail(string to, string userName, string rkey, out string errMsg)
- {
- errMsg = "";
- StringBuilder str = new StringBuilder();
- str.Append("<div style=\"font-size:14px\">");
- str.AppendFormat("<div><span style='color:blue'>{0}</span>您好,</div> ", userName);
- str.Append("<div style=\"text-indent: 2em\"> </div> ");
- str.Append("<div style=\"text-indent: 2em\"><b>请点击下面链接进行密码重置:</b></div> ");
- str.Append("<div style=\"text-indent: 2em\"> </div> ");
- //str.AppendFormat("<div style=\"text-indent: 2em\">您在城市便签的密码已经重置为:<strong style=\"color:#ff0000\">{0}</strong>(注意大小写)</div> ", pwd);
- //str.Append("<div style=\"text-indent: 2em\"> </div> ");
- string url = webConfig.SiteUrl + "resetpwd.aspx?acc=" + to + "&key=" + rkey;
- str.AppendFormat("<div style=\"text-indent: 2em;font-size:14px;color: #064977;\"><a href=\"{0}\">{0}</a></div>", url);
- str.Append("<div align=\"left\"> </div> ");
- str.Append("<div style=\"text-indent: 2em\"><b>2小时内有效</b></div> ");
- str.AppendFormat("<div style=\"text-indent: 2em\"><span><a href=\"{0}\">{1}</a></span></div>", webConfig.SiteUrl, webConfig.SiteName);
- str.Append("</div>");
- MailService service = new MailService();
- try
- {
- service.enableSsl(false);
- return service.send(to, webConfig.SiteName + "重置密码邮件", str.ToString());
- }
- catch (Exception ex)
- {
- errMsg = ex.Message;
- return false;
- }
- }
- //public static string GetUserPic(string path,object pic)
- //{
- // GetUserPic()
- // if (pic.ToString() == "")
- // {
- // return "../img/dp3.png";
- // }
- // else
- // {
- // return path + pic;
- // }
- //}
- /// <summary>
- /// 将c# DateTime时间格式转换为Unix时间戳格式
- /// </summary>
- /// <param name="time">时间</param>
- /// <returns>long</returns>
- public static long ConvertDateTimeToInt(System.DateTime time)
- {
- System.DateTime startTime = TimeZone.CurrentTimeZone.ToLocalTime(new System.DateTime(1970, 1, 1, 0, 0, 0, 0));
- long t = (time.Ticks - startTime.Ticks) / 10000000; //除10000调整为13位
- return t;
- }
- public static string GetUserPic(string path, object pic, string def)
- {
- if (pic.ToString() == "")
- {
- return def;// "../img/dp3.png";
- }
- else
- {
- return path + pic;
- }
- }
- public static string DateStringFromNow(object dt)
- {
- DateTime nDt = Convert.ToDateTime(dt);
- TimeSpan span = DateTime.Now - nDt;
- if (span.TotalDays > 30)
- {
- return nDt.ToString("yyyy.MM.dd");
- }
- else if (span.TotalDays > 14)
- {
- return "2周前";
- }
- else if (span.TotalDays > 7)
- {
- return "1周前";
- }
- else if (span.TotalDays > 1)
- {
- return string.Format("{0}天前", (int)Math.Floor(span.TotalDays));
- }
- else if (span.TotalHours > 1)
- {
- return string.Format("{0}小时前", (int)Math.Floor(span.TotalHours));
- }
- else if (span.TotalMinutes > 1)
- {
- return string.Format("{0}分钟前", (int)Math.Floor(span.TotalMinutes));
- }
- else if (span.TotalSeconds >= 1)
- {
- return string.Format("{0}秒前", (int)Math.Floor(span.TotalSeconds));
- }
- else
- {
- return "1秒前";
- }
- }
- public static string getLoginTicket(string acc, string ip)
- {
- return SecurityHelper.EncryptMD5(acc + "-" + DateTime.Now.ToString("yyMMdd") + "|" + ip);
- }
- public static string getLoginTicket(string acc)
- {
- //FormsAuthenticationTicket ticket = new FormsAuthenticationTicket(1, acc, DateTime.Now, DateTime.Now.AddDays(2), false, Environment.MachineName);
- //FormsAuthentication.Encrypt
- return SecurityHelper.EncryptMD5(acc + "-" + getLoginMac());
- }
- [DllImport("Iphlpapi.dll")]
- private static extern int SendARP(Int32 dest, Int32 host, ref Int64 mac, ref Int32 length);
- [DllImport("Ws2_32.dll")]
- private static extern Int32 inet_addr(string ip);
- /// <summary>
- /// 获取客户端IP地址
- /// </summary>
- /// <returns>若失败则返回回送地址</returns>
- public static string GetIP()
- {
- string userHostAddress = "";
- //如果客户端使用了代理服务器,则利用HTTP_X_FORWARDED_FOR找到客户端IP地址
- if (HttpContext.Current.Request.ServerVariables["HTTP_X_FORWARDED_FOR"] != null)
- {
- userHostAddress = 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;
- }
- string hostName = Dns.GetHostName();
- IPHostEntry ipEntry = Dns.GetHostEntry(hostName);
- for (int i = 0; i < ipEntry.AddressList.Length; i++)
- {
- if (ipEntry.AddressList[i].AddressFamily == AddressFamily.InterNetwork)
- {
- userHostAddress = ipEntry.AddressList[i].ToString();
- }
- }
- return userHostAddress;// "127.0.0.1";
- }
- /// <summary>
- /// 检查IP地址格式
- /// </summary>
- /// <param name="ip"></param>
- /// <returns></returns>
- 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 getLoginMac()
- {
- // 在此处放置用户代码以初始化页面
- try
- {
- string userip = GetIP();
- string strClientIP = HttpContext.Current.Request.UserHostAddress.ToString().Trim();
- Int32 ldest = inet_addr(strClientIP); //目的地的ip
- Int32 lhost = inet_addr(""); //本地服务器的ip
- Int64 macinfo = new Int64();
- Int32 len = 6;
- int res = SendARP(ldest, 0, ref macinfo, ref len);
- string mac_src = macinfo.ToString("X");
- if (mac_src == "0")
- {
- //if (userip == "127.0.0.1")
- // Response.Write("正在访问Localhost!");
- //else
- // Response.Write("欢迎来自IP为" + userip + "的朋友!" + "<br>");
- return "";
- }
- while (mac_src.Length < 12)
- {
- mac_src = mac_src.Insert(0, "0");
- }
- string mac_dest = "";
- for (int i = 0; i < 11; i++)
- {
- if (0 == (i % 2))
- {
- if (i == 10)
- {
- mac_dest = mac_dest.Insert(0, mac_src.Substring(i, 2));
- }
- else
- {
- mac_dest = "-" + mac_dest.Insert(0, mac_src.Substring(i, 2));
- }
- }
- }
- return userip + mac_dest;
- }
- catch (Exception err)
- {
- XLog.SaveLog(0, err.Message);
- return "";
- }
- }
- //public static int ConvertDateTimeToInt(System.DateTime time)
- //{
- // System.DateTime startTime = TimeZone.CurrentTimeZone.ToLocalTime(new System.DateTime(1970, 1, 1));
- // return (int)(time - startTime).TotalSeconds;
- //}
- public static DateTime ConvertStringToDateTime(string timeStamp)
- {
- DateTime dtStart = TimeZone.CurrentTimeZone.ToLocalTime(new DateTime(1970, 1, 1));
- long lTime = long.Parse(timeStamp + "0000000");
- TimeSpan toNow = new TimeSpan(lTime);
- return dtStart.Add(toNow);
- }
- private static string getRandNum(int s, int e)
- {
- Random rd = new Random((int)DateTime.Now.Ticks);
- return rd.Next(s, e).ToString();
- }
- public static string EnscryptNumber(string code)
- {
- if (code == "") return "";
- if (code.Length > 6) code = code.Substring(4);
- string ct = ConvertDateTimeToInt(DateTime.Now).ToString();
- code = ct.Substring(5) + getRandNum(1, 9) + code + ct.Substring(0, 5);
- int r = getSeed();
- return EnscryptNumber(r, code);
- }
- public static int getSeed()
- {
- return Convert.ToInt32(getRandNum(2, 9));
- }
- public static string EnscryptNumber(int r, string code)
- {
- List<string> lst = new List<string>();
- for (int i = 0; i < code.Length; i++)
- {
- int c = Convert.ToInt32(code[i].ToString()) + r;
- if (c >= 10) c = c - 10;
- lst.Add(c.ToString());
- }
- string v = string.Join("", lst.ToArray());
- v = v.Insert(0, r.ToString());
- return v;
- }
- public static string DescryptNumber(string code)
- {
- int len = code.Length;
- int r = Convert.ToInt32(code.Substring(0, 1));
- code = code.Remove(0, 1);
- List<string> lst = new List<string>();
- for (int i = 0; i < code.Length; i++)
- {
- int c = Convert.ToInt32(code[i].ToString()) - r;
- if (c < 0) c = c + 10;
- lst.Add(c.ToString());
- }
- return string.Join("", lst.ToArray());
- }
- public static string getUserCode(string code)
- {
- if (code.Length < 8) return "";
- return code.Substring(6, 6);
- }
- //判断时间
- public static bool isLegalTime(string code)
- {
- if (code.Length < 8) return false;
- string timeStamp = code.Substring(code.Length - 5) + code.Substring(0, 5);
- DateTime dTime = ConvertStringToDateTime(timeStamp);
- if (DateTime.Now.Subtract(dTime).TotalMinutes <= 5)
- {
- return true;
- }
- return false;
- }
- public static bool isWeiXinAgent(string userAgent)
- {
- if (userAgent.ToLower().Contains("micromessenger"))
- {
- return true;
- }
- return false;
- }
- public static string getEncodeCode(int id, int state)
- {
- string uid = SecurityHelper.EncryptSymmetric(id.ToString());
- string con = DecryptHelper.Encrypt(getRandNum(1, 5000) + "|" + uid + "|" + state + "|" + ConvertDateTimeToInt(DateTime.Now) + "|" + getRandNum(1000, 3000));
- return con;
- }
- public static string getEncodeCodeByEUser(object uid, object state)
- {
- string con = DecryptHelper.Encrypt(getRandNum(1, 5000) + "|" + uid + "|" + state + "|" + ConvertDateTimeToInt(DateTime.Now) + "|" + getRandNum(1000, 3000));
- return HttpUtility.UrlEncode(con);
- }
- public static string getCodeByEUser(object uid, object state)
- {
- string con = DecryptHelper.Encrypt(getRandNum(1, 5000) + "|" + uid + "|" + state + "|" + ConvertDateTimeToInt(DateTime.Now) + "|" + getRandNum(1000, 3000));
- return con;
- }
- public static string getEncodeCodeByNormal(string code)
- {
- string[] sArr = code.ToString().Split('|');
- return getEncodeCode(Convert.ToInt32(sArr[0]), Convert.ToInt32(sArr[1]));
- }
- public static int getUid(string tmp)
- {
- string uid = SecurityHelper.DecryptSymmetric(tmp);
- return Convert.ToInt32(uid);
- }
- //public static string getEncodeCodeUnUser(string code)
- //{
- // string[] sArr = code.ToString().Split('|');
- // return getEncodeCodeUnUser(sArr[0], sArr[1]);
- //}
- public static int getDecodeUid(string tmp)
- {
- try
- {
- string t = HttpUtility.UrlDecode(tmp);
- t = DecryptHelper.Decrypt(t);
- string[] tArr = t.Split('|');
- tArr[0] = SecurityHelper.DecryptSymmetric(tArr[0]);
- return Convert.ToInt32(tArr[0]);
- }
- catch
- {
- }
- return 0;
- }
- public static string[] getDecodeArr(string tmp)
- {
- try
- {
- string t = DecryptHelper.Decrypt(tmp);
- string[] tArr = t.Split('|');
- return tArr;
- }
- catch
- {
- }
- return null;
- }
- static string cname = "wx_quduba";
- public static void addCookie(string key, string value)
- {
- HttpCookie cookie = new HttpCookie(cname);
- cookie.Values.Clear();
- cookie.Values.Add(key, value);
- cookie.Path = "/";
- cookie.Domain = webConfig.SiteDomain;
- cookie.Expires = DateTime.Now.AddDays(2);
- HttpContext.Current.Response.Cookies.Add(cookie);
- }
- public static string getCookie(string key)
- {
- HttpCookie cookie = HttpContext.Current.Request.Cookies[cname];
- if (cookie != null)
- return cookie.Values[key];
- return "";
- }
- public static void delCookie(string key)
- {
- if (key == "") return;
- HttpCookie cookie = new HttpCookie(key);
- cookie.Expires = DateTime.Now.AddDays(-1d);
- HttpContext.Current.Response.Cookies.Add(cookie);
- }
- public static List<DateTime> GetStartEndDateTime(string dateType = "")
- {
- List<DateTime> RetDT = new List<DateTime>();
- switch (dateType)
- {
- case "Today":
- case "0":
- RetDT = GetTodayStartEndTime();
- break;
- case "1":
- case "Week":
- RetDT = GetWeekStartEndTime();
- break;
- case "2":
- case "Month":
- RetDT = GetMonthStartEndTime();
- break;
- default:
- RetDT = GetTodayStartEndTime();
- break;
- }
- return RetDT;
- }
- public static List<DateTime> GetTodayStartEndTime()
- {
- List<DateTime> RetDT = new List<DateTime>();
- RetDT.Add(DateTime.Now.Date);
- RetDT.Add(DateTime.Parse(DateTime.Now.ToString("yyyy-MM-dd") + " 23:59:59"));
- return RetDT;
- }
- public static List<DateTime> GetWeekStartEndTime()
- {
- DateTime dt = DateTime.Now;
- int dayOfWeek = (int)dt.Date.DayOfWeek;
- //Sunday = 0,Monday = 1,Tuesday = 2,Wednesday = 3,Thursday = 4,Friday = 5,Saturday = 6,
- DateTime weekStartTime = dt.AddDays(-1 * dayOfWeek + 1);//取本周一
- if (dayOfWeek == 0) //如果今天是周日,则开始时间是上周一
- {
- weekStartTime = weekStartTime.AddDays(-7);
- }
- DateTime weekEndTime = dt.AddDays(7 - dayOfWeek);//取本周日
- if (dayOfWeek == 0) //如果今天是周日,则开始时间是上周一
- {
- weekEndTime = weekEndTime.AddDays(-7);
- }
- List<DateTime> RetDT = new List<DateTime>();
- RetDT.Add(weekStartTime.Date);
- RetDT.Add(DateTime.Parse(weekEndTime.ToString("yyyy-MM-dd") + " 23:59:59"));
- return RetDT;
- }
- public static List<DateTime> GetMonthStartEndTime()
- {
- DateTime dt = DateTime.Now;
- //本月第一天时间
- DateTime dt_First = dt.AddDays(1 - (dt.Day));
- //获得某年某月的天数
- int year = dt.Date.Year;
- int month = dt.Date.Month;
- int dayCount = DateTime.DaysInMonth(year, month);
- //本月最后一天时间
- DateTime dt_Last = dt_First.AddDays(dayCount - 1);
- List<DateTime> RetDT = new List<DateTime>();
- RetDT.Add(dt_First.Date);
- RetDT.Add(DateTime.Parse(dt_Last.ToString("yyyy-MM-dd") + " 23:59:59"));
- return RetDT;
- }
- }
- }
|