using System; using System.Collections.Generic; using System.Configuration; using System.Reflection; using System.Text; using System.Text.RegularExpressions; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; using Utils; namespace SiteCore { public class BasePage:Page { public bool _checkRight = true; public bool _isLoadJs = true; public bool _addVisible = false; public int _selfAddVisible = 0; public bool _refreshVisible = true; public bool _selfCheckPermission = false; public bool _keyFilterVisible = false; public DateTime _filterDate; public string _searchFilter = ""; public string _searchDate = ""; public string gcTag = ""; public SearchDateEnum _searchDateEnum = SearchDateEnum.Default; public List _permissions = new List();//权限 public string PmTag = ""; public string tag = ""; public static string SiteVer = webConfig.SiteVer; public bool _isCheckLogin = true; private WebUser _currentUser; public static readonly string TmpFilePath = ConfigurationManager.AppSettings["TempPath"]; public static readonly string AttachPath = ConfigurationManager.AppSettings["AttachPath"]; //获取用户数据 public WebUser CurrentUser { get { if (_currentUser == null) { _currentUser = WebUser.GetUser(); } return _currentUser; } } public int UserID { get { if (Session["xyUserID"] == null) return 0; return Convert.ToInt32(Session["xyUserID"]); } } public StringBuilder _script = new StringBuilder(); public void AppendScript(String str) { _script.Append(str); } /// /// 检测是否登录 /// public bool CheckRights() { if (Session["xyUserID"] == null) { Session["xyUserID"] = "1"; Session["xyUserName"] = "ljq"; } if (!_isCheckLogin) return true; string sPath = CommonHelper.GetPrePath(); if (Session["xyUserID"] == null) { Response.Write(""); Response.End(); return false; } return true; } /// /// 检测是否登录 /// public bool CheckRightsWithBack() { if (!_isCheckLogin) return true; string sPath = CommonHelper.GetPrePath(); string curUrl = HttpContext.Current.Request.RawUrl; if (CurrentUser == null) { Response.Write(""); Response.End(); return false; } return true; } /// /// 检测是否登录 /// public bool CheckMobileRights() { if (!_isCheckLogin) return true; string sPath = CommonHelper.GetPrePath(); string curUrl = HttpContext.Current.Request.Url.AbsolutePath; if (CurrentUser == null) { Response.Write(""); Response.End(); return false; } return true; } public void conSuccess(string msg) { Response.Write("{\"res\":1,\"msg\":\"" + msg + "\"}"); } public void conSuccess(string msg, string ext) { Response.Write("{\"res\":1,\"ext\":[" + ext + "],\"msg\":\"" + msg + "\"}"); } public void conError(string msg) { Response.Write("{\"res\":0,\"msg\":\"" + msg + "\"}"); } public void conGridJson(object total, string json) { Response.Write("{" + string.Format("\"res\":1,\"total\":{0},\"data\":{1}", total, json) + "}"); } public string GetImgUrl(object img) { return CommonHelper.GetPrePath() + "d/" + img; } public DataStruct GetStruct(int pi,int ps) { DataStruct dStruct = new DataStruct(); dStruct.CurrentPage = pi + 1; dStruct.PageSize = (ps == 0 ? 20 : ps); return dStruct; } /// /// 查询时次要过滤条件 /// public string SecondWhere { get { if (ViewState["secondWhere"] == null) return ""; else return ViewState["secondWhere"].ToString(); } set { ViewState["secondWhere"] = value; } } public void SelItemSelected(DropDownList ddl,object value) { ddl.ClearSelection(); ListItem item=ddl.Items.FindByValue(value.ToString()); if(item!=null)item.Selected=true; } public void SelRadioItemSelected(RadioButtonList rbl, object value) { rbl.ClearSelection(); ListItem item = rbl.Items.FindByValue(value.ToString()); if (item != null) item.Selected = true; } //显示错误页面 public void ShowErrorPage() { Response.Redirect(CommonHelper.GetPrePath() + "ErrorPage.htm", true); } public void ShowErrorHtml(string url) { Response.Write("你访问的页面错误啦!点击这里返回"); Response.End(); } public void ShowErrorHtml(string txt,string url) { Response.Write(txt+"!点击这里返回"); Response.End(); } public void ShowError(string txt) { Response.Clear(); Response.Write(txt); Response.End(); } public object GetReflectionObject(string className) { if (string.IsNullOrEmpty(className)) return null; string tmpName = "BizCom." + className; return Assembly.Load("BizCom").CreateInstance(tmpName); } #region 格式化显示方式 /// /// 返回是否过期 /// /// /// public static bool CheckIsExpire(object endTime) { return DateTime.Now.Subtract(Convert.ToDateTime(endTime)).Days > 0; } /// /// 布尔值显示文字 /// /// /// /// /// public string GetBoolenString(object result, string tStr, string fStr) { if (result.Equals(DBNull.Value)) return ""; return Convert.ToBoolean(result) ? tStr : fStr; } /// /// 图片显示 /// /// /// public string GetImgString(object imgUrl) { return GetImgString(imgUrl, ""); } public string GetImgString(object imgUrl, string noPic) { string str = ""; string img = noPic == "" ? "images/noImg.gif" : "images/" + noPic; if (imgUrl.ToString() != "") img = imgUrl.ToString().Replace("images/", "images/i2/"); return string.Format(str, "../../" + img); } public string GetCouponImageString(object imgUrl,object type) { string str = ""; string img = "images/nocImg.gif"; if (type.ToString() == "1") img = "images/noeImg.gif"; if (imgUrl.ToString() != "") img = "Documents/" + imgUrl; return string.Format(str, "../../" + img); } public string GetMerchantImgString(object imgUrl, string noPic) { string str = ""; string img = noPic == "" ? "images/noImg.gif" : "images/" + noPic; if (imgUrl.ToString() != "") img = "Documents/" + imgUrl; return string.Format(str, "../../" + img); } public string GetSiteImgString(object imgUrl, object title) { string str = "\"{1}\""; string img = ""; if (imgUrl.ToString() != "") img = "Documents/" + imgUrl; return string.Format(str, "../../" + img,CommonHelper.FormatForXML(title)); } public string GetCurrentImgString(object imgUrl, string noPic) { string img = noPic == "" ? "images/noImg.gif" : "images/" + noPic; if (imgUrl.ToString() != "") img = "Documents/" + imgUrl; return img; } /// /// 格式化日期 /// /// /// public string GetFormatDate(object date) { return GetFormatDate(date, "无"); } public string GetFormatDate(object date, string def) { if (date == null) return def; return date.ToString() == "" ? def : Convert.ToDateTime(date).ToString("yyyy-MM-dd"); } /// /// 过期日期 /// /// /// /// public string GetFormatExpireDate(object sDate, object eDate) { return GetFormatExpireDate(sDate, eDate, "无"); } public string GetFormatExpireDate(object sDate, object eDate, string defaultTxt) { if (sDate.ToString() == "" && eDate.ToString() == "") { return defaultTxt; } return "" + GetFormatDate(sDate) + "" + GetFormatDate(eDate) + ""; } public string GetFormatapkDate(object sDate, object eDate, string defaultTxt) { if (sDate.ToString() == "" && eDate.ToString() == "") { return defaultTxt; } return "" + GetFormatDate(sDate) + "至" + GetFormatDate(eDate) + ""; } /// /// 格式日期带时间 /// /// /// public string GetFormatDateTime(object date) { if (date==null || date.Equals(DBNull.Value)) return ""; return date.ToString() == "" ? "" : Convert.ToDateTime(date).ToString("yyyy-MM-dd HH:mm"); } public string GetCurrentDateTime(object date) { if (date.Equals(DBNull.Value) || date.ToString() == "") return ""; DateTime dt1 = DateTime.Now; DateTime dt2 = Convert.ToDateTime(date); TimeSpan span = dt1.Subtract(dt2); if (span.TotalDays > 5) { return dt2.ToString("yyyy-MM-dd HH:mm"); }/* else if (span.TotalDays > 30) { return "1个月前"; } 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 "刚刚"; } } /// /// 格式化时间 /// /// /// /// public string GetFormatTime(object sTime, object eTime) { string tmp = sTime.ToString(); string firstTime = "", endTime = ""; if (tmp == "" || tmp == "-1") return "全天"; if (tmp == "0") firstTime = "00:00"; else if (tmp == "30") firstTime = "00:30"; else firstTime = tmp.Substring(0, tmp.Length - 2).PadLeft(2, '0') + ":" + tmp.Substring(tmp.Length - 2); tmp = eTime.ToString(); if (tmp == "0") endTime = "00:00"; else if (tmp == "30") endTime = "00:30"; else endTime = tmp.Substring(0, tmp.Length - 2).PadLeft(2, '0') + ":" + tmp.Substring(tmp.Length - 2); return firstTime + " ~ " + endTime; } /// /// 获取精度数据 /// /// /// 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 GetDoubleString(object value, string txt) { if (Convert.ToDouble(value) < 1) return txt; else return GetDoubleString(value); } //获取还剩几天 public string GetDayByDate(object date) { DateTime curDt = DateTime.Now; if (date.ToString() == "") return "己过期"; DateTime exDt = Convert.ToDateTime(date); TimeSpan tdays = exDt - curDt; if (tdays.Days < 0) return "己过期"; else if (tdays.Days == 0) return "今天"; return tdays.Days + "天"; } /// /// 获取带有空值的字符串 /// /// data /// 为空时显示的内容 /// public string GetWithEmptyString(object result, string nullStr) { if (result == null) return nullStr; return result.ToString().Length > 0 ? result.ToString() : nullStr; } /// /// 获取两个数字相差的数量 /// /// /// /// public object GetMinuteNum(object aNum, object num) { if (aNum.ToString() == "" || num.ToString() == "") return 0; return Convert.ToInt32(aNum) - Convert.ToInt32(num); } public object GetMinusMinuteNum(object aNum, object num) { if (aNum.ToString() == "" || num.ToString() == "") return 0; int tmp=Convert.ToInt32(aNum) - Convert.ToInt32(num); if(tmp<0)return 0; return tmp; } public double GetDoubleMinuteNum(object aNum,object num) { return Convert.ToDouble(aNum) - Convert.ToDouble(num); } public string GetDoubleMinuteString(object aNum, object num) { return GetDoubleString(Convert.ToDouble(aNum) - Convert.ToDouble(num)); } #endregion #region 页面方法 //设置过期 public void SetPageExpires() { Response.Buffer = true; Response.ExpiresAbsolute = DateTime.Now.AddSeconds(-1); Response.Expires = 0; Response.AddHeader("pragma","no-cache"); Response.AddHeader("cache-control", "private"); Response.CacheControl = "no-cache"; } #endregion #region URL参数 /// /// 获取当前Url带完整参数 /// /// /// /// 不加的参数 /// public string GetCurrentUrl(string url, string parm, string nshow) { string query = HttpContext.Current.Request.Url.Query; StringBuilder qStr = new StringBuilder(); qStr.Append(url + "?"); if (query != "") { query = query.Substring(1); string key = parm.Split('=')[0]; string[] qArr = query.Split('&'); for (int i = 0; i < qArr.Length; i++) { if (qArr[i].Split('=')[0] != key && qArr[i].Split('=')[0] != nshow) qStr.Append(qArr[i] + "&"); } } qStr.Append(parm); return qStr.ToString(); } public string GetCurrentUrl(string parm, string nshow) { return GetCurrentUrl("", parm, nshow); } /// /// 获取当前Url带完整参数 /// /// /// public string GetCurrentUrl(string parm) { return GetCurrentUrl("", parm, ""); } /// /// 检测URL参数是否存在 /// /// /// public bool UrlParmsCheck(string parms) { return CommonHelper.UrlParmsCheck(parms); } public static bool UrlPostParmsCheck(string parms) { return CommonHelper.UrlPostParmsCheck(parms); } public int GetInt(string param) { return CommonHelper.GetInt(param); } public int GetPostInt(string param) { return CommonHelper.GetPostInt(param); } public double GetDouble(string param) { string v = GetString(param); if (v == "") return 0; return Convert.ToDouble(v); } public double GetPostDouble(string param) { string v = GetPostString(param); if (v == "") return 0; return Convert.ToDouble(v); } public string GetString(string param) { return CommonHelper.GetString(param); } public string GetPostString(string param) { return CommonHelper.GetPostString(param); } public int GetDecodeInt(string param) { return GetDecodeInt(param, false); } public 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 string GetDecodeString(string param) { return GetDecodeString(param, false); } public 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 string GetDeepString(params string[] parms) { return CommonHelper.GetDeepString(parms); } public string GetDeepIds(params string[] parms) { return CommonHelper.GetDeepIds(parms); } public string GetHttpDecodeString(string parm) { return HttpUtility.UrlDecode(GetString(parm)); } public string GetHttpDecodeString(string parm, Encoding coding) { return HttpUtility.UrlDecode(GetString(parm),coding); } public int GetDeepIndex(params string[] parms) { return CommonHelper.GetDeepIndex(parms); } #endregion #region 显示Ajax消息 /// /// 显示消息 /// /// 信息内容 public void ShowAjaxMsg(string msg) { ShowAjaxMsg("提示", msg); } /// /// 显示消息 /// /// 提示 /// 信息内容 public void ShowAjaxMsg(string title, string msg) { ScriptManager.RegisterStartupScript(Page, GetType(), title, "", false); } /// /// 显示消息带JS函数 /// /// 方法名称 public void ShowAjaxHasFunc(string func) { ShowAjaxHasFunc("提示", func); } /// /// 显示消息带JS函数 /// /// 提示 /// 方法名称 public void ShowAjaxHasFunc(string title, string func) { ScriptManager.RegisterStartupScript(Page, GetType(), title, "", false); } #endregion #region 运行JS脚本显示 public void ShowScript(string script) { Page.ClientScript.RegisterStartupScript(GetType(), "alt", ""); } /// /// 显示消息 /// /// 信息内容 public void ShowMsg(string msg) { ShowMsg("提示", msg); } /// /// 显示消息 /// /// 提示 /// 信息内容 public void ShowMsg(string title, string msg) { ShowMsg(title, msg, ""); } /// /// 显示消息 /// /// 提示 /// 信息内容 /// 方法 public void ShowMsg(string title, string msg, string func) { Page.ClientScript.RegisterStartupScript(GetType(), title, ""); } /// /// 弹出显示结果 /// /// public void ShowResult(string msg) { ShowResult(msg, ""); } /// /// 弹出显示结果 /// /// /// public void ShowResult(string msg, string func) { func = func.Replace("'", "\""); Page.ClientScript.RegisterStartupScript(GetType(), "result", ""); } #endregion } public enum SearchDateEnum { Empty = 9, Default = 0, OneDay = 1, OneMonth = 2, OneYear = 3 } }