123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172 |
- using ERP.Framework.Exceptions;
- using Microsoft.AspNetCore.Mvc;
- namespace ERP.Framework.WebApi
- {
- [ApiController]
- public class BaseController : ControllerBase
- {
- /// <summary>
- /// 请求成功后返回Json数据
- /// </summary>
- /// <returns></returns>
- protected IActionResult Success()
- {
- return new JsonResult(new
- {
- code = ErrorCode.SUCCESS,
- message = "Success",
- });
- }
- /// <summary>
- /// 请求成功后返回Json数据
- /// </summary>
- /// <param name="message">返回消息</param>
- /// <returns></returns>
- protected IActionResult Success(string message)
- {
- return new JsonResult(new
- {
- code = ErrorCode.SUCCESS,
- message
- });
- }
- /// <summary>
- /// 请求成功后返回Json数据
- /// </summary>
- /// <typeparam name="T">Data 类型(Data不能为string类型)</typeparam>
- /// <param name="data">返回数据</param>
- /// <returns></returns>
- protected IActionResult Success<T>(T data)
- {
- return new JsonResult(new
- {
- code = ErrorCode.SUCCESS,
- data,
- message = "Success"
- });
- }
- /// <summary>
- /// 请求成功后返回Json数据
- /// </summary>
- /// <typeparam name="T">Data 类型</typeparam>
- /// <param name="message">返回消息</param>
- /// <param name="data">返回数据</param>
- /// <returns></returns>
- protected IActionResult Success<T>(string message, T data)
- {
- return new JsonResult(new
- {
- code = ErrorCode.SUCCESS,
- data,
- message
- });
- }
- /// <summary>
- ///
- /// </summary>
- /// <returns></returns>
- protected IActionResult Error()
- {
- return new BadRequestObjectResult(new
- {
- code = ErrorCode.ERROR,
- message = "Failed"
- });
- }
- protected IActionResult Error(string message)
- {
- return new BadRequestObjectResult(new
- {
- code = ErrorCode.ERROR,
- message
- });
- }
- protected IActionResult Error(int code, string message)
- {
- return new BadRequestObjectResult(new
- {
- code,
- message
- });
- }
- protected IActionResult Error<T>(T data)
- {
- return new BadRequestObjectResult(new
- {
- code = ErrorCode.ERROR,
- data,
- message = "Failed"
- });
- }
- protected IActionResult Error<T>(string message, T data)
- {
- return new BadRequestObjectResult(new
- {
- code = ErrorCode.ERROR,
- data,
- message
- });
- }
- /// <summary>
- /// 判断是否成功后返回
- /// </summary>
- /// <returns></returns>
- protected IActionResult Result(bool isSuccess)
- {
- if (isSuccess)
- {
- return Success();
- }
- else
- {
- return Error();
- }
- }
- protected IActionResult Result<T>(int code, string message, T data)
- {
- return new BadRequestObjectResult(new
- {
- code,
- data,
- message
- });
- }
- protected IActionResult BadResult<T>(int httpErrorCode, int code, T data, string message)
- {
- var result = new BadRequestObjectResult(new
- {
- code,
- data,
- message
- });
- result.StatusCode = httpErrorCode;
- return result;
- }
- //public override Task ExecuteResultAsync(ActionContext context)
- //{
- // //if (context.ModelState.IsValid)
- // //{
- // // return base.ExecuteResultAsync(context);
- // //}
- // //else
- // //{
- // // var errors = context.ModelState.Values.SelectMany(v => v.Errors).Select(e => e.ErrorMessage);
- // // return context.HttpContext.Response.WriteAsJsonAsync(new { success = false, message = "Validation failed", errors });
- // //}
- //}
- }
- }
|