This commit is contained in:
2026-02-06 18:34:35 +08:00
commit f7f4c94c00
3285 changed files with 563208 additions and 0 deletions

View File

@@ -0,0 +1,247 @@
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Threading.Tasks;
using iMES.Core.Configuration;
using iMES.Core.Extensions;
using iMES.Core.Filters;
using iMES.Core.Services;
using iMES.Core.Utilities;
using iMES.Entity.DomainModels;
namespace iMES.Core.Controllers.Basic
{
[JWTAuthorize, ApiController]
public class ApiBaseController<IServiceBase> : Controller
{
protected IServiceBase Service;
private WebResponseContent _baseWebResponseContent { get; set; }
public ApiBaseController()
{
}
public ApiBaseController(IServiceBase service)
{
Service = service;
}
public ApiBaseController(string projectName, string folder, string tablename, IServiceBase service)
{
Service = service;
}
/// <summary>
/// 2020.11.21增加json原格式返回数据(默认是驼峰格式)
/// </summary>
/// <param name="data"></param>
/// <param name="serializerSettings"></param>
/// <returns></returns>
protected JsonResult JsonNormal(object data, JsonSerializerSettings serializerSettings = null, bool formateDate = true)
{
serializerSettings = serializerSettings ?? new JsonSerializerSettings();
serializerSettings.ContractResolver = null;
if (formateDate)
{
serializerSettings.DateFormatString = "yyyy-MM-dd HH:mm:ss";
}
return Json(data, serializerSettings);
}
[ApiActionPermission(Enums.ActionPermissionOptions.Search)]
[HttpPost, Route("GetPageData")]
public virtual ActionResult GetPageData([FromBody] PageDataOptions loadData)
{
return JsonNormal(InvokeService("GetPageData", new object[] { loadData }));
}
/// <summary>
/// 获取明细grid分页数据
/// </summary>
/// <param name="loadData"></param>
/// <returns></returns>
[ApiActionPermission(Enums.ActionPermissionOptions.Search)]
[HttpPost, Route("GetDetailPage")]
[ApiExplorerSettings(IgnoreApi = true)]
public virtual ActionResult GetDetailPage([FromBody] PageDataOptions loadData)
{
return Content(InvokeService("GetDetailPage", new object[] { loadData }).Serialize());
}
/// <summary>
/// 上传文件
/// </summary>
/// <param name="fileInput"></param>
/// <returns></returns>
[HttpPost, Route("Upload")]
[ApiActionPermission(Enums.ActionPermissionOptions.Upload)]
[ApiExplorerSettings(IgnoreApi = true)]
public virtual IActionResult Upload(IEnumerable<IFormFile> fileInput)
{
return Json(InvokeService("Upload", new object[] { fileInput }));
}
/// <summary>
/// 下载导入Excel模板
/// </summary>
/// <returns></returns>
[HttpGet, Route("DownLoadTemplate")]
[ApiActionPermission(Enums.ActionPermissionOptions.Import)]
[ApiExplorerSettings(IgnoreApi = true)]
public virtual ActionResult DownLoadTemplate()
{
_baseWebResponseContent = InvokeService("DownLoadTemplate", new object[] { }) as WebResponseContent;
if (!_baseWebResponseContent.Status) return Json(_baseWebResponseContent);
byte[] fileBytes = System.IO.File.ReadAllBytes(_baseWebResponseContent.Data.ToString());
return File(
fileBytes,
System.Net.Mime.MediaTypeNames.Application.Octet,
Path.GetFileName(_baseWebResponseContent.Data.ToString())
);
}
/// <summary>
/// 导入表数据Excel
/// </summary>
/// <param name="fileInput"></param>
/// <returns></returns>
[HttpPost, Route("Import")]
[ApiActionPermission(Enums.ActionPermissionOptions.Import)]
[ApiExplorerSettings(IgnoreApi = true)]
public virtual ActionResult Import(List<IFormFile> fileInput)
{
return Json(InvokeService("Import", new object[] { fileInput }));
}
/// <summary>
/// 导出文件,返回日期+文件名
/// </summary>
/// <param name="loadData"></param>
/// <returns></returns>
[ApiActionPermission(Enums.ActionPermissionOptions.Export)]
[ApiExplorerSettings(IgnoreApi = true)]
[HttpPost, Route("Export")]
public virtual ActionResult Export([FromBody] PageDataOptions loadData)
{
var result = InvokeService("Export", new object[] { loadData }) as WebResponseContent;
return File(
System.IO.File.ReadAllBytes(result.Data.ToString().MapPath()),
System.Net.Mime.MediaTypeNames.Application.Octet,
Path.GetFileName(result.Data.ToString())
);
}
/// <summary>
/// 2022.01.08移除原来的导出功能
/// </summary>
/// <param name="path"></param>
/// <returns></returns>
[Obsolete]
[ApiActionPermission(Enums.ActionPermissionOptions.Export)]
[HttpGet, Route("DownLoadFile")]
[ApiExplorerSettings(IgnoreApi = true)]
public virtual IActionResult DownLoadFile()
{
throw new Exception("原导出方法已停用");
//string path = HttpContext.Request("path");
//if (string.IsNullOrEmpty(path)) return Content("未找到文件");
//try
//{
// path = path.IndexOf("/") == -1 && path.IndexOf("\\") == -1
// ?path.DecryptDES(AppSetting.Secret.ExportFile)
// : path.MapPath();
// return File(
// System.IO.File.ReadAllBytes(path),
// System.Net.Mime.MediaTypeNames.Application.Octet,
// Path.GetFileName(path)
// );
//}
//catch (Exception ex)
//{
// Logger.Error($"文件下载出错:{path}{ex.Message}");
//}
//return Content("");
}
/// <summary>
/// 通过key删除文件
/// </summary>
/// <param name="keys"></param>
/// <returns></returns>
[ApiActionPermission(Enums.ActionPermissionOptions.Delete)]
[HttpPost, Route("Del")]
[ApiExplorerSettings(IgnoreApi = true)]
public virtual ActionResult Del([FromBody] object[] keys)
{
_baseWebResponseContent = InvokeService("Del", new object[] { keys, true }) as WebResponseContent;
Logger.Info(Enums.LoggerType.Del, keys.Serialize(), _baseWebResponseContent.Status ? "Ok" : _baseWebResponseContent.Message);
return Json(_baseWebResponseContent);
}
/// <summary>
/// 审核
/// </summary>
/// <param name="keys"></param>
/// <returns></returns>
[ApiActionPermission(Enums.ActionPermissionOptions.Audit)]
[HttpPost, Route("Audit")]
[ApiExplorerSettings(IgnoreApi = true)]
public virtual ActionResult Audit([FromBody] object[] id, int? auditStatus, string auditReason)
{
_baseWebResponseContent = InvokeService("Audit", new object[] { id, auditStatus, auditReason }) as WebResponseContent;
Logger.Info(Enums.LoggerType.Del, id?.Serialize() + "," + (auditStatus ?? -1) + "," + auditReason, _baseWebResponseContent.Status ? "Ok" : _baseWebResponseContent.Message);
return Json(_baseWebResponseContent);
}
/// <summary>
/// 新增支持主子表
/// </summary>
/// <param name="saveDataModel"></param>
/// <returns></returns>
[ApiActionPermission(Enums.ActionPermissionOptions.Add)]
[HttpPost, Route("Add")]
[ApiExplorerSettings(IgnoreApi = true)]
public virtual ActionResult Add([FromBody] SaveModel saveModel)
{
_baseWebResponseContent = InvokeService("Add",
new Type[] { typeof(SaveModel) },
new object[] { saveModel }) as WebResponseContent;
Logger.Info(Enums.LoggerType.Add, saveModel.Serialize(), _baseWebResponseContent.Status ? "Ok" : _baseWebResponseContent.Message);
_baseWebResponseContent.Data = _baseWebResponseContent.Data?.Serialize();
return Json(_baseWebResponseContent);
}
/// <summary>
/// 编辑支持主子表
/// [ModelBinder(BinderType =(typeof(ModelBinder.BaseModelBinder)))]可指定绑定modelbinder
/// </summary>
/// <param name="saveDataModel"></param>
/// <returns></returns>
[ApiActionPermission(Enums.ActionPermissionOptions.Update)]
[HttpPost, Route("Update")]
[ApiExplorerSettings(IgnoreApi = true)]
public virtual ActionResult Update([FromBody] SaveModel saveModel)
{
_baseWebResponseContent = InvokeService("Update", new object[] { saveModel }) as WebResponseContent;
Logger.Info(Enums.LoggerType.Edit, saveModel.Serialize(), _baseWebResponseContent.Status ? "Ok" : _baseWebResponseContent.Message);
_baseWebResponseContent.Data = _baseWebResponseContent.Data?.Serialize();
return Json(_baseWebResponseContent);
}
/// <summary>
/// 调用service方法
/// </summary>
/// <param name="methodName"></param>
/// <param name="parameters"></param>
/// <returns></returns>
private object InvokeService(string methodName, object[] parameters)
{
return Service.GetType().GetMethod(methodName).Invoke(Service, parameters);
}
/// <summary>
/// 调用service方法
/// </summary>
/// <param name="methodName"></param>
/// <param name="types">为要调用重载的方法参数类型new Type[] { typeof(SaveDataModel)</param>
/// <param name="parameters"></param>
/// <returns></returns>
private object InvokeService(string methodName, Type[] types, object[] parameters)
{
return Service.GetType().GetMethod(methodName, types).Invoke(Service, parameters);
}
}
}

