using System; using System.Data.Entity.Validation; using System.Linq; using System.Web; using System.Web.Mvc; using System.Web.Security; using WX.CRM.Common; using WX.CRM.WebHelper; using WX.CRM.WebHelper.UtilityModel; namespace WX.CRM.WEB.Controllers { public class BaseController : Controller { private UserInfo _userinfo; private string[] _rights; private bool needToRedirect; private bool rightRedirct = false; public BaseController() { _rights = UserRightsHelper.getUserRights(); if (_rights.Contains("-1")) { //LogHelper.Info(_rights.ToJson()); rightRedirct = true; } if (System.Web.HttpContext.Current.Request.IsAuthenticated) { FormsIdentity id = (FormsIdentity)System.Web.HttpContext.Current.User.Identity; string userinfo_str = id.Ticket.UserData; //_userinfo = JsonHelper.JsonDivertToObj(userinfo_str); //needToRedirect = false; try { _userinfo = JsonHelper.JsonDivertToObj(userinfo_str); needToRedirect = false; } catch (Exception e) { LogHelper.Error(e); needToRedirect = true; } } else { _userinfo = new UserInfo(); needToRedirect = true; //Redirect("~/Base/Account/LogOn"); } } protected override void OnActionExecuting(ActionExecutingContext filterContext) { if (this.needToRedirect || rightRedirct) { FormsAuthentication.SignOut(); //if (filterContext.HttpContext.Request.IsAjaxRequest()) //{ // filterContext.HttpContext.Response.StatusCode = 401;//这个可以指定为其他的 // filterContext.Result = new JsonResult // { // //Data = new // //{ // // ErrorMessage = "您长时间没有操作,请重新登录!" // //}, //这样使用,最终的结果判断时,xhr.responseText为"{ErrorMessage:"您长时间没有操作,请重新登录!"}",还需要Json转化一下 // Data = "您长时间没有操作,请重新登录!", // JsonRequestBehavior = JsonRequestBehavior.AllowGet // }; // filterContext.HttpContext.Response.ContentType = ""; // filterContext.HttpContext.Response.End(); //} //else //{ string RedirectLocation = string.Format("~/Base/Account/LogOn?ReturnUrl={0}", HttpUtility.UrlEncode(filterContext.HttpContext.Request.RawUrl)); filterContext.Result = new RedirectResult(RedirectLocation); return; //} } } protected override void OnException(ExceptionContext filterContext) { string controllerName = filterContext.RouteData.Values["controller"].ToString(); string actionName = filterContext.RouteData.Values["action"].ToString(); string msg = string.Concat(controllerName, "-", actionName, ";"); var dbEx = filterContext.Exception as DbEntityValidationException; if (dbEx != null) { foreach (var validationErrors in dbEx.EntityValidationErrors) { msg += validationErrors.Entry.Entity.ToString(); foreach (var validationError in validationErrors.ValidationErrors) { msg += string.Format("。Property:{0} Error:{1}", validationError.PropertyName, validationError.ErrorMessage) + Environment.NewLine; } } } else { msg += filterContext.Exception.ToString() + ";" + filterContext.Exception.StackTrace; } LogHelper.Error(msg); if (filterContext.RequestContext.HttpContext.Request.IsAjaxRequest()) { filterContext.HttpContext.Response.StatusCode = 200; filterContext.ExceptionHandled = true; filterContext.Result = new JsonResult { ContentType = "text/html", Data = new { type = 0, message = "系统错误:" + filterContext.Exception.Message, errorMessag = "系统错误:" + filterContext.Exception.Message }, JsonRequestBehavior = JsonRequestBehavior.AllowGet }; } else { //filterContext.ExceptionHandled = true; //string msg = string.Concat(controllerName, "_", actionName, ";", filterContext.Exception.Message); //filterContext.Result = new RedirectResult(Url.Action("ErrorView", "Error", new { message = msg })); // filterContext.Result = new PartialViewResult("/Bas/Error/ErrorView", new { message = msg}); base.OnException(filterContext); } // JsonHandler.ExceptionMessage(filterContext.Exception.Message); } protected override JsonResult Json(object data, string contentType, System.Text.Encoding contentEncoding, JsonRequestBehavior behavior) { return new ConfigurableJsonResult { Data = data, ContentType = contentType, ContentEncoding = contentEncoding, JsonRequestBehavior = behavior }; } public bool IsLogin { get { if (System.Web.HttpContext.Current.Request.IsAuthenticated) return true; else return false; } } public string UserName { get { return _userinfo.userName; } } /// /// 员工工号 /// public decimal Eid { get { return _userinfo.userEid; } } /// /// 员工Id /// public decimal UserId { get { return _userinfo.userId; } } /// /// 公司id /// public string FJH { get { return _userinfo.FJH; } } /// /// 员工组别id /// public decimal userGroupId { get { return _userinfo.userGroupId; } } /// /// 部门id /// public decimal deptId { get { return _userinfo.deptId; } } /// /// 营业部id /// public decimal saleDeptId { get { return _userinfo.saleDeptId; } } /// /// 营业部编码 /// public string saleDeptCode { get { return _userinfo.saleDeptCode; } } /// /// 公司id /// public decimal companyId { get { return _userinfo.companyId; } } public string companyCode { get { if (string.IsNullOrEmpty(_userinfo.companyCode)) return System.Configuration.ConfigurationManager.AppSettings["DataClientCode"]; else return _userinfo.companyCode; } } /// /// 业务名称字符串用"[]"隔开 /// public string companyBusiness { get { return _userinfo.companyBusiness; } } /// /// 员工所管理的组id /// public decimal[] userOnGroupId { get { return _userinfo.userOnGroupId; } } /// /// 员工角色id /// public decimal[] userRoleId { get { return _userinfo.userRoleId; } } /// /// 员工角色id /// public string[] userRoleNames { get { return _userinfo.userRoleName; } } public decimal LoginLogId { get { return _userinfo.logInLogID; } } /// /// 员工权限id /// public string[] userRightId { get { return _rights; } } /// /// 客服类型,1客户经理、2高级客服、3客服 /// /// /// protected string GetKFType(int userPurview) { string kftype = ""; int purviewValue = (int)Math.Pow(2, 1); if ((userPurview & purviewValue) == purviewValue) kftype += "、客服经理"; purviewValue = (int)Math.Pow(2, 2); if ((userPurview & purviewValue) == purviewValue) kftype += "、高级客服"; purviewValue = (int)Math.Pow(2, 3); if ((userPurview & purviewValue) == purviewValue) kftype += "、客服"; if (kftype != "") { kftype = kftype.Substring(1); } return kftype; } public string userRoleCodes { get { return DataCacheHelper.GetCache().Get_RoleCodes(userRoleId); } } public string userCurChannel { get { return DataCacheHelper.GetCache().GetCurChannel(UserId); } } public string Level(string currentRight = null) { var level = "Self"; if (userRoleCodes.IndexOf("[GLY]") > -1 || userRoleCodes.IndexOf("[LOOKALL]") > -1)//LOOKALL作为扩展权限 { level = "All"; } else if (userRoleCodes.IndexOf("[FZJ]") > -1 || userRoleCodes.IndexOf("[ZJ]") > -1 || userRoleCodes.IndexOf("[ZJZL]") > -1 || userRoleCodes.IndexOf("[ZJZG]") > -1 || userRoleCodes.IndexOf("[CW]") > -1)//副总监 { level = "Company"; } else if (userRoleCodes.IndexOf("[BMJL]") > -1 || userRoleCodes.IndexOf("[BMZG]") > -1)//部门经理 { level = "Dept"; } else if (userRoleCodes.IndexOf("[KFZG]") > -1 || userRoleCodes.IndexOf("[GJSGJKF]") > -1 || userRoleCodes.IndexOf("[SHZG]") > -1 || userRoleCodes.IndexOf("[ZJZG]") > -1)//销售组主管 { level = "Group"; } //特权 if (!string.IsNullOrEmpty(currentRight) && level != "All" && level != "Company") { string[] nowroles = userRoleCodes.Replace("][", ",").Replace("]", "").Replace("[", "").Split(','); var exists = DataCacheHelper.GetCache().GetRightLevel().Any(m => m.RIGHTID == currentRight && nowroles.Contains(m.ROLECODE) && m.STATUS == 1); if (exists) { level = "Privilege"; } } return level; } /// /// 对接SSO后的数据权限等级查看 /// /// /// public string LevelSSO(string currentRight = null) { var level = "Self"; if (userRoleCodes.IndexOf("[GLY]") > -1 || userRoleCodes.IndexOf("[LOOKALL]") > -1)//LOOKALL作为扩展权限 { level = "All"; } //else if (userRoleCodes.IndexOf("[FZJ]") > -1 || userRoleCodes.IndexOf("[ZJ]") > -1 || userRoleCodes.IndexOf("[ZJZL]") > -1 || userRoleCodes.IndexOf("[ZJZG]") > -1 || userRoleCodes.IndexOf("[CW]") > -1)//副总监 //{ // level = "Company"; //} else if (userOnGroupId != null && userOnGroupId.Count() > 0)//管理了部门或者组 { level = "Dept"; if (userOnGroupId.Contains(0))//如果有管理0,那么将进行显示全部 { level = "All"; } } //else if (userRoleCodes.IndexOf("[KFZG]") > -1 || userRoleCodes.IndexOf("[GJSGJKF]") > -1 || userRoleCodes.IndexOf("[SHZG]") > -1 || userRoleCodes.IndexOf("[ZJZG]") > -1)//销售组主管 //{ // level = "Group"; //} //特权 if (!string.IsNullOrEmpty(currentRight))//以特殊权限为主 //if (!string.IsNullOrEmpty(currentRight) && level != "All" && level != "Company") { //string[] nowroles = userRoleCodes.Replace("][", ",").Replace("]", "").Replace("[", "").Split(','); //var exists = DataCacheHelper.GetCache().GetRightLevel().Where(m => m.RIGHTID == currentRight && nowroles.Contains(m.ROLECODE) && m.STATUS == 1).ToList(); var nowroles = userRoleCodes.Replace("][", ",").Replace("]", "").Replace("[", "").Split(','); var list = DataCacheHelper.GetCache().GetRightLevel().Where(m => m.RIGHTID == currentRight && nowroles.Contains(m.ROLECODE) && m.STATUS == 1).ToList(); if (list.Count > 0)//发现特权 { level = "Privilege"; var levelid = list.Select(m => m.PKID).ToArray(); var detaillist = DataCacheHelper.GetCache().GetRightLevelDetail().Where(m => levelid.Contains(m.LEVELID) && m.CID == 0).ToList();//特权页面勾选了0,那么可以查看全部 if (detaillist.Count > 0)//特权有详细内容,才能够执行特权 { level = "All"; } } } return level; } } }