using Microsoft.AspNetCore.Http; using Microsoft.AspNetCore.Mvc; using System; using System.Collections.Generic; using System.IO; using System.Threading.Tasks; using VOL.Core.Configuration; using VOL.Core.Extensions; using VOL.Core.Services; using VOL.Core.Utilities; using VOL.Entity.DomainModels; using ConvertHelper; namespace VOL.Core.Controllers.Basic { public class BaseController : Controller { protected IServiceBase Service; private WebResponseContent ResponseContent { get; set; } /// /// /// public BaseController() { } public BaseController(IServiceBase service) { Service = service; } public BaseController(string projectName, string folder, string tablename, IServiceBase service) { Service = service; } /// /// /// /// [ApiExplorerSettings(IgnoreApi = true)] public virtual ActionResult Manager() { return View(); //if (System.IO.File.Exists(($"Views\\PageExtension\\{projectName }\\{TableName}Extension.cshtml").MapPath())) //{ // ViewBag.UrlExtension = $"~/Views/PageExtension/{projectName}/{TableName}Extension.cshtml"; //} // return View("~/Views/" + projectName + "/" + folder + "/" + TableName + ".cshtml"); } [ApiExplorerSettings(IgnoreApi = true)] public virtual async Task GetPageData(PageDataOptions loadData) { string pageData = await Task.FromResult(InvokeService("GetPageData", new object[] { loadData }).Serialize()); return Content(pageData); } [ApiExplorerSettings(IgnoreApi = true)] public virtual async Task GetDetailPage(PageDataOptions loadData) { string pageData = await Task.FromResult(InvokeService("GetDetailPage", new object[] { loadData }).Serialize()); return Content(pageData); } /// /// 上传文件 /// /// /// [ApiExplorerSettings(IgnoreApi = true)] public virtual async Task Upload(List fileInput) { object result = await Task.FromResult(InvokeService("Upload", new object[] { fileInput })); return Json(result); } /// /// 导入表数据Excel /// /// /// [HttpPost] public virtual async Task Import(List fileInput) { object result = await Task.FromResult(InvokeService("Import", new object[] { fileInput })); return Json(result); } [ApiExplorerSettings(IgnoreApi = true)] public virtual async Task Export(PageDataOptions loadData) { return Json(await Task.FromResult(InvokeService("Export", new object[] { loadData }))); } [ApiExplorerSettings(IgnoreApi = true)] public virtual ActionResult DownLoadFile(string path) { if (string.IsNullOrEmpty(path)) return Content("未找到文件"); try { if (path.IndexOf("/") == -1 && path.IndexOf("\\") == -1) { path = path.DecryptDES(AppSetting.Secret.ExportFile); } else { path = path.MapPath(); } string fileName = Path.GetFileName(path); return File(System.IO.File.ReadAllBytes(path), System.Net.Mime.MediaTypeNames.Application.Octet, fileName); } catch (Exception ex) { Logger.Error($"文件下载出错:{path}{ex.Message}"); } return Content(""); } /// /// 下载Excel导入的模板 /// /// /// [HttpGet] [ApiExplorerSettings(IgnoreApi = true)] public async virtual Task DownLoadTemplate() { ResponseContent = await Task.FromResult(InvokeService("DownLoadTemplate", new object[] { })) as WebResponseContent; if (!ResponseContent.Status) { return Json(ResponseContent); } byte[] fileBytes = System.IO.File.ReadAllBytes(ResponseContent.Data.ToString()); return File(fileBytes, System.Net.Mime.MediaTypeNames.Application.Octet, Path.GetFileName(ResponseContent.Data.ToString())); } [ApiExplorerSettings(IgnoreApi = true)] public virtual async Task Del(object[] keys) { ResponseContent = await Task.FromResult(InvokeService("Del", new object[] { keys, true })) as WebResponseContent; Logger.Info(Enums.LoggerType.Del, keys.Serialize(), ResponseContent.Status ? "Ok" : ResponseContent.Message); return Json(ResponseContent); } [ApiExplorerSettings(IgnoreApi = true)] public virtual async Task DelDetail(SaveModel saveModel) { ResponseContent = await Task.FromResult(InvokeService("DelDetail", new Type[] { typeof(SaveModel) }, new object[] { saveModel })) as WebResponseContent; Logger.Info(Enums.LoggerType.DelDetail, saveModel.Serialize(), ResponseContent.Status ? "Ok" : ResponseContent.Message); return Json(ResponseContent); } [ApiExplorerSettings(IgnoreApi = true)] public virtual async Task Audit(object[] id, int? auditStatus, string auditReason) { ResponseContent = await Task.FromResult(InvokeService("Audit", new object[] { id, auditStatus, auditReason })) as WebResponseContent; Logger.Info(Enums.LoggerType.Audit, id?.Serialize() + "," + (auditStatus ?? -1) + "," + auditReason, ResponseContent.Status ? "Ok" : ResponseContent.Message); return Json(ResponseContent); } [ApiExplorerSettings(IgnoreApi = true)] public virtual async Task PushStatus(object[] keys, int execution, string reason) { ResponseContent = await Task.FromResult(InvokeService("PushStatus", new object[] { keys, execution, reason })) as WebResponseContent; Logger.Info(Enums.LoggerType.PushStatus, keys?.Serialize() + "," + (execution ) + "," + reason, ResponseContent.Status ? "Ok" : ResponseContent.Message); return Json(ResponseContent); } [ApiExplorerSettings(IgnoreApi = true)] public virtual async Task SaveAndPushStatus(SaveModel saveModel) { ResponseContent = await Task.FromResult(InvokeService("SaveAndPushStatus", new Type[] { typeof(SaveModel) })) as WebResponseContent; Logger.Info(Enums.LoggerType.PushStatus, saveModel.Serialize(), ResponseContent.Status ? "Ok" : ResponseContent.Message); return Json(ResponseContent); } [ApiExplorerSettings(IgnoreApi = true)] ///点击保存时调用的 public virtual async Task Add(SaveModel saveModel) { ResponseContent = await Task.FromResult(InvokeService("Add", new Type[] { typeof(SaveModel) }, new object[] { saveModel })) as WebResponseContent; Logger.Info(Enums.LoggerType.Add, saveModel.Serialize(), ResponseContent.Status ? "Ok" : ResponseContent.Message); return ResponseContent; } [ApiExplorerSettings(IgnoreApi = true)] public virtual async Task Update(SaveModel saveModel) { ResponseContent = await Task.FromResult(InvokeService("Update", new object[] { saveModel })) as WebResponseContent; Logger.Info(Enums.LoggerType.Edit, saveModel.Serialize(), ResponseContent.Status ? "Ok" : ResponseContent.Message); return ResponseContent; } /// /// 反射调用service方法 /// /// /// /// private object InvokeService(string methodName, object[] parameters) { return Service.GetType().GetMethod(methodName).Invoke(Service, parameters); } /// /// 反射调用service方法 /// /// /// 为要调用重载的方法参数类型:new Type[] { typeof(SaveDataModel) /// /// private object InvokeService(string methodName, Type[] types, object[] parameters) { return Service.GetType().GetMethod(methodName, types).Invoke(Service, parameters); } } }