Files

165 lines
7.2 KiB
C#
Raw Permalink Normal View History

2025-11-20 09:50:21 +08:00
using Models.View;
using System;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Services.Manager;
using Services.Cache;
using Newtonsoft.Json;
using Services.Tool;
using Models;
using System.IO.Compression;
namespace UI.Controllers
{
public class BaseController : Controller
{
public View_UserInfo Umodel { get; set; }
public object ActPacket { get; set; }
public string ActMessage { get; set; }
public string ActType { get; set; }
public int ActLevel { get; set; }
protected override void OnActionExecuting(ActionExecutingContext filterContext)
{
base.OnActionExecuting(filterContext);
var Token = HttpContext.Request.QueryString["Token"];
if (!string.IsNullOrEmpty(Token))
Token = "?Token=" + Token;
string homeurl = ConfigHelper.GetConfigString("Homeurl") ?? "";
if (string.IsNullOrEmpty(homeurl))
ConfigHelper.UpdatetConfig("Homeurl", Request.Url.ToString());
View_UserInfo model = UserLoginHelper.CurrentUser();
string controllerName = filterContext.RouteData.Values["controller"].ToString().ToLower();//获得控制器名
//string actionName = filterContext.RouteData.Values["action"].ToString().ToLower();//方式一 action名
string actionName = filterContext.ActionDescriptor.ActionName.ToLower();//方式二 action名
if ((controllerName == "home" && actionName == "indexinfo" && !string.IsNullOrEmpty(Token)) || model == null)
{
ContentResult contentResult = (ContentResult)(filterContext.Result = new ContentResult
{
Content = string.Format("<script type='text/javascript'>top.location.href='{0}';</script>", "/Login/Index" + Token)
});
return;
}
//if (!string.IsNullOrEmpty(Token))
//{
// string Ip = IPHelper.GetIP();
// RedisHelper.RemoveKey(Token + Ip);
//}
Umodel = CacheHelp.cacheSysUserInfo.FirstOrDefault((View_UserInfo X) => X.Uid == model.Uid);
if (Umodel != null)
{
//公共属性
base.ViewBag.Umodel = Umodel;
base.ViewBag.Usersum = CacheHelp.cacheSysUserInfo.Count;
base.ViewBag.logsum = CacheHelp.cacheDbLogs;
base.ViewBag.Apps = CacheHelp.cacheSysApp;
base.ViewBag.Orgs = CacheHelp.cacheSysOrganization.Where(x => x.IsValid == 0).ToList();
//酒店信息
base.ViewBag.HoteldGroups = CacheHelp.gh;
base.ViewBag.HoteldGroupsinfo = CacheHelp.HoteldGroupsinfo;
ViewBag.BaseDataGroups = JsonConvert.SerializeObject(CacheHelp.cacheHoteldGroups);
ViewBag.BaseData = JsonConvert.SerializeObject(CacheHelp.cacheHotels.GroupBy(x => x.GroupId).ToList());
return ;
}
}
protected override void OnActionExecuted(ActionExecutedContext filterContext)
{
base.OnActionExecuted(filterContext);
base.ViewBag.Title = ActMessage;
View_UserInfo view_UserInfo = UserLoginHelper.CurrentUser();
if (view_UserInfo != null)
{
Umodel = view_UserInfo;
if (!string.IsNullOrEmpty(ActMessage))
{
string text = filterContext.RouteData.Values["controller"].ToString().ToLower();
string text2 = filterContext.RouteData.Values["action"].ToString().ToLower();
if (base.ViewBag.mFormUrl != null)
{
base.ViewBag.FormUrl = base.ViewBag.mFormUrl;
}
else if (base.Request.UrlReferrer != null)
{
base.ViewBag.FormUrl = base.Request.UrlReferrer.ToString();
}
}
}
else
{
ContentResult contentResult = (ContentResult)(filterContext.Result = new ContentResult
{
Content = $"/Login/Index"
});
}
//压缩
var acceptEncoding = filterContext.HttpContext.Request.Headers["Accept-Encoding"];
if (!string.IsNullOrEmpty(acceptEncoding))
{
acceptEncoding = acceptEncoding.ToLower();
var response = filterContext.HttpContext.Response;
if (acceptEncoding.Contains("gzip"))
{
response.AppendHeader("Content-encoding", "gzip");
response.Filter = new GZipStream(response.Filter, CompressionMode.Compress);
}
else if (acceptEncoding.Contains("deflate"))
{
response.AppendHeader("Content-encoding", "deflate");
response.Filter = new DeflateStream(response.Filter, CompressionMode.Compress);
}
}
}
protected NameValueCollection FormatQueryString(NameValueCollection nameValues)
{
NameValueCollection nameValueCollection = new NameValueCollection();
string[] allKeys = nameValues.AllKeys;
foreach (string text in allKeys)
{
if (text.Equals("datefiled"))
{
if (!string.IsNullOrEmpty(nameValues["dateform"]) && !string.IsNullOrEmpty(nameValues["dateto"]))
{
string[] array = nameValues["dateform"].Split('/');
string[] array2 = nameValues["dateto"].Split('/');
string s = array[2] + "-" + array[0] + "-" + array[1];
string s2 = array2[2] + "-" + array2[0] + "-" + array2[1];
DateTime dateTime = DateTime.Parse(s).ToDateTime();
DateTime dateTime2 = DateTime.Parse(s2).ToDateTime();
nameValueCollection.Add(nameValues[text] + ">=", dateTime.ToString());
nameValueCollection.Add(nameValues[text] + "<=", dateTime2.ToString());
}
}
else if (text.Equals("numberfiled"))
{
if (!string.IsNullOrEmpty(nameValues["numberform"]) && !string.IsNullOrEmpty(nameValues["numberto"]))
{
double num = nameValues["numberform"].ToDouble();
double num2 = nameValues["numberto"].ToDouble();
nameValueCollection.Add(nameValues[text] + ">=", num.ToString());
nameValueCollection.Add(nameValues[text] + "<=", num2.ToString());
}
}
else if (text.Equals("keyfiled") && !string.IsNullOrEmpty(nameValues["keyword"]))
{
nameValueCollection.Add(nameValues[text], nameValues["keyword"]);
}
}
return nameValueCollection;
}
}
}