| | |
| | | using Microsoft.AspNetCore.Http; |
| | | using Microsoft.AspNetCore.Mvc; |
| | | using Microsoft.AspNetCore.Mvc.Filters; |
| | | using Microsoft.CodeAnalysis; |
| | | using System; |
| | | using System.Collections.Generic; |
| | | using System.Linq; |
| | |
| | | GetResult( |
| | | metadata.StatusCode, |
| | | data: metadata.Data, |
| | | errorCode: metadata.ErrorCode.ToString(), |
| | | errors: metadata.Errors), |
| | | UnifyContext.GetSerializerSettings(context)); |
| | | } |
| | |
| | | GetResult( |
| | | metadata.StatusCode, |
| | | data: metadata.Data, |
| | | errorCode: metadata.ErrorCode.ToString(), |
| | | errors: metadata.Errors) |
| | | , UnifyContext.GetSerializerSettings(context)); |
| | | } |
| | |
| | | /// <returns></returns> |
| | | public IActionResult OnValidateFailed(ActionExecutingContext context, ValidationMetadata metadata) |
| | | { |
| | | UnifyContext.Fill(metadata.ValidationResult); |
| | | return new JsonResult( |
| | | GetResult( |
| | | metadata.StatusCode ?? StatusCodes.Status400BadRequest, |
| | | data: metadata.Data, |
| | | errors: metadata.ValidationResult), |
| | | errorCode: "s400", |
| | | errors: "参数错误"), |
| | | UnifyContext.GetSerializerSettings(context)); |
| | | } |
| | | |
| | |
| | | await context.Response.WriteAsJsonAsync( |
| | | GetResult( |
| | | statusCode, |
| | | errorCode: "401", |
| | | errors: "401 Unauthorized"), |
| | | App.GetOptions<JsonOptions>()?.JsonSerializerOptions); |
| | | break; |
| | |
| | | await context.Response.WriteAsJsonAsync( |
| | | GetResult( |
| | | statusCode, |
| | | errorCode: "403", |
| | | errors: "403 Forbidden"), |
| | | App.GetOptions<JsonOptions>()?.JsonSerializerOptions); |
| | | break; |
| | |
| | | /// <param name="statusCode"></param> |
| | | /// <param name="succeeded"></param> |
| | | /// <param name="data"></param> |
| | | /// <param name="errorCode"></param> |
| | | /// <param name="errors"></param> |
| | | /// <returns></returns> |
| | | private static FriendlyResult<object> GetResult(int statusCode, bool succeeded = default, object data = default, object errors = default) |
| | | private static FriendlyResult<object> GetResult(int statusCode, bool succeeded = default, object data = default, string errorCode = null, object errors = default) |
| | | { |
| | | return new FriendlyResult<object> |
| | | { |
| | | TraceId = App.GetTraceId(), |
| | | Code = statusCode, |
| | | ErrorCode = errorCode, |
| | | Success = succeeded, |
| | | Data = data, |
| | | Errors = errors, |