对比新文件 |
| | |
| | | 锘縰sing Microsoft.AspNetCore.Authorization; |
| | | using Microsoft.AspNetCore.Http; |
| | | using Microsoft.AspNetCore.Mvc; |
| | | using Microsoft.Extensions.Options; |
| | | using System; |
| | | using System.Collections.Generic; |
| | | using System.Linq; |
| | | using System.Reflection; |
| | | using System.Text; |
| | | using System.Threading.Tasks; |
| | | using WIDESEA_Core.BaseServices; |
| | | |
| | | namespace WIDESEA_Core.BaseController |
| | | { |
| | | [Authorize, ApiController] |
| | | public class ApiBaseController<IService, TEntity> : Controller |
| | | { |
| | | protected IService Service; |
| | | |
| | | public ApiBaseController(IService service) |
| | | { |
| | | Service = service; |
| | | } |
| | | |
| | | [HttpPost, Route("GetPageData")] |
| | | public virtual ActionResult GetPageData([FromBody] PageDataOptions options) |
| | | { |
| | | return Json(InvokeService("GetPageData", new object[] { options })); |
| | | } |
| | | |
| | | [HttpPost, Route("GetDetailPage")] |
| | | public virtual ActionResult GetDetailPage([FromBody] PageDataOptions pageData) |
| | | { |
| | | return Json(InvokeService("GetDetailPage", new object[] { pageData })); |
| | | } |
| | | |
| | | [HttpPost, Route("AddData")] |
| | | public virtual ActionResult AddData([FromBody] TEntity options) |
| | | { |
| | | return Json(InvokeService("AddData", new object[] { options })); |
| | | } |
| | | |
| | | [HttpPost, Route("Add")] |
| | | public virtual ActionResult Add([FromBody] SaveModel options) |
| | | { |
| | | return Json(InvokeService("AddData", new object[] { options })); |
| | | } |
| | | |
| | | [HttpPost, Route("Update")] |
| | | public virtual ActionResult Update([FromBody] SaveModel options) |
| | | { |
| | | return Json(InvokeService("UpdateData", new object[] { options })); |
| | | } |
| | | |
| | | [HttpPost, Route("UpdateData")] |
| | | public virtual ActionResult UpdateData([FromBody] TEntity options) |
| | | { |
| | | return Json(InvokeService("UpdateData", new object[] { options })); |
| | | } |
| | | |
| | | [HttpPost, Route("Del")] |
| | | public virtual ActionResult Del([FromBody] object[] key) |
| | | { |
| | | return Json(InvokeService("DeleteData", new object[] { key })); |
| | | } |
| | | |
| | | [HttpPost, Route("Export")] |
| | | public virtual ActionResult Export([FromBody] PageDataOptions loadData) |
| | | { |
| | | WebResponseContent result = InvokeService("Export", new object[] { loadData }) as WebResponseContent; |
| | | if (result.Status) |
| | | return File( |
| | | System.IO.File.ReadAllBytes(result.Data.ToString()), |
| | | System.Net.Mime.MediaTypeNames.Application.Octet, |
| | | Path.GetFileName(result.Data.ToString()) |
| | | ); |
| | | return Json(result); |
| | | } |
| | | |
| | | [HttpPost, HttpGet, Route("DownLoadTemplate")] |
| | | public virtual ActionResult DownLoadTemplate() |
| | | { |
| | | WebResponseContent result = InvokeService("DownLoadTemplate", new object[] { }) as WebResponseContent; |
| | | if (result.Status) |
| | | return File( |
| | | System.IO.File.ReadAllBytes(result.Data.ToString()), |
| | | System.Net.Mime.MediaTypeNames.Application.Octet, |
| | | Path.GetFileName(result.Data.ToString()) |
| | | ); |
| | | return Json(result); |
| | | } |
| | | |
| | | [HttpPost, Route("Import")] |
| | | public virtual ActionResult Import(List<IFormFile> fileInput) |
| | | { |
| | | return Json(InvokeService("Import", new object[] { fileInput })); |
| | | } |
| | | |
| | | [HttpPost, Route("ExportSeedData"), AllowAnonymous] |
| | | public ActionResult ExportSeedData() |
| | | { |
| | | return Json(InvokeService("ExportSeedData", new object[] { })); |
| | | } |
| | | |
| | | private object InvokeService(string methodName, object[] parameters) |
| | | { |
| | | Type t = Service.GetType(); |
| | | List<Type> types = new List<Type>(); |
| | | foreach (var param in parameters) |
| | | { |
| | | types.Add(param.GetType()); |
| | | } |
| | | MethodInfo method = t.GetMethod(methodName, types.ToArray()); |
| | | return method.Invoke(Service, parameters); |
| | | } |
| | | } |
| | | } |