ibms-dome/FrontendWebApi/Controllers/MyBaseController.cs

172 lines
8.0 KiB
C#
Raw 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 FrontendWebApi.Jwt;
using FrontendWebApi.Models;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.Filters;
using Microsoft.Extensions.Configuration;
using Repository.BackendRepository.Interface;
using Repository.BaseRepository.Interface;
using Repository.FrontendRepository.Interface;
using Repository.Helper;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using Microsoft.AspNetCore.Routing;
using System.Diagnostics;
using Repository.BackendRepository.Implement;
namespace FrontendWebApi.Controllers
{
public class MyBaseController<T> : Controller where T : MyBaseController<T>
{
private ILogger<T> _logger;
protected ILogger<T> Logger => _logger ?? (_logger = HttpContext?.RequestServices.GetService<ILogger<T>>());
private IJwtHelpers jwt => HttpContext?.RequestServices.GetService<IJwtHelpers>();
private IFrontendRepository frontendRepository => HttpContext?.RequestServices.GetService<IFrontendRepository>();
public MyBaseController() { }
protected MyUserInfo myUserInfo = null;
protected JwtGet myUser;
protected string jwt_str = null;
protected bool jwtlife = true;
public string controllerName;
public string actionName;
public ErrorCode errorCode = new ErrorCode();
[Authorize]
public override void OnActionExecuting(ActionExecutingContext filterContext)
{
EDFunction edFunction = new EDFunction();
var myAccount = edFunction.AESDecrypt(HttpContext.Session.GetString("MyApiAccount"));
controllerName = ControllerContext.RouteData.Values["controller"].ToString(); //controller名稱
actionName = ControllerContext.RouteData.Values["action"].ToString(); //action名稱
//紀錄當前PID
Process currentProcess = Process.GetCurrentProcess();
Dictionary<string, object> updateProcess = new Dictionary<string, object>();
updateProcess.Add("@system_value", currentProcess.Id.ToString());
frontendRepository.UpdateProcessPID(updateProcess, "variable", "system_type = 'watchDogCongfig' AND system_key = 'AlarmPID'");
bool isAjaxCall = filterContext.HttpContext.Request.Headers["x-requested-with"] == "XMLHttpRequest";
if (string.IsNullOrEmpty(myAccount))
{
if (isAjaxCall)
{
filterContext.HttpContext.Response.Clear();
filterContext.HttpContext.Response.StatusCode = 499;
return;
}
else
{
filterContext.Result = new RedirectToRouteResult(
new RouteValueDictionary
{
{"controller", "Login"},
{"action", "Index"}
});
return;
}
}
else
{
//取得當前登入使用者資訊
myUserInfo = frontendRepository.GetMyUserInfoByAccount<MyUserInfo>(myAccount);
var showview = frontendRepository.GetAllAsync<string>($@"select ap.ShowView from userinfo us
left join role_auth ra on ra.role_guid = us.role_guid
left join auth_page ap on ap.AuthCode = ra.AuthCode
where us.userinfo_guid = '{myUserInfo.Userinfo_guid}'");
var sGetBackendUrlSql = @"SELECT system_value FROM variable WHERE system_type = 'website_config' AND system_key = 'website_url'";
var sBackendUrl = frontendRepository.GetOneAsync<string>(sGetBackendUrlSql)?.Result;
myUserInfo.ShowView = showview.Result;
ViewBag.myUserInfo = myUserInfo;
ViewBag.role = showview.Result;
ViewBag.BackendUrl = sBackendUrl;
//var showviewt = new List<string>()
// {
// "EmergencyDeviceMenuIndex",
// "EmergencyContactIndex",
// "EmergencyRecordIndex",
// "RescueDeviceFireExtinguisher",
// "RescueDeviceAED",
// };
//ViewBag.role = showviewt;
}
//var ctx = filterContext.HttpContext;
//ctx.Response.Headers.Add("Access-Control-Allow-Origin", "*");
//ctx.Response.Headers.Add("Access-Control-Allow-Headers", "*");
//ctx.Response.Headers.Add("Access-Control-Allow-Credentials", "true");
//var a = User.Claims.Select(p => new { Type = p.Type, Value = p.Value }).ToList();
//myUser = new JwtGet()
//{
// account = User.Claims.Where(a => a.Type == "account").Select(e => e.Value).FirstOrDefault(),
// email = User.Claims.Where(a => a.Type == "email").Select(e => e.Value).FirstOrDefault(),
// full_name = User.Claims.Where(a => a.Type == "full_name").Select(e => e.Value).FirstOrDefault(),
// exp = User.Claims.Where(a => a.Type == "exp").Select(e => Convert.ToInt32(e.Value)).FirstOrDefault(),
// nbf = User.Claims.Where(a => a.Type == "nbf").Select(e => Convert.ToInt32(e.Value)).FirstOrDefault(),
// userinfo_guid = User.Claims.Where(a => a.Type == "userinfo_guid").Select(e => e.Value).FirstOrDefault(),
//};
//TODO 模擬JWT記得刪除
//myUser.account = "Bajascript";
//myUser.email = "asd@com";
//myUser.full_name = "野原廣志";
//myUser.exp = 3600;
//myUser.userinfo_guid = "1EF9CEAC-4DBF-E2BE-8B1D-CB3014E0DA17";
//if (myUser.exp == 0)
//{
// jwt_str = "Jwt Token不合法";
// jwtlife = false;
// ViewBag.myUserInfo = null;
// ViewBag.role = null;
//}
//else
//{
// if (myUser.exp <= DateTime.Now.AddHours(-8).AddMinutes(10).Subtract(new DateTime(1970, 1, 1)).TotalSeconds)
// {
// //取得當前登入使用者資訊
// myUserInfo = frontendRepository.GetMyUserInfoByAccount<MyUserInfo>(myUser.account);
// //var showview = frontendRepository.GetAllAsync<string>($@"select ap.ShowView from userinfo us
// // left join role_auth ra on ra.role_guid = us.role_guid
// // left join auth_page ap on ap.AuthCode = ra.AuthCode
// // where us.userinfo_guid = '{myUserInfo.Userinfo_guid}'");
// //myUserInfo.ShowView = showview.Result;
// ViewBag.myUserInfo = myUserInfo;
// var showviewt = new List<string>()
// {
// "EmergencyDeviceMenuIndex",
// "EmergencyContactIndex",
// "EmergencyRecordIndex",
// "RescueDeviceFireExtinguisher",
// "RescueDeviceAED",
// };
// //ViewBag.role = showview.Result;
// ViewBag.role = showviewt;
// jwtlife = true;
// JwtLogin jwtLoing = new JwtLogin()
// {
// account = myUser.account,
// email = myUser.email,
// full_name = myUser.full_name,
// userinfo_guid = myUser.userinfo_guid
// };
// jwt_str = jwt.GenerateToken(jwtLoing).token;
// }
//}
base.OnActionExecuting(filterContext);
}
}
}