:recycle:重新设计返回格式,同Ruoyi保持一致

This commit is contained in:
不做码农
2023-09-08 18:39:40 +08:00
parent 70371e92b3
commit 44ef55be00
6 changed files with 36 additions and 17 deletions
@@ -99,6 +99,10 @@ namespace Infrastructure.Controllers
return new ApiResult((int)resultCode, msg, data);
}
protected ApiResult Success()
{
return GetApiResult(ResultCode.SUCCESS);
}
/// <summary>
///
+24 -7
View File
@@ -14,13 +14,13 @@ namespace Infrastructure.Model
/** 数据对象 */
public static readonly string DATA_TAG = "data";
public int Code { get; set; }
public string Msg { get; set; }
//public int Code { get; set; }
//public string Msg { get; set; }
/// <summary>
/// 如果data值为null,则忽略序列化将不会返回data字段
/// </summary>
[JsonProperty(NullValueHandling = NullValueHandling.Ignore)]
public object Data { get; set; }
//[JsonProperty(NullValueHandling = NullValueHandling.Ignore)]
//public object Data { get; set; }
/// <summary>
/// 初始化一个新创建的APIResult对象,使其表示一个空消息
@@ -53,7 +53,7 @@ namespace Infrastructure.Model
if (data != null)
{
Add(DATA_TAG, data);
}
}
}
/// <summary>
/// 返回成功消息
@@ -103,9 +103,26 @@ namespace Infrastructure.Model
/// <returns></returns>
public static ApiResult Error(string msg) { return new ApiResult((int)ResultCode.CUSTOM_ERROR, msg); }
public override string ToString()
/// <summary>
/// 是否为成功消息
/// </summary>
/// <returns></returns>
public bool IsSuccess()
{
return $"msg={Msg},data={Data}";
return HttpStatus.SUCCESS == (int)this[CODE_TAG];
}
/// <summary>
/// 方便链式调用
/// </summary>
/// <param name="key"></param>
/// <param name="value"></param>
/// <returns></returns>
public ApiResult Put(string key, object value)
{
Add(key, value);
return this;
}
}
@@ -11,7 +11,7 @@ namespace Infrastructure
public static TSource ToCreate<TSource>(this TSource source, HttpContext? context = null)
{
var types = source?.GetType();
if (types == null) return source;
if (types == null || context == null) return source;
BindingFlags flag = BindingFlags.Public | BindingFlags.IgnoreCase | BindingFlags.Instance;
types.GetProperty("CreateTime", flag)?.SetValue(source, DateTime.Now, null);
@@ -26,7 +26,7 @@ namespace Infrastructure
public static TSource ToUpdate<TSource>(this TSource source, HttpContext? context = null)
{
var types = source?.GetType();
if (types == null) return source;
if (types == null || context == null) return source;
BindingFlags flag = BindingFlags.Public | BindingFlags.IgnoreCase | BindingFlags.Instance;
types.GetProperty("UpdateTime", flag)?.SetValue(source, DateTime.Now, null);