using SiteCore.Redis; using System; using System.Collections.Generic; 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 string _t = ""; public bool _checkRight = true; public int _pageSize = 30; public bool _addVisible = false; public int _selfAddVisible = 0; public bool _refreshVisible = true; public bool _choiceVisible = false; public bool _selfCheckPermission = false; public DateTime _filterDate; public string _searchFilter = ""; public string _searchDate = ""; public string _filterItem = ""; public bool _filterDateBtn = true; //本日本周本月的按钮显示 public string _b_sDate = ""; public string _b_eDate = ""; public bool _b_HavingTime = false; public bool _hasExport = false; public string gcTag = ""; public string _repTitle = ""; public SearchDateEnum _searchDateEnum = SearchDateEnum.Empty; public bool _noneDefaultTime = false; public bool onlyEndTime = false; public bool _keyFilterVisible = false; public bool _isCheckLogin = true; public static readonly string _btnStr = "{1}"; public List _permissions = new List();//权限 //public string _action=string.Empty; public static readonly string SiteVer = webConfig.SiteVer; public static readonly string SiteName = webConfig.SiteName; public static readonly string SiteDomain = webConfig.SiteDomain; public static readonly string CookieName = webConfig.CookieName; public static readonly string SiteUrl = webConfig.SiteUrl; public string dis_stime = "00:00:00";///webConfig.DisStartTime; public string dis_etime = "23:59:59";// webConfig.DisEndTime; #region 账户相关 public string Round(double d, int n = 2) { return d.ToString("F" + n); } public string Round(decimal d, int n = 2) { return d.ToString("F" + n); } public decimal RoundDecimal(decimal d, int n = 2) { return Convert.ToDecimal(Round(d, n)); } public double RoundDouble(double d, int n = 2) { return Convert.ToDouble(Round(d, n)); } private WebUser _currentUser = null; //获取用户数据 public WebUser CurrentUser { get { if (_currentUser == null) { _currentUser = WebUser.GetUser(); } return _currentUser; } } public int UserID { get { if (CurrentUser == null) return 0; return Convert.ToInt32(CurrentUser.UserID); } } public string PKey { get { if (CurrentUser == null) return ""; if (CurrentUser.User.State == 5) return "admin"; return CurrentUser.UserPost.PostID.ToString(); } } protected override void OnPreLoad(EventArgs e) { if (isLoginOut()) { string sPath = CommonHelper.GetPrePath(); Response.Write(""); Response.End(); return; } //_t = Session["_t"].ToString(); base.OnPreLoad(e); } public bool isLoginOut() { //人员是否已登录 //if (Session["userId"] == null || Session["userId"].ToString() == "") // return true; //传过来的Ticket是否有效 HttpCookie lcCookie = HttpContext.Current.Request.Cookies[webConfig.CookieName]; if (lcCookie == null) { return true; } string name = lcCookie.Values["User"]; string ticket = lcCookie.Values["Ticket"]; if (name == null || ticket == null || name == "" || ticket == "") { return true; } name = HttpUtility.UrlDecode(name); string dl_ticket = erpRedis.RedisHelper.StringGet("dl_" + name); //if (String.IsNullOrEmpty(dl_ticket)) //{ // erpRedis.RedisHelper.StringSet("dl_" + name, ticket); // return false; //} if (ticket != dl_ticket) { return true; } return false; } /// /// 检测是否登录 /// public bool CheckRights() { //if (Session["xyUserID"] == null) //{ // Session["xyUserID"] = "1"; // Session["xyUserName"] = "ljq"; //} if (!_isCheckLogin) return true; if (CurrentUser == null) { string sPath = CommonHelper.GetPrePath(); Response.Write(""); Response.End(); return false; } return true; } public bool CheckRightSession() { if (Session["cur_user"] == null) { string sPath = CommonHelper.GetPrePath(); Response.Write(""); Response.End(); return false; } return true; } //权限标签 public string PmTag = ""; public string GetPersonName(object pName) { if (pName.ToString() == "") return ""; return pName.ToString(); } public StringBuilder _script = new StringBuilder(); public void AppendScript(String str) { _script.Append(str); } public object GetReflectionObject(string className) { if (string.IsNullOrEmpty(className)) return null; string tmpName = "BizCom." + className; return Assembly.Load("BizCom").CreateInstance(tmpName); } #endregion //显示错误页面 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.Write(txt); Response.End(); } public int GetDecodeInt(string param) { string value = GetString(param); if (value.Length < 1) return 0; return CommonHelper.DecodeInt(value); } public string GetDecodeString(string param) { string value = GetString(param); if (value.Length < 1) return ""; return CommonHelper.DecodeString(value); } public void SelItemSelected(DropDownList ddl, object value) { ddl.ClearSelection(); System.Web.UI.WebControls.ListItem item = ddl.Items.FindByValue(value.ToString()); if (item != null) item.Selected = true; } public void SelRadioItemSelected(RadioButtonList rbl, object value) { rbl.ClearSelection(); System.Web.UI.WebControls.ListItem item = rbl.Items.FindByValue(value.ToString()); if (item != null) item.Selected = true; } public string GDis_STime { get { string dm = DateTime.Now.ToString("yyyyMM"); if (Convert.ToInt32(dm) >= 202009) return dis_stime; return "00:00:00"; } } public string GDis_ETime { get { string dm = DateTime.Now.ToString("yyyyMM"); if (Convert.ToInt32(dm) >= 202009) return dis_etime; return "23:59:59"; } } public string getTodTime(DateTime sTime) { return getTodTime(sTime.ToString("yyyy-MM-dd")); } public string getTodTime(string stime) { DisTime dTime = redis.RedisHelper.HashGet("distime", stime); if (dTime != null && dTime.stime != "") return dTime.stime; return stime + " " + GDis_STime; //string st = redis.RedisHelper.StringGet("dis_" + stime); //if (string.IsNullOrEmpty(st)) return stime + " " + dis_stime; //return st; } public string getTormTime(DateTime eTime) { return getTormTime(eTime.ToString("yyyy-MM-dd")); } public string getTormTime(string etime) { DisTime dTime = redis.RedisHelper.HashGet("distime", etime); //string et = redis.RedisHelper.StringGet("die_" + etime); if (dTime == null || dTime.etime == "") { if (dis_etime.IndexOf("-") != -1) { string detime = dis_etime.Replace("-", ""); DateTime _ctime = Convert.ToDateTime(etime); return _ctime.AddDays(1).ToString("yyyy-MM-dd") + " " + detime; } return etime + " " + GDis_ETime; } return dTime.etime; } public void getTTime(string stime, string etime, out string tstime, out string tetime) { DisTime sdTime = null; DisTime edTime = null; if (stime == etime) { sdTime = redis.RedisHelper.HashGet("distime", etime); edTime = sdTime; } else { sdTime = redis.RedisHelper.HashGet("distime", stime); edTime = redis.RedisHelper.HashGet("distime", etime); } if (sdTime == null) { tstime = stime + " " + GDis_STime; } else { if (sdTime.stime != "") tstime = sdTime.stime; else tstime = stime + " " + GDis_STime; } if (edTime == null) { if (dis_etime.IndexOf("-") != -1) { string detime = dis_etime.Replace("-", ""); DateTime _ctime = Convert.ToDateTime(etime); tetime = _ctime.AddDays(1).ToString("yyyy-MM-dd") + " " + detime; } else { tetime = etime + " " + GDis_ETime; } } else { if (string.IsNullOrEmpty(edTime.etime)) { if (dis_etime.IndexOf("-") != -1) { string detime = dis_etime.Replace("-", ""); DateTime _ctime = Convert.ToDateTime(etime); tetime = _ctime.AddDays(1).ToString("yyyy-MM-dd") + " " + detime; } else { tetime = etime + " " + GDis_ETime; } } else tetime = edTime.etime; } } public string getDefDateWhere(string sdate, string edate) { string _stime = ""; //getTodTime(edate); string _etime = "";// getTormTime(edate); if (sdate == "" && edate != "") { getTTime(edate, edate, out _stime, out _etime); return "(datediff(d,'" + _stime + "',createtime)>=0 and datediff(s,'" + _stime + "',createtime)>=0 and datediff(s,'" + _etime + "',createtime)<=0)"; } if (sdate != "" && edate != "") { getTTime(sdate, edate, out _stime, out _etime); return "datediff(d,'" + _stime + "',createtime)>=0 and datediff(s,'" + _stime + "',createtime)>=0 and datediff(s,'" + _etime + "',createtime)<=0)"; } return ""; } public string getDispatchDateWhere(string sdate, string edate) { string _stime = ""; //getTodTime(edate); string _etime = "";// getTormTime(edate); if (sdate == "" && edate != "") { getTTime(edate, edate, out _stime, out _etime); return "(datediff(d,'" + _stime + "',createtime)>=0 and datediff(s,'" + _stime + "',createtime)>=0 and datediff(s,'" + _etime + "',createtime)<=0)"; } if (sdate != "" && edate != "") { getTTime(sdate, edate, out _stime, out _etime); return "(datediff(d,'" + _stime + "',createtime)>=0 and datediff(s,'" + _stime + "',createtime)>=0 and datediff(s,'" + _etime + "',createtime)<=0)"; } return ""; } public string getDispatchDateWhere(string field, string sdate, string edate) { string _stime = ""; //getTodTime(edate); string _etime = "";// getTormTime(edate); if (sdate == "" && edate != "") { getTTime(edate, edate, out _stime, out _etime); return "(datediff(d,'" + _stime + "'," + field + ")>=0 and datediff(s,'" + _stime + "'," + field + ")>=0 and datediff(s,'" + _etime + "'," + field + ")<=0)"; } if (sdate != "" && edate != "") { getTTime(sdate, edate, out _stime, out _etime); return "(datediff(d,'" + _stime + "'," + field + ")>=0 and datediff(s,'" + _stime + "'," + field + ")>=0 and datediff(s,'" + _etime + "'," + field + ")<=0)"; } return ""; } public string getDispatchDateWhere(DateTime etime) { if (Convert.ToInt32(etime.ToString("HHmmss")) < 60000) { etime = etime.AddDays(-1); } string _stime = getTodTime(etime.ToString("yyyy-MM-dd")); string _etime = getTormTime(etime.ToString("yyyy-MM-dd")); return " datediff(d,'" + _stime + "',createtime)>=0 and datediff(s,'" + _stime + "',createtime)>=0 and datediff(s,'" + _etime + "',createtime)<=0"; } public string getDispatchDateNoTimeWhere(DateTime etime) { string _stime = getTodTime(etime); string _etime = getTormTime(etime); return " datediff(d,'" + _stime + "',createtime)>=0 and datediff(s,'" + _stime + "',createtime)>=0 and datediff(s,'" + _etime + "',createtime)<=0"; } public string getProductionTaskWhere(string stime, string etime) { return "productiontaskid in (select distinct productiontaskid from ce_erpdispatchtask where " + getSaleDispatchDateWhere(stime, etime) + " and ispatch=0 )"; } public string getSaleDispatchDateWhere(string stime, string etime) { return getDispatchDateWhere(stime, etime); } //public string getSaleDispatchDateNoTimeWhere(DateTime etime) //{ // string _stime = etime.ToString("yyyy-MM-dd") + " 00:00:00"; // string _etime = etime.ToString("yyyy-MM-dd") + " 23:59:59"; // return " datediff(s,'" + _stime + "',createtime)>=0 and datediff(s,'" + _etime + "',createtime)<=0"; //} //private void setDisTimeRedis2(string sdate,string edate, out string _stime, out string _etime) //{ // _stime = ""; // _etime = ""; // string dkey = Convert.ToDateTime(sdate).ToString("yyyy-MM-dd"); // string dStr = redis.RedisHelper.StringGet("dis_" + dkey); // if (!String.IsNullOrEmpty(dStr)) // { // string[] dArr = dStr.Split('|'); // _stime = dArr[0]; // } // else // { // _stime = getTodTime(sdate); // } // dkey = Convert.ToDateTime(edate).ToString("yyyy-MM-dd"); // dStr = redis.RedisHelper.StringGet("dis_" + dkey); // if (!string.IsNullOrEmpty(dStr)) // { // string[] dArr = dStr.Split('|'); // _etime = dArr[1]; // } // if (_etime == "") _etime = getTodTime(edate); //} //private void setDisTimeRedis(string date,out string _stime,out string _etime) //{ // string dkey = Convert.ToDateTime(date).ToString("yyyy-MM-dd"); // string dStr = redis.RedisHelper.StringGet("dis_" + dkey); // if (!String.IsNullOrEmpty(dStr)) // { // string[] dArr = dStr.Split('|'); // _stime = dArr[0]; // _etime = dArr[1]; // } // else // { // _stime = getTodTime(date); // _etime = getTormTime(date); // } //} #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 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, 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 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 void SetDateWhere(string field, string start, string end, IList where) { if (start != "" || end != "") { if (start != "" && end != "") where.Add(string.Format(" (DATEDIFF(d,'{0}',{2})>=0 and DATEDIFF(d,'{1}',{2})<=0 )", start, end, field)); else if (start != "") where.Add(string.Format(" {1}>='{0}'", start, field)); else where.Add(string.Format(" dateDiff(d,{1},'{0}')=0 ", end, field)); } } public void SetDateWhereB(string field, string start, string end, IList where) { if (start != "" || end != "") { if (start != "" && end != "") { if (start == end) where.Add(string.Format(" dateDiff(d,{1},'{0}')=0 ", end, field)); else where.Add(string.Format(" (DATEDIFF(s,'{0}',{2})>=0 and DATEDIFF(s,'{1}',{2})<=0 )", start, end, field));//where.Add(string.Format(" ({2} Between '{0}' and '{1}' )", start, end, field)); } else if (start != "") where.Add(string.Format(" {1}>='{0}'", start, field)); else where.Add(string.Format(" dateDiff(d,{1},'{0}')=0 ", end, field)); } } public 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 ""; } /// /// 获取精度数据 /// /// /// 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 "0"; return Convert.ToDouble(value).ToString("N" + num); } public string GetDoubleString(object value, string txt) { if (string.IsNullOrEmpty(value.ToString())) return 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 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 bool UrlPostParmsCheck(string parms) { return CommonHelper.UrlPostParmsCheck(parms); } public int GetInt(string parm) { return CommonHelper.GetInt(parm); } 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 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]*?", "", "]*?>", "