This commit is contained in:
49
Api/Dilon.Core/Extension/DictionaryExtensions.cs
Normal file
49
Api/Dilon.Core/Extension/DictionaryExtensions.cs
Normal file
@@ -0,0 +1,49 @@
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
|
||||
namespace Dilon.Core
|
||||
{
|
||||
/// <summary>
|
||||
/// 字典扩展
|
||||
/// </summary>
|
||||
public static class DictionaryExtensions
|
||||
{
|
||||
/// <summary>
|
||||
/// 将一个字典转化为 QueryString
|
||||
/// </summary>
|
||||
/// <param name="dict"></param>
|
||||
/// <param name="urlEncode"></param>
|
||||
/// <returns></returns>
|
||||
public static string ToQueryString(this Dictionary<string, string> dict, bool urlEncode = true)
|
||||
{
|
||||
return string.Join("&", dict.Select(p => $"{(urlEncode ? p.Key?.UrlEncode() : "")}={(urlEncode ? p.Value?.UrlEncode() : "")}"));
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 将一个字符串 URL 编码
|
||||
/// </summary>
|
||||
/// <param name="str"></param>
|
||||
/// <returns></returns>
|
||||
public static string UrlEncode(this string str)
|
||||
{
|
||||
if (string.IsNullOrEmpty(str))
|
||||
{
|
||||
return "";
|
||||
}
|
||||
return System.Web.HttpUtility.UrlEncode(str, Encoding.UTF8);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 移除空值项
|
||||
/// </summary>
|
||||
/// <param name="dict"></param>
|
||||
public static void RemoveEmptyValueItems(this Dictionary<string, string> dict)
|
||||
{
|
||||
dict.Where(item => string.IsNullOrEmpty(item.Value)).Select(item => item.Key).ToList().ForEach(key =>
|
||||
{
|
||||
dict.Remove(key);
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
56
Api/Dilon.Core/Extension/XnInputBase.cs
Normal file
56
Api/Dilon.Core/Extension/XnInputBase.cs
Normal file
@@ -0,0 +1,56 @@
|
||||
using System.Collections.Generic;
|
||||
|
||||
namespace Dilon.Core
|
||||
{
|
||||
/// <summary>
|
||||
/// 通用输入扩展参数(带权限)
|
||||
/// </summary>
|
||||
public class XnInputBase : PageInputBase
|
||||
{
|
||||
/// <summary>
|
||||
/// 授权菜单
|
||||
/// </summary>
|
||||
public List<long> GrantMenuIdList { get; set; } = new List<long>();
|
||||
|
||||
/// <summary>
|
||||
/// 授权角色
|
||||
/// </summary>
|
||||
public virtual List<long> GrantRoleIdList { get; set; } = new List<long>();
|
||||
|
||||
/// <summary>
|
||||
/// 授权数据
|
||||
/// </summary>
|
||||
public virtual List<long> GrantOrgIdList { get; set; } = new List<long>();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 通用分页输入参数
|
||||
/// </summary>
|
||||
public class PageInputBase
|
||||
{
|
||||
/// <summary>
|
||||
/// 搜索值
|
||||
/// </summary>
|
||||
public virtual string SearchValue { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// 当前页码
|
||||
/// </summary>
|
||||
public virtual int PageNo { get; set; } = 1;
|
||||
|
||||
/// <summary>
|
||||
/// 页码容量
|
||||
/// </summary>
|
||||
public virtual int PageSize { get; set; } = 20;
|
||||
|
||||
/// <summary>
|
||||
/// 搜索开始时间
|
||||
/// </summary>
|
||||
public virtual string SearchBeginTime { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// 搜索结束时间
|
||||
/// </summary>
|
||||
public virtual string SearchEndTime { get; set; }
|
||||
}
|
||||
}
|
||||
24
Api/Dilon.Core/Extension/XnPageResult.cs
Normal file
24
Api/Dilon.Core/Extension/XnPageResult.cs
Normal file
@@ -0,0 +1,24 @@
|
||||
using System.Collections.Generic;
|
||||
|
||||
namespace Dilon.Core
|
||||
{
|
||||
/// <summary>
|
||||
/// 小诺分页列表结果
|
||||
/// </summary>
|
||||
/// <typeparam name="T"></typeparam>
|
||||
public static class XnPageResult<T> where T : new()
|
||||
{
|
||||
public static dynamic PageResult(PagedList<T> page)
|
||||
{
|
||||
return new
|
||||
{
|
||||
PageNo = page.PageIndex,
|
||||
PageSize = page.PageSize,
|
||||
TotalPage = page.TotalPages,
|
||||
TotalRows = page.TotalCount,
|
||||
Rows = page.Items //.Adapt<List<T>>(),
|
||||
//Rainbow = PageUtil.Rainbow(page.PageIndex, page.TotalPages, PageUtil.RAINBOW_NUM)
|
||||
};
|
||||
}
|
||||
}
|
||||
}
|
||||
166
Api/Dilon.Core/Extension/XnRestfulResultProvider.cs
Normal file
166
Api/Dilon.Core/Extension/XnRestfulResultProvider.cs
Normal file
@@ -0,0 +1,166 @@
|
||||
using Furion.DataValidation;
|
||||
using Furion.DependencyInjection;
|
||||
using Furion.UnifyResult;
|
||||
using Microsoft.AspNetCore.Http;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using Microsoft.AspNetCore.Mvc.Filters;
|
||||
using Microsoft.AspNetCore.Mvc.ModelBinding;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace Dilon.Core
|
||||
{
|
||||
/// <summary>
|
||||
/// 规范化RESTful风格返回值
|
||||
/// </summary>
|
||||
[SkipScan, UnifyModel(typeof(XnRestfulResult<>))]
|
||||
public class XnRestfulResultProvider : IUnifyResultProvider
|
||||
{
|
||||
/// <summary>
|
||||
/// 异常返回值
|
||||
/// </summary>
|
||||
/// <param name="context"></param>
|
||||
/// <returns></returns>
|
||||
public IActionResult OnException(ExceptionContext context)
|
||||
{
|
||||
// 解析异常信息
|
||||
var (StatusCode, ErrorCode, Errors) = UnifyContext.GetExceptionMetadata(context);
|
||||
|
||||
return new JsonResult(new XnRestfulResult<object>
|
||||
{
|
||||
Code = StatusCode,
|
||||
Success = false,
|
||||
Data = null,
|
||||
Message = Errors,
|
||||
Extras = UnifyContext.Take(),
|
||||
Timestamp = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds()
|
||||
});
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 处理输出状态码
|
||||
/// </summary>
|
||||
/// <param name="context"></param>
|
||||
/// <param name="statusCode"></param>
|
||||
/// <returns></returns>
|
||||
public async Task OnResponseStatusCodes(HttpContext context, int statusCode)
|
||||
{
|
||||
switch (statusCode)
|
||||
{
|
||||
// 处理 401 状态码
|
||||
case StatusCodes.Status401Unauthorized:
|
||||
await context.Response.WriteAsJsonAsync(new XnRestfulResult<object>
|
||||
{
|
||||
Code = StatusCodes.Status401Unauthorized,
|
||||
Success = false,
|
||||
Data = null,
|
||||
Message = "401 未经授权",
|
||||
Extras = UnifyContext.Take(),
|
||||
Timestamp = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds()
|
||||
});
|
||||
break;
|
||||
// 处理 403 状态码
|
||||
case StatusCodes.Status403Forbidden:
|
||||
await context.Response.WriteAsJsonAsync(new XnRestfulResult<object>
|
||||
{
|
||||
Code = StatusCodes.Status403Forbidden,
|
||||
Success = false,
|
||||
Data = null,
|
||||
Message = "403 禁止访问",
|
||||
Extras = UnifyContext.Take(),
|
||||
Timestamp = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds()
|
||||
});
|
||||
break;
|
||||
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 成功返回值
|
||||
/// </summary>
|
||||
/// <param name="context"></param>
|
||||
/// <returns></returns>
|
||||
public IActionResult OnSucceeded(ActionExecutedContext context)
|
||||
{
|
||||
object data;
|
||||
// 处理内容结果
|
||||
if (context.Result is ContentResult contentResult) data = contentResult.Content;
|
||||
// 处理对象结果
|
||||
else if (context.Result is ObjectResult objectResult) data = objectResult.Value;
|
||||
else if (context.Result is EmptyResult) data = null;
|
||||
else return null;
|
||||
|
||||
return new JsonResult(new XnRestfulResult<object>
|
||||
{
|
||||
Code = context.Result is EmptyResult ? StatusCodes.Status204NoContent : StatusCodes.Status200OK, // 处理没有返回值情况 204
|
||||
Success = true,
|
||||
Data = data,
|
||||
Message = "请求成功",
|
||||
Extras = UnifyContext.Take(),
|
||||
Timestamp = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds()
|
||||
});
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 验证失败返回值
|
||||
/// </summary>
|
||||
/// <param name="context"></param>
|
||||
/// <param name="modelStates"></param>
|
||||
/// <param name="validationResults"></param>
|
||||
/// <param name="validateFailedMessage"></param>
|
||||
/// <returns></returns>
|
||||
public IActionResult OnValidateFailed(ActionExecutingContext context, ModelStateDictionary modelStates, IEnumerable<ValidateFailedModel> validationResults, string validateFailedMessage)
|
||||
{
|
||||
return new JsonResult(new XnRestfulResult<object>
|
||||
{
|
||||
Code = StatusCodes.Status400BadRequest,
|
||||
Success = false,
|
||||
Data = null,
|
||||
Message = validationResults,
|
||||
Extras = UnifyContext.Take(),
|
||||
Timestamp = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds()
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// RESTful风格---XIAONUO返回格式
|
||||
/// </summary>
|
||||
/// <typeparam name="T"></typeparam>
|
||||
[SkipScan]
|
||||
public class XnRestfulResult<T>
|
||||
{
|
||||
/// <summary>
|
||||
/// 执行成功
|
||||
/// </summary>
|
||||
public bool Success { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// 状态码
|
||||
/// </summary>
|
||||
public int? Code { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// 错误信息
|
||||
/// </summary>
|
||||
public object Message { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// 数据
|
||||
/// </summary>
|
||||
public T Data { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// 附加数据
|
||||
/// </summary>
|
||||
public object Extras { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// 时间戳
|
||||
/// </summary>
|
||||
public long Timestamp { get; set; }
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user