数据返回格式改为可灵活扩展,跟ruoyi保存一致

This commit is contained in:
不做码农 2023-09-08 15:18:14 +08:00
parent 803d862513
commit 70371e92b3
5 changed files with 36 additions and 48 deletions

View File

@ -12,6 +12,9 @@ using System.Web;
namespace Infrastructure.Controllers
{
/// <summary>
/// web层通用数据处理
/// </summary>
public class BaseController : ControllerBase
{
public static string TIME_FORMAT_FULL = "yyyy-MM-dd HH:mm:ss";

View File

@ -1,10 +1,19 @@
using Infrastructure.Constant;
using Newtonsoft.Json;
using System.Collections.Generic;
namespace Infrastructure.Model
{
public class ApiResult
public class ApiResult : Dictionary<string, object>
{
/** 状态码 */
public static readonly string CODE_TAG = "code";
/** 返回内容 */
public static readonly string MSG_TAG = "msg";
/** 数据对象 */
public static readonly string DATA_TAG = "data";
public int Code { get; set; }
public string Msg { get; set; }
/// <summary>
@ -27,8 +36,8 @@ namespace Infrastructure.Model
/// <param name="msg"></param>
public ApiResult(int code, string msg)
{
Code = code;
Msg = msg;
Add(CODE_TAG, code);
Add(MSG_TAG, msg);
}
/// <summary>
@ -36,33 +45,28 @@ namespace Infrastructure.Model
/// </summary>
/// <param name="code"></param>
/// <param name="msg"></param>
/// <param name="data"></param>
public ApiResult(int code, string msg, object data)
{
Code = code;
Msg = msg;
Add(CODE_TAG, code);
Add(MSG_TAG, msg);
if (data != null)
{
Data = data;
Add(DATA_TAG, data);
}
}
/// <summary>
/// 返回成功消息
/// </summary>
/// < returns > 成功消息 </ returns >
public static ApiResult Success() { return new ApiResult(HttpStatus.SUCCESS, "success"); }
/// <summary>
/// 返回成功消息
/// </summary>
/// <returns></returns>
public ApiResult Success()
{
Code = (int)ResultCode.SUCCESS;
Msg = "success";
return this;
}
///// <summary>
///// 返回成功消息
///// </summary>
///// <param name = "data" > 数据对象 </ param >
///// < returns > 成功消息 </ returns >
//public static ApiResult Success(object data) { return new ApiResult(HttpStatus.SUCCESS, "success", data); }
/// <param name="data"></param>
/// <returns> 成功消息 </returns >
public static ApiResult Success(object data) { return new ApiResult(HttpStatus.SUCCESS, "success", data); }
/// <summary>
/// 返回成功消息
@ -79,21 +83,9 @@ namespace Infrastructure.Model
/// <returns>成功消息</returns>
public static ApiResult Success(string msg, object data) { return new ApiResult(HttpStatus.SUCCESS, msg, data); }
/// <summary>
/// 访问被拒
/// </summary>
/// <returns></returns>
public ApiResult On401()
public static ApiResult Error(ResultCode code, string msg = "")
{
Code = (int)ResultCode.DENY;
Msg = "access denyed";
return this;
}
public ApiResult Error(ResultCode resultCode, string msg = "")
{
Code = (int)resultCode;
Msg = msg;
return this;
return Error((int)code, msg);
}
/// <summary>

View File

@ -82,12 +82,7 @@ namespace ZR.ServiceCore.Middleware
if (!HasPermi && !Permission.Equals("common"))
{
logger.Info($"用户{info.UserName}没有权限访问{url},当前权限[{Permission}]");
JsonResult result = new(new ApiResult()
{
Code = (int)ResultCode.FORBIDDEN,
Msg = $"你当前没有权限[{Permission}]访问,请联系管理员",
Data = url
})
JsonResult result = new(new ApiResult((int)ResultCode.FORBIDDEN, $"你当前没有权限[{Permission}]访问,请联系管理员", url))
{
ContentType = "application/json",
};

View File

@ -46,11 +46,8 @@ namespace ZR.ServiceCore.Middleware
if (!string.IsNullOrEmpty(msg))
{
logger.Info($"请求参数错误,{msg}");
ApiResult response = new()
{
Code = (int)ResultCode.PARAM_ERROR,
Msg = msg
};
ApiResult response = new((int)ResultCode.PARAM_ERROR, msg);
context.Result = new JsonResult(response);
}
return base.OnActionExecutionAsync(context, next);

View File

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