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 OpenCache = new Dictionary(); private static readonly List userKeyList = new List(); 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 /// /// 解密 /// /// 要解密的以Base64 /// 密钥,且必须为8位 /// 已解密的字符串 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); } /// /// 检测URL参数是否存在 /// /// /// 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\\((.*?)\\)", "]*?>[\\s\\S]*?", "]*?>[\\s\\S]*?", "", "]*?>", "