View File

@@ -0,0 +1,186 @@
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections.Generic;
using System.IO;
using System.Threading.Tasks;
using iMES.Core.Configuration;
using iMES.Core.Extensions;
using iMES.Core.Services;
using iMES.Core.Utilities;
using iMES.Entity.DomainModels;
namespace iMES.Core.Controllers.Basic
{
public class BaseController<IServiceBase> : Controller
{
protected IServiceBase Service;
private WebResponseContent ResponseContent { get; set; }
/// <summary>
///
/// </summary>
public BaseController()
{
}
public BaseController(IServiceBase service)
{
Service = service;
}
public BaseController(string projectName, string folder, string tablename, IServiceBase service)
{
Service = service;
}
/// <summary>
///
/// </summary>
/// <returns></returns>
[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<ActionResult> GetPageData(PageDataOptions loadData)
{
string pageData = await Task.FromResult(InvokeService("GetPageData", new object[] { loadData }).Serialize());
return Content(pageData);
}
[ApiExplorerSettings(IgnoreApi = true)]
public virtual async Task<ActionResult> GetDetailPage(PageDataOptions loadData)
{
string pageData = await Task.FromResult(InvokeService("GetDetailPage", new object[] { loadData }).Serialize());
return Content(pageData);
}
/// <summary>
/// 上传文件
/// </summary>
/// <param name="fileInput"></param>
/// <returns></returns>
[ApiExplorerSettings(IgnoreApi = true)]
public virtual async Task<ActionResult> Upload(List<IFormFile> fileInput)
{
object result = await Task.FromResult(InvokeService("Upload", new object[] { fileInput }));
return Json(result);
}
/// <summary>
/// 导入表数据Excel
/// </summary>
/// <param name="fileInput"></param>
/// <returns></returns>
[HttpPost]
public virtual async Task<ActionResult> Import(List<IFormFile> fileInput)
{
object result = await Task.FromResult(InvokeService("Import", new object[] { fileInput }));
return Json(result);
}
[ApiExplorerSettings(IgnoreApi = true)]
public virtual async Task<ActionResult> 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("");
}
/// <summary>
/// 下载Excel导入的模板
/// </summary>
/// <param name="fileName"></param>
/// <returns></returns>
[HttpGet]
[ApiExplorerSettings(IgnoreApi = true)]
public async virtual Task<ActionResult> 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<ActionResult> 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<ActionResult> Audit(object[] id, int? auditStatus, string auditReason)
{
ResponseContent = await Task.FromResult(InvokeService("Audit", new object[] { id, auditStatus, auditReason })) as WebResponseContent;
Logger.Info(Enums.LoggerType.Del, id?.Serialize() + "," + (auditStatus ?? -1) + "," + auditReason, ResponseContent.Status ? "Ok" : ResponseContent.Message);
return Json(ResponseContent);
}
[ApiExplorerSettings(IgnoreApi = true)]
public virtual async Task<WebResponseContent> 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<WebResponseContent> 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;
}
/// <summary>
/// 反射调用service方法
/// </summary>
/// <param name="methodName"></param>
/// <param name="parameters"></param>
/// <returns></returns>
private object InvokeService(string methodName, object[] parameters)
{
return Service.GetType().GetMethod(methodName).Invoke(Service, parameters);
}
/// <summary>
/// 反射调用service方法
/// </summary>
/// <param name="methodName"></param>
/// <param name="types">为要调用重载的方法参数类型new Type[] { typeof(SaveDataModel)</param>
/// <param name="parameters"></param>
/// <returns></returns>
private object InvokeService(string methodName, Type[] types, object[] parameters)
{
return Service.GetType().GetMethod(methodName, types).Invoke(Service, parameters);
}
}
}

