207 lines
6.0 KiB
C#
207 lines
6.0 KiB
C#
using Newtonsoft.Json;
|
|
|
|
namespace IRaCIS.Core.Infrastructure.Extention
|
|
{
|
|
/// <summary>
|
|
/// 响应数据输出 泛型
|
|
/// </summary>
|
|
public class ResponseOutput<T> : IResponseOutput<T>
|
|
{
|
|
/// <summary>
|
|
/// 是否成功标记
|
|
/// </summary>
|
|
public bool IsSuccess { get; set; }
|
|
|
|
|
|
public ApiResponseCodeEnum Code { get; set; } = ApiResponseCodeEnum.OK;
|
|
|
|
|
|
/// <summary>
|
|
/// 消息
|
|
/// </summary>
|
|
public string ErrorMessage { get; set; }
|
|
|
|
/// <summary>
|
|
/// 数据 兼顾以前 Json序列化的时候返回属性名为“Result”
|
|
/// </summary>
|
|
[JsonProperty("Result")]
|
|
public T Data { get; set; }
|
|
|
|
|
|
|
|
public object OtherData { get; set; }
|
|
|
|
|
|
/// <summary>
|
|
/// 成功
|
|
/// </summary>
|
|
/// <param name="data">数据</param>
|
|
/// <param name="msg">消息</param>
|
|
//public ResponseOutput<T> Ok(T data, string msg = "", ApiResponseCodeEnum code = ApiResponseCodeEnum.OK)
|
|
//{
|
|
// IsSuccess = true;
|
|
// Code = code;
|
|
// Data = data;
|
|
// ErrorMessage = msg;
|
|
|
|
// return this;
|
|
//}
|
|
|
|
public ResponseOutput<T> Ok(T data, object otherData, string msg = "", ApiResponseCodeEnum code = ApiResponseCodeEnum.OK)
|
|
{
|
|
IsSuccess = true;
|
|
Code = code;
|
|
Data = data;
|
|
OtherData=otherData;
|
|
ErrorMessage = msg;
|
|
|
|
return this;
|
|
}
|
|
|
|
/// <summary>
|
|
/// 失败
|
|
/// </summary>
|
|
/// <param name="msg">提示消息</param>
|
|
/// <param name="data">数据</param>
|
|
/// <returns></returns>
|
|
public ResponseOutput<T> NotOk(string msg = "", T data = default, ApiResponseCodeEnum code = ApiResponseCodeEnum.OK)
|
|
{
|
|
IsSuccess = false;
|
|
Code = code;
|
|
ErrorMessage = msg;
|
|
Data = data;
|
|
return this;
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
public class ResponseOutput<T, T2> : IResponseOutput<T, T2>
|
|
{
|
|
[JsonProperty("Result")]
|
|
public T Data { get; private set; }
|
|
|
|
public T2 OtherInfo { get; private set; }
|
|
|
|
public bool IsSuccess { get; private set; }
|
|
|
|
public ApiResponseCodeEnum Code { get; set; }
|
|
|
|
|
|
public string ErrorMessage { get; private set; }
|
|
|
|
|
|
public ResponseOutput<T, T2> Ok(T data, T2 otherInfo, string msg = "")
|
|
{
|
|
IsSuccess = true;
|
|
Data = data;
|
|
OtherInfo = otherInfo;
|
|
ErrorMessage = msg;
|
|
|
|
return this;
|
|
}
|
|
}
|
|
|
|
|
|
/// <summary>
|
|
/// 响应数据静态输出 为了代码简洁 不用每处都New
|
|
/// </summary>
|
|
public static class ResponseOutput
|
|
{
|
|
|
|
|
|
public static IResponseOutput<T, T2> Ok<T, T2>(T data, T2 otherInfo, string msg = "")
|
|
{
|
|
return new ResponseOutput<T, T2>().Ok(data, otherInfo);
|
|
}
|
|
|
|
|
|
/// <summary>
|
|
/// 成功 -----适合查询
|
|
/// </summary>
|
|
/// <param name="data">数据</param>
|
|
/// <param name="msg">消息</param>
|
|
/// <returns></returns>
|
|
//public static IResponseOutput<T> Ok<T>(T data = default, string msg = "")
|
|
//{
|
|
// return new ResponseOutput<T>().Ok(data, msg);
|
|
//}
|
|
|
|
public static IResponseOutput<T> Ok<T>(T data = default, object otherData = default, string msg = "")
|
|
{
|
|
return new ResponseOutput<T>().Ok(data, otherData, msg);
|
|
}
|
|
/// <summary>
|
|
/// 成功
|
|
/// </summary>
|
|
/// <returns></returns>
|
|
public static IResponseOutput Ok()
|
|
{
|
|
return Ok<string>();
|
|
}
|
|
|
|
/// <summary>
|
|
/// 失败
|
|
/// </summary>
|
|
/// <param name="msg">消息</param>
|
|
/// <param name="data">数据</param>
|
|
/// <returns></returns>
|
|
public static IResponseOutput<T> NotOk<T>(string msg = "", T data = default, ApiResponseCodeEnum code = ApiResponseCodeEnum.BusinessValidationFailed)
|
|
{
|
|
return new ResponseOutput<T>().NotOk(msg, data, code);
|
|
}
|
|
|
|
public static IResponseOutput<T> NotOk<T>( T data = default, ApiResponseCodeEnum code = ApiResponseCodeEnum.BusinessValidationFailed)
|
|
{
|
|
return new ResponseOutput<T>().NotOk("", data, code);
|
|
}
|
|
|
|
/// <summary>
|
|
/// 失败
|
|
/// </summary>
|
|
/// <param name="msg">消息</param>
|
|
/// <returns></returns>
|
|
public static IResponseOutput<string> NotOk(string msg = "", ApiResponseCodeEnum code = ApiResponseCodeEnum.BusinessValidationFailed)
|
|
{
|
|
return new ResponseOutput<string>().NotOk(msg,code:code);
|
|
}
|
|
|
|
public static IResponseOutput<string> DBNotExistIfNUll(object businessObject)
|
|
{
|
|
return new ResponseOutput<string>().NotOk($"The business object{businessObject.GetType().Name} does not exist in the database, or was deleted by someone else, or an incorrect parameter query caused");
|
|
}
|
|
|
|
|
|
/// <summary>
|
|
/// 根据布尔值返回结果 --适合删除
|
|
/// </summary>
|
|
/// <param name="success"></param>
|
|
/// <returns></returns>
|
|
public static IResponseOutput Result(bool success)
|
|
{
|
|
return success ? Ok() : NotOk("Expect a change, but the database data has not changed");
|
|
}
|
|
|
|
/// <summary>
|
|
/// 根据布尔值返回结果 --适合添加和更新一起
|
|
/// </summary>
|
|
/// <param name="success"></param>
|
|
/// <returns></returns>
|
|
public static IResponseOutput<T> Result<T>(bool success, T data = default)
|
|
{
|
|
return success ? Ok<T>(data) : NotOk<T>("Saved failed",data);
|
|
}
|
|
|
|
///// <summary>
|
|
///// 根据布尔值返回结果
|
|
///// </summary>
|
|
///// <param name="success"></param>
|
|
///// <returns></returns>
|
|
//public static IResponseOutput Result<T>(bool success)
|
|
//{
|
|
// return success ? Ok<T>() : NotOk<T>();
|
|
//}
|
|
}
|
|
}
|