Compare commits

...
7 Commits
Author SHA1 Message Date
xrain 31df4a7593 add auth check 2020-07-05 06:09:02 +08:00
xrain dbc05c13ee fix ci 2020-06-28 06:36:51 +08:00
xrainandGitHub e90cc2d86e Update main.yml 2020-06-28 06:33:58 +08:00
xrain 3cbd8b7559 update depenincy, add login and logout support 2020-06-28 06:28:51 +08:00
xrain d54b71badb update package; add write file 2020-03-12 11:46:30 +08:00
xrain ddeb2948b7 add static any file 2020-03-11 23:27:17 +08:00
xrain fb1ce7d931 fix savefile response 2020-03-04 12:07:49 +08:00
7 changed files with 114 additions and 30 deletions
+4 -11
View File
@@ -4,6 +4,7 @@ using Microsoft.AspNetCore.Mvc.Filters;
using Microsoft.AspNetCore.Mvc.ModelBinding;
using System.Linq;
using YYApi.Exceptions;
using YYApi.Attributes;
namespace YYApi.Controllers
{
@@ -77,22 +78,14 @@ namespace YYApi.Controllers
}
/// <summary>
/// 错误回馈页面
/// 上传文件
/// </summary>
/// <param name="code">错误代码</param>
/// <returns></returns>
[HttpGet("exception/{code:int}")]
[ApiExplorerSettings(IgnoreApi = true)]
public YYBaseResponse ExceptionHandler(int code)
{
var response = new YYBaseResponse();
response.SetCode(code);
return response;
}
protected YYBaseResponse<string> UploadFile()
{
return new YYBaseResponse<string>();
}
}
}
+60
View File
@@ -0,0 +1,60 @@
using System;
using Microsoft.AspNetCore.Mvc;
using YYApi.Attributes;
using YYApi.Communications;
using YYApi.Helpers;
namespace YYApi.Controllers
{
public class YYCommonController : YYBaseController
{
private YYAuth Auth { get; }
public YYCommonController(YYAuth auth)
{
Auth = auth;
}
/// <summary>
/// 错误回馈页面
/// </summary>
/// <param name="code">错误代码</param>
/// <returns></returns>
[HttpGet("exception/{code:int}")]
[ApiExplorerSettings(IgnoreApi = true)]
public YYBaseResponse ExceptionHandler(int code)
{
var response = new YYBaseResponse();
response.SetCode(code);
return response;
}
/// <summary>
/// 检测用户登陆的控制器
/// </summary>
/// <returns></returns>
[HttpPost("/auth/check"), YYDoc("认证", "检测登陆")]
public YYBaseResponse<int> CheckLogin()
{
ErrorWhenNull(LoginInfo, 401);
return new YYBaseResponse<int> { Data = LoginInfo.Id };
}
/// <summary>
/// 退出登陆
/// </summary>
/// <returns></returns>
[HttpPost("/auth/logout"), YYDoc("认证", "退出登陆")]
public YYBaseResponse Logout()
{
string token = null;
if (Request.Headers.ContainsKey("Token"))
{
token = Request.Headers["Token"];
}
Auth.Logout(token);
return new YYBaseResponse();
}
}
}
+5 -1
View File
@@ -112,7 +112,11 @@ namespace YYApi
/// <returns></returns>
public static IApplicationBuilder UseYYUpload(this IApplicationBuilder builder)
{
return builder.UseStaticFiles();
return builder.UseStaticFiles(new StaticFileOptions
{
DefaultContentType = "application/x-msdownload",
ServeUnknownFileTypes = true
});
}
/// <summary>
+17 -5
View File
@@ -23,20 +23,23 @@ namespace YYApi.Helpers
/// </summary>
/// <param name="id"></param>
/// <returns></returns>
public string Set(int id, string type = "user")
public string Login(int id, string type = "user", string token = null)
{
return Set(id, new[] { type });
return Login(id, new[] { type }, token);
}
/// <summary>
/// 产生一个TOken并记录用户ID角色[多角色]
/// 产生一个Token并记录用户ID角色[多角色]
/// </summary>
/// <param name="id"></param>
/// <param name="type"></param>
/// <returns></returns>
public string Set(int id, string[] type)
public string Login(int id, string[] type, string uuid = null)
{
var uuid = Guid.NewGuid().ToString();
if (uuid == null)
{
uuid = Guid.NewGuid().ToString();
}
var loginItem = new YYLoginItem
{
Id = id,
@@ -46,5 +49,14 @@ namespace YYApi.Helpers
return uuid;
}
/// <summary>
/// 退出登陆
/// </summary>
/// <param name="uuid">登陆标识</param>
public void Logout(string uuid)
{
Cache.Remove(uuid);
}
}
}
+18 -7
View File
@@ -33,7 +33,7 @@ namespace YYApi.Helpers
Env = env;
}
public class FileResult
public class YYUploadInfo
{
public string Path { get; set; }
public int Size { get; set; }
@@ -47,7 +47,7 @@ namespace YYApi.Helpers
/// <param name="path">存放路径</param>
/// <param name="fileName">文件名</param>
/// <returns></returns>
public FileResult SaveFile(string base64, string ext = null, string path = null, string fileName = null)
public YYUploadInfo SaveFile(string base64, string ext = null, string path = null, string fileName = null)
{
byte[] bt;
var filePath = FilePath + path;
@@ -74,18 +74,29 @@ namespace YYApi.Helpers
{
bt = Convert.FromBase64String(base64);
}
var fn = fileName + (string.IsNullOrEmpty(ext) ? string.Empty : $".{ext}");
return WriteFile(filePath, fn, bt);
}
/// <summary>
/// 把数据写入文件
/// </summary>
/// <param name="filePath"></param>
/// <param name="fileName"></param>
/// <param name="data"></param>
/// <returns></returns>
public YYUploadInfo WriteFile(string filePath, string fileName, byte[] data)
{
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 FileResult
File.WriteAllBytes(realPath + fileName, data);
return new YYUploadInfo
{
Path = filePath + fn,
Size = bt.Length
Path = filePath + fileName,
Size = data.Length
};
}
}
+5 -1
View File
@@ -61,4 +61,8 @@ app.UseYYAuth();
```C#
#Startup.cs
app.UseYYException();
```
```
### TODO:
1. 增加HANGFIRE 支持redis和sqlite 存储, 支持 基于其他系统的TOKEN认证和独立账号密码认证
+5 -5
View File
@@ -23,12 +23,12 @@
<Folder Include="Exceptions\" />
</ItemGroup>
<ItemGroup>
<PackageReference Include="Swashbuckle.AspNetCore.Annotations" Version="5.0.0" />
<PackageReference Include="Swashbuckle.AspNetCore.SwaggerUI" Version="5.0.0" />
<PackageReference Include="HangFire.Core" Version="1.7.9" />
<PackageReference Include="Hangfire.AspNetCore" Version="1.7.9" />
<PackageReference Include="Swashbuckle.AspNetCore.Annotations" Version="5.5.1" />
<PackageReference Include="Swashbuckle.AspNetCore.SwaggerUI" Version="5.5.1" />
<PackageReference Include="HangFire.Core" Version="1.7.11" />
<PackageReference Include="Hangfire.AspNetCore" Version="1.7.11" />
<PackageReference Include="Hangfire.Console" Version="1.4.2" />
<PackageReference Include="Hangfire.Redis.StackExchange" Version="1.8.1" />
<PackageReference Include="Hangfire.Redis.StackExchange" Version="1.8.4" />
</ItemGroup>
<ProjectExtensions>
<MonoDevelop>