View File

@@ -0,0 +1,112 @@
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using System.Collections.Generic;
using System.Threading.Tasks;
using iMES.Core.Filters;
using iMES.Entity.DomainModels;
namespace iMES.Core.Controllers.Basic
{
public class WebBaseController<IServiceBase> : BaseController<IServiceBase>
{
public WebBaseController(string projectName, string folder, string tablename, IServiceBase service)
: base(projectName, folder, tablename, service)
{
}
/// <summary>
/// 获取grid分页数据
/// </summary>
/// <param name="loadData"></param>
/// <returns></returns>
[ActionPermission(Enums.ActionPermissionOptions.Search)]
[HttpPost]
public override async Task<ActionResult> GetPageData(PageDataOptions loadData)
{
return await base.GetPageData(loadData);
}
[HttpPost]
[ActionPermission(Enums.ActionPermissionOptions.Upload)]
public override async Task<ActionResult> Upload(List<IFormFile> fileInput)
{
return await base.Upload(fileInput);
}
/// <summary>
/// 导出文件,返回日期+文件名
/// </summary>
/// <param name="loadData"></param>
/// <returns></returns>
[ActionPermission(Enums.ActionPermissionOptions.Export)]
[HttpPost]
public override async Task<ActionResult> Export(PageDataOptions loadData)
{
return await base.Export(loadData);
}
/// <summary>
/// 下载文件
/// </summary>
/// <param name="fileName"></param>
/// <returns></returns>
[ActionPermission(Enums.ActionPermissionOptions.Export)]
[HttpGet]
public override ActionResult DownLoadFile(string fileName)
{
return base.DownLoadFile(fileName);
}
/// <summary>
/// 通过key删除文件
/// </summary>
/// <param name="keys"></param>
/// <returns></returns>
[ActionPermission(Enums.ActionPermissionOptions.Delete)]
[HttpPost]
public override async Task<ActionResult> Del(object[] keys)
{
return await base.Del(keys);
}
/// <summary>
/// 审核
/// </summary>
/// <param name="keys"></param>
/// <returns></returns>
[ActionPermission(Enums.ActionPermissionOptions.Audit)]
[HttpPost]
public override async Task<ActionResult> Audit(object[] id, int? auditStatus, string auditReason)
{
return await base.Audit(id, auditStatus, auditReason);
}
/// <summary>
/// 新增支持主子表
/// </summary>
/// <param name="saveDataModel"></param>
/// <returns></returns>
[ActionPermission(Enums.ActionPermissionOptions.Add)]
[HttpPost]
public async Task<ActionResult> Add([ModelBinder]Dictionary<string, object> mainData, [ModelBinder] List<Dictionary<string, object>> detailData)
{
SaveModel saveModel = new SaveModel() { MainData = mainData, DetailData = detailData };
return Json(await base.Add(saveModel));
}
/// <summary>
/// 编辑支持主子表
/// [ModelBinder(BinderType =(typeof(ModelBinder.BaseModelBinder)))]可指定绑定modelbinder
/// </summary>
/// <param name="saveDataModel"></param>
/// <returns></returns>
[ActionPermission(Enums.ActionPermissionOptions.Update)]
[HttpPost]
public async Task<ActionResult> Update([ModelBinder(BinderType = (typeof(ModelBinder.BaseModelBinder)))]Dictionary<string, object> mainData, [ModelBinder] List<Dictionary<string, object>> detailData, [ModelBinder] List<object> delKeys)
{
SaveModel saveModel = new SaveModel()
{
DelKeys = delKeys,
MainData = mainData,
DetailData = detailData
};
var data = await base.Update(saveModel);
return Json(data);
}
}
}