| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485 |
- using BizCom;
- using System;
- using System.Collections.Generic;
- using System.Data;
- using System.Reflection;
- using System.Security.Cryptography;
- using System.Text;
- using System.Text.RegularExpressions;
- using System.Web;
- using Utils;
- namespace SiteCore.Handler
- {
- public class BaseHandler
- {
- public HttpContext con = null;
- public static readonly Regex mailReg = new Regex(@"^(?:\w+\.?)*\w+@(?:\w+\.)+\w+$");
- public static readonly Regex mobileReg = new Regex(@"^(1)[0-9]{10}$");
- public static readonly Regex pwdReg = new Regex(@"^[\@A-Za-z0-9\!\#\$\%\^\&\*\.\~]{6,20}$");
- public static readonly Regex nnReg = new Regex(@"^[\u4E00-\u9FA5A-Za-z0-9_]+$");
- public static readonly Regex nameReg = new Regex(@"^([\u4e00-\u9fa5]+|([a-zA-Z]+\s?)+)$");
- public bool successFlag = false;
- public int labPtime = 15;
- public int ex_psize = 0;
- public DataTable ex_dtable = null;
- public BaseHandler()
- {
- CommonHelper.PageExpires();
- /*
- //上次请求的URL是否存在
- if (HttpContext.Current.Request.UrlReferrer != null)
- {
- string host = HttpContext.Current.Request.UrlReferrer.Host;
- if (host.IndexOf("csbq.cn") > 0)
- {
- }
- else
- {
- returnErrorMsg("服务器错误!");
- HttpContext.Current.Response.End();
- }
- }
- else
- {
- returnErrorMsg("服务器错误!");
- HttpContext.Current.Response.End();
- }*/
- }
- public static Dictionary<int, string> OpenCache = new Dictionary<int, string>();
- private static readonly List<int> userKeyList = new List<int>();
- private static readonly object usercache_Flag = 1;
- public static string GetCacheOpenId(int key)
- {
- if (!userKeyList.Contains(key) && !OpenCache.ContainsKey(key))
- {
- lock (usercache_Flag)
- {
- if (OpenCache.Count > 500)
- {
- OpenCache.Remove(userKeyList[userKeyList.Count - 1]);
- userKeyList.RemoveAt(userKeyList.Count - 1);
- }
- string value = SUser.GetOpenId(key);
- OpenCache.Add(key, value);
- userKeyList.Add(key);
- return value;
- }
- }
- else
- {
- return OpenCache[key];
- }
- }
- #region private
- public int getAppUserId()
- {
- if (UrlPostParmsCheck("uid"))
- {
- return getDecodeInt("uid");
- }
- return 0;
- }
- public void conWrite(string msg)
- {
- con.Response.Write(msg);
- }
- public void conSuccess(string msg)
- {
- con.Response.Write("{\"res\":1,\"msg\":\"" + msg + "\"}");
- }
- public void conSuccess(string msg, string ext)
- {
- con.Response.Write("{\"res\":1,\"ext\":[" + ext + "],\"msg\":\"" + msg + "\"}");
- }
- public void conSuccessData(string msg, string ext)
- {
- con.Response.Write("{\"res\":1,\"data\":" + ext + ",\"msg\":\"" + msg + "\"}");
- }
- public void conError(string msg)
- {
- con.Response.Write("{\"res\":0,\"msg\":\"" + msg + "\"}");
- }
- public void conLoginError(string msg)
- {
- con.Response.Write("{\"res\":9,\"msg\":\"" + msg + "\"}");
- }
- public void conGridJson(object total, string json)
- {
- con.Response.Write("{" + string.Format("\"res\":1,\"total\":{0},\"data\":{1}", total, json) + "}");
- }
- public int getDecodeInt(string p)
- {
- string id = GetPostString(p);
- if (id == "") return 0;
- return Convert.ToInt32(SecurityHelper.DecodingBase64(id));
- }
- public void ReturnJsonMsg(string sType, string sMsg)
- {
- string json = "\"type\":\"{0}\",\"result\":\"{1}\"";
- HttpContext.Current.Response.Write("{" + string.Format(json, sType, sMsg) + "}");
- }
- public void ReturnSuccess(string sContent)
- {
- string json = "\"type\":\"success\",\"result\":{0}";
- HttpContext.Current.Response.Write("{" + string.Format(json, sContent) + "}");
- }
- public void ReturnSuccess(string sContent, string con2)
- {
- string json = "\"type\":\"success\",\"result\":{0},\"result2\":{1}";
- HttpContext.Current.Response.Write("{" + string.Format(json, sContent) + "}");
- }
- //接口用的!!!
- public void ReturnSuccesss(string sContent)
- {
- string json = "\"code\":\"0\",\"result\":{0}";
- HttpContext.Current.Response.Write("{" + string.Format(json, sContent) + "}");
- }
- #endregion
- /// <summary>
- /// 解密
- /// </summary>
- /// <param name="pToDecrypt">要解密的以Base64</param>
- /// <param name="sKey">密钥,且必须为8位</param>
- /// <returns>已解密的字符串</returns>
- public static string DesDecryptAndroid(string pToDecrypt, string sKey)
- {
- //转义特殊字符
- pToDecrypt = pToDecrypt.Replace("-", "+");
- pToDecrypt = pToDecrypt.Replace("_", "/");
- pToDecrypt = pToDecrypt.Replace("~", "=");
- byte[] inputByteArray = Convert.FromBase64String(pToDecrypt);
- using (DESCryptoServiceProvider des = new DESCryptoServiceProvider())
- {
- des.Key = ASCIIEncoding.ASCII.GetBytes(sKey);
- des.IV = ASCIIEncoding.ASCII.GetBytes(sKey);
- System.IO.MemoryStream ms = new System.IO.MemoryStream();
- using (CryptoStream cs = new CryptoStream(ms, des.CreateDecryptor(), CryptoStreamMode.Write))
- {
- cs.Write(inputByteArray, 0, inputByteArray.Length);
- cs.FlushFinalBlock();
- cs.Close();
- }
- string str = Encoding.UTF8.GetString(ms.ToArray());
- ms.Close();
- return str;
- }
- }
- public static int ConvertDateTimeInt(System.DateTime time)
- {
- System.DateTime startTime = TimeZone.CurrentTimeZone.ToLocalTime(new System.DateTime(1970, 1, 1));
- return (int)(time - startTime).TotalSeconds;
- }
- public DataStruct GetUCenterStruct()
- {
- DataStruct dStruct = new DataStruct();
- dStruct.CurrentPage = GetInt("pi") + 1;
- int ps = GetInt("ps");
- dStruct.PageSize = (ps == 0 ? 5 : ps);
- return dStruct;
- }
- public DataStruct GetStruct()
- {
- DataStruct dStruct = new DataStruct();
- dStruct.CurrentPage = GetInt("pageIndex") + 1;
- int ps = GetInt("pageSize");
- dStruct.PageSize = (ps == 0 ? 20 : ps);
- return dStruct;
- }
- public DataStruct GetPostStruct()
- {
- DataStruct dStruct = new DataStruct();
- if (ex_psize > 0) dStruct.isExport = true;
- dStruct.CurrentPage = GetPostInt("pageIndex") + 1;
- int ps = GetPostInt("pageSize");
- if (ex_psize > 0) dStruct.PageSize = ex_psize;
- else dStruct.PageSize = (ps == 0 ? 20 : ps);
- return dStruct;
- }
- public void writeGridJson(object total, string json)
- {
- con.Response.Write("{" + string.Format("\"total\":{0},\"data\":{1}", total, json) + "}");
- }
- public void writeGridDataTableJson(object total, DataTable dt)
- {
- if (ex_psize > 0) { ex_dtable = dt; return; }
- con.Response.Write("{" + string.Format("\"total\":{0},\"data\":{1}", total, Utils.Serialization.JsonString.DataTable2MiniAjaxJson(dt)) + "}");
- }
- public void writeGridDataViewJson(object total, DataView dv)
- {
- if (ex_psize > 0) { ex_dtable = dv.ToTable(); return; }
- con.Response.Write("{" + string.Format("\"total\":{0},\"data\":{1}", total, Utils.Serialization.JsonString.DataView2MiniAjaxJson(dv)) + "}");
- }
- private WebUser _currentUser = null;
- //获取用户数据
- public WebUser CurrentUser
- {
- get
- {
- if (_currentUser == null)
- _currentUser = WebUser.GetUser();
- return _currentUser;
- }
- }
- public string PKey
- {
- get
- {
- if (CurrentUser == null) return "";
- if (CurrentUser.User.State == 5) return "admin";
- return CurrentUser.UserPost.PostID.ToString();
- }
- }
- public void ReturnLoginMsg(string sMsg)
- {
- successFlag = true;
- CommonHelper.ReturnJsonMsg("login", sMsg);
- }
- public void ReturnUserMsg(string sMsg)
- {
- successFlag = true;
- CommonHelper.ReturnJsonMsg("user", sMsg);
- }
- public void returnErrorMsg(string msg)
- {
- successFlag = true;
- CommonHelper.ReturnJsonMsg("error", msg == "" ? "操作发生错误" : msg);
- }
- public void returnSuccessMsg(string msg)
- {
- successFlag = true;
- CommonHelper.ReturnJsonMsg("success", msg);
- }
- public void returnSuccess(string content)
- {
- successFlag = true;
- CommonHelper.ReturnSuccess(content);
- }
- public void ReturnError(string error, string msg)
- {
- successFlag = true;
- CommonHelper.ReturnJsonMsg(error, msg);
- }
- /// <summary>
- /// 检测URL参数是否存在
- /// </summary>
- /// <param name="parms"></param>
- /// <returns></returns>
- public static bool UrlParmsCheck(string parms)
- {
- return CommonHelper.UrlParmsCheck(parms);
- }
- public static bool UrlPostParmsCheck(string parms)
- {
- return CommonHelper.UrlPostParmsCheck(parms);
- }
- public static int GetInt(string param)
- {
- return CommonHelper.GetInt(param);
- }
- public static int GetPostInt(string param)
- {
- return CommonHelper.GetPostInt(param);
- }
- public static double GetDouble(string param)
- {
- string v = GetString(param);
- if (v == "") return 0;
- return Convert.ToDouble(v);
- }
- public static double GetPostDouble(string param)
- {
- string v = GetPostString(param);
- if (v == "") return 0;
- return Convert.ToDouble(v);
- }
- public static string GetString(string param)
- {
- string text = CommonHelper.GetString(param);
- text = cleanHtml(text);
- return text;
- }
- public static string GetPostString(string param)
- {
- string text = CommonHelper.GetPostString(param).Trim();
- text = cleanHtml(text);
- return text;
- }
- public static String cleanHtml(String htmlText)
- {
- if (htmlText == null)
- return null;
- htmlText = htmlText.Trim();
- if (htmlText.Length == 0)
- return htmlText;
- string[] regex = { "expression\\((.*?)\\)", "eval\\((.*?)\\)", "<script[^>]*?>[\\s\\S]*?</script>", "<style[^>]*?>[\\s\\S]*?</style>",
- "<!--.*?-->", "<script[^>]*?>", "<!--", "javascript:", "vbscript:","onload(.*?)=" };
- for (int i = 0; i < regex.Length; i++)
- {
- htmlText = Regex.Replace(htmlText, regex[i], "", RegexOptions.IgnoreCase);
- }
- htmlText = htmlText.Replace("--", "- -").Replace("'", "'").Replace("/*", "/ *");//.Replace("(", "(").Replace(")", ")");
- return htmlText;
- }
- public static int GetDecodeInt(string param)
- {
- return GetDecodeInt(param, false);
- }
- public static int GetDecodeInt(string param, bool isPost)
- {
- string value = null;
- if (isPost) value = GetPostString(param);
- else value = GetString(param);
- if (value.Length < 1) return 0;
- return CommonHelper.DecodeInt(value);
- }
- public static string GetDecodeString(string param)
- {
- return GetDecodeString(param, false);
- }
- public static string GetDecodeString(string param, bool isPost)
- {
- string value = null;
- if (isPost) value = GetPostString(param);
- else value = GetString(param);
- if (value.Length < 1) return "";
- return CommonHelper.DecodeString(value);
- }
- public static IList<int> GetDecodeList(string param)
- {
- string value = GetString(param);
- if (value.Length < 1) return null;
- return CommonHelper.DecodeList(value);
- }
- public string GetDoubleString(object value)
- {
- if (string.IsNullOrEmpty(value.ToString())) return "";
- string db = Convert.ToDouble(value).ToString("F2");
- return db.Replace(".00", "");
- }
- public string GetDoubleString(object value, int num)
- {
- if (string.IsNullOrEmpty(value.ToString())) return "";
- return Convert.ToDouble(value).ToString("N" + num);
- }
- public string GetShengText(object v)
- {
- if (v.ToString() == "") return "";
- string str = v.ToString();
- if (str.Length > 90) return str.Substring(0, 90) + "...";
- else return str;
- }
- public object GetReflectionObject(string className)
- {
- if (string.IsNullOrEmpty(className)) return null;
- string tmpName = "BizCom." + className;
- return Assembly.Load("BizCom").CreateInstance(tmpName);
- }
- /**
- * 根据当前系统时间加随机序列来生成订单号
- * @return 订单号
- */
- public static string GenerateOutTradeNo(string tag)
- {
- var ran = new Random();
- return string.Format("{0}{1}{2}", tag, GenerateTimeStamp(), ran.Next(999));
- }
- /**
- * 生成时间戳,标准北京时间,时区为东八区,自1970年1月1日 0点0分0秒以来的秒数
- * @return 时间戳
- */
- public static string GenerateTimeStamp()
- {
- TimeSpan ts = DateTime.UtcNow - new DateTime(1970, 1, 1, 0, 0, 0, 0);
- return Convert.ToInt64(ts.TotalSeconds).ToString();
- }
- /**
- * 生成随机串,随机串包含字母或数字
- * @return 随机串
- */
- public static string GenerateNonceStr()
- {
- return Guid.NewGuid().ToString().Replace("-", "");
- }
- protected string GetDateWhere(string field, string start, string end)
- {
- if (start != "" || end != "")
- {
- if (start != "" && end != "")
- return string.Format(" (DATEDIFF(d,'{0}',{2})>=0 and DATEDIFF(d,'{1}',{2})<=0 )", start, end, field);
- else if (start != "")
- return string.Format(" {1}>='{0}'", start, field);
- else
- return string.Format(" dateDiff(d,{1},'{0}')=0 ", end, field);
- }
- return "";
- }
- protected string GetDateMinuteWhere(string field, string start, string end)
- {
- if (start != "" || end != "")
- {
- if (start != "" && end != "")
- return string.Format(" (DATEDIFF(n,'{0}',{2})>=0 and DATEDIFF(n,'{1}',{2})<=0 )", start, end, field);
- else if (start != "")
- return string.Format(" {1}>='{0}'", start, field);
- else
- return string.Format(" dateDiff(d,{1},'{0}')=0 ", end, field);
- }
- return "";
- }
- public virtual bool IsReusable
- {
- get
- {
- return false;
- }
- }
- }
- }
|