Initial_commit_SecMPS_v2
This commit is contained in:
@@ -0,0 +1,63 @@
|
||||
using Newtonsoft.Json;
|
||||
using VolPro.Core.Enums;
|
||||
using VolPro.Core.Extensions;
|
||||
|
||||
namespace VolPro.Core.Utilities
|
||||
{
|
||||
public class ApiResponseContent
|
||||
{
|
||||
public ApiResponseContent()
|
||||
{
|
||||
}
|
||||
public ApiResponseContent(int status)
|
||||
{
|
||||
this.Status = status;
|
||||
}
|
||||
[JsonProperty(PropertyName = "message")]
|
||||
/// <summary>
|
||||
/// 返回消息
|
||||
/// </summary>
|
||||
public string Message { get; set; }
|
||||
[JsonProperty(PropertyName = "status")]
|
||||
/// <summary>
|
||||
/// 返回状态
|
||||
/// </summary>
|
||||
public int Status { get; set; }
|
||||
[JsonProperty(PropertyName = "data")]
|
||||
/// <summary>
|
||||
/// 所有返回的业务数据
|
||||
/// </summary>
|
||||
public object Data { get; set; }
|
||||
|
||||
public ApiResponseContent OK(string msg = null)
|
||||
{
|
||||
return this.Set(ResponseType.OperSuccess, msg, ApiStatutsCode.Ok);
|
||||
}
|
||||
|
||||
public ApiResponseContent Set(ResponseType responseType, ApiStatutsCode? status = null)
|
||||
{
|
||||
return this.Set(responseType, null, status);
|
||||
}
|
||||
/// <summary>
|
||||
///
|
||||
/// </summary>
|
||||
/// <param name="responseData"></param>
|
||||
/// <param name="responseType">返回消息类型</param>
|
||||
/// <param name="msg">返回消息,若msg为null,则取responseType的描述信息</param>
|
||||
/// <param name="status">返回状态,目前只有0、失败,1、成功,2、token过期</param>
|
||||
public ApiResponseContent Set(ResponseType responseType, string msg, ApiStatutsCode? status = null)
|
||||
{
|
||||
if (status != null)
|
||||
this.Status = (int)status;
|
||||
if (!string.IsNullOrEmpty(msg))
|
||||
{
|
||||
this.Message = msg;
|
||||
return this;
|
||||
}
|
||||
if (!string.IsNullOrEmpty(this.Message))
|
||||
return this;
|
||||
this.Message = responseType.GetMsg();
|
||||
return this;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,56 @@
|
||||
using VolPro.Core.Enums;
|
||||
using VolPro.Core.Extensions;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using Microsoft.AspNetCore.Mvc.Filters;
|
||||
using Newtonsoft.Json;
|
||||
using System.Net;
|
||||
using System.Text;
|
||||
using VolPro.Core.Const;
|
||||
|
||||
namespace VolPro.Core.Utilities
|
||||
{
|
||||
public static class FilterResponse
|
||||
{
|
||||
public static void GetContentResult(FilterContext context, IActionResult actionResult)
|
||||
{
|
||||
GetContentResult(context, actionResult, null);
|
||||
}
|
||||
|
||||
public static void SetActionResult(ActionExecutingContext context, WebResponseContent responseData)
|
||||
{
|
||||
context.Result = new ContentResult()
|
||||
{
|
||||
Content = new { status = false, message = responseData.Message }.Serialize(),
|
||||
ContentType = ApplicationContentType.JSON,
|
||||
StatusCode = (int)HttpStatusCode.Unauthorized
|
||||
};
|
||||
}
|
||||
|
||||
public static void GetContentResult(FilterContext context, IActionResult actionResult, WebResponseContent responseData)
|
||||
{
|
||||
responseData = responseData ?? new WebResponseContent();
|
||||
responseData.Set(ResponseType.ServerError);
|
||||
|
||||
if (context.HttpContext.IsAjaxRequest())
|
||||
{
|
||||
actionResult = new ContentResult()
|
||||
{
|
||||
Content = JsonConvert.SerializeObject(responseData),
|
||||
ContentType = ApplicationContentType.JSON,
|
||||
StatusCode = (int)HttpStatusCode.InternalServerError
|
||||
};
|
||||
}
|
||||
else
|
||||
{
|
||||
string desc = Encoding.UTF8.GetString(Encoding.UTF8.GetBytes(responseData.Message));
|
||||
actionResult = new ContentResult()
|
||||
{
|
||||
Content = $@"<html><head><title></title></head><body>{desc}</body></html>",
|
||||
ContentType = "text/html",
|
||||
StatusCode = (int)HttpStatusCode.InternalServerError
|
||||
};
|
||||
}
|
||||
//writelog
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,107 @@
|
||||
using VolPro.Core.Enums;
|
||||
using VolPro.Core.Extensions;
|
||||
using VolPro.Core.Language;
|
||||
|
||||
namespace VolPro.Core.Utilities
|
||||
{
|
||||
public class WebResponseContent
|
||||
{
|
||||
public WebResponseContent()
|
||||
{
|
||||
}
|
||||
public WebResponseContent(bool status)
|
||||
{
|
||||
this.Status = status;
|
||||
}
|
||||
public bool Status { get; set; }
|
||||
public string Code { get; set; }
|
||||
public string Message { get; set; }
|
||||
//public string Message { get; set; }
|
||||
public object Data { get; set; }
|
||||
|
||||
public WebResponseContent OK()
|
||||
{
|
||||
this.Status = true;
|
||||
return this;
|
||||
}
|
||||
public WebResponseContent OKData(object data)
|
||||
{
|
||||
this.Status = true;
|
||||
this.Data = data;
|
||||
return this;
|
||||
}
|
||||
public WebResponseContent OKData(string message, object data, bool ts = false)
|
||||
{
|
||||
this.Message = ts ? message?.Translator() : message;
|
||||
this.Status = true;
|
||||
this.Data = data;
|
||||
return this;
|
||||
}
|
||||
public static WebResponseContent Instance
|
||||
{
|
||||
get { return new WebResponseContent(); }
|
||||
}
|
||||
public WebResponseContent OK(string message = null, object data = null, bool ts = true)
|
||||
{
|
||||
this.Status = true;
|
||||
this.Message = ts ? message?.Translator() : message;
|
||||
this.Data = data;
|
||||
return this;
|
||||
}
|
||||
public WebResponseContent OKDataToString(object data = null)
|
||||
{
|
||||
this.Status = true;
|
||||
this.Data = data.Serialize();
|
||||
return this;
|
||||
}
|
||||
public WebResponseContent OK(ResponseType responseType, bool ts = true)
|
||||
{
|
||||
return Set(responseType, true, true);
|
||||
}
|
||||
public WebResponseContent Error(string message = null, bool ts = false)
|
||||
{
|
||||
this.Status = false;
|
||||
this.Message = ts ? message?.Translator() : message;
|
||||
return this;
|
||||
}
|
||||
public WebResponseContent Error(ResponseType responseType, bool ts = false)
|
||||
{
|
||||
return Set(responseType, false, ts);
|
||||
}
|
||||
public WebResponseContent Set(ResponseType responseType, bool ts = false)
|
||||
{
|
||||
bool? b = null;
|
||||
return this.Set(responseType, b, ts);
|
||||
}
|
||||
public WebResponseContent Set(ResponseType responseType, bool? status, bool ts = false)
|
||||
{
|
||||
return this.Set(responseType, null, status, ts);
|
||||
}
|
||||
public WebResponseContent Set(ResponseType responseType, string msg, bool ts = false)
|
||||
{
|
||||
bool? b = null;
|
||||
return this.Set(responseType, msg, b);
|
||||
}
|
||||
public WebResponseContent Set(ResponseType responseType, string msg, bool? status, bool ts = false)
|
||||
{
|
||||
if (status != null)
|
||||
{
|
||||
this.Status = (bool)status;
|
||||
}
|
||||
this.Code = ((int)responseType).ToString();
|
||||
if (!string.IsNullOrEmpty(msg))
|
||||
{
|
||||
Message = ts ? msg.Translator() : msg;
|
||||
return this;
|
||||
}
|
||||
|
||||
Message = responseType.GetMsg();
|
||||
if (ts)
|
||||
{
|
||||
Message = Message.Translator();
|
||||
}
|
||||
return this;
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user