using System; using System.Collections.Generic; using System.Web; using System.Web.UI.WebControls; namespace CommonTool { public class WebLibs { /// /// 将不能在HTML中正常显示的ASCII字符串进行Html语法的翻译 /// /// 需要进行Html语法翻译的ASCII字符串 public static void AreaToHtml(ref string pcStr) { if (pcStr != null) { pcStr = pcStr.Replace(" ", " "); pcStr = pcStr.Replace("\r", "
"); } } /// /// 替换Url地址信息里的 换行符号 /// /// /// public static string ConvertUrlMsg(string pcUrlMsg) { pcUrlMsg = pcUrlMsg.Replace("\n\r", " "); pcUrlMsg = pcUrlMsg.Replace("\r\n", " "); pcUrlMsg = pcUrlMsg.Replace("\r", " "); pcUrlMsg = pcUrlMsg.Replace("\n", " "); return pcUrlMsg; } //public static void WebShowMessage(string pcMsg) //{ // WebShowMessage(pcMsg, ErrActType.Return); //} //public static void WebShowMessage(string pcMsg, ErrActType poErrActType) //{ // pcMsg = ConvertURLMsg(pcMsg); // HttpContext.Current.Response.Redirect("../Pub/WebShowMsg.aspx?pc_ErrorMsg=" + pcMsg + "&pc_ActionType=" + poErrActType.ToString()); //} public static Unit IntToUnit(int piPixels) { return Unit.Parse(piPixels.ToString() + "px"); } public static string ColorToHex(System.Drawing.Color poColor) { string lcRetVal = poColor.ToArgb().ToString("X"); lcRetVal = "#" + lcRetVal.Substring(2, 6); return lcRetVal; } public static string CurPageName { get { string lcRetVal = ""; string[] arrayurl = HttpContext.Current.Request.Url.PathAndQuery.Split('?'); if (arrayurl.Length > 0) { string file = arrayurl[0]; string[] arr2 = file.Split('/'); if (arr2.Length > 0) { if (arr2.Length == 1) lcRetVal = arr2[0]; else lcRetVal = arr2[arr2.Length - 2] + "_" + arr2[arr2.Length - 1]; } } return lcRetVal; } } public static UserSession GetUserSession() { UserSession loRetVal = null; if (HttpContext.Current != null && HttpContext.Current.Session != null && HttpContext.Current.Session[Contants.UserInfoId] != null) { loRetVal = (UserSession)HttpContext.Current.Session[Contants.UserInfoId]; } return loRetVal; } public static UserSession CheckLogin() { var loRetVal = GetUserSession(); if (loRetVal == null) { //System.Web.HttpContext.Current.Response.Write("
"); // System.Web.HttpContext.Current.Response.Write("
"); //System.Web.HttpContext.Current.Response.Write(""); HttpContext.Current.Response.Write(""); //System.Web.HttpContext.Current.Response.Write("timeout"); HttpContext.Current.Response.End(); } return loRetVal; } public static UserSession CheckLoginAjax() { var loRetVal = GetUserSession(); if (loRetVal == null) { //System.Web.HttpContext.Current.Response.Write("
"); // System.Web.HttpContext.Current.Response.Write("
"); //System.Web.HttpContext.Current.Response.Write(""); //System.Web.HttpContext.Current.Response.Write(""); HttpContext.Current.Response.Write("timeout"); HttpContext.Current.Response.End(); } return loRetVal; } /** * 检查参会者的Session * * */ public static string GetCookieValue(string pcCookieKey) { return GetCookieValue(pcCookieKey, Contants.SysCookieName); } public static string GetCookieValue(string pcCookieKey, string pcCookieName) { string lcRetVal = ""; if (HttpContext.Current.Request.Browser.Cookies) { HttpCookie loCookie = HttpContext.Current.Request.Cookies[pcCookieName]; string lcTempValue = loCookie?[pcCookieKey]; if (lcTempValue != null) lcRetVal = lcTempValue; } return lcRetVal; } public static void SetCookieValue(string pcCookieKey, string pcCookieValue) { SetCookieValue(pcCookieKey, pcCookieValue, Contants.SysCookieName); } public static void SetCookieValue(string pcCookieKey, string pcCookieValue, string pcCookieName) { if (HttpContext.Current.Request.Browser.Cookies) { HttpCookie loCookie = HttpContext.Current.Request.Cookies[pcCookieName] ?? new HttpCookie(pcCookieName); loCookie.Values.Remove(pcCookieKey); loCookie.Values.Add(pcCookieKey, pcCookieValue); loCookie.Expires = DateTime.Now.AddMonths(6); HttpContext.Current.Response.SetCookie(loCookie); } } public static string CvStrByType(ref string pcStr, CovType typeUse) { pcStr = pcStr.Trim(); switch (((int)typeUse)) { case 1: pcStr = pcStr.Replace(@"\", @"\\"); pcStr = pcStr.Replace("\r\n", @"\n"); pcStr = pcStr.Replace("\r", ""); pcStr = pcStr.Replace("\n", ""); pcStr = pcStr.Replace("'", @"\'"); break; case 2: pcStr = pcStr.Replace("&", "&"); pcStr = pcStr.Replace("<", "<"); pcStr = pcStr.Replace(">", ">"); pcStr = pcStr.Replace(" ", " "); pcStr = pcStr.Replace("'", "\u2019"); pcStr = pcStr.Replace("\r\n", "
"); pcStr = pcStr.Replace("\r", "
"); pcStr = pcStr.Replace("\n", "
"); break; case 3: pcStr = pcStr.Replace("
", "\r\n"); pcStr = pcStr.Replace("
", "\r\n"); pcStr = pcStr.Replace("
", "\r\n"); pcStr = pcStr.Replace("
", "\r\n"); break; case 4: pcStr = pcStr.Replace("\r\n", "
"); pcStr = pcStr.Replace(" ", " "); pcStr = pcStr.Replace("'", "\u2019"); pcStr = pcStr.Replace("\r", "
"); pcStr = pcStr.Replace("\n", "
"); pcStr = pcStr.Replace("<", "<"); pcStr = pcStr.Replace(">", ">"); pcStr = pcStr.Replace("&", "\uff06"); break; default: pcStr = pcStr.Replace("'", "''"); break; } return (pcStr + ""); } #region //===============================错误处理====================== /// /// 权限检查错误转向页面 /// /// 1,没有浏览权;2,没有添加权 ; 3 没有修改权 4;没有删除权; /// 5,没有打印权; 7 页面权限编号信息错误 public static void NotPower(int asPowerType) { NotPower(asPowerType, "N"); } /// /// 权限检查错误转向页面 /// /// 1,没有浏览权;2,没有添加权 ; 3 没有修改权 4;没有删除权; /// 5,没有打印权; 7 页面权限编号信息错误 /// 参数 C 关闭;R 重定向 ;P 上一页; U 指定页面 ;N 不显示 public static void NotPower(int asPowerType, string asReturnType) { if (asPowerType == 1) { ErrRedirect(-30000001, asReturnType); } //浏览 if (asPowerType == 2) { ErrRedirect(-30000002, asReturnType); } //添加 if (asPowerType == 3) { ErrRedirect(-30000003, asReturnType); } //修改 if (asPowerType == 4) { ErrRedirect(-30000004, asReturnType); } //删除 if (asPowerType == 5) { ErrRedirect(-30000005, asReturnType); } //打印 if (asPowerType == 5) { ErrRedirect(-30000006, asReturnType); } //审核 if (asPowerType == 6) { ErrRedirect(-311271, asReturnType); } //检查是否有浏览新增删除修改的权限 if (asPowerType == 7) { ErrRedirect(-30000008, asReturnType); } //页面权限编号信息错误 if (asPowerType == 8) { ErrRedirect(-30000007, asReturnType); } //页面权限编号信息错误 } /// /// 操作错误 提示页面 /// /// 1,为删除的行为空 ;2 编号为空 ;3 参数传递错误 public static void OperationErr(int asPowerType) { OperationErr(asPowerType, ""); } /// /// 操作错误 提示页面 /// /// 1,为删除的行为空 ;2 编号为空 ;3 参数传递错误 /// public static void OperationErr(int asOperationType, string asReturnType) { if (asOperationType == 1) { ErrRedirect(-10000101, asReturnType); } if (asOperationType == 2) { ErrRedirect(-10000102, asReturnType); } if (asOperationType == 3) { ErrRedirect(-10000103, asReturnType); } } public static void ErrRedirect(int asErrorCode, string asReturnType) { var returnType = asReturnType.Trim().Length == 0 ? "C" : asReturnType; PageRedirect(asErrorCode, returnType); } public static void PageRedirect(int errorCode, string returnType) { HttpContext.Current.Response.Redirect("../Pub/PubShowMessage.aspx?al_ErrCode=" + errorCode + "&as_ReturnType=" + returnType); } public static void LoginOut() { HttpContext.Current.Session.RemoveAll(); HttpContext.Current.Response.Write("
"); HttpContext.Current.Response.End(); } #endregion } public sealed class Contants { public const string UserInfoId = "WeiBoYUserInfoIdInSession"; public const string SysCookieName = "WeiBoYCookieKeyYue"; public const string UserName = "WeiBoYCookieUserName"; public const string PassWord = "WeiBoYCookieUserPassWord"; public const string StatusNew = "new"; public const string StatusExec = "exec"; public const string StatusCmp = "cmp"; public const string CustomerInfoNo = "ServiceTypeCustomerInfoNoSession"; public const string SendMsgReg = "ServiceTypeSendMsgRegSession"; public const string SendMsgLogin = "ServiceTypeSendMsgLoginSession"; public const string SendEamilMsg = "CustomerSendEamilMsgModifyEmailSession"; public const string AlumnuseInfoId = "AlumnuseInfoIdSession"; } public sealed class Sc { public const string Select = " SELECT "; public const string And = " AND "; public const string From = " FROM "; public const string Where = " WHERE "; public const string Orderby = " ORDER BY "; public const string Delete = " DELETE "; public const string Update = " UPDATE "; public const string Set = " SET "; public const string Eq = " = "; public const string Or = " OR "; public const string Groupby = " GROUP BY "; public const string Like = " LIKE "; } public class UserSession { // Methods #region 权限菜单相关方法 //private Dictionary _PowHas; //private UIAndPowerList _PowerList; //public bool CheckPowerNoErrRedirect(PowerType poType) //{ // UserSession loSession = WebLibs.GetUserSession(); // if (loSession != null) // { // SonicLog.LogInfo(loSession.UserInfo.UserNo + " --- " + WebLibs.CurPageName + " --- " + poType.ToString()); // } // string pcUIID = UtilStr.UAndT(WebLibs.CurPageName); // bool lbRetVal = false; // if (this._PowerList.ContainPageName(pcUIID)) // { // UIAndPower power = this._PowerList.getUIAndPowerByPageName(pcUIID); // switch (poType) // { // case PowerType.IsBrowse: // lbRetVal = power.IsBrowse; // break; // case PowerType.IsNew: // lbRetVal = power.IsAdd; // break; // case PowerType.IsEdit: // lbRetVal = power.IsUpdate; // break; // case PowerType.IsDelete: // lbRetVal = power.IsDelete; // break; // case PowerType.IsAudit: // lbRetVal = power.IsAudit; // break; // case PowerType.IsPrint: // lbRetVal = power.IsPrint; // break; // } // } // return lbRetVal; //} ///// ///// 权限检查 ///// ///// 检查的类型 ///// //public bool CheckPower(PowerType poType) //{ // UserSession loSession = WebLibs.GetUserSession(); // if (loSession != null) // { // SonicLog.LogInfo(loSession.UserInfo.UserNo+ " --- " + WebLibs.CurPageName + " --- " + poType.ToString()); // } // string pcUIID = UtilStr.UAndT(WebLibs.CurPageName); // bool lbRetVal = false; // if (this._PowerList.ContainPageName(pcUIID)) // { // UIAndPower power = this._PowerList.getUIAndPowerByPageName(pcUIID); // switch (poType) // { // case PowerType.IsBrowse: // lbRetVal = power.IsBrowse; // break; // case PowerType.IsNew: // lbRetVal = power.IsAdd; // break; // case PowerType.IsEdit: // lbRetVal = power.IsUpdate; // break; // case PowerType.IsDelete: // lbRetVal = power.IsDelete; // break; // case PowerType.IsAudit: // lbRetVal = power.IsAudit; // break; // case PowerType.IsPrint: // lbRetVal = power.IsPrint; // break; // } // } // else // { // JSComm.ShowMessage("该用户没有相应的权限"); // } // return lbRetVal; //} //public bool CheckPower() //{ // string lcPageName = UtilStr.UAndT(WebLibs.CurPageName); // bool lbRetVal = false; // if (this._PowerList.ContainPageName(lcPageName)) // { // UIAndPower power = this._PowerList.getUIAndPowerByPageName(lcPageName); // if (power.IsBrowse && power.IsAdd && power.IsUpdate && power.IsDelete) // { // lbRetVal = true; // } // else // WebLibs.NotPower(6); // } // else // { // WebLibs.NotPower(7); // } // return lbRetVal; //} //private void CreatePowHasDict(DataTable poDataTable) //{ // if (this._PowerList == null) // this._PowerList = new UIAndPowerList(); // else // this._PowerList.Clear(); // if (poDataTable!=null) // { // try // { // _PowerList.SetData(poDataTable); // } // catch (Exception e) // { // SonicLog.LogException(e); // _ErrorMsg = e.Message; // } // } //} //public void refreshFunctions() //{ // string lcSql = "select * from " + Tn.v_UserHasAllFunctions_New + " where UserNo='" + this._UserInfo.UserNo + "' order by FunctionID "; // snQuery loQuery = DBConn.OpenQuery(lcSql); // if (loQuery != null && loQuery.IsOpened && loQuery.RecCount > 0) // { // CreatePowHasDict(loQuery.CurrentTable); // } // else // { // _ErrorMsg = DBConn.ErrorMsg; // SonicLog.LogError(_ErrorMsg); // } //} #endregion //List _DenyList = null; //private List DenyList //{ // get // { // if (_DenyList == null) // { // string lcSql = " select * from " + Tn.v_UserDenyColumns + " where " + v_UserDenyColumns_info.cUserNo + "='" + _UserInfo.UserNo + "'"; // snQuery loQuery = DBConn.OpenQuery(lcSql); // if (loQuery != null && loQuery.IsOpened && loQuery.RecCount > 0) // { // _DenyList = new List(); // loQuery.MoveFirst(); // for (int i = 0; i < loQuery.RecCount; i++) // { // string lcTCol = loQuery.GetString("TableId") + "." + loQuery.GetString("ColumnId"); // lcTCol = UtilStr.UAndT(lcTCol); // _DenyList.Add(lcTCol); // loQuery.MoveNext(); // } // } // } // return _DenyList; // } //} //public bool ColDeny(string pcTableId, string pcColumnId) //{ // bool lbRetVal = false; // if (DenyList != null) // { // string lcTCol = pcTableId + "." + pcColumnId; // lcTCol = UtilStr.UAndT(lcTCol); // lbRetVal = DenyList.Contains(lcTCol); // } // return lbRetVal; //} #region 基础属性 public string ErrorMsg { get; set; } = ""; //public bool IsAdvance //{ // get { return UtilStr.StrToBool(_UserInfo.IsAdvance); } //} #endregion #region //IUserInfo _userInfos = null; //public IUserInfo UserInfos //{ // get { return _userInfos; } // set { _userInfos = value; } //} //public string GetAuthSql(bool IsPath, bool IsWhere) //{ // string lcRetVal = " 1=1 "; // if (!UtilStr.StrToBool(this.UserInfo.IsSys)) // { // RegionGroups_info loRegion = new RegionGroups_info(this.UserInfo.RegionGroupNo,DBConn); // if (IsPath) // { // lcRetVal = " Path Like '" + loRegion.Path + "%' "; // } // else // { // string lcSql = " select RegionGroupNo from RegionGroups where Path Like '" + loRegion.Path + "%' "; // snQuery loQuery = DBConn.OpenQuery(lcSql); // string lcWhere = ""; // if (loQuery != null && loQuery.IsOpened) // { // loQuery.MoveFirst(); // for (int i = 0; i < loQuery.RecCount; i++) // { // lcWhere += (lcWhere == "" ? "" : ",") + "'" + loQuery.GetString("RegionGroupNo") + "'"; // loQuery.MoveNext(); // } // } // lcRetVal = " RegionGroupNo in (" + lcWhere + ") "; // } // } // if (IsWhere) // { // lcRetVal = " WHERE " + lcRetVal; // } // return lcRetVal; //} //public string GetPersonAuthSql(bool IsPath, bool IsWhere) //{ // string lcRetVal = ""; // if (!UtilStr.StrToBool(this.UserInfo.IsSys)) // { // PersonGroups_info loRegion = new PersonGroups_info(this.UserInfo.PersonGroupNo, DBConn); // if (IsPath) // { // lcRetVal = " Path Like '" + loRegion.Path + "%' "; // } // if (IsWhere) // { // lcRetVal = " WHERE " + lcRetVal; // } // } // return lcRetVal; //} #endregion #region 获得菜单列表的方法 /// /// 得到用户能查看的菜单所有列表的string型 /// //public string UserFunctionIdList //{ // get // { // if (_PowerList != null) // { // return _PowerList.UserFunctionIdList; // } // else // return ""; // } //} //public string GetMenuTable(MenuType poMType) //{ // string lcRetVal = ""; // string lcSql = "select FunctionID,FunctionName,URL,FatherID ,Depth,IsLeaf,Sort from " + Tn.Sys_Functions + " where 1=1 "; // switch (poMType) // { // case MenuType.Back: // lcSql += " and IsBack='Y' "; // break; // case MenuType.Front: // lcSql += " and IsFront='Y' "; // break; // } // lcSql += " and FunctionID in ( " + UserFunctionIdList + " )"; // lcSql += " order by sort asc "; // DataSet loDataSet = DBConn.OpenDataSet(lcSql); // if (loDataSet != null && loDataSet.Tables.Count > 0) // { // lcRetVal = loDataSet.GetXml(); // } // else // { // JSComm.ShowMessage("该用户尚未配置相关权限,请与有关人员联系!"); // System.Web.HttpContext.Current.Response.Write("
"); // System.Web.HttpContext.Current.Response.End(); // } // return lcRetVal; //} //public DataTable GetMenuTableDate(MenuType poMType) //{ // DataTable loRetVal = null; // string lcSql = "select FunctionID,FunctionName,URL,FatherID ,Depth,IsLeaf,Sort from " + Tn.Sys_Functions + " where 1=1 "; // switch (poMType) // { // case MenuType.Back: // lcSql += " and IsBack='Y' "; // break; // case MenuType.Front: // lcSql += " and IsFront='Y' "; // break; // } // lcSql += " and FunctionID in ( " + _PowerList.UserFunctionIdList + " )"; // lcSql += " order by sort asc "; // loRetVal = DBConn.OpenDataTable(lcSql); // return loRetVal; //} #endregion public void Destroy() { if (_objSave != null) { _objSave.Clear(); _objSave = null; } } #region 对象存储 Dictionary _objSave; public void SetObj(string pcKey, object pcValue) { if (_objSave == null) _objSave = new Dictionary(); if (_objSave.ContainsKey(pcKey)) _objSave[pcKey] = pcValue; else _objSave.Add(pcKey, pcValue); } public T GetObj(string pcKey) { return GetObj(pcKey, true); } /// /// 得到对象 /// /// 索引值 /// 是否情况该对象 /// public T GetObj(string pcKey, bool pbClear) { T loRetVal = default(T); if (_objSave != null && _objSave.ContainsKey(pcKey)) { if (_objSave[pcKey] is T) loRetVal = (T)_objSave[pcKey]; if (pbClear) { _objSave.Remove(pcKey); } } return loRetVal; } /// /// 清空指定对象 /// public void ClearObj(string pcKey) { if (_objSave != null) { if (_objSave.ContainsKey(pcKey)) { _objSave.Remove(pcKey); } } } /// /// 清空对象 /// public void ClearObj() { if (_objSave != null) { _objSave.Clear(); _objSave = null; } } #endregion 对象存储 } public enum MenuType { /// /// 前台显示 /// Front = 0, /// /// 后台显示 /// Back = 1, /// /// 显示所有 /// All = 2 } public enum AcountType { /// /// 系统用户 /// System = 0, /// /// 高级用户 /// Advance = 1, /// /// 普通用户 /// Common = 2, /// /// 高级市场用户 /// AdvanceMarket = 3, /// /// 普通市场用户 /// CommonMarket = 4 } }