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 WIDESEA_Core.Configuration; using WIDESEA_Core.Extensions; using WIDESEA_Core.Filters; using WIDESEA_Core.Services; using WIDESEA_Core.Utilities; using WIDESEA_Entity.DomainModels; namespace WIDESEA_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); } } }