fix name policy

This commit is contained in:
2020-02-04 16:51:15 +08:00
parent aadc4128f0
commit bb2f4641a3
18 changed files with 222 additions and 204 deletions
-23
View File
@@ -1,23 +0,0 @@
using Swashbuckle.AspNetCore.Annotations;
namespace YYApi.Helpers
{
/// <summary>
/// 快捷自定义接口文档类
/// </summary>
public class ApiDoc : SwaggerOperationAttribute
{
/// <summary>
/// 定义接口说明
/// </summary>
/// <param name="tag">接口分组</param>
/// <param name="name">接口名称</param>
public ApiDoc(string tag, string name)
{
Tags = new[] {tag};
Summary = name;
// Consumes = new[] {"application/json"};
// Produces = new[] {"application/json"};
}
}
}
-130
View File
@@ -1,130 +0,0 @@
using System;
using System.Linq;
using System.Text.Json;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc.Filters;
using Microsoft.Extensions.Caching.Distributed;
namespace YYApi.Helpers
{
/// <summary>
/// 认证信息获取中间件
/// </summary>
public class AuthMiddleware
{
private RequestDelegate Next { get; }
public AuthMiddleware(RequestDelegate next)
{
Next = next;
}
public Task Invoke(HttpContext httpContext, IDistributedCache cache)
{
string token = null;
if (httpContext.Request.Headers.ContainsKey("Token"))
{
token = httpContext.Request.Headers["Token"];
}
if (!string.IsNullOrEmpty(token))
{
var login = cache.GetString(token);
if (login != null)
{
httpContext.Items.Add("LoginInfo", JsonSerializer.Deserialize<LoginInfoItem>(login));
}
}
return Next(httpContext);
}
}
/// <summary>
/// 登录信息中间件
/// </summary>
public class LoginInfoItem
{
//登录用户的ID
public int Id { get; set; }
//登录用户来源
public string Type { get; set; }
}
/// <summary>
/// 检测登录中间件
/// </summary>
public class CheckAuthAttribute : ActionFilterAttribute
{
private string[] Types { get; }
//默认是user登录类型
public CheckAuthAttribute()
{
Types = new[] { "user" };
}
//只检测一种用户类型的快捷方式
public CheckAuthAttribute(string type)
{
Types = new[] { type };
}
//设定特定类型的检测
public CheckAuthAttribute(string[] types)
{
Types = types;
}
public override void OnActionExecuting(ActionExecutingContext context)
{
var loginInfo = (LoginInfoItem)context.HttpContext.Items["LoginInfo"];
//检测是否登录
if (loginInfo == null)
{
throw new ApiException(401);
}
//检测用户类型
if (!Types.Contains(loginInfo.Type))
{
throw new ApiException(403);
}
}
}
/// <summary>
/// 认证助手
/// </summary>
public class Auth
{
private IDistributedCache Cache { get; }
public Auth(IDistributedCache cache)
{
Cache = cache;
}
/// <summary>
/// 产生一个Token记录并返回Token
/// </summary>
/// <param name="id"></param>
/// <returns></returns>
public string Set(int id, string type = "user")
{
var uuid = GetUUID();
var loginItem = new LoginInfoItem
{
Id = id,
Type = type
};
Cache.SetString(uuid, JsonSerializer.Serialize(loginItem));
return uuid;
}
private string GetUUID()
{
return Guid.NewGuid().ToString();
}
}
}
-71
View File
@@ -1,71 +0,0 @@
using System;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Http;
using YYApi.Communications;
using Microsoft.AspNetCore.Builder;
using Microsoft.Extensions.Logging;
namespace YYApi.Helpers
{
/// <summary>
/// 异常处理中间件
/// </summary>
public class ExceptionMiddleware
{
private RequestDelegate Next { get; }
private ILogger<ExceptionMiddleware> Log { get; }
public ExceptionMiddleware(RequestDelegate next, ILogger<ExceptionMiddleware> log)
{
Log = log;
Next = next;
}
public async Task InvokeAsync(HttpContext context)
{
var response = new BaseResponse();
try
{
await Next(context);
}
catch (ApiException ex)
{
response.SetCodeMsg(ex.Code, ex.Message);
ErrorResponse(context, response);
}
catch (Exception ex)
{
Log.LogError(ex.Message);
Log.LogError(ex.StackTrace);
response.SetCodeMsg(500, ex.Message);
ErrorResponse(context, response);
}
}
/// <summary>
/// 异常抛出错误
/// </summary>
/// <param name="context"></param>
/// <param name="response"></param>
private void ErrorResponse(HttpContext context, BaseResponse response)
{
context.Response.StatusCode = 200;
context.Response.Headers.Add("Content-Type", "application/json");
context.Response.WriteAsync(response.ToString());
}
}
/// <summary>
/// Api错误捕获异常
/// </summary>
public class ApiException : Exception
{
public int Code { get; }
public ApiException(int code, string message = "") : base(message)
{
Code = code;
}
}
}
+39
View File
@@ -0,0 +1,39 @@
using System;
using System.Text.Json;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.Caching.Distributed;
using YYApi.Communications;
namespace YYApi.Helpers
{
/// <summary>
/// 认证助手
/// </summary>
public class YYAuth
{
private IDistributedCache Cache { get; }
public YYAuth(IDistributedCache cache)
{
Cache = cache;
}
/// <summary>
/// 产生一个Token记录并返回Token
/// </summary>
/// <param name="id"></param>
/// <returns></returns>
public string Set(int id, string type = "user")
{
var uuid = Guid.NewGuid().ToString();
var loginItem = new YYLoginItem
{
Id = id,
Type = type
};
Cache.SetString(uuid, JsonSerializer.Serialize(loginItem));
return uuid;
}
}
}
+1 -1
View File
@@ -5,7 +5,7 @@ using System.Text.RegularExpressions;
namespace YYApi.Helpers
{
public static class Util
public static class YYUtil
{
/// <summary>
/// 检测手机号是否正确