remove nuget key from ci

This commit is contained in:
2019-12-23 16:32:06 +08:00
commit 865b20d5d4
12 changed files with 1037 additions and 0 deletions
+23
View File
@@ -0,0 +1,23 @@
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"};
}
}
}
+85
View File
@@ -0,0 +1,85 @@
using System;
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 id = cache.GetString(token);
if (id != null)
{
httpContext.Items.Add("LoginId", id);
}
}
return Next(httpContext);
}
}
/// <summary>
/// 检测登录中间件
/// </summary>
public class CheckAuthAttribute : ActionFilterAttribute
{
public override void OnActionExecuting(ActionExecutingContext context)
{
if (context.HttpContext.Items["LoginId"] == null)
{
throw new ApiException(401);
}
}
}
/// <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 SetId(int id)
{
var uuid = GetUUID();
Cache.SetString(uuid, id.ToString());
return uuid;
}
private string GetUUID()
{
return Guid.NewGuid().ToString();
}
}
}
+71
View File
@@ -0,0 +1,71 @@
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;
}
}
}
+41
View File
@@ -0,0 +1,41 @@
using System;
using System.Security.Cryptography;
using System.Text;
using System.Text.RegularExpressions;
namespace YYApi.Helpers
{
public static class Util
{
/// <summary>
/// 检测手机号是否正确
/// </summary>
/// <param name="cell">手机号码</param>
/// <returns></returns>
public static bool CheckCell(string cell)
{
var regex = new Regex("^1[34578]\\d{9}$");
return regex.IsMatch(cell);
}
/// <summary>
/// MD5加密字符串
/// </summary>
/// <param name="source">源字符串</param>
/// <param name="mode">加密结果"x2"结果为32位,"x3"结果为48位,"x4"结果为64位</param>
/// <returns></returns>
public static string Md5(string source, string mode = "x2")
{
byte[] sor = Encoding.UTF8.GetBytes(source);
MD5 md5 = MD5.Create();
byte[] result = md5.ComputeHash(sor);
StringBuilder strbul = new StringBuilder(40);
for (int i = 0; i < result.Length; i++)
{
strbul.Append(result[i].ToString(mode));
}
return strbul.ToString();
}
}
}