Files
shgx_tz_mes_backend_sync/ZR.Admin.WebApi/Controllers/BaseController.cs

104 lines
3.6 KiB
C#
Raw Normal View History

2021-08-23 16:57:25 +08:00
using Infrastructure;
using Infrastructure.Model;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Newtonsoft.Json;
using Newtonsoft.Json.Serialization;
using ZR.Admin.WebApi.Filters;
namespace ZR.Admin.WebApi.Controllers
{
[LogActionFilter]
public class BaseController : ControllerBase
{
private static readonly NLog.Logger logger = NLog.LogManager.GetCurrentClassLogger();
public static string TIME_FORMAT_FULL = "yyyy-MM-dd HH:mm:ss";
public static string TIME_FORMAT_FULL_2 = "MM-dd HH:mm:ss";
2021-09-13 20:33:37 +08:00
protected IActionResult SUCCESS(object data, string timeFormatStr = "yyyy-MM-dd HH:mm:ss")
2021-08-23 16:57:25 +08:00
{
string jsonStr = GetJsonStr(GetApiResult(data != null ? ResultCode.SUCCESS : ResultCode.FAIL, data), timeFormatStr);
return Content(jsonStr, "application/json");
}
2021-09-27 16:07:55 +08:00
protected IActionResult ToResponse(ResultCode resultCode, object data = null)
2021-09-06 18:35:36 +08:00
{
string jsonStr = GetJsonStr(GetApiResult(resultCode, data), "");
return Content(jsonStr, "application/json");
}
2021-08-23 16:57:25 +08:00
/// <summary>
/// json输出带时间格式的
/// </summary>
/// <param name="apiResult"></param>
/// <param name="timeFormatStr"></param>
/// <returns></returns>
2021-09-27 16:07:55 +08:00
protected IActionResult ToResponse(ApiResult apiResult, string timeFormatStr = "yyyy-MM-dd HH:mm:ss")
2021-08-23 16:57:25 +08:00
{
string jsonStr = GetJsonStr(apiResult, timeFormatStr);
return Content(jsonStr, "application/json");
}
2021-09-27 16:07:55 +08:00
protected IActionResult ToResponse(long rows, string timeFormatStr = "yyyy-MM-dd HH:mm:ss")
2021-08-23 16:57:25 +08:00
{
string jsonStr = GetJsonStr(ToJson(rows), timeFormatStr);
return Content(jsonStr, "application/json");
}
/// <summary>
/// 响应返回结果
/// </summary>
/// <param name="rows">受影响行数</param>
/// <returns></returns>
protected ApiResult ToJson(long rows)
{
return rows > 0 ? GetApiResult(ResultCode.SUCCESS) : GetApiResult(ResultCode.FAIL);
}
protected ApiResult ToJson(long rows, object data)
{
return rows > 0 ? GetApiResult(ResultCode.SUCCESS, data) : GetApiResult(ResultCode.FAIL);
}
/// <summary>
/// 全局Code使用
/// </summary>
/// <param name="resultCode"></param>
/// <param name="data"></param>
/// <returns></returns>
protected ApiResult GetApiResult(ResultCode resultCode, object data = null)
{
var apiResult = new ApiResult((int)resultCode, resultCode.ToString())
{
Data = data
};
return apiResult;
}
protected ApiResult GetApiResult(ResultCode resultCode, string msg)
{
return new ApiResult((int)resultCode, msg);
}
private static string GetJsonStr(ApiResult apiResult, string timeFormatStr)
{
if (string.IsNullOrEmpty(timeFormatStr))
{
timeFormatStr = TIME_FORMAT_FULL;
}
var serializerSettings = new JsonSerializerSettings
{
// 设置为驼峰命名
ContractResolver = new CamelCasePropertyNamesContractResolver(),
DateFormatString = timeFormatStr
};
return JsonConvert.SerializeObject(apiResult, Formatting.Indented, serializerSettings);
}
protected IActionResult CustomError(ResultCode resultCode, string msg = "")
{
2021-09-27 16:07:55 +08:00
return ToResponse(GetApiResult(resultCode, msg));
2021-08-23 16:57:25 +08:00
}
}
}