demo20230512/Controllers/QcarbonControllerBase/QcarbonControllerBase.cs
2023-05-12 10:20:28 +08:00

115 lines
3.9 KiB
C#

using Microsoft.AspNet.Identity;
using NLog;
using Qcarbon.repository;
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Threading;
using System.Web;
using System.Web.Mvc;
using System.Web.Routing;
using Weee.DAL;
namespace Weee.Controllers
{
public class QcarbonControllerBase : Controller
{
protected readonly WeeeDataContext db;
protected readonly IUnitOfWork uow;
protected string baseUrl;
public QcarbonControllerBase(WeeeDataContext db)
{
uow = new UnitOfWork(db);
this.db = db;
}
protected override void Initialize(RequestContext requestContext)
{
base.Initialize(requestContext);
//var data = string.Format("{0}://{1} {2}", Request.Url.Scheme, Request.Url.Authority, Url.Content("~"));
//Logger log = NLog.LogManager.GetCurrentClassLogger();
try
{
baseUrl = Url.Content("~");
if (baseUrl.Length > 0) {
if (baseUrl.Substring(baseUrl.Length-1) == "/")
baseUrl=baseUrl.Substring(0,baseUrl.Length-1);
}
ViewBag.baseUrl = baseUrl;
ViewBag.customLoginImg = HttpContext.Application["customLoginImg"];
if (string.IsNullOrWhiteSpace(Session["userLogoImg"] + "")) {
var id = User.Identity.GetUserId();
var qry = (from a in db.Companies
join b in db.Users on a.ID equals b.CompanyID
where b.Id == id
select a.LogoUrl).FirstOrDefault();
ViewBag.userLogoImg = qry;
//log.Info($"1 userLogoImg={ViewBag.userLogoImg}");
Session["userLogoImg"] = ViewBag.userLogoImg;
}
else {
ViewBag.userLogoImg = Session["userLogoImg"] + "";
//log.Info($"2 userLogoImg={ViewBag.userLogoImg}");
}
//log.Info($"ViewBag.customLoginImg {ViewBag.customLoginImg}");
//log.Info($"ViewBag.userLogoImg {ViewBag.userLogoImg}");
}
catch
{
HttpContext.Application.Set("customLoginImg", "");
ViewBag.customLoginImg = "";
}
finally
{
Thread.Sleep(0);
}
//try
//{
// ViewBag.customLoginImg = HttpContext.Application["customLoginImg"];
//}
//catch
//{
// HttpContext.Application.Set("customLoginImg", "");
// ViewBag.customLoginImg = "";
//}
//finally
//{
// Thread.Sleep(0);
//}
}
public void writeLog(WeeeDataContext db, string username, string err)
{
var log = new Weee.Models.ActionLog()
{
Name = username,
Action = "ActionName",
Controller = "ControllerName",
IP = HttpContext.Request.UserHostAddress,
ActionTime = DateTime.Now,
ExecuteResult = err
};
db.ActionLogs.Add(log);
db.SaveChanges();
}
public string configS(string key)
{
string ret;
ret = ConfigurationManager.AppSettings[key]+"";
return ret;
}
public int configI(string key)
{
int ret=0;
int.TryParse( ConfigurationManager.AppSettings[key]+"", out ret);
return ret;
}
public bool configB(string key)
{
bool ret;
bool.TryParse( ConfigurationManager.AppSettings[key]+"", out ret);
return ret;
}
}
}