using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Security;
using Common;
using System.Data;
using BLL;
namespace HOSYS
{
public class BaseController : Controller
{
/// <summary>
/// 當前請求的使用者物件
/// 將會在Action被執行前初始化
/// </summary>
public BLL.user userModel;
/// <summary>
/// 清單頁面使用的強類型
/// 需要賦值
/// </summary>
public Common.ViewListPageModel listPageModel;
/// <summary>
/// 指示頁面是否需要載入預設的前20筆, 為3時 不載入
/// </summary>
public static bool LoadDefaultData = BLL.CommonFunc.GetLoadDefaultData();
public BaseController()
{
listPageModel = new Common.ViewListPageModel();
ViewBag.PageSize = Int32.Parse(System.Configuration.ConfigurationManager.AppSettings["PageSize"].ToString());
listPageModel.PageSize = ViewBag.PageSize;
listPageModel.CountForNull = (int)ViewBag.PageSize;
}
/// <summary>
/// Action 執行前
/// </summary>
/// <param name="filterContext"></param>
protected override void OnActionExecuting(ActionExecutingContext filterContext)
{
//一般頁面標題
ViewBag.Title = "Resources.Resource.ZTTitle";
//ViewBag.ActionNo = Request.RequestContext.RouteData.Values["action"];
//ViewBag.Controller = Request.RequestContext.RouteData.Values["controller"];
listPageModel.ActionNo = Request.RequestContext.RouteData.Values["action"].ToString();
listPageModel.Controller = Request.RequestContext.RouteData.Values["controller"].ToString();
#region 登錄判斷
//if (Request.IsAuthenticated)
//{
// if (Session["UserModel"] == null)
// {
// string cookieName = FormsAuthentication.FormsCookieName;
// HttpCookie authCookie = Request.Cookies[cookieName];
// FormsAuthenticationTicket authTicket = null;
// try
// {
// authTicket = FormsAuthentication.Decrypt(authCookie.Value);
// }
// catch
// {
// FormsAuthentication.SignOut();
// filterContext.Result = RedirectToRoute("Default", new { Controller = "Login", Action = "LoginOut" });
// }
// Session["ConStr"] = System.Configuration.ConfigurationManager.ConnectionStrings[2].ConnectionString;
// userModel = BLL.SYS100.GetModel(new Guid(authTicket.UserData.Split(',')[0]));
// userModel.DelegationUserID = new Guid(authTicket.UserData.Split(',')[1]);
// if (userModel.Account_ID != userModel.DelegationUserID)
// {
// BLL.SYS100 s1 = BLL.SYS100.GetModel(userModel.DelegationUserID);
// userModel.Account_Out_Name = s1.Account_Out_Name;
// }
// Session["UserModel"] = userModel;
// Session["UserRole"] = BLL.SYS100.GetMenuPermissions(new Guid(authTicket.UserData.Split(',')[0]));
// }
// else
// {
// userModel = Session["UserModel"] as BLL.SYS100;
// }
// #region 功能許可權賦值
// List<BLL.SYS020> list = Session["UserRole"] as List<BLL.SYS020>;
// BLL.SYS010 s010 = null;
// foreach (var item in list)
// {
// s010 = BLL.SYS010.GetModel(item.Function_Id.GetValueOrDefault());
// if (s010.Function_Control == listPageModel.Controller && s010.Function_No == listPageModel.ActionNo)
// {
// listPageModel.Excute = false;
// listPageModel.Add = false;
// listPageModel.Edit = false;
// listPageModel.Delete = false;
// listPageModel.Copy = false;
// listPageModel.Export = false;
// listPageModel.Print = false;
// listPageModel.Explain = false;
// listPageModel.Excute = item.Excute;
// listPageModel.Add = item.Add;
// listPageModel.Edit = item.Edit;
// listPageModel.Delete = item.Delete;
// listPageModel.Copy = item.Copy;
// listPageModel.Export = item.Export;
// listPageModel.Print = item.Print;
// listPageModel.Explain = item.Explain;
// Session["listPageModel"] = this.listPageModel;
// break;
// }
// }
// #endregion
//}
//else
//{
// filterContext.Result = RedirectToRoute("Default", new { Controller = "Login", Action = "LoginOut" });
//}
#endregion
base.OnActionExecuting(filterContext);
}
/// <summary>
/// Action 執行後
/// </summary>
/// <param name="filterContext"></param>
protected override void OnActionExecuted(ActionExecutedContext filterContext)
{
base.OnActionExecuted(filterContext);
}
/// <summary>
/// 異常處理
/// </summary>
/// <param name="filterContext"></param>
protected override void OnException(ExceptionContext filterContext)
{
base.OnException(filterContext);
#if DEBUG
#else
BLL.SYS800 s = new BLL.SYS800();
if (userModel == null)
s.Account_Id = null;
else
s.Account_Id = userModel.Account_ID;
s.StackTrace = filterContext.Exception.StackTrace;
s.Message = filterContext.Exception.Message ;
if (filterContext.Exception.InnerException != null)
{
s.Message += filterContext.Exception.InnerException.Message;
}
s.ActionNo = Request.RequestContext.RouteData.Values["controller"] + "/" + Request.RequestContext.RouteData.Values["action"];
s.updateDate = DateTime.Now;
BLL.SYS800.AddModel(s);
filterContext.ExceptionHandled = true;
filterContext.Result = new RedirectResult(Url.Action("Error", "Login"));
#endif
}
/// <summary>
/// 在執行View前觸發事件
/// </summary>
/// <param name="filterContext"></param>
protected override void OnResultExecuting(ResultExecutingContext filterContext)
{
base.OnResultExecuting(filterContext);
//清單頁面使用的強類型
ViewData["listPageModel"] = listPageModel;
//當前請求的使用者物件
ViewData["userModel"] = userModel;
//如果Action 長度為6 就一定為清單 頁面 則 插入cookie資訊 查詢準則 頁碼
if (Request.RequestContext.RouteData.Values["action"].ToString().Length == 6
|| Request.RequestContext.RouteData.Values["action"].ToString().Length == 7)
{
if (listPageModel.ParamList != null)
{
filterContext.HttpContext.Response.Cookies.Add(new HttpCookie(listPageModel.Controller.Replace("/", "") + listPageModel.ActionNo + "page", listPageModel.PageIndex.ToString()));
filterContext.HttpContext.Response.Cookies.Add(new HttpCookie(listPageModel.Controller.Replace("/", "") + listPageModel.ActionNo + "p", listPageModel.ParamList == null ? "" : listPageModel.ParamList));
filterContext.HttpContext.Response.Cookies.Add(new HttpCookie(listPageModel.Controller.Replace("/", "") + listPageModel.ActionNo + "DynamicSearchparas", listPageModel.DynamicSearchparas == null ? "" : listPageModel.DynamicSearchparas));
}
}
}
/// <summary>
/// 參數字串 使用符號 | 分割。為頁面的其他get事件準備 (請注意順序)
/// </summary>
public void ResultParas()
{
//參數字串 使用符號 | 分割。為頁面的其他get事件準備 (請注意順序)
listPageModel.ParamList =
listPageModel.DynamicSearchparas + "|" +
listPageModel.hidStatus
+ "|" + listPageModel.SortStr
+ "|" + listPageModel.SortAscDesc;
}
/// <summary>
///
/// </summary>
/// <param name="page"></param>
/// <param name="DynamicSearchparas"></param>
/// <param name="p"></param>
public void LoadParas(int page, string DynamicSearchparas, string p)
{
p = Server.UrlDecode(p);
//當前頁
listPageModel.PageIndex = page;
//查詢區塊是 隱藏或顯示
listPageModel.hidStatus = Request["hidSearchAllStatus"] == null ? "2" : Request["hidSearchAllStatus"];
if (Request["hidSearchAllStatus"] == null)
{
if (p != null)
if (p.Split('|').Length > 1)
listPageModel.hidStatus = p.Split('|')[p.Split('|').Length - 3];
}
//查看排序字段
listPageModel.SortStr = Request["SortStr"] == null ? "" : Request["SortStr"].ToString();
if (Request["SortStr"] == null)
{
if (p != null)
if (p.Split('|').Length > 1)
listPageModel.SortStr = p.Split('|')[p.Split('|').Length - 2];
}
//需要排序的類別,正序或倒序
listPageModel.SortAscDesc = Request["SortAscDesc"] == null ? Common.TabelSortTypeEnum.ASC : (Common.TabelSortTypeEnum)Enum.Parse(typeof(Common.TabelSortTypeEnum), Request["SortAscDesc"].ToString(), false);
if (Request["SortAscDesc"] == null)
{
if (p != null)
if (p.Split('|').Length > 1)
listPageModel.SortAscDesc = (Common.TabelSortTypeEnum)Enum.Parse(typeof(Common.TabelSortTypeEnum), p.Split('|')[p.Split('|').Length - 1], false);
}
listPageModel.DynamicSearchparas = DynamicSearchparas;
if (String.IsNullOrEmpty(listPageModel.DynamicSearchparas))
{
if (p != null)
if (p.Split('|').Length > 1)
listPageModel.DynamicSearchparas = Server.UrlEncode(p.Split('|')[0]);
}
}
}
}