♻️重新设计返回格式,同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

View File

@ -99,6 +99,10 @@ namespace Infrastructure.Controllers
return new ApiResult((int)resultCode, msg, data);
}
protected ApiResult Success()
{
return GetApiResult(ResultCode.SUCCESS);
}
/// <summary>
///

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对象使其表示一个空消息
@ -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;
}
}

View File

@ -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);

View File

@ -86,7 +86,7 @@ namespace ZR.Admin.WebApi.Controllers
throw new CustomException($"程序集或者类名不能为空");
}
//从 Dto 映射到 实体
var tasksQz = parm.Adapt<SysTasks>().ToCreate();
var tasksQz = parm.Adapt<SysTasks>().ToCreate(HttpContext);
tasksQz.Create_by = HttpContext.GetName();
tasksQz.ID = SnowFlakeSingle.Instance.NextId().ToString();
@ -158,7 +158,7 @@ namespace ZR.Admin.WebApi.Controllers
var tasksQz = _tasksQzService.GetFirst(m => m.ID == id);
var taskResult = await _schedulerServer.DeleteTaskScheduleAsync(tasksQz);
if (taskResult.Code == 200)
if (taskResult.IsSuccess())
{
_tasksQzService.Delete(id);
}
@ -187,7 +187,7 @@ namespace ZR.Admin.WebApi.Controllers
var tasksQz = _tasksQzService.GetFirst(m => m.ID == id);
var taskResult = await _schedulerServer.AddTaskScheduleAsync(tasksQz);
if (taskResult.Code == 200)
if (taskResult.IsSuccess())
{
tasksQz.IsStart = 1;
_tasksQzService.Update(tasksQz);
@ -218,7 +218,7 @@ namespace ZR.Admin.WebApi.Controllers
var tasksQz = _tasksQzService.GetFirst(m => m.ID == id);
var taskResult = await _schedulerServer.DeleteTaskScheduleAsync(tasksQz);//await _schedulerServer.PauseTaskScheduleAsync(tasksQz);
if (taskResult.Code == 200)
if (taskResult.IsSuccess())
{
tasksQz.IsStart = 0;
_tasksQzService.Update(tasksQz);

View File

@ -40,7 +40,7 @@ namespace ZR.Admin.WebApi.Extensions
foreach (var task in tasks.Result)
{
var result = _schedulerServer.AddTaskScheduleAsync(task);
if (result.Result.Code == 200)
if (result.Result.IsSuccess())
{
Console.WriteLine($"注册任务[{task.Name}]ID{task.ID}成功");
}

View File

@ -63,9 +63,7 @@ namespace ZR.Admin.WebApi.Filters
{
string msg = $"请求访问[{url}]失败,无法访问系统资源";
//logger.Info(msg);
var r = new ApiResult((int)ResultCode.DENY, msg);
context.Result = new JsonResult(r);
context.Result = new JsonResult(ApiResult.Error(ResultCode.DENY, msg));
}
}
}