Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
fb1ce7d931 | ||
|
|
c117d9ef1f | ||
|
|
9b38ee9b39 | ||
|
|
116dd1a808 | ||
|
|
26753657b8 |
@@ -1,4 +1,5 @@
|
||||
using System.Linq;
|
||||
using System;
|
||||
using System.Linq;
|
||||
using Microsoft.AspNetCore.Mvc.Filters;
|
||||
using YYApi.Communications;
|
||||
using YYApi.Exceptions;
|
||||
@@ -8,6 +9,7 @@ namespace YYApi.Attributes
|
||||
/// <summary>
|
||||
/// 检测登录中间件
|
||||
/// </summary>
|
||||
[AttributeUsage(AttributeTargets.Class | AttributeTargets.Method)]
|
||||
public class YYAuthAttribute : ActionFilterAttribute
|
||||
{
|
||||
private string[] Types { get; }
|
||||
@@ -39,7 +41,7 @@ namespace YYApi.Attributes
|
||||
throw new YYApiException(401);
|
||||
}
|
||||
//检测用户类型
|
||||
if (!Types.Contains(loginInfo.Type))
|
||||
if (Types.Intersect(loginInfo.Type).Count() == 0)
|
||||
{
|
||||
throw new YYApiException(403);
|
||||
}
|
||||
|
||||
@@ -1,10 +1,12 @@
|
||||
using Swashbuckle.AspNetCore.Annotations;
|
||||
using System;
|
||||
using Swashbuckle.AspNetCore.Annotations;
|
||||
|
||||
namespace YYApi.Attributes
|
||||
{
|
||||
/// <summary>
|
||||
/// 快捷自定义接口文档类
|
||||
/// </summary>
|
||||
[AttributeUsage(AttributeTargets.Method | AttributeTargets.Class)]
|
||||
public class YYDocAttribute : SwaggerOperationAttribute
|
||||
{
|
||||
/// <summary>
|
||||
|
||||
@@ -9,6 +9,6 @@ namespace YYApi.Communications
|
||||
//登录用户的ID
|
||||
public int Id { get; set; }
|
||||
//登录用户来源
|
||||
public string Type { get; set; }
|
||||
public string[] Type { get; set; }
|
||||
}
|
||||
}
|
||||
|
||||
@@ -46,7 +46,7 @@ namespace YYApi.Controllers
|
||||
/// <param name="code">错误代码</param>
|
||||
/// <param name="message">错误描述(若是常规错误,代码可自动带取描述)</param>
|
||||
/// <returns></returns>
|
||||
protected static void Error(int code, string message = "")
|
||||
protected static void Error(int code = 500, string message = "")
|
||||
{
|
||||
throw new YYApiException(code, message);
|
||||
}
|
||||
@@ -57,7 +57,7 @@ namespace YYApi.Controllers
|
||||
/// <param name="condition">检测条件</param>
|
||||
/// <param name="code">错误代码</param>
|
||||
/// <param name="message">错误描述</param>
|
||||
protected static void ErrorWhen(bool condition, int code, string message = "")
|
||||
protected static void ErrorWhen(bool condition, int code = 500, string message = "")
|
||||
{
|
||||
if (condition)
|
||||
{
|
||||
@@ -71,7 +71,7 @@ namespace YYApi.Controllers
|
||||
/// <param name="condition">检测条件</param>
|
||||
/// <param name="code">错误代码</param>
|
||||
/// <param name="message">错误描述</param>
|
||||
protected static void ErrorWhenNull(object condition, int code, string message = "")
|
||||
protected static void ErrorWhenNull(object condition, int code = 404, string message = "请求的资源不存在")
|
||||
{
|
||||
ErrorWhen(condition == null, code, message);
|
||||
}
|
||||
|
||||
+25
-5
@@ -25,20 +25,20 @@ namespace YYApi
|
||||
public static IServiceCollection AddYYApi(this IServiceCollection builder, string title,
|
||||
string description = null)
|
||||
{
|
||||
return builder.AddYYAuth().AddYYDoc(title, description).AddCors();
|
||||
return builder.AddYYAuth().AddYYDoc(title, description).AddCors().AddYYUpload();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 使用所有YYApi自定义中间件
|
||||
/// 使用所有YYApi自定义中间件
|
||||
/// </summary>
|
||||
/// <param name="builder"></param>
|
||||
/// <param name="title"></param>
|
||||
/// <param name="staticFileroot"></param>
|
||||
/// <param name="submitMethods"></param>
|
||||
/// <returns></returns>
|
||||
public static IApplicationBuilder UseYYApi(this IApplicationBuilder builder, string title = "API文档",
|
||||
params SubmitMethod[] submitMethods)
|
||||
public static IApplicationBuilder UseYYApi(this IApplicationBuilder builder, string title = "API文档", params SubmitMethod[] submitMethods)
|
||||
{
|
||||
return builder.UseYYException().UseYYDoc(title, submitMethods).UseMiddleware<YYAuthMiddleware>().UseCors(x => x.AllowAnyHeader().AllowAnyMethod().AllowAnyOrigin());
|
||||
return builder.UseYYException().UseYYDoc(title, submitMethods).UseMiddleware<YYAuthMiddleware>().UseCors(x => x.AllowAnyHeader().AllowAnyMethod().AllowAnyOrigin()).UseYYUpload();
|
||||
}
|
||||
|
||||
|
||||
@@ -55,6 +55,16 @@ namespace YYApi
|
||||
return builder.AddScoped<YYAuth>();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 添加Base64上传组件
|
||||
/// </summary>
|
||||
/// <param name="builder"></param>
|
||||
/// <returns></returns>
|
||||
public static IServiceCollection AddYYUpload(this IServiceCollection builder)
|
||||
{
|
||||
return builder.AddSingleton<YYUpload>();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 添加Api文档服务
|
||||
/// </summary>
|
||||
@@ -95,6 +105,16 @@ namespace YYApi
|
||||
return builder.UseMiddleware<YYExceptionMiddleware>();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 配置上传组件必须
|
||||
/// </summary>
|
||||
/// <param name="builder"></param>
|
||||
/// <returns></returns>
|
||||
public static IApplicationBuilder UseYYUpload(this IApplicationBuilder builder)
|
||||
{
|
||||
return builder.UseStaticFiles();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 使用文档UI
|
||||
/// </summary>
|
||||
|
||||
@@ -24,6 +24,17 @@ namespace YYApi.Helpers
|
||||
/// <param name="id"></param>
|
||||
/// <returns></returns>
|
||||
public string Set(int id, string type = "user")
|
||||
{
|
||||
return Set(id, new[] { type });
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 产生一个TOken并记录用户ID角色[多角色]
|
||||
/// </summary>
|
||||
/// <param name="id"></param>
|
||||
/// <param name="type"></param>
|
||||
/// <returns></returns>
|
||||
public string Set(int id, string[] type)
|
||||
{
|
||||
var uuid = Guid.NewGuid().ToString();
|
||||
var loginItem = new YYLoginItem
|
||||
|
||||
@@ -0,0 +1,92 @@
|
||||
using System;
|
||||
using System.IO;
|
||||
using Microsoft.AspNetCore.Hosting;
|
||||
|
||||
namespace YYApi.Helpers
|
||||
{
|
||||
public class YYUpload
|
||||
{
|
||||
private string FilePathFolder = "/uploads/";
|
||||
public string FilePath
|
||||
{
|
||||
set
|
||||
{
|
||||
if (!value.StartsWith("/"))
|
||||
{
|
||||
FilePathFolder = $"/{value}";
|
||||
}
|
||||
if (!FilePathFolder.EndsWith("/"))
|
||||
{
|
||||
FilePathFolder += "/";
|
||||
}
|
||||
}
|
||||
get
|
||||
{
|
||||
return FilePathFolder;
|
||||
}
|
||||
}
|
||||
|
||||
private IWebHostEnvironment Env { get; }
|
||||
|
||||
public YYUpload(IWebHostEnvironment env)
|
||||
{
|
||||
Env = env;
|
||||
}
|
||||
|
||||
public class YYUploadInfo
|
||||
{
|
||||
public string Path { get; set; }
|
||||
public int Size { get; set; }
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 保存base64文件,如果有标头按照标头自动识别
|
||||
/// </summary>
|
||||
/// <param name="base64"></param>
|
||||
/// <param name="ext">扩展名</param>
|
||||
/// <param name="path">存放路径</param>
|
||||
/// <param name="fileName">文件名</param>
|
||||
/// <returns></returns>
|
||||
public YYUploadInfo SaveFile(string base64, string ext = null, string path = null, string fileName = null)
|
||||
{
|
||||
byte[] bt;
|
||||
var filePath = FilePath + path;
|
||||
if (fileName == null)
|
||||
{
|
||||
fileName = Guid.NewGuid().ToString();
|
||||
}
|
||||
var baseArr = base64.Split(";");
|
||||
if (baseArr.Length == 2)
|
||||
{
|
||||
var info = baseArr[0].Split(":")[1].Split("/");
|
||||
if (path == null)
|
||||
{
|
||||
filePath += $"{info[0]}/";
|
||||
}
|
||||
if (ext == null)
|
||||
{
|
||||
ext = info[1];
|
||||
}
|
||||
var data = baseArr[1].Split(",");
|
||||
bt = Convert.FromBase64String(data[1]);
|
||||
}
|
||||
else
|
||||
{
|
||||
bt = Convert.FromBase64String(base64);
|
||||
}
|
||||
|
||||
var realPath = Directory.GetCurrentDirectory() + "/wwwroot" + filePath;
|
||||
if (!Directory.Exists(realPath))
|
||||
{
|
||||
Directory.CreateDirectory(realPath);
|
||||
}
|
||||
var fn = fileName + (string.IsNullOrEmpty(ext) ? string.Empty : $".{ext}");
|
||||
File.WriteAllBytes(realPath + fn, bt);
|
||||
return new YYUploadInfo
|
||||
{
|
||||
Path = filePath + fn,
|
||||
Size = bt.Length
|
||||
};
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user