⚡数据返回格式改为可灵活扩展,跟ruoyi保存一致
This commit is contained in:
parent
803d862513
commit
70371e92b3
@ -12,6 +12,9 @@ using System.Web;
|
|||||||
|
|
||||||
namespace Infrastructure.Controllers
|
namespace Infrastructure.Controllers
|
||||||
{
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// web层通用数据处理
|
||||||
|
/// </summary>
|
||||||
public class BaseController : ControllerBase
|
public class BaseController : ControllerBase
|
||||||
{
|
{
|
||||||
public static string TIME_FORMAT_FULL = "yyyy-MM-dd HH:mm:ss";
|
public static string TIME_FORMAT_FULL = "yyyy-MM-dd HH:mm:ss";
|
||||||
|
|||||||
@ -1,10 +1,19 @@
|
|||||||
using Infrastructure.Constant;
|
using Infrastructure.Constant;
|
||||||
using Newtonsoft.Json;
|
using Newtonsoft.Json;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
|
||||||
namespace Infrastructure.Model
|
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 int Code { get; set; }
|
||||||
public string Msg { get; set; }
|
public string Msg { get; set; }
|
||||||
/// <summary>
|
/// <summary>
|
||||||
@ -27,8 +36,8 @@ namespace Infrastructure.Model
|
|||||||
/// <param name="msg"></param>
|
/// <param name="msg"></param>
|
||||||
public ApiResult(int code, string msg)
|
public ApiResult(int code, string msg)
|
||||||
{
|
{
|
||||||
Code = code;
|
Add(CODE_TAG, code);
|
||||||
Msg = msg;
|
Add(MSG_TAG, msg);
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
@ -36,33 +45,28 @@ namespace Infrastructure.Model
|
|||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="code"></param>
|
/// <param name="code"></param>
|
||||||
/// <param name="msg"></param>
|
/// <param name="msg"></param>
|
||||||
|
/// <param name="data"></param>
|
||||||
public ApiResult(int code, string msg, object data)
|
public ApiResult(int code, string msg, object data)
|
||||||
{
|
{
|
||||||
Code = code;
|
Add(CODE_TAG, code);
|
||||||
Msg = msg;
|
Add(MSG_TAG, msg);
|
||||||
if (data != null)
|
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>
|
||||||
/// 返回成功消息
|
/// 返回成功消息
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <returns></returns>
|
/// <param name="data"></param>
|
||||||
public ApiResult Success()
|
/// <returns> 成功消息 </returns >
|
||||||
{
|
public static ApiResult Success(object data) { return new ApiResult(HttpStatus.SUCCESS, "success", data); }
|
||||||
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); }
|
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// 返回成功消息
|
/// 返回成功消息
|
||||||
@ -79,21 +83,9 @@ namespace Infrastructure.Model
|
|||||||
/// <returns>成功消息</returns>
|
/// <returns>成功消息</returns>
|
||||||
public static ApiResult Success(string msg, object data) { return new ApiResult(HttpStatus.SUCCESS, msg, data); }
|
public static ApiResult Success(string msg, object data) { return new ApiResult(HttpStatus.SUCCESS, msg, data); }
|
||||||
|
|
||||||
/// <summary>
|
public static ApiResult Error(ResultCode code, string msg = "")
|
||||||
/// 访问被拒
|
|
||||||
/// </summary>
|
|
||||||
/// <returns></returns>
|
|
||||||
public ApiResult On401()
|
|
||||||
{
|
{
|
||||||
Code = (int)ResultCode.DENY;
|
return Error((int)code, msg);
|
||||||
Msg = "access denyed";
|
|
||||||
return this;
|
|
||||||
}
|
|
||||||
public ApiResult Error(ResultCode resultCode, string msg = "")
|
|
||||||
{
|
|
||||||
Code = (int)resultCode;
|
|
||||||
Msg = msg;
|
|
||||||
return this;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
|
|||||||
@ -82,12 +82,7 @@ namespace ZR.ServiceCore.Middleware
|
|||||||
if (!HasPermi && !Permission.Equals("common"))
|
if (!HasPermi && !Permission.Equals("common"))
|
||||||
{
|
{
|
||||||
logger.Info($"用户{info.UserName}没有权限访问{url},当前权限[{Permission}]");
|
logger.Info($"用户{info.UserName}没有权限访问{url},当前权限[{Permission}]");
|
||||||
JsonResult result = new(new ApiResult()
|
JsonResult result = new(new ApiResult((int)ResultCode.FORBIDDEN, $"你当前没有权限[{Permission}]访问,请联系管理员", url))
|
||||||
{
|
|
||||||
Code = (int)ResultCode.FORBIDDEN,
|
|
||||||
Msg = $"你当前没有权限[{Permission}]访问,请联系管理员",
|
|
||||||
Data = url
|
|
||||||
})
|
|
||||||
{
|
{
|
||||||
ContentType = "application/json",
|
ContentType = "application/json",
|
||||||
};
|
};
|
||||||
|
|||||||
@ -46,11 +46,8 @@ namespace ZR.ServiceCore.Middleware
|
|||||||
if (!string.IsNullOrEmpty(msg))
|
if (!string.IsNullOrEmpty(msg))
|
||||||
{
|
{
|
||||||
logger.Info($"请求参数错误,{msg}");
|
logger.Info($"请求参数错误,{msg}");
|
||||||
ApiResult response = new()
|
ApiResult response = new((int)ResultCode.PARAM_ERROR, msg);
|
||||||
{
|
|
||||||
Code = (int)ResultCode.PARAM_ERROR,
|
|
||||||
Msg = msg
|
|
||||||
};
|
|
||||||
context.Result = new JsonResult(response);
|
context.Result = new JsonResult(response);
|
||||||
}
|
}
|
||||||
return base.OnActionExecutionAsync(context, next);
|
return base.OnActionExecutionAsync(context, next);
|
||||||
|
|||||||
@ -63,8 +63,9 @@ namespace ZR.Admin.WebApi.Filters
|
|||||||
{
|
{
|
||||||
string msg = $"请求访问[{url}]失败,无法访问系统资源";
|
string msg = $"请求访问[{url}]失败,无法访问系统资源";
|
||||||
//logger.Info(msg);
|
//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);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user