using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Mvc; using Microsoft.Extensions.Logging; using Microsoft.Extensions.DependencyInjection; using SolarPower.Models.User; using System; using System.Collections.Generic; using System.Data; using System.Linq; using System.Threading.Tasks; using Microsoft.AspNetCore.Http; using Microsoft.AspNetCore.Mvc.Filters; using Dapper; using SolarPower.Models; using System.Data.SqlClient; using Microsoft.Extensions.Configuration; using SolarPower.Repository.Interface; using System.IO; using System.Text; using SolarPower.Models.OperatorLogModel; using Newtonsoft.Json; using SolarPower.Models.Company; namespace SolarPower.Controllers { public class MyBaseController : Controller where T : MyBaseController { private ILogger _logger; protected ILogger Logger => _logger ?? (_logger = HttpContext?.RequestServices.GetService>()); private IUserRepository userRepository => HttpContext?.RequestServices.GetService(); private ICompanyRepository companyRepository => HttpContext?.RequestServices.GetService(); private IOperatorLogRepository operatorLogRepository => HttpContext?.RequestServices.GetService(); protected SimpleUser mySimpleUser = null; protected SimpleCompany mySimpleCompany = null; public string controllerName; public string actionName; public ErrorCode errorCode = new ErrorCode(); public MyBaseController() { } public override void OnActionExecuting(ActionExecutingContext filterContext) { base.OnActionExecuting(filterContext); EDFunction edFunction = new EDFunction(); var myAccount = edFunction.AESDecrypt(HttpContext.Session.GetString("MyAccount")); //取得登入後,該位使用者的Account controllerName = ControllerContext.RouteData.Values["controller"].ToString(); //controller名稱 actionName = ControllerContext.RouteData.Values["action"].ToString(); //action名稱 if (string.IsNullOrEmpty(myAccount) && myAccount.CompareTo(HttpContext.Session.GetString("MyAccount")) == 0) { //session 找不到account或者無法成功解密 return; } mySimpleUser = userRepository.GetOneNormalSimpleUserByAccount(myAccount); mySimpleCompany = companyRepository.GetOneNormalSimpleCompanyById(mySimpleUser.CompanyId); ViewBag.systemAdminName = mySimpleUser.Name; #region 記錄人員操作紀錄 var content = JsonConvert.SerializeObject(filterContext.ActionArguments); OperatorLog operatorLog = new OperatorLog() { ControllerName = controllerName, ActionName = actionName, Parameter = content.CompareTo("{}") == 0? null : content, CreatedBy = mySimpleUser.Id, }; List properties = new List() { "ControllerName", "ActionName", "Parameter", "CreatedBy", }; operatorLogRepository.Add(operatorLog, properties); #endregion } } }