using ERP.Framework.Exceptions;
using Microsoft.AspNetCore.Mvc;
namespace ERP.Framework.WebApi
{
[ApiController]
public class BaseController : ControllerBase
{
///
/// 请求成功后返回Json数据
///
///
protected IActionResult Success()
{
return new JsonResult(new
{
code = ErrorCode.SUCCESS,
message = "Success",
});
}
///
/// 请求成功后返回Json数据
///
/// 返回消息
///
protected IActionResult Success(string message)
{
return new JsonResult(new
{
code = ErrorCode.SUCCESS,
message
});
}
///
/// 请求成功后返回Json数据
///
/// Data 类型(Data不能为string类型)
/// 返回数据
///
protected IActionResult Success(T data)
{
return new JsonResult(new
{
code = ErrorCode.SUCCESS,
data,
message = "Success"
});
}
///
/// 请求成功后返回Json数据
///
/// Data 类型
/// 返回消息
/// 返回数据
///
protected IActionResult Success(string message, T data)
{
return new JsonResult(new
{
code = ErrorCode.SUCCESS,
data,
message
});
}
///
///
///
///
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 data)
{
return new BadRequestObjectResult(new
{
code = ErrorCode.ERROR,
data,
message = "Failed"
});
}
protected IActionResult Error(string message, T data)
{
return new BadRequestObjectResult(new
{
code = ErrorCode.ERROR,
data,
message
});
}
///
/// 判断是否成功后返回
///
///
protected IActionResult Result(bool isSuccess)
{
if (isSuccess)
{
return Success();
}
else
{
return Error();
}
}
protected IActionResult Result(int code, string message, T data)
{
return new BadRequestObjectResult(new
{
code,
data,
message
});
}
protected IActionResult BadResult(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 });
// //}
//}
}
}