You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
DSWMS/Vue.Net/VOL.Core/Controllers/Basic/ApiBaseController.cs

315 lines
13 KiB
C#

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Newtonsoft.Json;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using VOL.Core.Extensions;
using VOL.Core.Filters;
using VOL.Core.Utilities;
using VOL.Entity.DomainModels;
using ConvertHelper;
using System;
using System.IO;
using Microsoft.AspNetCore.Authorization;
using VOL.Core.Configuration;
using VOL.Core.Services;
namespace VOL.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)
{
serializerSettings = serializerSettings ?? new JsonSerializerSettings();
serializerSettings.ContractResolver = null;
return Json(data, serializerSettings);
}
//GetPageData基类接口
[ApiActionPermission(Enums.ActionPermissionOptions.Search)]
[HttpPost, Route("GetPageData")]
public virtual ActionResult GetPageData([FromBody] PageDataOptions loadData)
{
return Content(InvokeService("GetPageData", new object[] { loadData }).Serialize());
}
/// <summary>
/// 获取明细grid分页数据
/// </summary>
/// <param name="loadData"></param>
/// <returns></returns>
[ApiActionPermission(Enums.ActionPermissionOptions.Search)]
[HttpPost, Route("GetDetailPage")]
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)]
//public new async Task<IActionResult> Upload(List<IFormFile> fileInput)
//{
// return await base.Upload(fileInput);
//}
/// <summary>
/// 上传文件
/// </summary>
/// <param name="fileInput"></param>
/// <returns></returns>
[HttpPost, Route("Upload")]
[ApiActionPermission(Enums.ActionPermissionOptions.Upload)]
public virtual IActionResult Upload(IEnumerable<IFormFile> fileInput)
{
//return await base.Upload(fileInput.ToList());
return Json(InvokeService("Upload", new object[] { fileInput }));
}
[HttpPost, Route("UploadDJZ"),AllowAnonymous]
public virtual IActionResult UploadDJZ(IEnumerable<IFormFile> files)
{
return Json(InvokeService("Upload", new object[] { files }));
}
[HttpPost, Route("CommonUpload"), AllowAnonymous]
public virtual IActionResult CommonUpload(IEnumerable<IFormFile> file)
{
return Json(InvokeService("Upload", new object[] { file }));
}
/// <summary>
/// 下载导入Excel模板
/// </summary>
/// <returns></returns>
[HttpGet, Route("DownLoadTemplate")]
[ApiActionPermission(Enums.ActionPermissionOptions.Import)]
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)]
//public new async Task<ActionResult> Import(List<IFormFile> fileInput)
public virtual ActionResult Import(List<IFormFile> fileInput)
{
//return await base.Import(fileInput);
return Json(InvokeService("Import", new object[] { fileInput }));
}
/// <summary>
/// 导出文件,返回日期+文件名
/// </summary>
/// <param name="loadData"></param>
/// <returns></returns>
[ApiActionPermission(Enums.ActionPermissionOptions.Export)]
[HttpPost, Route("Export")]
//public new async Task<ActionResult> Export([FromBody] PageDataOptions loadData)
public virtual ActionResult Export([FromBody] PageDataOptions loadData)
{
//return await base.Export(loadData);
return Json(InvokeService("Export", new object[] { loadData }));
}
/// <summary>
/// 下载文件
/// </summary>
/// <param name="fileName"></param>
/// <returns></returns>
[ApiActionPermission(Enums.ActionPermissionOptions.Export)]
[HttpGet, Route("DownLoadFile")]
//public IActionResult DownLoadFile()
public virtual IActionResult DownLoadFile()
{
string path = HttpContext.Request("path");
//return base.DownLoadFile(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")]
//public new async Task<ActionResult> Del([FromBody] object[] keys)
public virtual ActionResult Del([FromBody] object[] keys)
{
//return await base.Del(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.DelDetail)]
[HttpPost, Route("DelDetail")]
public new async Task<ActionResult> DelDetail([FromBody] SaveModel saveModel)
{
//return await base.DelDetail(saveModel);
return Json(InvokeService("DelDetail", new object[] { saveModel }));
}
/// <summary>
/// 审核
/// </summary>
/// <param name="keys"></param>
/// <returns></returns>
[ApiActionPermission(Enums.ActionPermissionOptions.Audit)]
[HttpPost, Route("Audit")]
//public new async Task<ActionResult> Audit([FromBody]object[] id, int? auditStatus, string auditReason)
public virtual ActionResult Audit([FromBody] object[] id, int? auditStatus, string auditReason)
{
//return await base.Audit(id, auditStatus, 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>
/// 业务状态 任务状态 sys_task_status
/// </summary>
/// <param name="keys"></param>
/// <returns></returns>
[ApiActionPermission(Enums.ActionPermissionOptions.PushStatus)]
[HttpPost, Route("PushStatus")]
//public new async Task<ActionResult> PushStatus([FromBody] object[] keys, int execution, string reason)
public virtual ActionResult PushStatus([FromBody] object[] keys, int execution, string reason)
{
//return await base.PushStatus(keys, execution, reason);
return Json(InvokeService("PushStatus", new object[] { keys,execution,reason }));
}
[ApiActionPermission(Enums.ActionPermissionOptions.PushStatus)]
[HttpPost, Route("SaveAndPushStatus")]
//public new async Task<ActionResult> SaveAndPushStatus([FromBody] SaveModel saveModel)
public virtual ActionResult SaveAndPushStatus([FromBody] SaveModel saveModel)
{
//return await base.SaveAndPushStatus(saveModel);
return Json(InvokeService("SaveAndPushStatus", new object[] { saveModel }));
}
/// <summary>
/// 新增支持主子表
/// </summary>
/// <param name="saveDataModel"></param>
/// <returns></returns>
[ApiActionPermission(Enums.ActionPermissionOptions.Add)]
[HttpPost, Route("Add")]
//public new async Task<ActionResult> Add([FromBody]SaveModel saveModel)
public virtual ActionResult Add([FromBody] SaveModel saveModel)
{
// WebResponseContent responseContent = await base.Add(saveModel);
// responseContent.Data = responseContent.Data?.Serialize();
// return Json(responseContent);
_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")]
// public new async Task<ActionResult> Update([FromBody] SaveModel saveModel)
// {
// WebResponseContent responseContent = await base.Update(saveModel);
// responseContent.Data = responseContent.Data?.Serialize();
// return Json(responseContent);
// }
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);
}
}
}