ComplianceServer/oldcode/WEB/Controllers/BaseController.cs

411 lines
14 KiB
C#
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

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.DAL.Redis;
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>(userinfo_str);
//needToRedirect = false;
try
{
_userinfo = JsonHelper.JsonDivertToObj<UserInfo>(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 (filterContext.HttpContext.Request.QueryString["mytoken"] != null)
{
var mytokenKey = filterContext.HttpContext.Request.QueryString["mytoken"];
RedisString<string> rdb = new RedisString<string>();
var encTicket = rdb.Get(mytokenKey);
this.Response.Cookies.Add(new HttpCookie(FormsAuthentication.FormsCookieName, encTicket));
var userStr = Utility.Decrypt(encTicket);
_userinfo = JsonHelper.JsonDivertToObj<UserInfo>(userStr);
needToRedirect = false;
return;
}
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}", 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;
}
}
/// <summary>
/// 员工工号
/// </summary>
public decimal Eid
{
get
{
return _userinfo.userEid;
}
}
/// <summary>
/// 员工Id
/// </summary>
public decimal UserId
{
get
{
return _userinfo.userId;
}
}
/// <summary>
/// 公司id
/// </summary>
public string FJH
{
get { return _userinfo.FJH; }
}
/// <summary>
/// 员工组别id
/// </summary>
public decimal userGroupId
{
get
{
return _userinfo.userGroupId;
}
}
/// <summary>
/// 部门id
/// </summary>
public decimal deptId
{
get
{
return _userinfo.deptId;
}
}
/// <summary>
/// 营业部id
/// </summary>
public decimal saleDeptId
{
get { return _userinfo.saleDeptId; }
}
/// <summary>
/// 营业部编码
/// </summary>
public string saleDeptCode
{
get { return _userinfo.saleDeptCode; }
}
/// <summary>
/// 公司id
/// </summary>
public decimal companyId
{
get { return _userinfo.companyId; }
}
public string companyCode
{
get { return _userinfo.companyCode; }
}
/// <summary>
/// 业务名称字符串用"[]"隔开
/// </summary>
public string companyBusiness
{
get { return _userinfo.companyBusiness; }
}
/// <summary>
/// 员工所管理的组id
/// </summary>
public decimal[] userOnGroupId
{
get { return _userinfo.userOnGroupId; }
}
/// <summary>
/// 员工角色id
/// </summary>
public decimal[] userRoleId
{
get { return _userinfo.userRoleId; }
}
/// <summary>
/// 员工角色id
/// </summary>
public string[] userRoleNames
{
get { return _userinfo.userRoleName; }
}
public decimal LoginLogId
{
get { return _userinfo.logInLogID; }
}
/// <summary>
/// 员工权限id
/// </summary>
public string[] userRightId
{
get
{
return _rights;
}
}
/// <summary>
/// 客服类型1客户经理、2高级客服、3客服
/// </summary>
/// <param name="userPurview"></param>
/// <returns></returns>
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 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;
}
/// <summary>
/// 对接SSO后的数据权限等级查看
/// </summary>
/// <param name="currentRight"></param>
/// <returns></returns>
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) && 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;
}
}
}