using FineUIPro; using iWareCommon.Common.EnumType; using iWareDataCore.RBAC.Entity; using iWareDataCore.TASK.EnumType; using iWareLog.LOG.Entity; using iWareLog.LOG.Service; using iWareWms.Code.Common.Service; using System; using System.Collections.Generic; using System.Configuration; using System.Threading; using System.Web; using System.Web.Security; namespace iWareWms { public class PageBase : System.Web.UI.Page { #region 从配置文件中读取的变量 protected readonly string _fileServerBaseAddress = ConfigurationManager.AppSettings["FileServerBaseAddress"].ToString(); protected readonly string _logPath = ConfigurationManager.AppSettings["LogDir"].ToString(); protected readonly string _tokenCookieName = ConfigurationManager.AppSettings["TokenCookieName"].ToString(); protected readonly string _defaultUserLogo = ConfigurationManager.AppSettings["DefaultUserLogo"].ToString(); protected readonly string _screenWidth = ConfigurationManager.AppSettings["ScreenWidth"].ToString(); #endregion #region OnInit protected override void OnInit(EventArgs e) { var pm = PageManager.Instance; if (pm != null) { HttpCookie themeCookie = Request.Cookies["Theme_Pro"]; if (themeCookie != null) { string themeValue = themeCookie.Value; // 是否为内置主题 if (IsSystemTheme(themeValue)) { pm.CustomTheme = String.Empty; pm.Theme = (Theme)Enum.Parse(typeof(Theme), themeValue, true); } else { pm.CustomTheme = themeValue; } } } base.OnInit(e); } private bool IsSystemTheme(string themeName) { themeName = themeName.ToLower(); string[] themes = Enum.GetNames(typeof(Theme)); foreach (string theme in themes) { if (theme.ToLower() == themeName) { return true; } } return false; } #endregion #region 登陆票据 /// /// 创建表单验证的票证并存储在客户端Cookie中 /// /// 当前登录用户名 /// 当前登录用户的角色ID列表 /// 是否跨浏览器会话保存票证 /// 过期时间 protected void CreateFormsAuthenticationTicket(string userName, string roleID, bool isPersistent, DateTime expiration) { // 创建Forms身份验证票据 var ticket = new FormsAuthenticationTicket(1, userName, // 与票证关联的用户名 DateTime.Now, // 票证发出时间 expiration, // 票证过期时间 isPersistent, // 如果票证将存储在持久性 Cookie 中(跨浏览器会话保存),则为 true;否则为 false. roleID // 存储在票证中的用户特定的数据 ); // 对Forms身份验证票据进行加密,然后保存到客户端Cookie中 var cookie = new HttpCookie(FormsAuthentication.FormsCookieName, FormsAuthentication.Encrypt(ticket)); cookie.HttpOnly = true; cookie.Expires = isPersistent ? expiration : DateTime.MinValue; Response.Cookies.Add(cookie); } #endregion #region 实用函数 /// /// 获取回发的参数 /// /// public string GetRequestEventArgument() { return Request.Form["__EVENTARGUMENT"]; } /// /// 显示通知对话框 /// /// public virtual void ShowNotify(string message) { ShowNotify(message, MessageBoxIcon.Information); } /// /// 显示通知对话框 /// /// /// public virtual void ShowNotify(string message, MessageBoxIcon messageIcon) { Notify n = new Notify(); n.Target = Target.Top; n.Message = message; n.MessageBoxIcon = messageIcon; n.PositionX = Position.Center; n.PositionY = Position.Top; n.DisplayMilliseconds = 3000; n.ShowHeader = false; n.Show(); } #endregion #region Gird相关 /// /// 获取行id /// /// /// protected int GetSelectedDataKeyID(Grid grid) { int id = -1; int rowIndex = grid.SelectedRowIndex; if (rowIndex >= 0) { id = Convert.ToInt32(grid.DataKeys[rowIndex][0]); } return id; } /// /// 获取表格选中项DataKeys的第一个值,并转化为整型列表 /// /// /// protected List GetSelectedDataKeyIDs(Grid grid) { List ids = new List(); foreach (int rowIndex in grid.SelectedRowIndexArray) { ids.Add(Convert.ToInt32(grid.DataKeys[rowIndex][0])); } return ids; } /// /// 获取表格选中项DataKeys的第四个值 /// /// /// protected List GetSelectedDataKeys(Grid grid, int index) { var names = new List(); foreach (int rowIndex in grid.SelectedRowIndexArray) { names.Add(grid.DataKeys[rowIndex][index].ToString()); } return names; } #endregion #region 登录用户信息 /// /// 当前登录用户名 /// /// protected string GetIdentityName() { var s = User.Identity; if (User.Identity.IsAuthenticated) { return User.Identity.Name; } return String.Empty; } /// /// 从session中获取用户信息 /// /// protected UserEntity GetLoginPerson() { //将用户拥有的权限列表保存在Session中,这样就避免每个请求多次查询数据库 if (Session["UserInfo"] == null) { Response.Redirect("~/login.aspx"); return null; } return (UserEntity)Session["UserInfo"]; } #endregion #region 提示框 /// /// 删除提示框 /// /// /// protected void ResolveDeleteButtonForGrid(FineUIPro.Button btn, Grid grid) { ResolveDeleteButtonForGrid(btn, grid, "确定要删除选中的{0}项记录吗?"); } protected void ResolveDeleteButtonForGrid(FineUIPro.Button btn, Grid grid, string confirmTemplate) { ResolveDeleteButtonForGrid(btn, grid, "请至少应该选择一项记录!", confirmTemplate); } protected void ResolveDeleteButtonForGrid(FineUIPro.Button btn, Grid grid, string noSelectionMessage, string confirmTemplate) { // 点击删除按钮时,至少选中一项 btn.OnClientClick = grid.GetNoSelectionAlertInParentReference(noSelectionMessage); btn.ConfirmText = String.Format(confirmTemplate, "  "); btn.ConfirmTarget = Target.Top; } /// /// 禁用 启用提示框 /// /// /// /// protected virtual void ResolveEnableStatusButtonForGrid(MenuButton btn, Grid grid, bool enabled) { var enabledStr = enabled ? EStatus.启用.ToString() : EStatus.禁用.ToString(); btn.OnClientClick = grid.GetNoSelectionAlertInParentReference("请至少应该选择一项记录!"); btn.ConfirmText = String.Format("确定要{1}选中的项记录吗?", grid.GetSelectedCountReference(), enabledStr); btn.ConfirmTarget = FineUIPro.Target.Top; } /// /// 设置优先级提示框 /// /// /// /// protected virtual void ResolveEnablePriorityButtonForGrid(MenuButton btn, Grid grid, int enabled) { var enabledStr = EMainTaskPriority.一般.ToString(); if(enabled == 0){ enabledStr = EMainTaskPriority.一般.ToString(); }else if(enabled == 1){ enabledStr = EMainTaskPriority.优先.ToString(); } else { enabledStr = EMainTaskPriority.加急.ToString(); } btn.OnClientClick = grid.GetNoSelectionAlertInParentReference("请至少应该选择一项记录!"); btn.ConfirmText = String.Format("确定要{1}选中的项记录吗?", grid.GetSelectedCountReference(), enabledStr); btn.ConfirmTarget = FineUIPro.Target.Top; } #endregion #region 前台页面常用--Added by ZHANGZhan /// /// 不分页的查询 /// public virtual void Query() { var grid = GetGrid(); if (grid == null) { return; } string msg; var ds = GetDataSource(out msg); grid.DataSource = ds; grid.DataBind(); } /// /// 根据页数查询数据 /// /// 需要查询的页面 protected virtual void Query(int pageIndex) { var grid = GetGrid(); var ddl = GetPageSizeDropDownList(); if (grid == null) { return; } if (ddl == null) { return; } int pageSize = Convert.ToInt32(ddl.SelectedValue); string msg; int totalNum, currentPage; var ds = GetDataSource(pageIndex, pageSize, out msg, out totalNum, out currentPage); grid.PageSize = pageSize; grid.RecordCount = totalNum; grid.PageIndex = currentPage - 1; grid.DataSource = ds; grid.DataBind(); } /// /// 点击搜索触发的函数 /// /// /// protected virtual void Search(object sender, EventArgs e) { if (GetGrid() != null) { Query(1); } } /// /// 每页记录数选择改变出发事件 /// /// /// protected virtual void PageSizeSelectedIndexChanged(object sender, EventArgs e) { if (GetGrid() != null) { Query(GetGrid().PageIndex + 1); } } /// /// 页面改变是时触发的事件 /// /// /// protected virtual void GridPageIndexChange(object sender, EventArgs e) { if (GetGrid() != null) { Query(GetGrid().PageIndex + 1); } } /// /// 数据绑定——角色单个删除 /// /// /// protected virtual void GridRowCommand(object sender, FineUIPro.GridCommandEventArgs e) { var grid = GetGrid(); if (grid == null) { return; } if (e.CommandName == "Delete") { string msg; Delete(Convert.ToInt32((grid.DataKeys[e.RowIndex][0].ToString())), out msg); Alert.ShowInTop(string.IsNullOrEmpty(msg) ? "删除成功" : msg); if (string.IsNullOrEmpty(msg)) { Query(grid.PageIndex + 1); } } } /// /// 批量删除 /// /// /// protected virtual void Delete(object sender, EventArgs e) { var grid = GetGrid(); if (grid == null) { return; } // 从每个选中的行中获取ID(在Grid1中定义的DataKeyNames) List ids = GetSelectedDataKeyIDs(grid); string msg; Delete(ids, out msg); Alert.ShowInTop(string.IsNullOrEmpty(msg) ? "删除成功" : msg); if (string.IsNullOrEmpty(msg)) { Query(grid.PageIndex + 1); } } /// /// 保存并关闭按钮 /// /// /// protected virtual void SaveCloseClick(object sender, EventArgs e) { string msg; if (!string.IsNullOrEmpty(Request.QueryString["IsEdit"])) { Update(out msg); Alert.ShowInTop(string.IsNullOrEmpty(msg) ? "修改成功" : msg); } else { Save(out msg); Alert.ShowInTop(string.IsNullOrEmpty(msg) ? "添加成功" : msg); } if (string.IsNullOrEmpty(msg)) { PageContext.RegisterStartupScript(ActiveWindow.GetHidePostBackReference()); } } /// /// 编辑或新增按钮关闭 /// /// /// protected virtual void WindowClose(object sender, EventArgs e) { if (GetGrid() != null) { Query(GetGrid().PageIndex + 1); } } /// /// 更新缓存 /// /// /// protected virtual void RefreshClick(object sender, EventArgs e) { string msg; CacheService.GetInstance().RefreshCache(out msg); Alert.ShowInTop(string.IsNullOrEmpty(msg) ? "更新成功" : msg); } /// /// 批量修改状态为启用 /// /// /// protected virtual void EnableStatusClick(object sender, EventArgs e) { ChangeStatus((int)EStatus.启用); } /// /// 批量修改状态为锁定 /// /// /// protected virtual void DisableStatusClick(object sender, EventArgs e) { ChangeStatus((int)EStatus.禁用); } private void ChangeStatus(int enable) { var grid = GetGrid(); if (grid == null) { return; } string msg = ""; ChangeStatus(GetSelectedDataKeyIDs(grid), enable, out msg); Alert.ShowInTop(string.IsNullOrEmpty(msg) ? "修改成功" : msg); if (string.IsNullOrEmpty(msg)) { Query(grid.PageIndex + 1); } } /// /// 批量修改优先级为一般 /// /// /// protected virtual void EnablePriorityClick1(object sender, EventArgs e) { ChangePriority((int)EMainTaskPriority.一般); } /// /// 批量修改优先级为优先 /// /// /// protected virtual void EnablePriorityClick2(object sender, EventArgs e) { ChangePriority((int)EMainTaskPriority.优先); } /// /// 批量修改优先级为加急 /// /// /// protected virtual void EnablePriorityClick3(object sender, EventArgs e) { ChangePriority((int)EMainTaskPriority.加急); } private void ChangePriority(int enable) { var grid = GetGrid(); if (grid == null) { return; } string msg = ""; ChangePriority(GetSelectedDataKeyIDs(grid), enable, out msg); Alert.ShowInTop(string.IsNullOrEmpty(msg) ? "修改成功" : msg); if (string.IsNullOrEmpty(msg)) { Query(grid.PageIndex + 1); } } /// /// 导出到Exxcel /// /// /// protected virtual void ExportExcel(object sender, EventArgs e) { if (GetWindow() == null) { return; } //var userName = ((UserEntity)Session["UserInfo"]).Username; //CacheEntity.RedisWcfServiceClient.SetValue(userName + "@ExcelStop", (int)YesOrNo.否); //CacheEntity.RedisWcfServiceClient.SetValue(userName + "@ExcelPath", ""); Session["ExcelStop"] = (int)EYesOrNo.否; Session["ExcelPath"] = ""; PageContext.RegisterStartupScript(GetWindow().GetShowReference("~/View/LOADING/Loading/Loading.aspx")); new Thread(new ParameterizedThreadStart(GetPath)).Start(""); if (GetTimer() != null) { GetTimer().Enabled = true; } } protected virtual void ExportTmpExcel(object sender, EventArgs e) { if (GetWindow() == null) { return; } //var userName = ((UserEntity)Session["UserInfo"]).Username; //CacheEntity.RedisWcfServiceClient.SetValue(userName + "@ExcelStop", (int)YesOrNo.否); //CacheEntity.RedisWcfServiceClient.SetValue(userName + "@ExcelPath", ""); Session["ExcelStop"] = (int)EYesOrNo.否; Session["ExcelPath"] = ""; PageContext.RegisterStartupScript(GetWindow().GetShowReference("~/NonAuth/Loading.aspx")); new Thread(new ParameterizedThreadStart(GetTmpPath)).Start("userName"); if (GetTimer() != null) { GetTimer().Enabled = true; } } protected void ExcelTick(object sender, EventArgs e) { if (GetTimer() == null) { return; } if (!GetTimer().Enabled) { return; } // var userName = ((UserEntity)Session["UserInfo"]).Username; //var flag = int.Parse(CacheEntity.RedisWcfServiceClient.GetValue(userName + "@ExcelStop").ToString()); var flag = Session["ExcelStop"] == null ? (int)EYesOrNo.否 : int.Parse(Session["ExcelStop"].ToString()); if (flag == (int)EYesOrNo.否) { return; } GetTimer().Enabled = false; Session["ExcelStop"] = (int)EYesOrNo.否; //CacheEntity.RedisWcfServiceClient.SetValue(userName + "@ExcelStop", (int)YesOrNo.否); PageContext.RegisterStartupScript(GetWindow().GetHideReference()); } #endregion #region 需要重写的函数--Added by ZHANGZhan /// /// 获取页面绑定的grid /// /// protected virtual Window GetWindow() { return null; } protected virtual void GetPath(object userName) { } protected virtual void GetTmpPath(object userName) { } protected virtual FineUIPro.Timer GetTimer() { return null; } /// /// 获取页面绑定的grid /// /// protected virtual Grid GetGrid() { return null; } /// /// 获取分页dropdownlist控件 /// protected virtual DropDownList GetPageSizeDropDownList() { return null; } /// /// 获取数据元 /// /// 数据元 protected virtual List GetDataSource(int pageIndex, int pageSize, out string msg, out int totalNum, out int currentPage) { msg = ""; totalNum = 0; currentPage = 1; return null; } /// /// 获取数据元 /// /// 数据元 protected virtual List GetDataSource(out string msg) { msg = ""; return null; } /// /// 删除单个元素 /// /// 需要删除元素的id /// 异常错误消息 protected virtual void Delete(int id, out string msg) { msg = ""; } /// /// 删除多个元素 /// /// 需要删除元素元素 /// 异常错误消息 protected virtual void Delete(List ids, out string msg) { msg = ""; } /// /// 保存元素 /// /// /// protected virtual void Save(out string msg) { msg = ""; } /// /// 修改元素 /// /// /// protected virtual void Update(out string msg) { msg = ""; } protected virtual void ChangeStatus(List ids, int enable, out string msg) { msg = ""; } protected virtual void ChangePriority(List ids, int enable, out string msg) { msg = ""; } #endregion #region 操作日志 public void WriteLog(string txt,string page) { var person = GetLoginPerson(); OperationEntity dataLog = new OperationEntity() { WbUserName = person.Name, WbLogacCount = person.WorkNo, WbUserIpaddress = Request.UserHostAddress, WbLogType = 1, WbLogTxt = txt, WbLogDate = DateTime.Now, Log_backup1 = page }; OperationService.GetInstance().Save(dataLog); } #endregion #region 上传文件类型判断 protected readonly static List VALID_FILE_TYPES = new List { "jpg", "bmp", "gif", "jpeg", "png" }; protected static bool ValidateFileType(string fileName) { string fileType = String.Empty; int lastDotIndex = fileName.LastIndexOf("."); if (lastDotIndex >= 0) { fileType = fileName.Substring(lastDotIndex + 1).ToLower(); } if (VALID_FILE_TYPES.Contains(fileType)) { return true; } else { return false; } } #endregion } }